2011-01-04 17:11:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2011-01-04 23:07:08 +00:00
|
|
|
#include <string.h>
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
#include <enet/enet.h>
|
2011-01-04 17:11:59 +00:00
|
|
|
#include <GL/glfw.h>
|
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "game.h"
|
2011-01-05 22:02:10 +00:00
|
|
|
#include "Explosion.h"
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
GLUquadricObj *quadratic;
|
2011-01-04 23:07:08 +00:00
|
|
|
ENetHost *host;
|
|
|
|
ENetPeer *client_peer;
|
|
|
|
player_t *local_player;
|
2011-01-05 22:02:10 +00:00
|
|
|
Explosion explosion;
|
2011-01-04 23:07:08 +00:00
|
|
|
|
|
|
|
void key_callback(int key, int state) {
|
|
|
|
|
|
|
|
}
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-04 21:24:18 +00:00
|
|
|
void setup_opengl() {
|
2011-01-04 17:11:59 +00:00
|
|
|
// Initialise GLFW
|
2011-01-04 21:24:18 +00:00
|
|
|
if (!glfwInit()) {
|
|
|
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2011-01-04 17:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open OpenGL window
|
2011-01-04 23:07:08 +00:00
|
|
|
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) {
|
2011-01-04 21:24:18 +00:00
|
|
|
fprintf(stderr, "Failed to open GLFW window\n");
|
2011-01-04 17:11:59 +00:00
|
|
|
glfwTerminate();
|
2011-01-04 21:24:18 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-04 17:11:59 +00:00
|
|
|
}
|
2011-01-04 23:07:08 +00:00
|
|
|
//glfwDisable(GLFW_MOUSE_CURSOR);
|
|
|
|
glfwSetWindowTitle("Gremlin Lan Party Game");
|
|
|
|
glfwSetKeyCallback(key_callback);
|
2011-01-04 17:11:59 +00:00
|
|
|
// Enable sticky keys
|
2011-01-04 21:24:18 +00:00
|
|
|
glfwEnable(GLFW_STICKY_KEYS);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// general settings
|
2011-01-05 22:02:10 +00:00
|
|
|
glShadeModel( GL_SMOOTH);
|
2011-01-04 17:11:59 +00:00
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
2011-01-05 22:02:10 +00:00
|
|
|
glEnable( GL_CULL_FACE);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// setup depth buffer
|
|
|
|
glClearDepth(1.0f);
|
2011-01-05 22:02:10 +00:00
|
|
|
glEnable( GL_DEPTH_TEST);
|
|
|
|
glDepthFunc( GL_LEQUAL);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-04 21:24:18 +00:00
|
|
|
GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
|
|
|
|
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
|
|
|
GLfloat LightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f };
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// setup directional light
|
|
|
|
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
|
2011-01-04 21:24:18 +00:00
|
|
|
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
|
|
|
|
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
|
2011-01-05 22:02:10 +00:00
|
|
|
glEnable( GL_LIGHT1);
|
|
|
|
glEnable( GL_LIGHTING);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// Enable vertical sync (on cards that support it)
|
2011-01-04 21:24:18 +00:00
|
|
|
glfwSwapInterval(1);
|
2011-01-04 17:11:59 +00:00
|
|
|
}
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
void draw_team(team_t *team) {
|
2011-01-04 17:11:59 +00:00
|
|
|
size_t i = 0;
|
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
|
2011-01-05 22:02:10 +00:00
|
|
|
glMatrixMode( GL_MODELVIEW);
|
2011-01-04 17:11:59 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(team->x, team->y, team->z);
|
2011-01-04 21:24:18 +00:00
|
|
|
gluSphere(quadratic, 50.f, 32, 32);
|
2011-01-04 17:11:59 +00:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
void draw_player(player_t *player) {
|
|
|
|
if (player->status == 0)
|
|
|
|
return;
|
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color);
|
2011-01-05 22:02:10 +00:00
|
|
|
glMatrixMode( GL_MODELVIEW);
|
2011-01-05 14:00:09 +00:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(player->x, player->y, player->z);
|
|
|
|
gluSphere(quadratic, 10.f, 32, 32);
|
|
|
|
glPopMatrix();
|
2011-01-04 23:07:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
void draw_bomb(bomb_t *bomb) {
|
|
|
|
if (bomb->status != 1)
|
|
|
|
return;
|
|
|
|
GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
|
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
|
|
|
glMatrixMode( GL_MODELVIEW);
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(bomb->x, bomb->y, bomb->z);
|
|
|
|
gluSphere(quadratic, 3.f, 32, 32);
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
void setup_network(const char *remote) {
|
|
|
|
if (enet_initialize() != 0) {
|
|
|
|
fprintf(stderr, "An error occurred while initializing ENet.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remote == NULL) {
|
|
|
|
ENetAddress address;
|
|
|
|
|
|
|
|
address.host = ENET_HOST_ANY;
|
|
|
|
address.port = 1234;
|
|
|
|
|
|
|
|
host = enet_host_create(&address, 32, 2, 0, 0);
|
|
|
|
if (host == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"An error occurred while trying to create an ENet server host.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ENetAddress address;
|
|
|
|
ENetEvent event;
|
|
|
|
|
|
|
|
host = enet_host_create(NULL, 1, 2, 57600 / 8, 14400 / 8);
|
|
|
|
if (host == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"An error occurred while trying to create an ENet client host.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_address_set_host(&address, remote);
|
|
|
|
address.port = 1234;
|
|
|
|
|
|
|
|
/* Initiate the connection, allocating the two channels 0 and 1. */
|
|
|
|
client_peer = enet_host_connect(host, &address, 2, 0);
|
|
|
|
|
|
|
|
if (client_peer == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"No available peers for initiating an ENet connection.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait up to 5 seconds for the connection attempt to succeed. */
|
|
|
|
if (enet_host_service(host, &event, 5000) > 0 && event.type
|
|
|
|
== ENET_EVENT_TYPE_CONNECT) {
|
|
|
|
puts("Connection succeeded.");
|
|
|
|
} else {
|
|
|
|
/* Either the 5 seconds are up or a disconnect event was */
|
|
|
|
/* received. Reset the peer in the event the 5 seconds */
|
|
|
|
/* had run out without any significant event. */
|
|
|
|
enet_peer_reset(client_peer);
|
|
|
|
|
|
|
|
fprintf(stderr, "Connection to %s failed.", remote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
#define MESSAGE_PLAYER_SPAWN 0
|
|
|
|
#define MESSAGE_PLAYER_KILL 1
|
|
|
|
#define MESSAGE_ACCEPT 2
|
|
|
|
#define MESSAGE_PLAYER_UPDATE 3
|
|
|
|
#define MESSAGE_PLAYER_ACCELERATE 4
|
2011-01-06 12:51:52 +00:00
|
|
|
#define MESSAGE_BOMB_DROP 5
|
|
|
|
#define MESSAGE_BOMB_UPDATE 6
|
2011-01-05 14:00:09 +00:00
|
|
|
|
|
|
|
typedef struct message_t {
|
|
|
|
uint16_t msg_id;
|
|
|
|
} message_t;
|
|
|
|
|
|
|
|
typedef struct player_spawn_message_t {
|
|
|
|
uint16_t msg_id;
|
|
|
|
uint8_t team_id;
|
2011-01-06 12:51:52 +00:00
|
|
|
uint16_t player_id;
|
2011-01-05 14:00:09 +00:00
|
|
|
} player_spawn_message_t;
|
|
|
|
|
|
|
|
typedef struct player_kill_message_t {
|
|
|
|
uint16_t msg_id;
|
2011-01-06 12:51:52 +00:00
|
|
|
uint16_t player_id;
|
2011-01-05 14:00:09 +00:00
|
|
|
} player_kill_message_t;
|
|
|
|
|
|
|
|
typedef struct accept_message_t {
|
|
|
|
uint16_t msg_id;
|
2011-01-06 12:51:52 +00:00
|
|
|
uint16_t player_id;
|
2011-01-05 14:00:09 +00:00
|
|
|
} accept_message_t;
|
|
|
|
|
|
|
|
typedef struct player_update_message_t {
|
|
|
|
uint16_t msg_id;
|
2011-01-06 12:51:52 +00:00
|
|
|
uint16_t player_id;
|
2011-01-05 14:00:09 +00:00
|
|
|
unsigned int session;
|
|
|
|
double x, y, z;
|
|
|
|
double vx, vy, vz;
|
|
|
|
} player_update_message_t;
|
|
|
|
|
|
|
|
typedef struct player_accelerate_message_t {
|
|
|
|
uint16_t msg_id;
|
2011-01-06 12:51:52 +00:00
|
|
|
uint16_t player_id;
|
2011-01-05 14:00:09 +00:00
|
|
|
double x, y, z;
|
|
|
|
} player_accelerate_message_t;
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
typedef struct bomb_drop_meesage_t {
|
|
|
|
uint16_t msg_id;
|
|
|
|
double x, y, z;
|
|
|
|
double vx, vy, vz;
|
|
|
|
double ttl;
|
|
|
|
} bomb_drop_meesage_t;
|
|
|
|
|
|
|
|
typedef struct bomb_update_meesage_t {
|
|
|
|
uint16_t msg_id;
|
|
|
|
uint16_t bomb_index;
|
|
|
|
double x, y, z;
|
|
|
|
double vx, vy, vz;
|
|
|
|
double ttl;
|
|
|
|
} bomb_update_meesage_t;
|
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
void send_player_updates(game_t *game) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
|
|
|
|
if (game->player[i].status == 0)
|
|
|
|
continue;
|
|
|
|
player_update_message_t msg;
|
|
|
|
msg.msg_id = MESSAGE_PLAYER_UPDATE;
|
|
|
|
msg.player_id = game->player[i].id;
|
|
|
|
msg.x = game->player[i].x;
|
|
|
|
msg.y = game->player[i].y;
|
|
|
|
msg.z = game->player[i].z;
|
|
|
|
msg.vx = game->player[i].vx;
|
|
|
|
msg.vy = game->player[i].vy;
|
|
|
|
msg.vz = game->player[i].vz;
|
2011-01-05 22:02:10 +00:00
|
|
|
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
|
2011-01-05 14:00:09 +00:00
|
|
|
enet_host_broadcast(host, 0, packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
void dispatch_message(enet_uint8 *data, size_t length, game_t *game) {
|
|
|
|
message_t *msg = (message_t *) data;
|
|
|
|
switch (msg->msg_id) {
|
|
|
|
case MESSAGE_PLAYER_SPAWN: {
|
|
|
|
player_spawn_message_t *sm = (player_spawn_message_t *) data;
|
|
|
|
team_t *team = game_team(game, sm->team_id);
|
|
|
|
player_t *player = game_spawn_player_id(game, team, sm->player_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_PLAYER_KILL: {
|
|
|
|
player_kill_message_t *sm = (player_kill_message_t *) data;
|
|
|
|
player_t *player = game_player(game, sm->player_id);
|
|
|
|
player->status = 0;
|
|
|
|
player->team = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_ACCEPT: {
|
|
|
|
accept_message_t *am = (accept_message_t *) data;
|
|
|
|
local_player = game_player(game, am->player_id);
|
|
|
|
printf("Spwan as %d.%d\n", local_player->team->id, local_player->id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_PLAYER_UPDATE: {
|
|
|
|
player_update_message_t *um = (player_update_message_t *) data;
|
|
|
|
player_t *player = game_player(game, um->player_id);
|
|
|
|
player->x = um->x;
|
|
|
|
player->y = um->y;
|
|
|
|
player->z = um->z;
|
|
|
|
player->vx = um->vx;
|
|
|
|
player->vy = um->vy;
|
|
|
|
player->vz = um->vz;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_PLAYER_ACCELERATE: {
|
|
|
|
player_accelerate_message_t *um = (player_accelerate_message_t *) data;
|
|
|
|
player_t *player = game_player(game, um->player_id);
|
|
|
|
player->vx += um->x;
|
|
|
|
player->vy += um->y;
|
|
|
|
player->vz += um->z;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_BOMB_DROP: {
|
|
|
|
bomb_drop_meesage_t *m = (bomb_drop_meesage_t *) data;
|
|
|
|
bomb_t *bomb = game_spawn_bomb(game);
|
|
|
|
if (bomb == NULL)
|
|
|
|
return;
|
|
|
|
bomb->x = m->x;
|
|
|
|
bomb->y = m->y;
|
|
|
|
bomb->z = m->z;
|
|
|
|
bomb->vx = m->vx;
|
|
|
|
bomb->vy = m->vy;
|
|
|
|
bomb->vz = m->vz;
|
|
|
|
bomb->ttl = m->ttl;
|
|
|
|
if (client_peer == NULL) {
|
|
|
|
ENetPacket * packet = enet_packet_create(data, length,
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_host_broadcast(host, 0, packet);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-01-05 14:00:09 +00:00
|
|
|
void service_network(game_t *game) {
|
2011-01-04 23:07:08 +00:00
|
|
|
ENetEvent event;
|
|
|
|
|
|
|
|
/* Wait up to 1000 milliseconds for an event. */
|
|
|
|
while (enet_host_service(host, &event, 0) > 0) {
|
|
|
|
switch (event.type) {
|
|
|
|
case ENET_EVENT_TYPE_CONNECT:
|
|
|
|
printf("A new client connected from %x:%u.\n",
|
|
|
|
event.peer->address.host, event.peer->address.port);
|
|
|
|
{
|
2011-01-05 14:00:09 +00:00
|
|
|
// bring new client up to date
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
|
|
|
|
if (game->player[i].status == 0)
|
|
|
|
continue;
|
|
|
|
// send player spawn message
|
|
|
|
player_spawn_message_t spwan_msg;
|
|
|
|
spwan_msg.msg_id = MESSAGE_PLAYER_SPAWN;
|
|
|
|
spwan_msg.team_id = game->player[i].team->id;
|
2011-01-05 22:02:10 +00:00
|
|
|
spwan_msg.player_id = game->player[i].id;
|
|
|
|
ENetPacket * packet = enet_packet_create(&spwan_msg,
|
|
|
|
sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE);
|
2011-01-05 14:00:09 +00:00
|
|
|
enet_peer_send(event.peer, 0, packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
team_t *team = game_team_with_least_players(game);
|
|
|
|
player_t *player = game_spawn_player(game, team);
|
|
|
|
printf("Spwan as %d.%d\n", team->id, player->id);
|
|
|
|
event.peer->data = player;
|
|
|
|
|
|
|
|
// send player spawn message
|
|
|
|
player_spawn_message_t spwan_msg;
|
|
|
|
spwan_msg.msg_id = MESSAGE_PLAYER_SPAWN;
|
|
|
|
spwan_msg.team_id = team->id;
|
|
|
|
spwan_msg.player_id = player->id;
|
2011-01-05 22:02:10 +00:00
|
|
|
ENetPacket * packet = enet_packet_create(&spwan_msg,
|
|
|
|
sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE);
|
2011-01-05 14:00:09 +00:00
|
|
|
enet_host_broadcast(host, 0, packet);
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
// send team and player
|
2011-01-05 14:00:09 +00:00
|
|
|
accept_message_t msg;
|
|
|
|
msg.msg_id = MESSAGE_ACCEPT;
|
|
|
|
msg.player_id = player->id;
|
2011-01-05 22:02:10 +00:00
|
|
|
packet = enet_packet_create(&msg, sizeof(msg),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(event.peer, 0, packet);
|
2011-01-05 14:00:09 +00:00
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
// send state
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
case ENET_EVENT_TYPE_RECEIVE: {
|
2011-01-06 12:51:52 +00:00
|
|
|
dispatch_message(event.packet->data, event.packet->dataLength, game);
|
2011-01-04 23:07:08 +00:00
|
|
|
enet_packet_destroy(event.packet);
|
|
|
|
break;
|
2011-01-05 22:02:10 +00:00
|
|
|
}
|
2011-01-04 23:07:08 +00:00
|
|
|
case ENET_EVENT_TYPE_DISCONNECT:
|
|
|
|
printf("%s disconected.\n", event.peer -> data);
|
2011-01-05 14:00:09 +00:00
|
|
|
{
|
2011-01-05 22:02:10 +00:00
|
|
|
/* Reset the peer's client information. */
|
|
|
|
player_t *player = (player_t *) event.peer->data;
|
|
|
|
player->status = 0;
|
|
|
|
player->team = 0;
|
|
|
|
|
|
|
|
// send player spawn message
|
|
|
|
player_kill_message_t msg;
|
|
|
|
msg.msg_id = MESSAGE_PLAYER_KILL;
|
|
|
|
msg.player_id = player->id;
|
|
|
|
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_host_broadcast(host, 0, packet);
|
2011-01-05 14:00:09 +00:00
|
|
|
}
|
2011-01-04 23:07:08 +00:00
|
|
|
event.peer->data = NULL;
|
2011-01-05 14:00:09 +00:00
|
|
|
break;
|
2011-01-04 23:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
void accelerate(game_t *game, double x, double y, double z) {
|
|
|
|
player_accelerate_message_t msg;
|
|
|
|
msg.msg_id = MESSAGE_PLAYER_ACCELERATE;
|
|
|
|
msg.player_id = local_player->id;
|
|
|
|
msg.x = x;
|
|
|
|
msg.y = y;
|
|
|
|
msg.z = z;
|
|
|
|
if (client_peer) {
|
|
|
|
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client_peer, 0, packet);
|
|
|
|
} else {
|
|
|
|
dispatch_message((uint8_t*) &msg, sizeof(msg), game);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void drop_bomb(game_t *game, double x, double y, double z, double ttl) {
|
|
|
|
bomb_drop_meesage_t msg;
|
|
|
|
msg.msg_id = MESSAGE_BOMB_DROP;
|
|
|
|
msg.x = x;
|
|
|
|
msg.y = y;
|
|
|
|
msg.z = z;
|
|
|
|
msg.vx = local_player->vx;
|
|
|
|
msg.vy = local_player->vy;
|
|
|
|
msg.vz = local_player->vz;
|
|
|
|
msg.ttl = ttl;
|
2011-01-05 14:00:09 +00:00
|
|
|
if (client_peer) {
|
2011-01-05 22:02:10 +00:00
|
|
|
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg),
|
|
|
|
ENET_PACKET_FLAG_RELIABLE);
|
|
|
|
enet_peer_send(client_peer, 0, packet);
|
2011-01-06 12:51:52 +00:00
|
|
|
} else {
|
|
|
|
dispatch_message((uint8_t*) &msg, sizeof(msg), game);
|
2011-01-05 14:00:09 +00:00
|
|
|
}
|
2011-01-04 23:07:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-05 22:02:10 +00:00
|
|
|
void setup_explosion() {
|
|
|
|
|
|
|
|
// Loads particle texture
|
|
|
|
GLuint textures[5];
|
|
|
|
|
|
|
|
glGenTextures(5, textures);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glfwLoadTexture2D("data/explosion/explosion.tga", GLFW_ALPHA_MAP_BIT);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[1]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glfwLoadTexture2D("data/explosion/flash.tga", 0);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[2]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glfwLoadTexture2D("data/explosion/spark.tga", 0);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[3]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glfwLoadTexture2D("data/explosion/point.tga", GLFW_ALPHA_MAP_BIT);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[4]);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glfwLoadTexture2D("data/explosion/wave.tga", 0);
|
|
|
|
|
|
|
|
explosion.initialize(textures[0], textures[1], textures[2], textures[3],
|
|
|
|
textures[4]);
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
void explosion_callback(double x, double y, double z) {
|
|
|
|
explosion.add(x, y, z);
|
|
|
|
}
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
int main(int argc, char ** argv) {
|
2011-01-04 17:11:59 +00:00
|
|
|
int width, height, x, y, last_x, last_y;
|
2011-01-06 12:51:52 +00:00
|
|
|
double time, last_time, phi = 0.0, theta = 0.0, last_player_update,
|
|
|
|
last_bomb;
|
2011-01-04 17:11:59 +00:00
|
|
|
GLboolean running;
|
2011-01-04 23:07:08 +00:00
|
|
|
int server = 0;
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
quadratic = gluNewQuadric();
|
|
|
|
gluQuadricNormals(quadratic, GLU_SMOOTH);
|
|
|
|
gluQuadricTexture(quadratic, GL_TRUE);
|
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
game_t game;
|
|
|
|
|
|
|
|
game_setup(&game);
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
setup_opengl();
|
|
|
|
|
2011-01-05 22:02:10 +00:00
|
|
|
setup_explosion();
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
server = 0;
|
|
|
|
setup_network(argv[1]);
|
|
|
|
} else {
|
|
|
|
server = 1;
|
|
|
|
setup_network(NULL);
|
2011-01-05 14:00:09 +00:00
|
|
|
team_t *team = game_team(&game, 0);
|
|
|
|
local_player = game_spawn_player(&game, team);
|
2011-01-04 23:07:08 +00:00
|
|
|
}
|
2011-01-05 22:02:10 +00:00
|
|
|
float plane_color[] = { 0.2f, 0.3f, 0.4f, 1.0f };
|
2011-01-05 14:00:09 +00:00
|
|
|
size_t i;
|
2011-01-04 17:11:59 +00:00
|
|
|
running = GL_TRUE;
|
|
|
|
last_time = glfwGetTime();
|
2011-01-05 14:00:09 +00:00
|
|
|
last_player_update = last_time;
|
2011-01-06 12:51:52 +00:00
|
|
|
last_bomb = last_time;
|
|
|
|
double camX, camY, camZ;
|
2011-01-04 21:24:18 +00:00
|
|
|
glfwGetMousePos(&last_x, &last_y);
|
|
|
|
while (running) {
|
2011-01-04 17:11:59 +00:00
|
|
|
// Get time and mouse position
|
|
|
|
time = glfwGetTime();
|
2011-01-04 23:07:08 +00:00
|
|
|
double dt = time - last_time;
|
2011-01-04 21:24:18 +00:00
|
|
|
glfwGetMousePos(&x, &y);
|
2011-01-04 23:07:08 +00:00
|
|
|
if (glfwGetMouseButton(GLFW_MOUSE_BUTTON_2) == GLFW_PRESS) {
|
|
|
|
phi += (x - last_x) * 0.01;
|
|
|
|
theta += (y - last_y) * -0.01;
|
|
|
|
if (theta > 1.5)
|
|
|
|
theta = 1.5;
|
|
|
|
if (theta < -1.5)
|
|
|
|
theta = -1.5;
|
|
|
|
}
|
2011-01-04 17:11:59 +00:00
|
|
|
last_x = x;
|
|
|
|
last_y = y;
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
if (local_player) {
|
|
|
|
camX = local_player->x;
|
|
|
|
camY = local_player->y;
|
|
|
|
camZ = local_player->z;
|
|
|
|
} else {
|
|
|
|
camX = 1000.0;
|
|
|
|
camY = 1000.0;
|
|
|
|
camZ = 1000.0;
|
|
|
|
}
|
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
double rx = cos(phi) * cos(theta);
|
|
|
|
double ry = sin(theta);
|
|
|
|
double rz = sin(phi) * cos(theta);
|
2011-01-05 14:00:09 +00:00
|
|
|
double v = 50.0f * dt;
|
2011-01-04 23:07:08 +00:00
|
|
|
if (glfwGetKey('W')) {
|
2011-01-06 12:51:52 +00:00
|
|
|
accelerate(&game, rx * v, ry * v, rz * v);
|
2011-01-04 23:07:08 +00:00
|
|
|
} else if (glfwGetKey('S')) {
|
2011-01-06 12:51:52 +00:00
|
|
|
accelerate(&game, rx * -v, ry * -v, rz * -v);
|
2011-01-05 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (glfwGetKey('A')) {
|
2011-01-06 12:51:52 +00:00
|
|
|
accelerate(&game, rz * v, 0, -rx * v);
|
2011-01-04 23:07:08 +00:00
|
|
|
} else if (glfwGetKey('D')) {
|
2011-01-06 12:51:52 +00:00
|
|
|
accelerate(&game, -rz * v, 0, rx * v);
|
2011-01-05 22:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (glfwGetKey(GLFW_KEY_SPACE)) {
|
2011-01-06 12:51:52 +00:00
|
|
|
accelerate(&game, local_player->vx * -dt, local_player->vy * -dt,
|
2011-01-05 22:02:10 +00:00
|
|
|
local_player->vz * -dt);
|
|
|
|
}
|
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
if (glfwGetKey(GLFW_KEY_LCTRL)) {
|
|
|
|
if (time - last_bomb > 5.0) {
|
|
|
|
last_bomb = time;
|
|
|
|
drop_bomb(&game, local_player->x + rx - 20.0, local_player->y
|
|
|
|
+ ry * -20.0, local_player->z + rz * -20.0, 5.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-05 22:02:10 +00:00
|
|
|
if (glfwGetKey(GLFW_KEY_ENTER)) {
|
|
|
|
explosion.add(200.0, 200.0, 200.0);
|
2011-01-04 23:07:08 +00:00
|
|
|
}
|
|
|
|
|
2011-01-04 17:11:59 +00:00
|
|
|
// Get window size (may be different than the requested size)
|
2011-01-04 21:24:18 +00:00
|
|
|
glfwGetWindowSize(&width, &height);
|
2011-01-04 17:11:59 +00:00
|
|
|
height = height > 0 ? height : 1;
|
|
|
|
|
|
|
|
// Set viewport
|
2011-01-04 21:24:18 +00:00
|
|
|
glViewport(0, 0, width, height);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// Clear color buffer
|
2011-01-04 21:24:18 +00:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// Select and setup the projection matrix
|
2011-01-05 22:02:10 +00:00
|
|
|
glMatrixMode( GL_PROJECTION);
|
2011-01-04 17:11:59 +00:00
|
|
|
glLoadIdentity();
|
2011-01-04 21:24:18 +00:00
|
|
|
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f,
|
|
|
|
10000.0f);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
// Select and setup the modelview matrix
|
2011-01-05 22:02:10 +00:00
|
|
|
glMatrixMode( GL_MODELVIEW);
|
2011-01-04 17:11:59 +00:00
|
|
|
glLoadIdentity();
|
2011-01-06 12:51:52 +00:00
|
|
|
gluLookAt(camX, camY, camZ, camX + cos(phi) * cos(theta) * 10.0f, camY
|
|
|
|
+ sin(theta) * 10.0f, camZ + sin(phi) * cos(theta) * 10.0f, // View-point
|
|
|
|
0.0f, 1.0f, 0.0f); // Up-vector
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
service_network(&game);
|
|
|
|
if (server) {
|
2011-01-05 22:02:10 +00:00
|
|
|
if (time > last_player_update + 0.1) {
|
2011-01-05 14:00:09 +00:00
|
|
|
send_player_updates(&game);
|
|
|
|
last_player_update = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
game_update_players(&game, dt);
|
2011-01-06 12:51:52 +00:00
|
|
|
game_update_bombs(&game, dt, explosion_callback);
|
2011-01-05 14:00:09 +00:00
|
|
|
|
2011-01-05 22:02:10 +00:00
|
|
|
glEnable( GL_LIGHT0);
|
|
|
|
glEnable( GL_LIGHTING);
|
|
|
|
glEnable( GL_CULL_FACE);
|
|
|
|
|
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, plane_color);
|
|
|
|
// Draw a textured quad
|
|
|
|
glBegin( GL_QUADS);
|
|
|
|
glVertex3f(-5000.0f, 5000.0f, -5000.0f);
|
|
|
|
glVertex3f(5000.0f, 5000.0f, -5000.0f);
|
|
|
|
glVertex3f(5000.0f, 5000.0f, 5000.0f);
|
|
|
|
glVertex3f(-5000.0f, 5000.0f, 5000.0f);
|
|
|
|
glEnd();
|
2011-01-05 14:00:09 +00:00
|
|
|
for (i = 0; i < GAME_TEAM_COUNT; i++)
|
|
|
|
draw_team(&game.team[i]);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-05 14:00:09 +00:00
|
|
|
for (i = 0; i < GAME_PLAYER_COUNT; i++)
|
|
|
|
draw_player(&game.player[i]);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-06 12:51:52 +00:00
|
|
|
for (i = 0; i < GAME_BOMB_COUNT; i++)
|
|
|
|
draw_bomb(&game.bomb[i]);
|
|
|
|
|
2011-01-05 22:02:10 +00:00
|
|
|
glDisable(GL_LIGHT0);
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
glDisable(GL_CULL_FACE);
|
2011-01-06 12:51:52 +00:00
|
|
|
explosion.update(dt * 1000.0, camX, camY, camZ);
|
2011-01-05 22:02:10 +00:00
|
|
|
explosion.render();
|
|
|
|
|
2011-01-04 17:11:59 +00:00
|
|
|
// Swap buffers
|
|
|
|
glfwSwapBuffers();
|
|
|
|
|
|
|
|
// Check if the ESC key was pressed or the window was closed
|
2011-01-04 21:24:18 +00:00
|
|
|
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
|
|
|
last_time = time;
|
|
|
|
}
|
|
|
|
|
2011-01-04 21:24:18 +00:00
|
|
|
gluDeleteQuadric(quadratic);
|
2011-01-04 17:11:59 +00:00
|
|
|
|
2011-01-04 23:07:08 +00:00
|
|
|
enet_host_destroy(host);
|
|
|
|
enet_deinitialize();
|
|
|
|
|
2011-01-04 17:11:59 +00:00
|
|
|
// Close OpenGL window and terminate GLFW
|
|
|
|
glfwTerminate();
|
|
|
|
|
2011-01-05 22:02:10 +00:00
|
|
|
return 0;
|
2011-01-04 17:11:59 +00:00
|
|
|
}
|
|
|
|
|