-
Notifications
You must be signed in to change notification settings - Fork 0
/
swGame.cpp
201 lines (177 loc) · 5.36 KB
/
swGame.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
#include "swGame.h"
#include "swStarfield.h"
#include "swStar.h"
#include "swMesh.h"
#include "swShip.h"
#include "swLabel.h"
#include "swClient.h"
#include "swServer.h"
#include "swPlayer.h"
#include "swFactory.h"
#include "swMainMenu.h"
#include "swPlayerCreateMsg.h"
#include "swPhysCreateMsg.h"
#include "swPlayerInputMsg.h"
#include "swHeadsUpDisplay.h"
#include "swPhysUpdateMsg.h"
#include "swMissile.h"
#include <QFile>
#include <QTimer>
#include <QKeyEvent>
swGame::swGame(QWidget *parent) : QGLWidget(parent), client(NULL), server(NULL) {
// enable this so we get mousemove events without mouse buttons held down
setMouseTracking(true);
// create the background
drawables.append(new swStarfield("data/starfield.binpts"));
// load the font
font.swObject::read("data/swfont.vfont");
// we connect to localhost unless set by the host menu
serverName = "localhost";
// show the main menu
drawables.append(new swMainMenu(this));
/*
* The menu system is made up of a set of menu classes that
* destroy and create each other as neccessary, passing around
* a reference to to the game object
*
* swMainMenu:
* - Host Game -> swNameMenu -> swShipMenu -> swLobbyMenu
* - Join Game -> swHostMenu -> swNameMenu -> swShipMenu -> swLobbyMenu
* - Options -> swOptionsMenu -> swMainMenu
*/
/*
// TEMP: Create and display the HUD
drawables.append(new swHeadsUpDisplay(this));
// TEMP: create a demo ship and star, and simulate
server = new swServer(this, 6668);
client = new swClient(this, "localhost", 6668);
// TEMP: use simulateOmega
simulator.simulateOmega = true;
// TEMP: connect our local key handler for ship movement
connect(this, SIGNAL(keyEvent(QKeyEvent*)), SLOT(keyHandle(QKeyEvent*)));
*/
// start the screen refresh timer
qtimer = new QTimer(this);
connect(qtimer, SIGNAL(timeout()), this, SLOT(updateGL()));
qtimer->start(15);
}
void swGame::paintGL() {
// clean up after the last frame
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// draw everything
foreach(swDrawable* drawable, drawables) {
drawable->draw();
}
simulator.draw();
// step the simulation
simulator.simulate();
}
void swGame::keyHandle(QKeyEvent* event) {
double value;
switch(event->type()) {
case(QKeyEvent::KeyPress):
value = 1;
break;
case(QKeyEvent::KeyRelease):
value = -1;
break;
default:
return;
break;
}
if(!event->isAutoRepeat()) {
swPlayer* player = &client->players[client->clientId];
if(player->shipId != -1) {
swShip* ship = (swShip*)simulator.objects[player->shipId];
switch(event->key()) {
case(Qt::Key_Up):
player->controlAxes.y += value;
break;
case(Qt::Key_Down):
player->controlAxes.y -= value;
break;
case(Qt::Key_Left):
player->controlAxes.x -= value;
break;
case(Qt::Key_Right):
player->controlAxes.x += value;
break;
case(Qt::Key_Return):
if(value == 1) {
// warp to a random location
ship->pos.x = double(rand()) / RAND_MAX * 3 - 1.5;
ship->pos.y = double(rand()) / RAND_MAX * 3 - 1.5;
}
break;
case(Qt::Key_Space):
// fire
if(value == 1) {
ship->fire();
}
break;
}
// send a playerInputMsg
swPlayerInputMsg inputMsg;
inputMsg.index = client->clientId;
inputMsg.controlAxes = player->controlAxes;
swFactory::writeObject(&inputMsg, client->sock);
// send a physupdate
swPhysUpdateMsg updateMsg;
updateMsg.index = player->shipId;
updateMsg.object = ship;
swFactory::writeObject(&updateMsg, client->sock);
}
}
}
void swGame::keyPressEvent(QKeyEvent* event) {
keyEvent(event);
}
void swGame::keyReleaseEvent(QKeyEvent* event) {
keyEvent(event);
}
swVector swGame::projMousePos(QPointF pos) {
// the screen top and bottom are at 1.1 and -1.1 respectively
// the edges of the sides of the screen move. This transform
// is based on the glOrtho params used to set up this screen
return swVector((pos.x() / size().width() * 2 - 1) *
(size().width() * 1.0 / size().height()),
-pos.y() / size().height() * 2 + 1) * 1.1;
}
void swGame::mouseMoveEvent(QMouseEvent* event) {
mouseEvent(event);
}
void swGame::mousePressEvent(QMouseEvent* event) {
mouseEvent(event);
}
void swGame::initializeGL() {
glClearColor(0, 0, 0, 0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void swGame::resizeGL(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((-width * 1.0 / height), (width * 1.0 / height), -1.1, 1.1, 0.0, 4.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
swGame::~swGame() {
if(client)
delete client;
if(server)
delete server;
foreach(swDrawable* drawable, drawables) {
delete drawable;
}
}
QSize swGame::sizeHint() const {
return QSize(800, 600);
}
QSize swGame::minimumSizeHint() const {
return QSize(640, 480);
}