-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
325 lines (294 loc) · 8.78 KB
/
Copy pathMain.cpp
File metadata and controls
325 lines (294 loc) · 8.78 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
////////////////////////////////////////////////////////////////////////////////
/*!
@file Main.cpp
@author Willow Hayward
willhayw@gmail.com
@par This file contains the main implementation of the Computer Graphics Framework.
@todo Make the window resizable
Create a base class for all objects that can be drawn
Create a base class for all objects that can be animated
Implement a unified object list for adding, drawing, and animating objects
Define the number of clicks to create each object (default is 2, can override in derived class)
Use best practice for animation timing - based on performance counter
Move menu-related functions in Main.cpp and Windows.* to a new Menu class
*/
////////////////////////////////////////////////////////////////////////////////
// Includes
#include "Color.hpp"
#include "Line2D.hpp"
#include "Point2D.hpp"
#include "Shapes/Box2D.hpp"
#include "Shapes/Circle.hpp"
#include "Shapes/Ellipse.hpp"
#include "Window.hpp"
#include <memory>
#include <windows.h>
#define TICKS_PER_SEC 1000
#define FPS_30 (TICKS_PER_SEC/30.f)
#define TICKS_PER_ANIM (10*TICKS_PER_SEC) // 10 seconds per animation
// Global objects
std::vector<std::unique_ptr<CGF::Shape>> shapeList;
std::vector<CGF::Box2D> boxList;
std::vector<CGF::Circle> circleList;
std::vector<CGF::Ellipse> ellipseList;
std::vector<CGF::Line2D> lineList;
CGF::Point2D firstPoint;
bool useFirstPoint(false);
CGF::Color redClr(255, 0, 0, 255);
CGF::Color blueClr(0, 0, 255, 255);
CGF::Color grnClr(0, 255, 0, 128);
CGF::Color blackClr(0, 0, 0, 255);
CGF::Color whiteClr(255, 255, 255, 255);
// Prototypes
void Update(CGF::Window & window, DWORD & oldTime);
void Draw(CGF::Window & window);
void DrawCross(CGF::Window & window, unsigned int centerX, unsigned int centerY, unsigned int radius, CGF::Color & color);
void SetPreviousPoint(unsigned int x, unsigned int y);
void ClearPreviousPoint();
void AddBox2D(CGF::Box2D & box);
void ClearBox2D();
void AddCircle(CGF::Circle & circle);
void ClearCircle();
void AddEllipse(CGF::Ellipse & ellipse);
void ClearEllipse();
void AddLine2D(CGF::Line2D & line);
void ClearLine2D();
////////////////////////////////////////////////////////////////////////////////
/*!
@brief WinMain
@param hInstance
Window instance
@param hInstancePrev
Not used
@param lpCmdLine
Command line input -- not used
@param nCmdShow
Not used
@return 0 for success, anything else for error
*/
////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hInstancePrev, LPSTR lpCmdLine, int nCmdShow)
{
// Create the main window
CGF::Window window;
if(!window.Create(hInstance, "Computer Graphics Framework", 800, 600))
return 1;
DWORD oldTime = 0; // Explicitly initialize oldTime
oldTime = ::timeGetTime();
while(window.Running())
{
Update(window, oldTime);
Draw(window);
// Swap the window surfaces
window.Present();
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Top-level function to read user input & update game objects
@param window
The active Window object
@param oldTime
The previous time
*/
////////////////////////////////////////////////////////////////////////////////
void Update(CGF::Window & window, DWORD & oldTime)
{
// Handle window messages
window.ProcessMessages();
// Update shapes
for (const auto & shape : shapeList)
{
shape->Update();
}
// Update the animation time
// Hold to 30fps
DWORD newTime = 0; // Explicitly initialize newTime
while (1)
{
// Get the updated time
newTime = ::timeGetTime();
if(newTime - oldTime < FPS_30) // 1/30 second
continue;
// Re-set the time
oldTime = newTime;
break;
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Top-level function to render all game objects
@param window
The active Window object
*/
////////////////////////////////////////////////////////////////////////////////
void Draw(CGF::Window & window)
{
// Check which menu item was active and act appropriately
if(IsMenuItemActive(LINE_DDA))
{
for (int i = 0; i < static_cast<int>(lineList.size()); i++)
{
lineList[i].SetColor(redClr);
lineList[i].DrawDDA(window);
}
}
if(IsMenuItemActive(LINE_BRESENHAM))
{
for (int i = 0; i < static_cast<int>(lineList.size()); i++)
{
lineList[i].SetColor(blueClr);
lineList[i].DrawBresenham(window);
}
}
if(IsMenuItemActive(LINE_ANIMATE))
{
for(int i = 0; i < static_cast<int>(boxList.size()); i++)
{
boxList[i].Draw(window);
auto center = boxList[i].GetCenter();
// The center of the crosshair should be drawn at the same pixel as the center of the box, which is truncated.
DrawCross(window, (unsigned int)(center.GetX()), (unsigned int)(center.GetY()), 2, blackClr);
}
}
if(IsMenuItemActive(OBJECT_CIRCLE))
{
for (int i = 0; i < static_cast<int>(circleList.size()); i++)
{
circleList[i].Draw(window);
}
}
if(IsMenuItemActive(OBJECT_ELLIPSE))
{
for (int i = 0; i < static_cast<int>(ellipseList.size()); i++)
{
ellipseList[i].Draw(window);
}
}
if(useFirstPoint)
{
// The center of the crosshair should be drawn at the same pixel as the endpoint of a line, which is rounded.
unsigned int centerX = roundToUInt(firstPoint.GetX());
unsigned int centerY = roundToUInt(firstPoint.GetY());
DrawCross(window, centerX, centerY, 2, blackClr);
}
}
void DrawCross(CGF::Window & window, unsigned int centerX, unsigned int centerY, unsigned int radius, CGF::Color &color)
{
window.SetPixel(centerX, centerY, color);
for (unsigned int i = 1; i <= radius; i++)
{
window.SetPixel(centerX - i, centerY, color);
window.SetPixel(centerX + i, centerY, color);
window.SetPixel(centerX, centerY - i, color);
window.SetPixel(centerX, centerY + i, color);
}
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Adds a line to the list
@param line
The line to add
*/
////////////////////////////////////////////////////////////////////////////////
void AddLine2D(CGF::Line2D & line)
{
lineList.push_back(line);
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Erases the line list
@param line
The line list to erase
*/
////////////////////////////////////////////////////////////////////////////////
void ClearLine2D()
{
lineList.clear();
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Adds a box to the list
@param box
The box to add
*/
////////////////////////////////////////////////////////////////////////////////
void AddBox2D(CGF::Box2D & box)
{
boxList.push_back(box);
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Erases the box list
@param box
The box list to erase
*/
////////////////////////////////////////////////////////////////////////////////
void ClearBox2D()
{
boxList.clear();
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Adds a circle to the list
@param circle
The circle to add
*/
////////////////////////////////////////////////////////////////////////////////
void AddCircle(CGF::Circle & circle)
{
circleList.push_back(circle);
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Erases the circle list
*/
////////////////////////////////////////////////////////////////////////////////
void ClearCircle()
{
circleList.clear();
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Adds an ellipse to the list
@param ellipse
The ellipse to add
*/
////////////////////////////////////////////////////////////////////////////////
void AddEllipse(CGF::Ellipse & ellipse)
{
ellipseList.push_back(ellipse);
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Erases the ellipse list
*/
////////////////////////////////////////////////////////////////////////////////
void ClearEllipse()
{
ellipseList.clear();
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Sets the previous point
@param x
The x-coordinate
@param y
The y-coordinate
*/
void SetPreviousPoint(unsigned int x, unsigned int y)
{
firstPoint = CGF::Point2D(x, y);
firstPoint.SetColor(blackClr);
useFirstPoint = true;
}
////////////////////////////////////////////////////////////////////////////////
/*!
@brief Clears the previous point
*/
void ClearPreviousPoint()
{
useFirstPoint = false;
firstPoint = CGF::Point2D();
}