-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMap.cpp
More file actions
285 lines (257 loc) · 8.33 KB
/
Copy pathTileMap.cpp
File metadata and controls
285 lines (257 loc) · 8.33 KB
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
#include "TileMap.h"
#include <iostream>
// Setups tilemap and game window
TileMap::TileMap(std::shared_ptr<CPUTileMapData> chip8sd) {
// Checks if the SDL library is initialised and working
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
SDL_Log("Couldn't initialise SDL: %s\n", SDL_GetError());
return;
}
// Creating and checking window was created
window = SDL_CreateWindow("Just a chill dude", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (!window) {
SDL_Log("Couldn't create window: %s\n", SDL_GetError());
return;
}
// Creating and checking renderer was created
renderer = SDL_CreateRenderer(window, NULL);
if (!renderer) {
SDL_Log("Couldn't create renderer: %s\n", SDL_GetError());
return;
}
// Contents of game window will be black or white
whiteTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, TEXTURE_SIZE, TEXTURE_SIZE);
blackTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, TEXTURE_SIZE, TEXTURE_SIZE);
// Checks to see if the texture was created
if (!whiteTexture) {
SDL_Log("Couldn't create white texture: %s\n", SDL_GetError());
return;
}
if (!blackTexture) {
SDL_Log("Couldn't create black texture: %s\n", SDL_GetError());
return;
}
// Setting the colour of each texture to their respective colours e.g. white for white texture
SDL_SetRenderTarget(renderer, whiteTexture);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderTarget(renderer, blackTexture);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Reseting renderer back to main game window
SDL_SetRenderTarget(renderer, NULL);
// Initialising all values in tilemap to false, false = black and true = white
resetMap();
// Initialise audio data
audioData = NULL;
audioDataLen = 0;
// Check if audio can be loaded from .wav file
if (!SDL_LoadWAV("Audio-Files/beep-02.wav", &audioSpec, &audioData, &audioDataLen)) {
SDL_Log("Couldn't load audio from .wave file: %s", SDL_GetError());
return;
}
// Open audio device stream
stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audioSpec, NULL, NULL);
// Check to see if stream was created
if (!stream) {
SDL_Log("Couldn't create audio stream: %s\n", SDL_GetError());
return;
}
// Unpause audio (usually off to begin with)
SDL_ResumeAudioStreamDevice(stream);
// Initialising Shared Data
Chip8SD = std::move(chip8sd);
}
// Updates tilemap with a given starting (x, y), size and contents to update tilemap with
void TileMap::updateMap(std::size_t x, std::size_t y, std::size_t N, const std::vector<std::vector<bool>>& updateArea) {
// Set x in range 0 - 63 and make sure it doesn't go off edge of screen
std::size_t x_start = Chip8SD->getVRegister(x) % TILEMAP_WIDTH;
std::size_t x_end = (x_start + 8 < TILEMAP_WIDTH) ? x_start + 8 : TILEMAP_WIDTH;
// Set y in range 0 - 32 and make sure it doesn't go off edge of screen
std::size_t y_start = Chip8SD->getVRegister(y) % TILEMAP_HEIGHT;
std::size_t y_end = (y_start + N < TILEMAP_HEIGHT) ? y_start + N : TILEMAP_HEIGHT;
// Set VF to 0
Chip8SD->setVRegister(0xF, 0);
// Both tilemap and updated portion are 2D arrays
// Two pointers for each array are used to track current index
auto updateAreaRow = updateArea.begin();
for (std::size_t i = y_start; i < y_end; i++) {
auto updateAreaCol = updateAreaRow->begin();
for (std::size_t j = x_start; j < x_end; j++) {
// Check for collision
if (tileMap[i][j] && *(updateAreaCol)){
Chip8SD->setVRegister(0xF, 1);
}
tileMap[i][j] ^= *(updateAreaCol);
updateAreaCol++;
}
updateAreaRow ++;
}
}
// Add audio to stream to be played
void TileMap::getAudio() {
// When current audio is being used, put same audio back into queue
if (SDL_GetAudioStreamQueued(stream) <= (int)audioDataLen) {
SDL_PutAudioStreamData(stream, audioData, audioDataLen);
}
}
// Nested loop to output contents of tilemap to game window
void TileMap::Draw() {
for (std::size_t i = 0; i < TILEMAP_HEIGHT; i++) {
for (std::size_t j = 0; j < TILEMAP_WIDTH; j++) {
// rectangle is used to help set position of texture within game window
textureRect = { (float) j * RECT_SIZE, (float) i * RECT_SIZE, RECT_SIZE, RECT_SIZE};
// If cell in tilemap is set to true, then white is shown, else black
if (tileMap[i][j] == true) {
SDL_RenderTexture(renderer, whiteTexture, NULL, &textureRect);
}
else {
SDL_RenderTexture(renderer, blackTexture, NULL, &textureRect);
}
}
}
// Outputs updated tilemap to game window
SDL_RenderPresent(renderer);
}
// Nested loop to set every pixel in tilemap to false (off)
void TileMap::resetMap() {
for (std::size_t i = 0; i < TILEMAP_HEIGHT; i++) {
for (std::size_t j = 0; j < TILEMAP_WIDTH; j++) {
// tilemap pixel is reset on this line
tileMap[i][j] = false;
}
}
}
// Destroys all game window relevant attributes in order to prevent memory leaks
void TileMap::Destroy() {
// These attributes need to be manually deleted when terminating program
SDL_free(audioData);
SDL_DestroyAudioStream(stream);
SDL_DestroyTexture(whiteTexture);
SDL_DestroyTexture(blackTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
// End program once all necessary attributes have been destroyed
SDL_Quit();
}
// scan for current events happening
void TileMap::getEvent() {
SDL_Event e;
//Get event data
while (SDL_PollEvent(&e) == true)
{
switch (e.type) {
// checks if exit button is pressed
case SDL_EVENT_QUIT:
Chip8SD->setExitStatus(true); // This will set quit to true (game loop ends)
break;
// All possible key presses (0-F)
case SDL_EVENT_KEY_DOWN:
// If key is held down, then add it mark it true in key press array
switch (e.key.scancode) {
case SDL_SCANCODE_1:
Chip8SD->setKeyPress(0x1, true);
break;
case SDL_SCANCODE_2:
Chip8SD->setKeyPress(0x2, true);
break;
case SDL_SCANCODE_3:
Chip8SD->setKeyPress(0x3, true);
break;
case SDL_SCANCODE_4:
Chip8SD->setKeyPress(0xC, true);
break;
case SDL_SCANCODE_Q:
Chip8SD->setKeyPress(0x4, true);
break;
case SDL_SCANCODE_W:
Chip8SD->setKeyPress(0x5, true);
break;
case SDL_SCANCODE_E:
Chip8SD->setKeyPress(0x6, true);
break;
case SDL_SCANCODE_R:
Chip8SD->setKeyPress(0xD, true);
break;
case SDL_SCANCODE_A:
Chip8SD->setKeyPress(0x7, true);
break;
case SDL_SCANCODE_S:
Chip8SD->setKeyPress(0x8, true);
break;
case SDL_SCANCODE_D:
Chip8SD->setKeyPress(0x9, true);
break;
case SDL_SCANCODE_F:
Chip8SD->setKeyPress(0xE, true);
break;
case SDL_SCANCODE_Z:
Chip8SD->setKeyPress(0xA, true);
break;
case SDL_SCANCODE_X:
Chip8SD->setKeyPress(0x0, true);
break;
case SDL_SCANCODE_C:
Chip8SD->setKeyPress(0xB, true);
break;
case SDL_SCANCODE_V:
Chip8SD->setKeyPress(0xF, true);
break;
}
break;
// If key is released, then mark it false in key press array
case SDL_EVENT_KEY_UP:
switch (e.key.scancode) {
case SDL_SCANCODE_1:
Chip8SD->setKeyPress(0x1, false);
break;
case SDL_SCANCODE_2:
Chip8SD->setKeyPress(0x2, false);
break;
case SDL_SCANCODE_3:
Chip8SD->setKeyPress(0x3, false);
break;
case SDL_SCANCODE_4:
Chip8SD->setKeyPress(0xC, false);
break;
case SDL_SCANCODE_Q:
Chip8SD->setKeyPress(0x4, false);
break;
case SDL_SCANCODE_W:
Chip8SD->setKeyPress(0x5, false);
break;
case SDL_SCANCODE_E:
Chip8SD->setKeyPress(0x6, false);
break;
case SDL_SCANCODE_R:
Chip8SD->setKeyPress(0xD, false);
break;
case SDL_SCANCODE_A:
Chip8SD->setKeyPress(0x7, false);
break;
case SDL_SCANCODE_S:
Chip8SD->setKeyPress(0x8, false);
break;
case SDL_SCANCODE_D:
Chip8SD->setKeyPress(0x9, false);
break;
case SDL_SCANCODE_F:
Chip8SD->setKeyPress(0xE, false);
break;
case SDL_SCANCODE_Z:
Chip8SD->setKeyPress(0xA, false);
break;
case SDL_SCANCODE_X:
Chip8SD->setKeyPress(0x0, false);
break;
case SDL_SCANCODE_C:
Chip8SD->setKeyPress(0xB, false);
break;
case SDL_SCANCODE_V:
Chip8SD->setKeyPress(0xF, false);
break;
}
break;
}
}
}