-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (136 loc) · 4.8 KB
/
main.cpp
File metadata and controls
170 lines (136 loc) · 4.8 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
#include <thread>
#include <SDL.h>
#include <SDL_ttf.h>
#include "BoardProxy.h"
#include "BroadcastAgent.h"
#include "Box.h"
#include "MainSetup.h"
#include "Printer.h"
#include "Recorder.h"
#include "Threader.h"
// Define screen dimensions
#define SCREEN_WIDTH 600
#define SCREEN_HEIGHT 600
using namespace std;
void quitTTFandSDL()
{
TTF_Quit();
SDL_Quit();
}
int main(int argc, char* argv[])
{
// Unused argc, argv
(void) argc;
(void) argv;
// Initialize SDL2 and SDL2_ttf
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL2 could not be initialized!\nSDL2 Error: %s\n", SDL_GetError());
return 0;
}
TTF_Init();
#if defined linux && SDL_VERSION_ATLEAST(2, 0, 8)
// Disable compositor bypass
if(!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"))
{
printf("SDL can not disable compositor bypass!\n");
return 0; }
#endif
// Create window
SDL_Window *window = SDL_CreateWindow("Plaza Walk",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if(!window)
{
printf("Window could not be created!\nSDL_Error: %s\n", SDL_GetError());
quitTTFandSDL();
return 0;
}
// Create renderer
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!renderer)
{
printf("Renderer could not be created!\nSDL_Error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
quitTTFandSDL();
return 0;
}
/* At this point we know SDL, TTF, window, and renderer work! */
// In-bound and out-bound rectangles are where the boxes start from and terminate at. A box can not start and end at the same rectangle in inOutBoundRectangles.
auto inOutBoundRectangles = MainSetup::getInOutBoundRectangles(SCREEN_WIDTH, SCREEN_HEIGHT);
// Create Boxes
// Group0 with 200 boxes (red) will tread safely. Boxes start at north wall.
// Group1 with 400 boxes (blue) will tread safely. Boxes start at top of west and east walls.
// Group2 with 400 boxes (yellow) will tread recklessly. Boxes start at bottom of west and east walls.
// Group3 with 400 boxes (purple) will tread recklessly. Boxes start at south wall.
vector<Box> boxes{};
MainSetup::addAGroupOfBoxes(boxes, 0, 0, 200);
MainSetup::addAGroupOfBoxes(boxes, 200, 1, 400);
MainSetup::addAGroupOfBoxes(boxes, 600, 2, 400);
MainSetup::addAGroupOfBoxes(boxes, 1000, 3, 400);
// Create Board
Board board{SCREEN_WIDTH, SCREEN_HEIGHT, std::move(boxes)};
// Create BroadcastAgent. It will periodically ask Board (via BoardProxy) to send changes to recorder.
BroadcastAgent broadcastAgent{board.getBoardProxy()};
// Create Recorder, it will listen for changes from Board and send those changes to the printer.
Recorder recorder{};
board.registerListener(&recorder);
// Create the printer and have it listen for changes from the recorder.
Printer printer(renderer);
recorder.registerListener(&printer);
// Add Colors per group to Printer
unordered_map<int, Color> colorPerGroupNumber{};
colorPerGroupNumber.insert({0, MainSetup::red()});
colorPerGroupNumber.insert({1, MainSetup::cyan()});
colorPerGroupNumber.insert({2, MainSetup::amber()});
colorPerGroupNumber.insert({3, MainSetup::purple()});
printer.setGroupColors(colorPerGroupNumber);
// Add the in-bound and out-bound rectangles to printer (where the boxes start and end).
printer.addInOutBoundRectangles(inOutBoundRectangles);
// Prints empty Board.
broadcastAgent.requestBroadcast();
// Event loop exit flag
bool running = true;
// Create vector of type thread and for each Box push a thread onto vector.
vector<unique_ptr<thread>> threads{};
Threader threader{};
// Number of Boxes is 200 * 7 same as the number of Boxes in _boxes.
threader.populateThreads(
threads,
200,
7,
inOutBoundRectangles,
board,
running);
// Event loop
while(running)
{
SDL_Event e;
if (SDL_PollEvent(&e) != 0)
{
switch (e.type)
{
case SDL_QUIT:
running = false;
break;
}
}
broadcastAgent.requestBroadcast();
}
// Join threads
for(uint32_t ii=0; ii<threads.size(); ++ii)
{
threads[ii]->join();
}
// Destroy renderer
SDL_DestroyRenderer(renderer);
// Destroy window
SDL_DestroyWindow(window);
// Quit SDL2_ttf
TTF_Quit();
// Quit SDL
SDL_Quit();
return 0;
}