-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.cpp
More file actions
342 lines (255 loc) · 5.93 KB
/
Copy pathCore.cpp
File metadata and controls
342 lines (255 loc) · 5.93 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "Core.h"
//set the instance to zero so only one is created in getinstance
Core *Core::instance = 0;
void Core::Init(int w, int h, string title)
{
Log::Init();
//set private values
ScreenWidth = w;
ScreenHeight = h;
ScreenPixels = w * h;
//create our pixel array
PixelArray = new Uint32[ScreenPixels];
//init sdl
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
Log::SendMessage(SDL_GetError(), ERROR);
}
else
{
Log::SendMessage("SDL Initialized Correctly", MESSAGE);
}
//create window and get window surface
theWindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN);
if (theWindow == NULL)
{
Log::SendMessage(SDL_GetError(), ERROR);
}
else
{
Log::SendMessage("SDL Created Window Correctly", MESSAGE);
}
windowSurface = SDL_GetWindowSurface(theWindow);
//create render so we can draw
theRenderer = SDL_CreateRenderer(theWindow, NULL, SDL_RENDERER_ACCELERATED);
if (theRenderer == NULL)
{
Log::SendMessage(SDL_GetError(), ERROR);
}
else
{
Log::SendMessage("SDL Created Renderer Correctly", MESSAGE);
}
}
void Core::Quit()
{
//delete allocated memory
delete PixelArray;
//destroy renderer
SDL_DestroyRenderer(theRenderer);
Log::SendMessage("Destroyed Renderer", MESSAGE);
//destroy window surface
SDL_FreeSurface(windowSurface);
//destroy window
SDL_DestroyWindow(theWindow);
Log::SendMessage("Destroyed Window", MESSAGE);
//quit
SDL_Quit();
Log::SendMessage("Quit SDL", MESSAGE);
Log::Close();
}
SDL_Window *Core::GetWindow()
{
return theWindow;
}
SDL_Renderer *Core::GetRenderer()
{
return theRenderer;
}
//returns singleton
Core *Core::GetInstance()
{
if (instance == 0)
{
instance = new Core();
}
return instance;
}
//puts a passed color pixel at an x,y point
void Core::PutPixel(int x, int y, Uint32 color)
{
int Point = y * ScreenWidth + x;
if (Point >= 0 && Point < ScreenPixels)
{
PixelArray[Point] = color;
}
}
//returns the color at a x,y point
Uint32 Core::GetPixel(int x, int y)
{
int Point = y * ScreenWidth + x;
if (Point >= 0 && Point < ScreenPixels)
{
return PixelArray[Point];
}
return 0;
}
int Core::GetWidth()
{
return ScreenWidth;
}
int Core::GetHeight()
{
return ScreenHeight;
}
int Core::GetTotalPixels()
{
return ScreenPixels;
}
//clears the SDL render and pixel array
void Core::ClearScreen()
{
//clear pizel array
for (int x = 0; x < ScreenPixels; x++)
{
PixelArray[x] = 0;
}
//clear renderer
SDL_RenderClear(theRenderer);
}
void Core::PresentScreen()
{
//create raster texture
rasterTexture = SDL_CreateTexture(theRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, ScreenWidth, ScreenHeight);
//update the texture with the pixel array edited with PutPixel
SDL_UpdateTexture(rasterTexture, NULL, PixelArray, ScreenWidth * sizeof(Uint32));
//draw raster texture
SDL_RenderCopy(theRenderer, rasterTexture, NULL, NULL);
//flip buffers
SDL_RenderPresent(theRenderer);
//destroy the texture to prevent memory leak
SDL_DestroyTexture(rasterTexture);
}
Core::Core()
{
ScreenWidth = -1;
ScreenHeight = -1;
ScreenPixels = -1;
}
//mybresenhams line drawing algorithm implementation
//using floating point vars for error even though integer math could work and be marginally faster
void Core::DrawLine(int x1, int y1, int x2, int y2, Uint32 color)
{
int deltaX = x2 - x1;
int deltaY = y2 - y1;
//handles straight up and down lines
if (deltaX == 0)
{
int yStart;
if (y2 < y1)
{
yStart = y2;
}
else
{
yStart = y1;
}
for (int y = yStart; y < yStart + abs(deltaY); y++)
{
PutPixel(x1, y, color);
}
return;
}
//switches the direction we draw the line in
int xStep = 1;
int yStep = 1;
if (x2 < x1)
{
xStep = -1;
}
if (y2 < y1)
{
yStep = -1;
}
//start points for the line drawing
int xSpot = x1;
int ySpot = y1;
//error used to increase pixels on the non stepping axis
float error;
//traverse through the steps and draw the line
if (abs(deltaX) > abs(deltaY))
{
float deltaError = abs((float)((float)deltaY / (float)deltaX));
error = deltaError - 0.5f;
for (int x = 0; x < abs(deltaX); x++)
{
PutPixel(xSpot, ySpot, color);
xSpot += xStep;
error += deltaError;
if (error > 0.5f)
{
ySpot += yStep;
error -= 1.0f;
}
if (xSpot < 0 || ySpot < 0 || xSpot > GetWidth() || ySpot > GetHeight())
{
return;
}
}
}
//same as above except Y axis is the primary stepping axis
else
{
float deltaError = abs((float)((float)deltaX / (float)deltaY));
error = deltaError - 0.5f;
for (int x = 0; x < abs(deltaY); x++)
{
PutPixel(xSpot, ySpot, color);
ySpot += yStep;
error += deltaError;
if (error > 0.5f)
{
xSpot += xStep;
error -= 1.0f;
}
if (xSpot < 0 || ySpot < 0 || xSpot > GetWidth() || ySpot > GetHeight())
{
return;
}
}
}
}
//draw line overload to use points instead pof raw x,y coordinates
void Core::DrawLine(Point p1, Point p2, Uint32 color)
{
DrawLine(p1.x, p1.y, p2.x, p2.y, color);
}
//flood fill algorithm
void Core::Fill(int x, int y, Uint32 newColor)
{
//get the first color
Uint32 startColor = Core::GetInstance()->GetPixel(x, y);
//all the pixels to be changed
vector<Point> Points;
//add first value
Points.push_back(Point() = { x,y });
//while there are pixels in the array to be filled
while (Points.size() > 0)
{
//get the most recent point
Point p = Points.back();
//remove it
Points.pop_back();
//if the pixel needs to be filled
if (p.x >= 0 && p.y >= 0 && p.x < Core::GetInstance()->GetWidth() && p.y < Core::GetInstance()->GetHeight() && Core::GetInstance()->GetPixel(p.x, p.y) == startColor)
{
//put the pixel
PutPixel(p.x, p.y, newColor);
//add the pixels around it to be filled
Points.push_back(Point() = { p.x + 1,p.y });
Points.push_back(Point() = { p.x,p.y + 1 });
Points.push_back(Point() = { p.x - 1,p.y });
Points.push_back(Point() = { p.x,p.y - 1 });
}
}
}