-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
174 lines (132 loc) · 2.99 KB
/
Engine.cpp
File metadata and controls
174 lines (132 loc) · 2.99 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
#include "App.h"
int main(int argc, char* argv[])
{
// Start SDL2
SDL_Init(SDL_INIT_EVERYTHING);
// Initialize the engine and user code from app
App engine;
engine.EngineInitialize();
engine.EngineLoad();
// Main game loop
SDL_Event e;
while (engine.IsRunning())
{
engine.Tick();
SDL_SetWindowTitle(engine.GetSDLWindow(), std::to_string(engine.GetFrameTime()).c_str());
// Clear Input
// previousKeyState = currentKeyState
// currentKeyState = Nothing
engine.PushKeyState();
// Fill currentKeyState
while (SDL_PollEvent(&e) != 0)
{
// Call input to notify
engine.Input(e);
if (e.type == SDL_QUIT)
engine.Quit();
}
// Return control to programmer for implementation
engine.EngineUpdate();
// Draw a pretty picture
engine.EngineDraw();
}
engine.EngineUnLoad();
SDL_Quit();
return 0;
}
Engine::Engine() : mRunning(true), fTime(0.0f), prevTime(0), currTime(0)
{
UINT width = 1600;
UINT height = 900;
sDevice.InitDevice(width, height);
mWindow = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
if (mWindow == NULL)
throw "Could not create SDL Window";
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_SOFTWARE);
mTexture = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, sDevice.screen.GetScreenWidth(), sDevice.screen.GetScreenHeight());
// Initialize the array to false
for (unsigned int i = 0; i < INPUT_SIZE; i++)
{
mCurrentKeyState[i] = false;
mPreviousKeyState[i] = false;
}
}
Engine::~Engine()
{
}
void Engine::EngineInitialize()
{
Initialize();
}
// Virtual Method Implemented in App
void Engine::Initialize()
{
}
void Engine::EngineLoad()
{
Load();
}
// Virtual Method Implemented in App
void Engine::Load()
{
}
void Engine::Tick()
{
prevTime = currTime;
currTime = SDL_GetTicks();
fTime = (float)(currTime - prevTime) / 1000.0f;
}
void Engine::EngineUpdate()
{
Update();
}
void Engine::Update()
{
}
void Engine::Input(SDL_Event e)
{
switch (e.type)
{
case SDL_KEYDOWN:
KeyIsDown(e.key.keysym.sym);
break;
case SDL_KEYUP:
KeyIsUp(e.key.keysym.sym);
break;
default:
break;
}
}
inline void Engine::PushKeyState()
{
// Put previousKeyState in currentKeyState
memcpy(&mPreviousKeyState, mCurrentKeyState, INPUT_SIZE);
}
void Engine::EngineUnLoad()
{
// Cleanup and Quit
UnLoad();
SDL_DestroyTexture(mTexture);
SDL_DestroyRenderer(mRenderer);
SDL_DestroyWindow(mWindow);
}
// Virtual Method Implemented in App
void Engine::UnLoad()
{
//delete
}
void Engine::EngineDraw()
{
// Prepare contents on screen buffer
Draw();
// Display texture
unsigned char* frameBuffer = sDevice.screen.GetBuffer();
SDL_UpdateTexture(mTexture, NULL, frameBuffer, sDevice.screen.GetScreenWidth() * 3 * sizeof(unsigned char));
SDL_RenderClear(mRenderer);
SDL_RenderCopyEx(mRenderer, mTexture, NULL, NULL, 0, 0, SDL_FLIP_VERTICAL);
SDL_RenderPresent(mRenderer);
}
// Virtual Method Implemented in App
void Engine::Draw()
{
}