-
Notifications
You must be signed in to change notification settings - Fork 57
/
osint.c
224 lines (198 loc) · 4.53 KB
/
osint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "SDL.h"
#include "SDL_image.h"
#include "SDL2_gfxPrimitives.h"
#include "osint.h"
#include "e8910.h"
#include "vecx.h"
#define EMU_TIMER 20 /* the emulators heart beats at 20 milliseconds */
static SDL_Window *screen = NULL;
static SDL_Renderer *renderer= NULL;
static SDL_Texture *overlay = NULL;
static long scl_factor;
static long offx;
static long offy;
void osint_render(void){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
int v;
for(v = 0; v < vector_draw_cnt; v++){
Uint8 c = vectors_draw[v].color * 256 / VECTREX_COLORS;
aalineRGBA(renderer,
offx + vectors_draw[v].x0 / scl_factor,
offy + vectors_draw[v].y0 / scl_factor,
offx + vectors_draw[v].x1 / scl_factor,
offy + vectors_draw[v].y1 / scl_factor,
c, c, c, 0xff);
}
if(overlay){
SDL_Rect dest_rect = {offx, offy, ((double)ALG_MAX_X / (double)scl_factor), ((double)ALG_MAX_Y / (double)scl_factor)};
SDL_RenderCopy(renderer, overlay, NULL, &dest_rect);
}
SDL_RenderPresent(renderer);
}
static char *romfilename = "rom.dat";
static char *cartfilename = NULL;
static void init(){
FILE *f;
if(!(f = fopen(romfilename, "rb"))){
perror(romfilename);
exit(EXIT_FAILURE);
}
if(fread(rom, 1, sizeof (rom), f) != sizeof (rom)){
printf("Invalid rom length\n");
exit(EXIT_FAILURE);
}
fclose(f);
memset(cart, 0, sizeof (cart));
if(cartfilename){
FILE *f;
if(!(f = fopen(cartfilename, "rb"))){
perror(cartfilename);
exit(EXIT_FAILURE);
}
fread(cart, 1, sizeof (cart), f);
fclose(f);
}
}
void resize(int width, int height){
long sclx, scly;
long screenx = width;
long screeny = height;
sclx = ALG_MAX_X / width;
scly = ALG_MAX_Y / height;
scl_factor = sclx > scly ? sclx : scly;
offx = (screenx - ALG_MAX_X / scl_factor) / 2;
offy = (screeny - ALG_MAX_Y / scl_factor) / 2;
}
static void readevents(){
SDL_Event e;
while(SDL_PollEvent(&e)){
switch(e.type){
case SDL_QUIT:
exit(EXIT_SUCCESS);
break;
case SDL_WINDOWEVENT:
switch (e.window.event) {
case SDL_WINDOWEVENT_RESIZED:
resize(e.window.data1, e.window.data2);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
resize(e.window.data1, e.window.data2);
break;
}
break;
case SDL_KEYDOWN:
switch(e.key.keysym.sym){
case SDLK_ESCAPE:
exit(EXIT_SUCCESS);
case SDLK_a:
snd_regs[14] &= ~0x01;
break;
case SDLK_s:
snd_regs[14] &= ~0x02;
break;
case SDLK_d:
snd_regs[14] &= ~0x04;
break;
case SDLK_f:
snd_regs[14] &= ~0x08;
break;
case SDLK_LEFT:
alg_jch0 = 0x00;
break;
case SDLK_RIGHT:
alg_jch0 = 0xff;
break;
case SDLK_UP:
alg_jch1 = 0xff;
break;
case SDLK_DOWN:
alg_jch1 = 0x00;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch(e.key.keysym.sym){
case SDLK_a:
snd_regs[14] |= 0x01;
break;
case SDLK_s:
snd_regs[14] |= 0x02;
break;
case SDLK_d:
snd_regs[14] |= 0x04;
break;
case SDLK_f:
snd_regs[14] |= 0x08;
break;
case SDLK_LEFT:
alg_jch0 = 0x80;
break;
case SDLK_RIGHT:
alg_jch0 = 0x80;
break;
case SDLK_UP:
alg_jch1 = 0x80;
break;
case SDLK_DOWN:
alg_jch1 = 0x80;
break;
default:
break;
}
break;
default:
break;
}
}
}
void osint_emuloop(){
Uint32 next_time = SDL_GetTicks() + EMU_TIMER;
vecx_reset();
for(;;){
vecx_emu((VECTREX_MHZ / 1000) * EMU_TIMER);
readevents();
{
Uint32 now = SDL_GetTicks();
if(now < next_time)
SDL_Delay(next_time - now);
else
next_time = now;
next_time += EMU_TIMER;
}
}
}
void load_overlay(const char *filename){
SDL_Surface *image;
image = SDL_LoadBMP(filename);
if(image){
overlay = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
}else{
fprintf(stderr, "IMG_Load: %s\n", IMG_GetError());
}
}
int main(int argc, char *argv[]){
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){
fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
exit(-1);
}
SDL_CreateWindowAndRenderer(330*3/2, 410*3/2, SDL_WINDOW_RESIZABLE, &screen, &renderer);
if(screen == NULL || renderer == NULL){
fprintf(stderr, "Failed to initialize SDL window/renderer: %s\n", SDL_GetError());
exit(-2);
}
resize(330*3/2, 410*3/2);
if(argc > 1)
cartfilename = argv[1];
if(argc > 2)
load_overlay(argv[2]);
init();
e8910_init_sound();
osint_emuloop();
e8910_done_sound();
SDL_Quit();
return 0;
}