-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
323 lines (278 loc) · 8.46 KB
/
Game.cpp
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
#include "Game.h"
using namespace std;
Game::Game() : cpu(0), loopUFOSound(false), exit(false)
{
//load Space Invaders ROM
cpu.readIntoMem("roms/invaders.h", 0);
cpu.readIntoMem("roms/invaders.g", 0x800);
cpu.readIntoMem("roms/invaders.f", 0x1000);
cpu.readIntoMem("roms/invaders.e", 0x1800);
//cpu.copyROM();
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL init error: " << SDL_GetError() << endl;
}
else
{
initVideo();
}
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
{
cout << "SDL mixer init error" << endl;
}
else
{
initSound();
}
}
bool Game::isRunning()
{
return !exit;
}
void Game::initSound()
{
for (uint i = 0; i < sounds.size(); i++)
{
string soundFile = string("sounds/") + to_string(i) + string(".wav");
sounds[i] = Mix_LoadWAV(soundFile.c_str());
if (sounds[i] == NULL)
{
cout << "warning: failure loading sound '" << i << ".wav'" << endl;
}
}
}
void Game::initVideo()
{
//get host monitor size (for window scaling)
SDL_DisplayMode displayMode;
SDL_GetCurrentDisplayMode(0, &displayMode);
int displayHeight = displayMode.h;
int scaleFactor = displayHeight / SCREEN_HEIGHT;
mainWin = SDL_CreateWindow("Space Invaders", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
scaleFactor * SCREEN_HEIGHT, scaleFactor * SCREEN_WIDTH,
SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(mainWin, -1, SDL_RENDERER_ACCELERATED);
//maintain render aspect ratio in full-screen
SDL_RenderSetLogicalSize(renderer, SCREEN_HEIGHT, SCREEN_WIDTH);
windowTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, SCREEN_HEIGHT, SCREEN_WIDTH);
pixelBuffer = new uint[SCREEN_WIDTH * SCREEN_HEIGHT];
}
void Game::copyToPixelBuffer()
{
array<unsigned char, 0x10000> memory = cpu.getMemory();
int buffIndex = 0;
int bitShift = 0x1;
for (int i = 0x2400; i < 0x2420; i++)
{
for (int p = 0; p < 8; p++)
{
for (int j = 0; j < SCREEN_HEIGHT; j++)
{
int addr = 32 * j + i;
unsigned char currByte = memory[addr];
if ((currByte & bitShift) == bitShift)
{
pixelBuffer[buffIndex++] = 0xffffffff;
}
else
{
pixelBuffer[buffIndex++] = 0x0;
}
}
bitShift <<= 1;
}
bitShift = 0x1;
}
}
void Game::clearPixelBuffer()
{
for (int i = 0; i < SCREEN_HEIGHT*SCREEN_WIDTH; i++)
{
pixelBuffer[i] = 0;
}
}
void Game::updateWindow()
{
//update pixel buffer from CPU memory map
copyToPixelBuffer();
SDL_UpdateTexture(windowTexture, NULL, pixelBuffer, SCREEN_HEIGHT * sizeof(uint));
//render texture to window (flip vertically to make screen upright portrait)
SDL_RenderClear(renderer);
SDL_RenderCopyEx(renderer, windowTexture, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
SDL_RenderPresent(renderer);
}
void Game::runCPU()
{
double now = getTimeUsec();
if (lastTimer == 0.0)
{
lastTimer = now;
nextInterrupt = lastTimer + 16000.0;
whichInterrupt = 1;
}
//graphics interrupts
if (cpu.getInterruptStatus() && (now > nextInterrupt))
{
if (whichInterrupt == 1)
{
cpu.genInterrupt(1);
whichInterrupt = 2;
//updateWindow();
}
else
{
cpu.genInterrupt(2);
whichInterrupt = 1;
//updateWindow();
}
nextInterrupt = now + 8000.0;
}
//ensure that CPU performs ~2M cycles/sec (2MHz)
double sinceLast = now - lastTimer;
int catchUpCycles = 2 * sinceLast;
int cycleCount = 0;
while (catchUpCycles > cycleCount)
{
cycleCount += cpu.emulateCycle();
//cpu.debugPrint();
}
lastTimer = now;
}
double Game::getTimeUsec()
{
timeval time;
gettimeofday(&time, NULL);
return ((double)time.tv_sec * 1E6) + ((double)time.tv_usec);
}
void Game::playSounds()
{
unsigned char currSoundPort3 = cpu.getSoundPort3();
unsigned char currSoundPort5 = cpu.getSoundPort5();
if (loopUFOSound)
{
Mix_PlayChannel(1, sounds[0], 0);
}
if (currSoundPort3 != lastSoundPort3)
{
if ((currSoundPort3 & 0x1) && !(lastSoundPort3 & 0x1))
{
//play UFO repeatedly (0.wav)
loopUFOSound = true;
}
else if (!(currSoundPort3 & 0x1) && (lastSoundPort3 & 0x1))
{
Mix_HaltChannel(1);
loopUFOSound = false;
}
if ((currSoundPort3 & 0x2) && !(lastSoundPort3 & 0x2))
{
//player shot (1.wav)
Mix_PlayChannel(-1, sounds[1], 0);
}
if ((currSoundPort3 & 0x4) && !(lastSoundPort3 & 0x4))
{
//player die (2.wav)
Mix_PlayChannel(-1, sounds[2], 0);
}
if ((currSoundPort3 & 0x8) && !(lastSoundPort3 & 0x8))
{
//invader die (3.wav)
Mix_PlayChannel(-1, sounds[3], 0);
}
lastSoundPort3 = cpu.getSoundPort3();
}
if (currSoundPort5 != lastSoundPort5)
{
if ((currSoundPort5 & 0x1) && !(lastSoundPort5 & 0x1))
{
//play invader sound (4.wav)
Mix_PlayChannel(-1, sounds[4], 0);
}
if ((currSoundPort5 & 0x2) && !(lastSoundPort5 & 0x2))
{
//play invader sound (5.wav)
Mix_PlayChannel(-1, sounds[5], 0);
}
if ((currSoundPort5 & 0x4) && !(lastSoundPort5 & 0x4))
{
//play invader sound (6.wav)
Mix_PlayChannel(-1, sounds[6], 0);
}
if ((currSoundPort5 & 0x8) && !(lastSoundPort5 & 0x8))
{
//play invader sound (7.wav)
Mix_PlayChannel(-1, sounds[7], 0);
}
if ((currSoundPort5 & 0x10) && !(lastSoundPort5 & 0x10))
{
//play UFO hit sound (8.wav)
Mix_PlayChannel(-1, sounds[8], 0);
}
lastSoundPort5 = cpu.getSoundPort5();
}
}
void Game::pollKeyboard()
{
SDL_PollEvent(&keyEvent);
switch (keyEvent.type)
{
case SDL_KEYDOWN:
switch (keyEvent.key.keysym.sym)
{
//set port bits here!
case SDLK_LEFT:
//cout << "left" << endl;
cpu.setP1Left(true);
break;
case SDLK_RIGHT:
//cout << "right" << endl;
cpu.setP1Right(true);
break;
case SDLK_SPACE:
//cout << "fire" << endl;
cpu.setP1Fire(true);
break;
case SDLK_RETURN:
//cout << "start" << endl;
cpu.setP1Start(true);
break;
case SDLK_RSHIFT:
//cout << "start" << endl;
cpu.setCoin(true);
break;
case SDLK_TAB:
cpu.setDebug(true);
break;
case SDLK_ESCAPE:
SDL_Quit();
exit = true;
break;
}
break;
case SDL_KEYUP:
switch (keyEvent.key.keysym.sym)
{
case SDLK_LEFT:
cpu.setP1Left(false);
break;
case SDLK_RIGHT:
cpu.setP1Right(false);
break;
case SDLK_SPACE:
cpu.setP1Fire(false);
break;
case SDLK_RETURN:
cpu.setP1Start(false);
break;
case SDLK_RSHIFT:
cpu.setCoin(false);
break;
case SDLK_TAB:
cpu.setDebug(false);
break;
}
break;
}
}