#include #include #include #include #include #include #include #include "common.h" #include "game.h" #include "Explosion.h" #include "Schedule.h" #include "oglfont.h" GLUquadricObj *quadratic; ENetHost *host; ENetPeer *client_peer; player_t *local_player; Explosion explosion; void key_callback(int key, int state) { } void setup_light() { GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat LightPosition[] = { 1.0f, 1.0f, 1.0f, 0.0f }; // setup directional light glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); glEnable( GL_LIGHT0); glEnable( GL_LIGHTING); } void setup_opengl() { // Initialise GLFW if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); exit(EXIT_FAILURE); } // Open OpenGL window if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) { fprintf(stderr, "Failed to open GLFW window\n"); glfwTerminate(); exit(EXIT_FAILURE); } //glfwDisable(GLFW_MOUSE_CURSOR); glfwSetWindowTitle("Gremlin Lan Party Game"); glfwSetKeyCallback(key_callback); // Enable sticky keys glfwEnable(GLFW_STICKY_KEYS); // general settings glShadeModel( GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable( GL_CULL_FACE); // setup depth buffer glClearDepth(1.0f); glEnable( GL_DEPTH_TEST); glDepthFunc( GL_LEQUAL); // Enable vertical sync (on cards that support it) glfwSwapInterval(1); } void draw_team(team_t *team) { size_t i = 0; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color); glMatrixMode( GL_MODELVIEW); glPushMatrix(); glTranslated(team->x, team->y, team->z); gluSphere(quadratic, 50.f, 32, 32); glPopMatrix(); } void draw_player(player_t *player) { if (player->status == 0) return; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color); glMatrixMode( GL_MODELVIEW); glPushMatrix(); glTranslated(player->x, player->y, player->z); gluSphere(quadratic, 10.f, 32, 32); glPopMatrix(); } 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, 4, 4); glPopMatrix(); } void draw_point(point_t *point) { if (point->status == 0) return; GLfloat red[] = { 0.0f, 0.0f, 1.0f, 1.0f }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); glMatrixMode( GL_MODELVIEW); glPushMatrix(); glTranslated(point->x, point->y, point->z); gluSphere(quadratic, 3.f, 12, 12); glPopMatrix(); } 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); } } } #define MESSAGE_PLAYER_SPAWN 0 #define MESSAGE_PLAYER_KILL 1 #define MESSAGE_ACCEPT 2 #define MESSAGE_PLAYER_UPDATE 3 #define MESSAGE_PLAYER_ACCELERATE 4 #define MESSAGE_BOMB_DROP 5 #define MESSAGE_BOMB_UPDATE 6 #define MESSAGE_POINT_UPDATE 7 #define MESSAGE_TEAM_UPDATE 8 typedef struct message_t { uint16_t msg_id; } message_t; typedef struct player_spawn_message_t { uint16_t msg_id; uint8_t team_id; uint16_t player_id; } player_spawn_message_t; typedef struct player_kill_message_t { uint16_t msg_id; uint16_t player_id; } player_kill_message_t; typedef struct accept_message_t { uint16_t msg_id; uint16_t player_id; } accept_message_t; typedef struct player_update_message_t { uint16_t msg_id; uint16_t player_id; unsigned int session; double x, y, z; double vx, vy, vz; uint16_t points; } player_update_message_t; typedef struct player_accelerate_message_t { uint16_t msg_id; uint16_t player_id; double x, y, z; } player_accelerate_message_t; 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; typedef struct point_update_mesage_t { uint16_t msg_id; uint16_t point_index; uint8_t status; double x, y, z; } point_update_meesage_t; struct team_update_message_t { uint16_t msg_id; uint16_t team_id; uint16_t points; uint16_t wins; }; 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; msg.points = game->player[i].points; ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0); enet_host_broadcast(host, 0, packet); } for (i = 0; i < GAME_TEAM_COUNT; i++) { team_update_message_t msg; msg.msg_id = MESSAGE_TEAM_UPDATE; msg.team_id = game->team[i].id; msg.points = game->team[i].points; msg.wins = game->team[i].wins; ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0); enet_host_broadcast(host, 0, packet); } } 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; player->points = um->points; break; } case MESSAGE_POINT_UPDATE: { point_update_mesage_t *msg = (point_update_mesage_t *) data; point_t *p = &game->point[msg->point_index]; p->status = msg->status; p->x = msg->x; p->y = msg->y; p->z = msg->z; 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; } case MESSAGE_TEAM_UPDATE: { team_update_message_t *m = (team_update_message_t *) data; team_t *team = game_team(game, m->team_id); if (team == NULL) return; team->points = m->points; team->wins = m->wins; break; } }; } void service_network(game_t *game) { 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); { // 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; spwan_msg.player_id = game->player[i].id; ENetPacket * packet = enet_packet_create(&spwan_msg, sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, packet); } for (i = 0; i < GAME_POINT_COUNT; i++) { if (game->point[i].status == 0) continue; point_update_mesage_t msg; msg.msg_id = MESSAGE_POINT_UPDATE; msg.point_index = i; msg.status = game->point[i].status; msg.x = game->point[i].x; msg.y = game->point[i].y; msg.z = game->point[i].z; ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), ENET_PACKET_FLAG_RELIABLE); 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; ENetPacket * packet = enet_packet_create(&spwan_msg, sizeof(spwan_msg), ENET_PACKET_FLAG_RELIABLE); enet_host_broadcast(host, 0, packet); // send team and player accept_message_t msg; msg.msg_id = MESSAGE_ACCEPT; msg.player_id = player->id; packet = enet_packet_create(&msg, sizeof(msg), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, packet); // send state } break; case ENET_EVENT_TYPE_RECEIVE: { dispatch_message(event.packet->data, event.packet->dataLength, game); enet_packet_destroy(event.packet); break; } case ENET_EVENT_TYPE_DISCONNECT: printf("%s disconected.\n", event.peer -> data); { /* 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); } event.peer->data = NULL; break; } } } void accelerate(game_t *game, double x, double y, double z) { if (local_player == 0) return; 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 rx, double ry, double rz, double ttl) { bomb_drop_meesage_t msg; msg.msg_id = MESSAGE_BOMB_DROP; msg.x = local_player->x + rx * 20; msg.y = local_player->y + ry * 20; msg.z = local_player->z + rz * 20; msg.vx = local_player->vx + rx * 100; msg.vy = local_player->vy + ry * 100; msg.vz = local_player->vz + rz * 100; msg.ttl = ttl; 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 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]); } void explosion_callback(double x, double y, double z) { explosion.add(x, y, z); } GLuint wallTex = 0; void draw_box() { glBindTexture(GL_TEXTURE_2D, wallTex); glEnable( GL_TEXTURE_2D); if (wallTex == 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/wall.tga", 0); } GLfloat red[] = { 0.1f, 0.2f, 0.3f, 1.0f }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); // Enable/Disable features glPushAttrib( GL_ENABLE_BIT); glEnable(GL_TEXTURE_2D); //glDisable( GL_DEPTH_TEST); // glEnable( GL_LIGHTING); // glDisable( GL_BLEND); glMatrixMode( GL_MODELVIEW); glPushMatrix(); glTranslated(2000.0, 0.0, 0.0); //glScaled(5000.0f, 5000.0f, 5000.0f); float s = 2500.0; // Just in case we set all vertices to white. glColor4f(1, 1, 1, 1); // Render the front quad // glBindTexture(GL_TEXTURE_2D, _skybox[0]); glBegin( GL_QUADS); glTexCoord2f(0, 0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(s, -s, -s); glTexCoord2f(0, 1); glNormal3f(0.0, 0.0, 1.0); glVertex3f(s, s, -s); glTexCoord2f(1, 1); glNormal3f(0.0, 0.0, 1.0); glVertex3f(-s, s, -s); glTexCoord2f(1, 0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(-s, -s, -s); glEnd(); // Render the left quad // glBindTexture(GL_TEXTURE_2D, _skybox[1]); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(s, -s, s); glTexCoord2f(0, 1); glVertex3f(s, s, s); glTexCoord2f(1, 1); glVertex3f(s, s, -s); glTexCoord2f(1, 0); glVertex3f(s, -s, -s); glEnd(); // Render the back quad // glBindTexture(GL_TEXTURE_2D, _skybox[2]); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(-s, -s, s); glTexCoord2f(0, 1); glVertex3f(-s, s, s); glTexCoord2f(1, 1); glVertex3f(s, s, s); glTexCoord2f(1, 0); glVertex3f(s, -s, s); glEnd(); // Render the right quad // glBindTexture(GL_TEXTURE_2D, _skybox[3]); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(-s, -s, -s); glTexCoord2f(0, 1); glVertex3f(-s, s, -s); glTexCoord2f(1, 1); glVertex3f(-s, s, s); glTexCoord2f(1, 0); glVertex3f(-s, -s, s); glEnd(); // Render the top quad //glBindTexture(GL_TEXTURE_2D, _skybox[4]); glBegin(GL_QUADS); glTexCoord2f(0, 1); glVertex3f(-s, s, -s); glTexCoord2f(1, 1); glVertex3f(s, s, -s); glTexCoord2f(1, 0); glVertex3f(s, s, s); glTexCoord2f(0, 0); glVertex3f(-s, s, s); glEnd(); // Render the bottom quad //glBindTexture(GL_TEXTURE_2D, _skybox[5]); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(-s, -s, -s); glTexCoord2f(0, 1); glVertex3f(-s, -s, s); glTexCoord2f(1, 1); glVertex3f(s, -s, s); glTexCoord2f(1, 0); glVertex3f(s, -s, -s); glEnd(); // Restore enable bits and matrix glPopAttrib(); glPopMatrix(); } class Application { public: void initialize(int argc, char ** argv); void update(); void shutdown(); bool isRunning(); private: bool running; double last_time, last_bomb; //TODO: use bomb schedule; int last_x, last_y; double phi, theta; double camX, camY, camZ; Schedule accelerate_schudule; Schedule player_update_schudule; game_t game; int server; int width, height; //TrueTyeFont font; oglf_font_t font; void prepareFrame(double rx, double ry, double rz); void loadConsoleFont(); }; void Application::loadConsoleFont() { GLuint font_id = 0; glGenTextures(1, &font_id); glBindTexture(GL_TEXTURE_2D, font_id); // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Upload texture from file to texture memory glfwLoadTexture2D("data/fonts/console.tga", 0); oglf_load(&font, "data/fonts/console.fnt", font_id); } void Application::prepareFrame(double rx, double ry, double rz) { // Get window size (may be different than the requested size) glfwGetWindowSize(&width, &height); height = height > 0 ? height : 1; // Set viewport glViewport(0, 0, width, height); // Clear color buffer glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Select and setup the projection matrix glMatrixMode( GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f, 10000.0f); // Select and setup the modelview matrix glMatrixMode( GL_MODELVIEW); glLoadIdentity(); gluLookAt(camX, camY, camZ, camX + rx * 10.0f, camY + ry * 10.0f, camZ + rz * 10.0f, 0.0f, 1.0f, 0.0f); } void Application::initialize(int argc, char ** argv) { server = 0; double time, last_player_update; quadratic = gluNewQuadric(); gluQuadricNormals(quadratic, GLU_SMOOTH); gluQuadricTexture(quadratic, GL_TRUE); if (argc > 1) { server = 0; } else { server = 1; } game_setup(&game, server, explosion_callback); setup_opengl(); setup_explosion(); if (server) { setup_network(NULL); team_t *team = game_team(&game, 0); local_player = game_spawn_player(&game, team); } else { setup_network(argv[1]); } size_t i; running = GL_TRUE; last_time = glfwGetTime(); last_player_update = last_time; last_bomb = last_time - 5.; glfwGetMousePos(&last_x, &last_y); accelerate_schudule.setExact(true); accelerate_schudule.setInterval(0.05); player_update_schudule.setExact(true); player_update_schudule.setInterval(0.1); loadConsoleFont(); } void Application::update() { // Get time and mouse position double time = glfwGetTime(); double dt = time - last_time; int x, y; glfwGetMousePos(&x, &y); 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; } last_x = x; last_y = y; 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; } service_network(&game); if (server && player_update_schudule.next(time)) { send_player_updates(&game); } game_update(&game, dt); double rx = cos(phi) * cos(theta); double ry = sin(theta); double rz = sin(phi) * cos(theta); if (accelerate_schudule.next(time)) { double t = accelerate_schudule.getInterval(); double v = 50.0 * t; if (glfwGetKey('W')) { accelerate(&game, rx * v, ry * v, rz * v); } else if (glfwGetKey('S')) { accelerate(&game, rx * -v, ry * -v, rz * -v); } if (glfwGetKey('A')) { accelerate(&game, rz * v, 0, -rx * v); } else if (glfwGetKey('D')) { accelerate(&game, -rz * v, 0, rx * v); } if (glfwGetKey(GLFW_KEY_SPACE)) { accelerate(&game, local_player->vx * -0.1, local_player->vy * -0.1, local_player->vz * -0.1); } } if (glfwGetKey(GLFW_KEY_LCTRL)) { if (time - last_bomb > 1.0) { last_bomb = time; drop_bomb(&game, rx, ry, rz, 5.0); } } service_network(&game); prepareFrame(rx, ry, rz); setup_light(); glEnable( GL_LIGHT0); glEnable( GL_LIGHTING); glEnable( GL_CULL_FACE); for (size_t i = 0; i < GAME_TEAM_COUNT; i++) draw_team(&game.team[i]); for (size_t i = 0; i < GAME_PLAYER_COUNT; i++) draw_player(&game.player[i]); for (size_t i = 0; i < GAME_BOMB_COUNT; i++) draw_bomb(&game.bomb[i]); for (size_t i = 0; i < GAME_POINT_COUNT; i++) draw_point(&game.point[i]); draw_box(); glDisable(GL_LIGHT0); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); explosion.update(dt * 1000.0, camX, camY, camZ); explosion.render(); oglf_begin(&font, width, height); if (local_player) { std::stringstream sstr; sstr << "Points: " << local_player->points; oglf_print(&font, 10, 10, sstr.str().c_str()); } for (size_t i = 0; i < GAME_TEAM_COUNT; i++) { std::stringstream sstr; if (local_player && &game.team[i] == local_player->team) { sstr << "Team " << i << " (yours) : " << game.team[i].points; } else { sstr << "Team " << i << " (other) : " << game.team[i].points; } oglf_print(&font, 10, (i + 2) * 15, sstr.str().c_str()); } oglf_end(); // Swap buffers glfwSwapBuffers(); // Check if the ESC key was pressed or the window was closed running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED); last_time = time; } void Application::shutdown() { gluDeleteQuadric(quadratic); enet_host_destroy(host); enet_deinitialize(); oglf_destroy(&font); // Close OpenGL window and terminate GLFW glfwTerminate(); } bool Application::isRunning() { return running; } int main(int argc, char ** argv) { Application app; app.initialize(argc, argv); while (app.isRunning()) app.update(); app.shutdown(); return 0; }