-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameboy.cpp
260 lines (211 loc) · 6.2 KB
/
Gameboy.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
#include "Gameboy.h"
#include "utils.h"
static const int windowWidth = 160;
static const int windowHeight = 144;
Gameboy* Gameboy::GameboyInstancePtr = nullptr;
Emulator* Gameboy::EmulatorInstancePtr = nullptr;
SDL_Window* Gameboy::gWindow = nullptr;
void initEmulatorRender()
{
Gameboy* gbPtr = Gameboy::getGameBoyInstance();
if(gbPtr)
{
gbPtr -> renderGame();
}
else
{
printf("Unable to renderGame because gameboy is not initialized yet");
}
}
//Used to create gameboy instance
Gameboy* Gameboy::createGameBoyInstance()
{
if(GameboyInstancePtr == nullptr)
{
GameboyInstancePtr = new Gameboy();
GameboyInstancePtr -> initSDL();
return GameboyInstancePtr;
}
else
{
return GameboyInstancePtr;
}
}
//Used to get gameboyinstance
Gameboy* Gameboy::getGameBoyInstance()
{
return GameboyInstancePtr;
}
//Used to define the RenderScreen function in the Emulator Class.
//We pass in this function as an argument
//Private Constructor
Gameboy::Gameboy()
{
EmulatorInstancePtr = new Emulator();
EmulatorInstancePtr -> loadCartridge("ROM/Mario.gb");
EmulatorInstancePtr -> initEmulator(initEmulatorRender);
}
//Public Delete
Gameboy::~Gameboy()
{
delete EmulatorInstancePtr;
}
//Implement RenderGame For emulator.
//Need to research on it
void Gameboy::renderGame()
{
//Reset the Color and Depth
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//Reset the current matrix
glLoadIdentity();
//Used to align
// glPixelStorei()
glDrawPixels(windowWidth, windowHeight, GL_RGB, GL_UNSIGNED_BYTE, EmulatorInstancePtr -> screen_Display);
//Not sure what I am supposed to put here
SDL_GL_SwapWindow(gWindow);
}
/*
-----------------------------------------------------------------------------------------
-------------------------------Gameboy Simulation----------------------------------------
-----------------------------------------------------------------------------------------
*/
//Used to start Gameboy simulation in the GameBoyRun.cpp file
void Gameboy::startGameboySimulation()
{
bool quit = false;
SDL_Event event;
float fps = 59.8;
//Interval before frames refresh is 1000miliseconds / 60
float interval = 1000 / fps;
unsigned int totalTimeinMiliPassed = SDL_GetTicks64();
while(!quit)
{
while(SDL_PollEvent(&event))
{
handleInput(event);
if(event.type == quit)
{
quit = true;
}
//Check frames and if it is the case then update the Emulator
unsigned int currentTime = SDL_GetTicks64();
if(totalTimeinMiliPassed + interval < currentTime)
{
//Probably need to implement later on when we want to check issues with timing and what not
// checkFPS();
totalTimeinMiliPassed = SDL_GetTicks64();
EmulatorInstancePtr -> Update();
}
}
}
SDL_Quit();
}
bool Gameboy::initSDL()
{
printf("Initializing SDL.\n");
/* Initialize defaults, Video and Joystick */
if((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
return false;
}
printf("SDL initialized.\n");
//Create the Window
gWindow = SDL_CreateWindow("GameBoy Color", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, 0);
if (gWindow == NULL)
{
printf("Failed To Create SDL Window");
return false;
}
printf("Successfully Created The Window\n");
//Initialize GL
if(!initGL())
{
printf("Unable to initialize openGL!\n");
return false;
}
return true;
}
bool Gameboy::initGL()
{
GLenum error = GL_NO_ERROR;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
error = glGetError();
if(error != GL_NO_ERROR)
{
printf( "Error Loading MatrixMode! %d\n", error);
return false;
}
glOrtho(0, windowWidth, windowHeight, 0, -1.0, 1.0);
error = glGetError();
if(error != GL_NO_ERROR)
{
printf( "Error Setting Up Othographic Camera! %d\n", error);
return false;
}
//-------------------------------
//Set Properties
//-------------------------------
//Set color to black with opacity 1
glClearColor(0, 0, 0, 1.0);
//Clear the buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//Use a flat shading, no gradient or smoothness
glShadeModel(GL_FLAT);
//Enable 2D GL_Texture
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_DITHER);
glDisable(GL_BLEND);
return true;
}
void Gameboy::handleInput(SDL_Event &event)
{
if(event.type == SDL_KEYDOWN)
{
int key = -1;
switch(event.key.keysym.sym)
{
case SDLK_a : key = 4 ; break ;
case SDLK_s : key = 5 ; break ;
case SDLK_SPACE : key = 6 ; break ;
case SDLK_RETURN : key = 7 ; break ;
case SDLK_RIGHT : key = 0 ; break ;
case SDLK_LEFT : key = 1 ; break ;
case SDLK_UP : key = 2 ; break ;
case SDLK_DOWN : key = 3 ; break ;
}
if(key != -1)
{
setKeyPressed(key);
}
}
else if (event.type == SDL_KEYUP)
{
int key = -1;
switch(event.key.keysym.sym)
{
case SDLK_a : key = 4 ; break ;
case SDLK_s : key = 5 ; break ;
case SDLK_SPACE : key = 6 ; break ;
case SDLK_RETURN : key = 7 ; break ;
case SDLK_RIGHT : key = 0 ; break ;
case SDLK_LEFT : key = 1 ; break ;
case SDLK_UP : key = 2 ; break ;
case SDLK_DOWN : key = 3 ; break ;
}
if(key != -1)
{
setKeyReleased(key);
}
}
}
void Gameboy::setKeyPressed(int keyPressed)
{
EmulatorInstancePtr->keyPressed(keyPressed);
}
void Gameboy::setKeyReleased(int keyReleased)
{
EmulatorInstancePtr->keyReleased(keyReleased);
}