added FPS display, fixed player update
This commit is contained in:
parent
8e80efe3a2
commit
6c0b9f7f34
85
src/main.cpp
85
src/main.cpp
@ -31,9 +31,9 @@ void setup_light() {
|
|||||||
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
|
glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
|
||||||
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
|
||||||
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
|
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
|
||||||
glEnable(GL_LIGHT0);
|
glEnable( GL_LIGHT0);
|
||||||
|
|
||||||
glEnable(GL_LIGHTING);
|
glEnable( GL_LIGHTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_opengl() {
|
void setup_opengl() {
|
||||||
@ -51,23 +51,23 @@ void setup_opengl() {
|
|||||||
glfwEnable(GLFW_STICKY_KEYS);
|
glfwEnable(GLFW_STICKY_KEYS);
|
||||||
|
|
||||||
// general settings
|
// general settings
|
||||||
glShadeModel(GL_SMOOTH);
|
glShadeModel( GL_SMOOTH);
|
||||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable( GL_CULL_FACE);
|
||||||
|
|
||||||
// setup depth buffer
|
// setup depth buffer
|
||||||
glClearDepth(1.0f);
|
glClearDepth(1.0f);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable( GL_DEPTH_TEST);
|
||||||
glDepthFunc(GL_LEQUAL);
|
glDepthFunc( GL_LEQUAL);
|
||||||
|
|
||||||
// Enable vertical sync (on cards that support it)
|
// Enable vertical sync (on cards that support it)
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_team(team_t *team) {
|
void draw_team(team_t *team) {
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode( GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(team->x, team->y, team->z);
|
glTranslated(team->x, team->y, team->z);
|
||||||
gluSphere(quadratic, 50.f, 32, 32);
|
gluSphere(quadratic, 50.f, 32, 32);
|
||||||
@ -78,7 +78,7 @@ void draw_player(player_t *player) {
|
|||||||
if (player->status == 0)
|
if (player->status == 0)
|
||||||
return;
|
return;
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode( GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(player->x, player->y, player->z);
|
glTranslated(player->x, player->y, player->z);
|
||||||
gluSphere(quadratic, 10.f, 32, 32);
|
gluSphere(quadratic, 10.f, 32, 32);
|
||||||
@ -90,7 +90,7 @@ void draw_bomb(bomb_t *bomb) {
|
|||||||
return;
|
return;
|
||||||
GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
|
GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode( GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(bomb->x, bomb->y, bomb->z);
|
glTranslated(bomb->x, bomb->y, bomb->z);
|
||||||
gluSphere(quadratic, 3.f, 4, 4);
|
gluSphere(quadratic, 3.f, 4, 4);
|
||||||
@ -101,7 +101,7 @@ void draw_point(point_t *point) {
|
|||||||
return;
|
return;
|
||||||
GLfloat red[] = { 0.0f, 0.0f, 1.0f, 1.0f };
|
GLfloat red[] = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode( GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(point->x, point->y, point->z);
|
glTranslated(point->x, point->y, point->z);
|
||||||
gluSphere(quadratic, 3.f, 12, 12);
|
gluSphere(quadratic, 3.f, 12, 12);
|
||||||
@ -162,27 +162,30 @@ GLuint wallTex = 0;
|
|||||||
|
|
||||||
void draw_box() {
|
void draw_box() {
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, wallTex);
|
|
||||||
glEnable(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
if (wallTex == 0) {
|
if (wallTex == 0) {
|
||||||
|
glGenTextures(1, &wallTex);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, wallTex);
|
||||||
|
glEnable( GL_TEXTURE_2D);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
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_WRAP_T, GL_REPEAT);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glfwLoadTexture2D("data/wall.tga", 0);
|
glfwLoadTexture2D("data/wall.tga", GLFW_BUILD_MIPMAPS_BIT);
|
||||||
|
} else {
|
||||||
|
glBindTexture(GL_TEXTURE_2D, wallTex);
|
||||||
|
glEnable( GL_TEXTURE_2D);
|
||||||
}
|
}
|
||||||
|
|
||||||
GLfloat red[] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
GLfloat red[] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
||||||
|
|
||||||
// Enable/Disable features
|
// Enable/Disable features
|
||||||
glPushAttrib(GL_ENABLE_BIT);
|
glPushAttrib( GL_ENABLE_BIT);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable( GL_TEXTURE_2D);
|
||||||
//glDisable( GL_DEPTH_TEST);
|
//glDisable( GL_DEPTH_TEST);
|
||||||
// glEnable( GL_LIGHTING);
|
// glEnable( GL_LIGHTING);
|
||||||
// glDisable( GL_BLEND);
|
// glDisable( GL_BLEND);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode( GL_MODELVIEW);
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(2000.0, 0.0, 0.0);
|
glTranslated(2000.0, 0.0, 0.0);
|
||||||
//glScaled(5000.0f, 5000.0f, 5000.0f);
|
//glScaled(5000.0f, 5000.0f, 5000.0f);
|
||||||
@ -191,7 +194,7 @@ void draw_box() {
|
|||||||
glColor4f(1, 1, 1, 1);
|
glColor4f(1, 1, 1, 1);
|
||||||
|
|
||||||
// Render the front quad
|
// Render the front quad
|
||||||
glBegin(GL_QUADS);
|
glBegin( GL_QUADS);
|
||||||
glTexCoord2f(0, t);
|
glTexCoord2f(0, t);
|
||||||
glNormal3f(0.0, 0.0, 1.0);
|
glNormal3f(0.0, 0.0, 1.0);
|
||||||
glVertex3f(-s, s, -s);
|
glVertex3f(-s, s, -s);
|
||||||
@ -299,7 +302,7 @@ private:
|
|||||||
double phi, theta;
|
double phi, theta;
|
||||||
double camX, camY, camZ;
|
double camX, camY, camZ;
|
||||||
Schedule accelerate_schudule;
|
Schedule accelerate_schudule;
|
||||||
Schedule player_update_schudule;
|
Schedule game_update_schudule;
|
||||||
game_t game;
|
game_t game;
|
||||||
int server;
|
int server;
|
||||||
int width, height;
|
int width, height;
|
||||||
@ -367,12 +370,12 @@ void Application::prepareFrame(double rx, double ry, double rz) {
|
|||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// Select and setup the projection matrix
|
// Select and setup the projection matrix
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode( GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f, 10000.0f);
|
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f, 10000.0f);
|
||||||
|
|
||||||
// Select and setup the modelview matrix
|
// Select and setup the modelview matrix
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode( GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluLookAt(camX, camY, camZ, camX + rx * 10.0f, camY + ry * 10.0f, camZ + rz
|
gluLookAt(camX, camY, camZ, camX + rx * 10.0f, camY + ry * 10.0f, camZ + rz
|
||||||
* 10.0f, 0.0f, 1.0f, 0.0f);
|
* 10.0f, 0.0f, 1.0f, 0.0f);
|
||||||
@ -428,8 +431,8 @@ void Application::initialize(int argc, char ** argv) {
|
|||||||
accelerate_schudule.setExact(true);
|
accelerate_schudule.setExact(true);
|
||||||
accelerate_schudule.setInterval(0.05);
|
accelerate_schudule.setInterval(0.05);
|
||||||
|
|
||||||
player_update_schudule.setExact(true);
|
game_update_schudule.setExact(true);
|
||||||
player_update_schudule.setInterval(0.05);
|
game_update_schudule.setInterval(0.05);
|
||||||
|
|
||||||
loadConsoleFont();
|
loadConsoleFont();
|
||||||
}
|
}
|
||||||
@ -452,15 +455,15 @@ void Application::update() {
|
|||||||
}
|
}
|
||||||
last_x = x;
|
last_x = x;
|
||||||
last_y = y;
|
last_y = y;
|
||||||
|
|
||||||
service_network(&game);
|
|
||||||
game_update(&game, dt);
|
|
||||||
if (server && player_update_schudule.next(time)) {
|
|
||||||
send_player_updates(&game);
|
|
||||||
}
|
|
||||||
double rx = cos(phi) * cos(theta);
|
double rx = cos(phi) * cos(theta);
|
||||||
double ry = sin(theta);
|
double ry = sin(theta);
|
||||||
double rz = sin(phi) * cos(theta);
|
double rz = sin(phi) * cos(theta);
|
||||||
|
service_network(&game);
|
||||||
|
game_update(&game, dt);
|
||||||
|
if (server && game_update_schudule.next(time)) {
|
||||||
|
send_game_updates(&game);
|
||||||
|
}
|
||||||
|
|
||||||
if (accelerate_schudule.next(time)) {
|
if (accelerate_schudule.next(time)) {
|
||||||
double t = accelerate_schudule.getInterval();
|
double t = accelerate_schudule.getInterval();
|
||||||
double v = 50.0 * t;
|
double v = 50.0 * t;
|
||||||
@ -497,13 +500,12 @@ void Application::update() {
|
|||||||
camY = 1000.0;
|
camY = 1000.0;
|
||||||
camZ = 1000.0;
|
camZ = 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareFrame(rx, ry, rz);
|
prepareFrame(rx, ry, rz);
|
||||||
setup_light();
|
setup_light();
|
||||||
glEnable(GL_LIGHT0);
|
glEnable( GL_LIGHT0);
|
||||||
glEnable(GL_LIGHTING);
|
glEnable( GL_LIGHTING);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable( GL_CULL_FACE);
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable( GL_TEXTURE_2D);
|
||||||
for (size_t i = 0; i < GAME_TEAM_COUNT; i++)
|
for (size_t i = 0; i < GAME_TEAM_COUNT; i++)
|
||||||
draw_team(&game.team[i]);
|
draw_team(&game.team[i]);
|
||||||
|
|
||||||
@ -524,10 +526,12 @@ void Application::update() {
|
|||||||
explosion.update(dt * 1000.0, camX, camY, camZ);
|
explosion.update(dt * 1000.0, camX, camY, camZ);
|
||||||
explosion.render();
|
explosion.render();
|
||||||
oglf_begin(&font, width, height);
|
oglf_begin(&font, width, height);
|
||||||
|
size_t fy = 1;
|
||||||
if (game.local_player) {
|
if (game.local_player) {
|
||||||
std::stringstream sstr;
|
std::stringstream sstr;
|
||||||
sstr << "Points: " << game.local_player->points;
|
sstr << "Points: " << game.local_player->points;
|
||||||
oglf_print(&font, 10, 10, sstr.str().c_str());
|
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) {
|
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) {
|
||||||
@ -537,7 +541,14 @@ void Application::update() {
|
|||||||
} else {
|
} else {
|
||||||
sstr << "Team " << i << " (other) : " << game.team[i].points;
|
sstr << "Team " << i << " (other) : " << game.team[i].points;
|
||||||
}
|
}
|
||||||
oglf_print(&font, 10, (i + 2) * 15, sstr.str().c_str());
|
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::stringstream sstr;
|
||||||
|
sstr << "FPS: " << (int) (1 / dt) << " Time: " << round(dt * 10000.0)
|
||||||
|
/ 10.0;
|
||||||
|
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
oglf_end();
|
oglf_end();
|
||||||
|
@ -8,7 +8,9 @@
|
|||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
|
||||||
#include <enet/enet.h>
|
#include <enet/enet.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
ENetHost *host = 0;
|
ENetHost *host = 0;
|
||||||
ENetPeer *client_peer = 0;
|
ENetPeer *client_peer = 0;
|
||||||
@ -16,7 +18,7 @@ ENetPeer *client_peer = 0;
|
|||||||
void setup_network(const char *remote) {
|
void setup_network(const char *remote) {
|
||||||
if (enet_initialize() != 0) {
|
if (enet_initialize() != 0) {
|
||||||
fprintf(stderr, "An error occurred while initializing ENet.\n");
|
fprintf(stderr, "An error occurred while initializing ENet.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit( EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remote == NULL) {
|
if (remote == NULL) {
|
||||||
@ -30,7 +32,7 @@ void setup_network(const char *remote) {
|
|||||||
if (host == NULL) {
|
if (host == NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"An error occurred while trying to create an ENet server host.\n");
|
"An error occurred while trying to create an ENet server host.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit( EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stdout, "Start client.\n");
|
fprintf(stdout, "Start client.\n");
|
||||||
@ -41,7 +43,7 @@ void setup_network(const char *remote) {
|
|||||||
if (host == NULL) {
|
if (host == NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"An error occurred while trying to create an ENet client host.\n");
|
"An error occurred while trying to create an ENet client host.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit( EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
enet_address_set_host(&address, remote);
|
enet_address_set_host(&address, remote);
|
||||||
@ -53,7 +55,7 @@ void setup_network(const char *remote) {
|
|||||||
if (client_peer == NULL) {
|
if (client_peer == NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"No available peers for initiating an ENet connection.\n");
|
"No available peers for initiating an ENet connection.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit( EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Wait up to 5 seconds for the connection attempt to succeed. */
|
/* Wait up to 5 seconds for the connection attempt to succeed. */
|
||||||
@ -77,7 +79,7 @@ void shutdown_network() {
|
|||||||
enet_deinitialize();
|
enet_deinitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_player_updates(game_t *game) {
|
void send_game_updates(game_t *game) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
|
for (i = 0; i < GAME_PLAYER_COUNT; i++) {
|
||||||
if (game->player[i].status == 0)
|
if (game->player[i].status == 0)
|
||||||
@ -104,6 +106,8 @@ void send_player_updates(game_t *game) {
|
|||||||
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
|
ENetPacket * packet = enet_packet_create(&msg, sizeof(msg), 0);
|
||||||
enet_host_broadcast(host, 0, packet);
|
enet_host_broadcast(host, 0, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enet_host_flush(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatch_message(enet_uint8 *data, size_t length, game_t *game) {
|
void dispatch_message(enet_uint8 *data, size_t length, game_t *game) {
|
||||||
@ -140,9 +144,24 @@ void dispatch_message(enet_uint8 *data, size_t length, game_t *game) {
|
|||||||
player->vy = um->vy;
|
player->vy = um->vy;
|
||||||
player->vz = um->vz;
|
player->vz = um->vz;
|
||||||
} else {
|
} else {
|
||||||
|
if (fabs(um->x - player->x) < 10.0) {
|
||||||
player->vx = um->vx + (um->x - player->x);
|
player->vx = um->vx + (um->x - player->x);
|
||||||
|
} else {
|
||||||
|
player->x = um->x;
|
||||||
|
player->vx = um->vx;
|
||||||
|
}
|
||||||
|
if (fabs(um->y - player->y) < 10.0) {
|
||||||
player->vy = um->vy + (um->y - player->y);
|
player->vy = um->vy + (um->y - player->y);
|
||||||
|
} else {
|
||||||
|
player->y = um->y;
|
||||||
|
player->vy = um->vy;
|
||||||
|
}
|
||||||
|
if (fabs(um->z - player->z) < 10.0) {
|
||||||
player->vz = um->vz + (um->z - player->z);
|
player->vz = um->vz + (um->z - player->z);
|
||||||
|
} else {
|
||||||
|
player->z = um->z;
|
||||||
|
player->vz = um->vz;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
player->points = um->points;
|
player->points = um->points;
|
||||||
break;
|
break;
|
||||||
|
@ -89,7 +89,7 @@ void setup_network(const char *remote);
|
|||||||
void shutdown_network();
|
void shutdown_network();
|
||||||
void dispatch_message(uint8_t *data, size_t length, game_t *game);
|
void dispatch_message(uint8_t *data, size_t length, game_t *game);
|
||||||
void service_network(game_t *game);
|
void service_network(game_t *game);
|
||||||
void send_player_updates(game_t *game);
|
void send_game_updates(game_t *game);
|
||||||
void send_message(uint8_t *data, size_t length, game_t *game);
|
void send_message(uint8_t *data, size_t length, game_t *game);
|
||||||
|
|
||||||
#endif /* NETWORK_H_ */
|
#endif /* NETWORK_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user