forked from Lupus590AtUni/Uni_NetworksAssignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsTemplate.cpp
233 lines (170 loc) · 6.03 KB
/
GraphicsTemplate.cpp
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
// GraphicsTemplate.cpp
//
//////////////////////////////////////////////////////////////////////////////////////////
// includes
//////////////////////////////////////////////////////////////////////////////////////////
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms737629(v=vs.85).aspx
// I think opengl glut may need some window.h stuff, so add this just in case
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <iostream>
using std::cout;
#include <tchar.h>
#include <windows.h>
#include "GL/glut.h"
#include "globals.h"
#include "cRenderClass.h"
#include <vector>
using std::vector;
//
#include "NA_Boid.h"
#include "NA_MathsLib.h"
#include "NA_Timer.h"
#include "NA_CriticalSection.h"
#include "NA_NetworkManager.h"
#include "NA_PeerListener.h"
//LOW: multithread boids?
//////////////////////////////////////////////////////////////////////////////////////////
// externals
//////////////////////////////////////////////////////////////////////////////////////////
extern cRenderClass graphics;
extern NA_MathsLib na_maths;
vector<NA_Boid> boidList; //not really a list
//////////////////////////////////////////////////////////////////////////////////////////
// renderScene() - render the scene
//////////////////////////////////////////////////////////////////////////////////////////
void renderScene()
{
//TODO: change for variying number of boids
for (int i = 0; i < BOID_MAX; i++)
{
boidList[i].draw();
}
if (DEBUG_AVERAGE_POS)
{
NA_Vector sumPosition;
for (int i = 0; i < BOID_MAX; i++)
{
sumPosition.x += boidList[i].position.x;
sumPosition.y += boidList[i].position.y;
}
//convert to average
sumPosition.x = sumPosition.x / (BOID_MAX);
sumPosition.y = sumPosition.y / (BOID_MAX);
graphics.setPointSize(10);
graphics.setColour(0.0f, 0.0f, 1.0f);
graphics.drawPixel(sumPosition.x, sumPosition.y);
}
// render the scene
graphics.render();
}
//////////////////////////////////////////////////////////////////////////////////////////
// update() - update function
//////////////////////////////////////////////////////////////////////////////////////////
void update()
{
//TODO: change for variying number of boids
static bool first = true;
if (first)
renderScene();
first = false;
//renderScene();
//cout << "first render done\n";
// add any update code here...
static NA_Timer fpsCap;//wait if FPS is too high (if boids move too fast)
fpsCap.restart();
if (DEBUG_PRINT_POS_OF_ALL_BOIDS)
fpsCap.setDuration(DEBUG_UPDATE_FREQUENCY);
else
fpsCap.setDuration(1.0f / FPS_MAX);
if (first && !DEBUG_RUN_TOP_SPEED)
fpsCap.wait();
for (int i = 0; i < BOID_MAX; i++)
{
boidList[i].update();
}
if (DEBUG_PRINT_POS_OF_ALL_BOIDS || DEBUG_PRINT_POS_OF_FIRST_BOID) system("cls");
for (int i = 0; i < BOID_MAX; i++)
{
boidList[i].postUpdate();
if (DEBUG_PRINT_POS_OF_ALL_BOIDS || DEBUG_PRINT_POS_OF_FIRST_BOID && i == 0) cout << "pos: " << boidList[i].position.x << " " << boidList[i].position.y << "\n";
}
//TODO: check for network messages for boids being added to our screen
//cout << "updates done, waiting\n";
//extern void debugMouse();
//debugMouse();
//cout << "mouse scary? " << graphics.mouseIsScary << "\n";
if (!DEBUG_RUN_TOP_SPEED) fpsCap.wait();
// always re-render the scene..
renderScene();
//cout << " post render done\n";
}
//////////////////////////////////////////////////////////////////////////////////////////
// _tmain() - program entry point
//////////////////////////////////////////////////////////////////////////////////////////
/*
int _tmain(int argc, _TCHAR* argv[])
{
// init glut stuff..
graphics.create(argc, argv);
// good place for one-off initialisations and objects creation..
//make all boids
na_maths.seedDice();
for (int i = 0; i < BOID_MAX; i++)
{
NA_Boid temp;
temp.position.x = na_maths.dice(SCREEN_WIDTH);
temp.position.y = na_maths.dice(SCREEN_HEIGHT);
//temp.position.x = 100.0f;
//temp.position.y = 100.0f;
temp.currentVelocity.x = float(na_maths.dice(-100,100))/100.0f;
temp.currentVelocity.y = float(na_maths.dice(-100, 100))/100.0f;
boidList.push_back(temp);
//cout << "POS: X: " << temp.position.x << " Y: " << temp.position.y << "\n";
//cout << "VEL: X: " << temp.currentVelocity.x << " Y: " << temp.currentVelocity.y << "\n";
//NA_Vector t = temp.currentVelocity;
//t.normalise();
//cout << "NV: X: " << t.x << " Y: " << t.y << "\n\n";
}
// enter game loop..
graphics.loop();
return 0;
}
*/
int _tmain(int argc, _TCHAR* argv[])
{
extern NA_NetworkManager na_netman;
if (!na_netman.init())
{
return false; //network manager failed, winsock not ready
}
//Set up client listener
//create UDP socket
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in rx_addr;
rx_addr.sin_family = AF_INET; // TCPIP
rx_addr.sin_port = htons((u_short)atol(DEFAULT_PORT));
rx_addr.sin_addr.s_addr = htonl(INADDR_ANY);
iResult = bind(sock, (SOCKADDR*)&rx_addr, sizeof(rx_addr));
NA_PeerListener peerListener;
peerListener.sock = sock;
peerListener.startThread();
//https://gamedevelopment.tutsplus.com/tutorials/building-a-peer-to-peer-multiplayer-networked-game--gamedev-10074
//TODO: boardcast and try to find a peer
struct addrinfo *addressList = NULL;
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &addressList);
//TODO: go through everything it addressList, connect to each one and ask to join
//On start, clients add their own boids
//Clients control boids on their screen
//Clients 'handover' boids when they fly off screen
//Clients should only need to talk to next and previous peer
peerListener.requestSelfTerminate();
peerListener.waitForTerminate();
na_netman.cleanup();
}