added FPS display, fixed player update

This commit is contained in:
gmueller 2011-01-12 23:41:33 +01:00
parent 8e80efe3a2
commit 6c0b9f7f34
3 changed files with 76 additions and 46 deletions

View File

@ -61,7 +61,7 @@ void setup_opengl() {
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) {
@ -162,15 +162,18 @@ GLuint wallTex = 0;
void draw_box() { void draw_box() {
if (wallTex == 0) {
glGenTextures(1, &wallTex);
glBindTexture(GL_TEXTURE_2D, wallTex); glBindTexture(GL_TEXTURE_2D, wallTex);
glEnable( GL_TEXTURE_2D); glEnable( GL_TEXTURE_2D);
if (wallTex == 0) {
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 };
@ -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;
@ -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,7 +500,6 @@ 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);
@ -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();

View File

@ -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;
@ -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;

View File

@ -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_ */