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
|
#include <iostream>
#include "Config.h"
#include <stack>
#include "Global.h"
#include "interface/Window.h"
#include "interface/Platform.h"
#include "interface/Engine.h"
#include "Graphics.h"
using namespace ui;
using namespace std;
Engine::Engine():
state_(NULL),
mousex_(0),
mousey_(0),
mousexp_(0),
mouseyp_(0),
FpsLimit(60.0f),
windows(stack<Window*>()),
lastBuffer(NULL),
prevBuffers(stack<pixel*>())
{
}
Engine::~Engine()
{
if(state_ != NULL)
delete state_;
//Dispose of any Windows.
while(!windows.empty())
{
delete windows.top();
windows.pop();
}
}
void Engine::Begin(int width, int height)
{
//engine is now ready
running_ = true;
width_ = width;
height_ = height;
}
void Engine::Exit()
{
running_ = false;
}
void Engine::ShowWindow(Window * window)
{
if(window->Position.X==-1)
{
window->Position.X = (width_-window->Size.X)/2;
}
if(window->Position.Y==-1)
{
window->Position.Y = (height_-window->Size.Y)/2;
}
if(state_)
{
if(lastBuffer)
{
prevBuffers.push(lastBuffer);
}
lastBuffer = (pixel*)malloc((width_ * height_) * PIXELSIZE);
g->fillrect(0, 0, width_, height_, 0, 0, 0, 100);
memcpy(lastBuffer, g->vid, (width_ * height_) * PIXELSIZE);
windows.push(state_);
}
state_ = window;
}
void Engine::CloseWindow()
{
if(!windows.empty())
{
if(!prevBuffers.empty())
{
lastBuffer = prevBuffers.top();
prevBuffers.pop();
}
else
{
free(lastBuffer);
lastBuffer = NULL;
}
state_ = windows.top();
windows.pop();
}
else
{
state_ = NULL;
}
}
/*void Engine::SetState(State * state)
{
if(state_) //queue if currently in a state
statequeued_ = state;
else
{
state_ = state;
if(state_)
state_->DoInitialized();
}
}*/
void Engine::SetSize(int width, int height)
{
width_ = width;
height_ = height;
}
void Engine::Tick(float dt)
{
if(state_ != NULL)
state_->DoTick(dt);
/*if(statequeued_ != NULL)
{
if(state_ != NULL)
{
state_->DoExit();
delete state_;
state_ = NULL;
}
state_ = statequeued_;
statequeued_ = NULL;
if(state_ != NULL)
state_->DoInitialized();
}*/
}
void Engine::Draw()
{
if(lastBuffer && !(state_->Position.X == 0 && state_->Position.Y == 0 && state_->Size.X == width_ && state_->Size.Y == height_))
{
memcpy(g->vid, lastBuffer, (width_ * height_) * PIXELSIZE);
}
else
{
g->Clear();
}
if(state_)
state_->DoDraw();
g->Blit();
}
void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt)
{
if(state_)
state_->DoKeyPress(key, shift, ctrl, alt);
}
void Engine::onKeyRelease(int key, bool shift, bool ctrl, bool alt)
{
if(state_)
state_->DoKeyRelease(key, shift, ctrl, alt);
}
void Engine::onMouseClick(int x, int y, unsigned button)
{
if(state_)
state_->DoMouseDown(x, y, button);
}
void Engine::onMouseUnclick(int x, int y, unsigned button)
{
if(state_)
state_->DoMouseUp(x, y, button);
}
void Engine::onMouseMove(int x, int y)
{
mousex_ = x;
mousey_ = y;
if(state_)
{
state_->DoMouseMove(x, y, mousex_ - mousexp_, mousey_ - mouseyp_);
}
mousexp_ = x;
mouseyp_ = y;
}
void Engine::onMouseWheel(int x, int y, int delta)
{
if(state_)
state_->DoMouseWheel(x, y, delta);
}
void Engine::onResize(int newWidth, int newHeight)
{
SetSize(newWidth, newHeight);
}
void Engine::onClose()
{
if(state_)
state_->DoExit();
}
|