-
Notifications
You must be signed in to change notification settings - Fork 0
/
space-invaders.c
716 lines (654 loc) · 19.5 KB
/
space-invaders.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* Space Invaders emulator.
Copyright (C) 2023-2024 Collin Funk
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "i8080.h"
/*
*
* 0000 - 1fff rom
* 2000 - 23ff ram
* 2400 - 3fff video RAM
* 4000 - ram mirror
*/
#define SI_MEMORY_SIZE 0x4000
#define SI_VRAM_OFFSET 0x2400
/*
* Space Invaders was originally made for the Taito 8080 in Japan and
* then was liscened to Midway for US/EU markets. It's kind of hard to
* find information on the Taito 8080 and Midway 8080 and I am NOT smart
* enough to read the ciruit diagrams and stuff. From what I can find the
* i8080 used had a clock speed of 2MHz or slightly under 2 MHz.
*
* The screen was 256x224 but rotated 90 degrees counter-clockwise in the
* Space Invaders cabinet. It had a refresh rate of 60 Hz. Each pixel is
* on/off so 1 byte encodes for 8 pixels ((256 * 244) / 8 = 7168 bytes).
* Original machines used physical covers on portions on the screen for color
* but later supported colored images.
*
* The game uses RST 1 (call $0x08) and RST 2 (call $0x10) for interrupts.
* Interrupts happen around twice per second since their timings are based
* on the vertical blanking interval of the CRT monitor. Interrupt 1 (RST 1)
* is used when the beam is around the middle of the screen. The second
* interrupt (RST 2) is used when the beam is at the last line of the screen.
*/
/* 2 MHz */
#define SI_CLOCK_SPEED 2000000
/* 60 Hz screen */
#define SI_REFRESH_RATE 60
/* Clock speed / Refresh rate */
#define SI_CYCLES_PER_FRAME 33333
/* Interrupts twice per frame, see above */
#define SI_CYCLES_PER_INT 16666
/* 8 pixels per bit, see above */
#define SI_SCREEN_BITS 7168
/*
* SI_SCREEN_WIDTH and SI_SCREEN_HEIGHT are name based on the rotated
* screen for clarity with SDL functions.
*/
#define SI_SCREEN_WIDTH 224
#define SI_SCREEN_HEIGHT 256
/*
* ABGR colors
* These colors are just a guess from images online. I have
* no clue what color codes they actually use.
*/
#define SI_ABGR_GREEN 0xff33ff00
#define SI_ABGR_RED 0xff0000ff
#define SI_ABGR_WHITE 0xffffffff
#define SI_ABGR_BLACK 0xff000000
struct spaceinvaders
{
struct i8080 cpu;
uint8_t *memory;
size_t memory_size;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
uint32_t *video_buffer;
bool exit_flag; /* Signals the end of the loop. */
bool pause_flag; /* 1 if emulation is paused. */
bool color_flag; /* 1 for color, 0 for black and white */
uint8_t inp0; /* Input port 0, unused? */
uint8_t inp1; /* Input port 1 */
uint8_t inp2; /* Input port 2 */
uint8_t shift0; /* Shift register lsb */
uint8_t shift1; /* Shift register msb */
uint8_t shift_offset; /* Shift offset */
uint8_t next_int; /* RST 1 (0xcf) or RST 2 (0xd7) */
uint32_t curr_time;
uint32_t prev_time;
uint32_t delta_time;
};
static void usage (void);
static struct spaceinvaders *spaceinvaders_create (void);
static void spaceinvaders_destroy (struct spaceinvaders *);
static int spaceinvaders_load_file (struct spaceinvaders *, const char *);
static int sdl_init (struct spaceinvaders *);
static uint8_t spaceinvaders_read_byte (void *, uint16_t);
static void spaceinvaders_write_byte (void *, uint16_t, uint8_t);
static uint8_t spaceinvaders_io_inb (void *, uint8_t);
static void spaceinvaders_io_outb (void *, uint8_t, uint8_t);
static void spaceinvaders_update_texture (struct spaceinvaders *);
static void spaceinvaders_update_screen (struct spaceinvaders *);
static void spaceinvaders_handle_keydown (struct spaceinvaders *,
SDL_Scancode);
static void spaceinvaders_handle_keyup (struct spaceinvaders *, SDL_Scancode);
static void spaceinvaders_handle_cpu (struct spaceinvaders *);
static void spaceinvaders_set_pixel (struct spaceinvaders *, uint32_t,
uint32_t, uint32_t);
static void spaceinvaders_handle_vram_bit (struct spaceinvaders *, uint8_t,
uint32_t, uint32_t);
static void spaceinvaders_handle_vram (struct spaceinvaders *);
static uint32_t spaceinvaders_get_deltatime32 (struct spaceinvaders *);
static void spaceinvaders_loop (struct spaceinvaders *);
int
main (int argc, char **argv)
{
struct spaceinvaders *emu;
if (argc != 2)
usage ();
emu = spaceinvaders_create ();
if (emu == NULL)
return 1;
if (spaceinvaders_load_file (emu, argv[1]) < 0 || sdl_init (emu) < 0)
{
spaceinvaders_destroy (emu);
return 1;
}
while (!emu->exit_flag)
spaceinvaders_loop (emu);
spaceinvaders_destroy (emu);
return 0;
}
static void
usage (void)
{
fprintf (stderr, "spaceinvaders file\n");
exit (1);
}
static struct spaceinvaders *
spaceinvaders_create (void)
{
struct spaceinvaders *emu;
emu = (struct spaceinvaders *) calloc (1, sizeof (struct spaceinvaders));
if (emu == NULL)
return NULL;
i8080_init (&emu->cpu, emu, spaceinvaders_read_byte,
spaceinvaders_write_byte, spaceinvaders_io_inb,
spaceinvaders_io_outb);
emu->color_flag = true;
emu->next_int = 0xcf;
return emu;
}
static void
spaceinvaders_destroy (struct spaceinvaders *emu)
{
if (emu != NULL)
{
SDL_DestroyTexture (emu->texture);
SDL_DestroyRenderer (emu->renderer);
SDL_DestroyWindow (emu->window);
SDL_Quit ();
free (emu->video_buffer);
free (emu->memory);
free (emu);
}
}
static int
sdl_init (struct spaceinvaders *emu)
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
uint32_t *video_buffer;
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
fprintf (stderr, "SDL_Init(): %s.\n", SDL_GetError ());
return -1;
}
/* User defined ratios for screen? */
window = SDL_CreateWindow ("Space Invaders Emulator", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SI_SCREEN_WIDTH * 4,
SI_SCREEN_HEIGHT * 4, SDL_WINDOW_RESIZABLE);
if (window == NULL)
{
fprintf (stderr, "SDL_CreateWindow(): %s.\n", SDL_GetError ());
return -1;
}
renderer = SDL_CreateRenderer (
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL)
{
fprintf (stderr, "SDL_CreateRenderer(): %s.\n", SDL_GetError ());
SDL_DestroyWindow (window);
return -1;
}
/* If window is maximized it won't stretch. */
if (SDL_RenderSetLogicalSize (renderer, SI_SCREEN_WIDTH, SI_SCREEN_HEIGHT)
< 0)
{
fprintf (stderr, "SDL_RendererSetLogicalSize(): %s.\n", SDL_GetError ());
SDL_DestroyWindow (window);
SDL_DestroyRenderer (renderer);
return -1;
}
texture = SDL_CreateTexture (renderer, SDL_PIXELFORMAT_ABGR8888,
SDL_TEXTUREACCESS_STREAMING, SI_SCREEN_WIDTH,
SI_SCREEN_HEIGHT);
if (texture == NULL)
{
fprintf (stderr, "SDL_CreateTexture(): %s.\n", SDL_GetError ());
SDL_DestroyWindow (window);
SDL_DestroyRenderer (renderer);
return -1;
}
video_buffer = (uint32_t *) calloc (sizeof (uint32_t),
SI_SCREEN_WIDTH * SI_SCREEN_HEIGHT);
if (video_buffer == NULL)
{
fprintf (stderr, "Failed to allocate video memory.\n");
SDL_DestroyWindow (window);
SDL_DestroyRenderer (renderer);
SDL_DestroyTexture (texture);
return -1;
}
SDL_UpdateTexture (texture, NULL, video_buffer, 4 * SI_SCREEN_WIDTH);
emu->window = window;
emu->renderer = renderer;
emu->texture = texture;
emu->video_buffer = video_buffer;
return 0;
}
static int
spaceinvaders_load_file (struct spaceinvaders *emu, const char *file)
{
struct stat st;
FILE *fp;
uint8_t *memory;
if (stat (file, &st) < 0)
{
fprintf (stderr, "%s: %s.\n", file, strerror (errno));
return -1;
}
if (!S_ISREG (st.st_mode))
{
fprintf (stderr, "%s: Not a regular file.\n", file);
return -1;
}
if (st.st_size != 8192)
{
fprintf (stderr,
"%s: Invalid file size. Input the invaders "
"image combined.\n",
file);
return -1;
}
fp = fopen (file, "rb");
if (fp == NULL)
{
fprintf (stderr, "%s: %s.\n", file, strerror (errno));
return -1;
}
memory = (uint8_t *) calloc (1, UINT16_MAX);
if (memory == NULL)
{
fprintf (stderr, "Memory allocation failed.\n");
fclose (fp);
return -1;
}
if (fread (memory, (size_t) st.st_size, 1, fp) != 1)
{
fprintf (stderr, "%s: Failed to load file.\n", file);
fclose (fp);
free (memory);
return -1;
}
emu->memory = memory;
emu->memory_size = UINT16_MAX;
fclose (fp);
return 0;
}
static uint8_t
spaceinvaders_read_byte (void *emuptr, uint16_t address)
{
struct spaceinvaders *emu = (struct spaceinvaders *) emuptr;
if (emu->memory_size > address && address <= UINT16_C (0x6000))
{
if (address < UINT16_C (0x4000))
return emu->memory[address];
else
return emu->memory[address - UINT16_C (0x2000)];
}
else
return UINT8_C (0);
}
static void
spaceinvaders_write_byte (void *emuptr, uint16_t address, uint8_t value)
{
struct spaceinvaders *emu = (struct spaceinvaders *) emuptr;
if (emu->memory_size > address && address >= UINT16_C (0x2000)
&& address <= UINT16_C (0x4000))
emu->memory[address] = value;
}
static uint8_t
spaceinvaders_io_inb (void *emuptr, uint8_t port)
{
struct spaceinvaders *emu = (struct spaceinvaders *) emuptr;
uint8_t value;
uint16_t s;
switch (port)
{
case 0x00: /* Unused ? */
value = emu->inp0;
break;
case 0x01: /* Input 1 */
value = emu->inp1;
break;
case 0x02: /* Input 2 */
value = emu->inp2;
break;
case 0x03: /* Shift register */
s = ((uint16_t) emu->shift1 << 8);
s |= ((uint16_t) emu->shift0);
value = (s >> (8 - emu->shift_offset)) & 0xff;
break;
default: /* Invalid port */
value = 0;
break;
}
return value;
}
static void
spaceinvaders_io_outb (void *emuptr, uint8_t port, uint8_t value)
{
struct spaceinvaders *emu = (struct spaceinvaders *) emuptr;
switch (port)
{
case 0x02: /* Shift amount (3 bits) */
emu->shift_offset = value & 0x07;
break;
case 0x03: /* Sound bits */
break;
case 0x04: /* Shift data */
emu->shift0 = emu->shift1;
emu->shift1 = value;
break;
case 0x05: /* Sound bits */
break;
case 0x06: /* Watch dog */
/* Pretty sure this checks if the machine crashes? */
break;
}
}
static void
spaceinvaders_update_texture (struct spaceinvaders *emu)
{
void *pixels = NULL;
int pitch;
if (SDL_LockTexture (emu->texture, NULL, &pixels, &pitch) < 0)
{
fprintf (stderr, "SDL_LockTexture(): %s.\n", SDL_GetError ());
return;
}
memcpy (pixels, emu->video_buffer, pitch * SI_SCREEN_HEIGHT);
SDL_UnlockTexture (emu->texture);
}
static void
spaceinvaders_update_screen (struct spaceinvaders *emu)
{
SDL_RenderClear (emu->renderer);
SDL_RenderCopy (emu->renderer, emu->texture, NULL, NULL);
SDL_RenderPresent (emu->renderer);
}
/*
* Inputs:
* Port 1:
* Bit 0 (0x01): Credit
* Bit 1 (0x02): 2 player start
* Bit 2 (0x04): 1 player start
* Bit 3 (0x08): Always 1
* Bit 4 (0x10): Player 1 fired missle
* Bit 5 (0x20): Player 1 moved left
* Bit 6 (0x40): Player 1 moved right
* Bit 7 (0x80): Not connected
* Port 2:
* Bit 0 (0x01): ???
* Bit 1 (0x02): ???
* Bit 2 (0x04): ???
* Bit 3 (0x08): ???
* Bit 4 (0x10): Player 2 fired missle
* Bit 5 (0x20): Player 2 moved left
* Bit 6 (0x40): Player 2 moved right
* Bit 7 (0x80): ???
*/
static void
spaceinvaders_handle_keydown (struct spaceinvaders *emu, SDL_Scancode key)
{
switch (key)
{
case SDL_SCANCODE_3: /* Insert coin */
emu->inp1 |= 0x01;
break;
case SDL_SCANCODE_2: /* Two players */
emu->inp1 |= 0x02;
break;
case SDL_SCANCODE_1: /* One player */
emu->inp1 |= 0x04;
break;
case SDL_SCANCODE_SPACE: /* Fire missle */
emu->inp1 |= 0x10;
emu->inp2 |= 0x10;
break;
case SDL_SCANCODE_A: /* Move left */
/* fallthrough */
case SDL_SCANCODE_LEFT:
emu->inp1 |= 0x20;
emu->inp2 |= 0x20;
break;
case SDL_SCANCODE_D: /* Move right */
/* fallthrough */
case SDL_SCANCODE_RIGHT:
emu->inp1 |= 0x40;
emu->inp2 |= 0x40;
break;
case SDL_SCANCODE_ESCAPE: /* Exit */
emu->exit_flag = false;
break;
case SDL_SCANCODE_E: /* Toggle color emulation */
emu->color_flag = (emu->color_flag == 1);
break;
case SDL_SCANCODE_Q: /* Pause toggle */
emu->pause_flag = (emu->pause_flag == 0);
default:
break;
}
}
static void
spaceinvaders_handle_keyup (struct spaceinvaders *emu, SDL_Scancode key)
{
switch (key)
{
case SDL_SCANCODE_3: /* Insert coin */
emu->inp1 &= ~0x01;
break;
case SDL_SCANCODE_2: /* Two players */
emu->inp1 &= ~0x02;
break;
case SDL_SCANCODE_1: /* One player */
emu->inp1 &= ~0x04;
break;
case SDL_SCANCODE_SPACE: /* Fire missle */
emu->inp1 &= ~0x10;
emu->inp2 &= ~0x10;
break;
case SDL_SCANCODE_A: /* Move left */
/* fallthrough */
case SDL_SCANCODE_LEFT:
emu->inp1 &= ~0x20;
emu->inp2 &= ~0x20;
break;
case SDL_SCANCODE_D: /* Move right */
/* fallthrough */
case SDL_SCANCODE_RIGHT:
emu->inp1 &= ~0x40;
emu->inp2 &= ~0x40;
break;
default:
break;
}
}
/*
* Each frame has 120 interrupts total, 60 of each RST 1 and RST 2.
* The interrupts are based on the raster scanning of the CRT monitor.
* RST 1 is used when the beam is towards the middle of the monitor and
* RST 2 is used when the beam is about to do a verticle retrace to
* draw the next frame. Therefore we alternate between each interrupt
* performing 120 (refresh rate * 2) interrupts total. When RST 2 is called
* we also update the actual screen based on the contents of vram.
*/
static void
spaceinvaders_handle_cpu (struct spaceinvaders *emu)
{
struct i8080 *cpu = &emu->cpu;
const uint64_t need = (emu->delta_time * SI_CLOCK_SPEED) / 1000;
uint64_t i, prev, diff;
for (i = diff = 0; i < need; i += diff)
{
prev = emu->cpu.cycles;
i8080_step (cpu);
diff = cpu->cycles - prev;
if (cpu->cycles >= SI_CYCLES_PER_INT)
{
cpu->cycles -= SI_CYCLES_PER_INT;
i8080_interrupt (cpu, emu->next_int);
if (emu->next_int == 0xcf)
emu->next_int = 0xd7;
else
{
spaceinvaders_handle_vram (emu);
emu->next_int = 0xcf;
}
}
}
}
/*
* Estimation of the colors based on the overlay images I could find online.
* Probably a little off but it is what it is. This can def be cleaned up.
* If you comment out:
* if (set == 0)
* emu->video_buffer[off] = SI_ABGR_BLACK;
* else
* in spaceinvaders_handle_vram_bit() you can see the overlay which might help
* if actual dimensions are out there somewhere.
*/
static void
spaceinvaders_set_pixel (struct spaceinvaders *emu, uint32_t off, uint32_t cx,
uint32_t cy)
{
/* Lives / Credit area */
if (cy >= 240)
{
/* Lives Number. */
if (cx < 16)
emu->video_buffer[off] = SI_ABGR_WHITE;
/* Ships avaliable. */
else if (cx < 102)
emu->video_buffer[off] = SI_ABGR_GREEN;
/* Credits. */
else
emu->video_buffer[off] = SI_ABGR_WHITE;
}
else if (cy >= 184)
{
/* Barrier and player, 10 point alien on start screen. */
emu->video_buffer[off] = SI_ABGR_GREEN;
}
else if (cy >= 64)
{
/* The main portion of the screen with all the aliens. */
emu->video_buffer[off] = SI_ABGR_WHITE;
}
else if (cy >= 32)
{
/* UFO and missle explosions. */
emu->video_buffer[off] = SI_ABGR_RED;
}
else
{
/*
* High score. I've seen this red in some images but I think
* it is supposed to be white.
*/
emu->video_buffer[off] = SI_ABGR_WHITE;
}
}
/*
* Space invaders machines have a screen thats rotated 90 degrees counter
* clockwise. A good diagram is at the bottom of this page:
* https://computerarcheology.com/Arcade/SpaceInvaders/Hardware.html
*/
static void
spaceinvaders_handle_vram_bit (struct spaceinvaders *emu, uint8_t cb,
uint32_t xoff, uint32_t y)
{
uint32_t *vbuff;
uint32_t i, cx, cy, tx, off;
uint8_t set;
vbuff = emu->video_buffer;
for (i = 0; i < 8; ++i, cb = (cb >> 1))
{
cx = xoff + i;
cy = y;
set = (cb & 0x01) != 0 ? 1 : 0;
/* Rotate */
tx = cx;
cx = cy;
cy = SI_SCREEN_HEIGHT - tx - 1;
off = (cy * SI_SCREEN_WIDTH) + cx;
/* Unlit pixels */
if (set == 0)
vbuff[off] = SI_ABGR_BLACK;
else if (emu->color_flag)
spaceinvaders_set_pixel (emu, off, cx, cy);
else
vbuff[off] = SI_ABGR_WHITE;
}
}
static void
spaceinvaders_handle_vram (struct spaceinvaders *emu)
{
uint32_t i, y, xoff;
for (i = 0; i < SI_SCREEN_BITS; ++i)
{
y = (i * 8) / 256;
xoff = (i * 8) & 255;
spaceinvaders_handle_vram_bit (emu, emu->memory[SI_VRAM_OFFSET + i],
xoff, y);
}
spaceinvaders_update_texture (emu);
}
/*
* Probably a more percise way to handle timing but SDL_GetTicks() returns
* milliseconds which seems to work fine. SDL version 2.0.18 has
* SDL_GetTicks64() which should be used to avoid overflows but Debian stable
* still uses version 2.0.14. This function just handles any overflows if
* someone spends ~49 days on space invaders.
*/
static uint32_t
spaceinvaders_get_deltatime32 (struct spaceinvaders *emu)
{
/* Only time this happens is if curr_time overflows. */
if (emu->prev_time > emu->curr_time)
return UINT32_MAX - emu->prev_time + emu->curr_time;
else
return emu->curr_time - emu->prev_time;
}
static void
spaceinvaders_loop (struct spaceinvaders *emu)
{
SDL_Event event;
/* Milliseconds since SDL_Init() */
emu->curr_time = SDL_GetTicks ();
emu->delta_time = spaceinvaders_get_deltatime32 (emu);
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_QUIT:
emu->exit_flag = true;
break;
case SDL_KEYDOWN:
spaceinvaders_handle_keydown (emu, event.key.keysym.scancode);
break;
case SDL_KEYUP:
spaceinvaders_handle_keyup (emu, event.key.keysym.scancode);
break;
default:
break;
}
}
/* If delta time is 0 we can chill. */
if (emu->delta_time > 0 && !emu->pause_flag)
{
spaceinvaders_handle_cpu (emu);
spaceinvaders_update_screen (emu);
}
emu->prev_time = emu->curr_time;
}