2011-01-04 18:11:59 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2011-01-05 00:07:08 +01:00
|
|
|
#include <string.h>
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-09 21:56:48 +01:00
|
|
|
#include <sstream>
|
2011-01-04 18:11:59 +01:00
|
|
|
#include <GL/glfw.h>
|
2011-01-12 22:05:22 +01:00
|
|
|
#include <enet/enet.h>
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-05 15:00:09 +01:00
|
|
|
#include "common.h"
|
|
|
|
#include "game.h"
|
2011-01-05 23:02:10 +01:00
|
|
|
#include "Explosion.h"
|
2011-01-08 17:53:45 +01:00
|
|
|
#include "Schedule.h"
|
|
|
|
#include "oglfont.h"
|
2011-01-12 22:05:22 +01:00
|
|
|
#include "Time.h"
|
|
|
|
#include "network.h"
|
2011-01-04 18:11:59 +01:00
|
|
|
|
|
|
|
GLUquadricObj *quadratic;
|
2011-01-05 23:02:10 +01:00
|
|
|
Explosion explosion;
|
2011-01-05 00:07:08 +01:00
|
|
|
|
|
|
|
void key_callback(int key, int state) {
|
|
|
|
|
|
|
|
}
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-10 21:44:38 +01:00
|
|
|
void setup_light() {
|
2011-01-10 23:02:38 +01:00
|
|
|
GLfloat LightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
|
|
|
|
GLfloat LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
|
2011-01-10 21:44:38 +01:00
|
|
|
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);
|
2011-01-12 15:28:44 +01:00
|
|
|
glEnable(GL_LIGHT0);
|
2011-01-10 21:44:38 +01:00
|
|
|
|
2011-01-12 15:28:44 +01:00
|
|
|
glEnable(GL_LIGHTING);
|
2011-01-10 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
2011-01-04 22:24:18 +01:00
|
|
|
void setup_opengl() {
|
2011-01-04 18:11:59 +01:00
|
|
|
|
|
|
|
// Open OpenGL window
|
2011-01-05 00:07:08 +01:00
|
|
|
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) {
|
2011-01-04 22:24:18 +01:00
|
|
|
fprintf(stderr, "Failed to open GLFW window\n");
|
2011-01-04 18:11:59 +01:00
|
|
|
glfwTerminate();
|
2011-01-04 22:24:18 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-04 18:11:59 +01:00
|
|
|
}
|
2011-01-05 00:07:08 +01:00
|
|
|
//glfwDisable(GLFW_MOUSE_CURSOR);
|
|
|
|
glfwSetWindowTitle("Gremlin Lan Party Game");
|
|
|
|
glfwSetKeyCallback(key_callback);
|
2011-01-04 18:11:59 +01:00
|
|
|
// Enable sticky keys
|
2011-01-04 22:24:18 +01:00
|
|
|
glfwEnable(GLFW_STICKY_KEYS);
|
2011-01-04 18:11:59 +01:00
|
|
|
|
|
|
|
// general settings
|
2011-01-12 15:28:44 +01:00
|
|
|
glShadeModel(GL_SMOOTH);
|
2011-01-04 18:11:59 +01:00
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
2011-01-12 15:28:44 +01:00
|
|
|
glEnable(GL_CULL_FACE);
|
2011-01-04 18:11:59 +01:00
|
|
|
|
|
|
|
// setup depth buffer
|
|
|
|
glClearDepth(1.0f);
|
2011-01-12 15:28:44 +01:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
2011-01-04 18:11:59 +01:00
|
|
|
|
|
|
|
// Enable vertical sync (on cards that support it)
|
2011-01-09 21:56:48 +01:00
|
|
|
glfwSwapInterval(1);
|
2011-01-04 18:11:59 +01:00
|
|
|
}
|
|
|
|
|
2011-01-05 00:07:08 +01:00
|
|
|
void draw_team(team_t *team) {
|
2011-01-04 18:11:59 +01:00
|
|
|
size_t i = 0;
|
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2011-01-04 18:11:59 +01:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(team->x, team->y, team->z);
|
2011-01-04 22:24:18 +01:00
|
|
|
gluSphere(quadratic, 50.f, 32, 32);
|
2011-01-04 18:11:59 +01:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2011-01-05 15:00:09 +01:00
|
|
|
void draw_player(player_t *player) {
|
|
|
|
if (player->status == 0)
|
|
|
|
return;
|
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, player->team->color);
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2011-01-05 15:00:09 +01:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(player->x, player->y, player->z);
|
|
|
|
gluSphere(quadratic, 10.f, 32, 32);
|
|
|
|
glPopMatrix();
|
2011-01-05 00:07:08 +01:00
|
|
|
}
|
|
|
|
|
2011-01-06 13:51:52 +01: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);
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2011-01-06 13:51:52 +01:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(bomb->x, bomb->y, bomb->z);
|
2011-01-09 21:56:48 +01:00
|
|
|
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);
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2011-01-09 21:56:48 +01:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(point->x, point->y, point->z);
|
|
|
|
gluSphere(quadratic, 3.f, 12, 12);
|
2011-01-06 13:51:52 +01:00
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2011-01-05 23:02:10 +01: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 13:51:52 +01:00
|
|
|
void explosion_callback(double x, double y, double z) {
|
|
|
|
explosion.add(x, y, z);
|
|
|
|
}
|
|
|
|
|
2011-01-10 23:14:56 +01:00
|
|
|
GLuint wallTex = 0;
|
|
|
|
|
|
|
|
void draw_box() {
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, wallTex);
|
2011-01-12 15:28:44 +01:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2011-01-10 23:14:56 +01:00
|
|
|
|
|
|
|
if (wallTex == 0) {
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
2011-01-10 23:14:56 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-12 15:28:44 +01:00
|
|
|
GLfloat red[] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
2011-01-10 23:14:56 +01:00
|
|
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
|
|
|
|
|
|
|
|
// Enable/Disable features
|
2011-01-12 15:28:44 +01:00
|
|
|
glPushAttrib(GL_ENABLE_BIT);
|
2011-01-10 23:14:56 +01:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
//glDisable( GL_DEPTH_TEST);
|
|
|
|
// glEnable( GL_LIGHTING);
|
|
|
|
// glDisable( GL_BLEND);
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2011-01-10 23:14:56 +01:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslated(2000.0, 0.0, 0.0);
|
|
|
|
//glScaled(5000.0f, 5000.0f, 5000.0f);
|
2011-01-12 15:28:44 +01:00
|
|
|
float s = 3000.0f, t = 5.0f;
|
2011-01-10 23:14:56 +01:00
|
|
|
// Just in case we set all vertices to white.
|
|
|
|
glColor4f(1, 1, 1, 1);
|
|
|
|
|
|
|
|
// Render the front quad
|
2011-01-12 15:28:44 +01:00
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2f(0, t);
|
|
|
|
glNormal3f(0.0, 0.0, 1.0);
|
|
|
|
glVertex3f(-s, s, -s);
|
2011-01-10 23:14:56 +01:00
|
|
|
|
|
|
|
glTexCoord2f(0, 0);
|
|
|
|
glNormal3f(0.0, 0.0, 1.0);
|
2011-01-12 15:28:44 +01:00
|
|
|
glVertex3f(-s, -s, -s);
|
2011-01-10 23:14:56 +01:00
|
|
|
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, 0);
|
2011-01-10 23:14:56 +01:00
|
|
|
glNormal3f(0.0, 0.0, 1.0);
|
2011-01-12 15:28:44 +01:00
|
|
|
glVertex3f(s, -s, -s);
|
2011-01-10 23:14:56 +01:00
|
|
|
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glNormal3f(0.0, 0.0, 1.0);
|
2011-01-12 15:28:44 +01:00
|
|
|
glVertex3f(s, s, -s);
|
2011-01-10 23:14:56 +01:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// Render the left quad
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2f(0, 0);
|
|
|
|
glVertex3f(s, -s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
|
|
|
|
glTexCoord2f(0, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(s, s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
|
|
|
|
glTexCoord2f(t, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(s, s, -s);
|
2011-01-12 15:28:44 +01:00
|
|
|
|
|
|
|
glTexCoord2f(t, 0);
|
2011-01-10 23:14:56 +01:00
|
|
|
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);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(0, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(-s, s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(s, s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, 0);
|
2011-01-10 23:14:56 +01:00
|
|
|
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);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(0, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(-s, s, -s);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(-s, s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, 0);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(-s, -s, s);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// Render the top quad
|
|
|
|
//glBindTexture(GL_TEXTURE_2D, _skybox[4]);
|
|
|
|
glBegin(GL_QUADS);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(0, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(-s, s, -s);
|
|
|
|
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(s, s, -s);
|
|
|
|
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, 0);
|
2011-01-10 23:14:56 +01:00
|
|
|
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);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(0, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(-s, -s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, t);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(s, -s, s);
|
2011-01-12 15:28:44 +01:00
|
|
|
glTexCoord2f(t, 0);
|
2011-01-10 23:14:56 +01:00
|
|
|
glVertex3f(s, -s, -s);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
// Restore enable bits and matrix
|
|
|
|
glPopAttrib();
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
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);
|
2011-01-12 22:05:22 +01:00
|
|
|
void accelerate(double x, double y, double z);
|
|
|
|
void drop_bomb(double rx, double ry, double rz, double ttl);
|
2011-01-08 17:53:45 +01:00
|
|
|
void loadConsoleFont();
|
|
|
|
};
|
|
|
|
|
2011-01-12 22:05:22 +01:00
|
|
|
void Application::accelerate(double x, double y, double z) {
|
|
|
|
if (game.local_player == 0)
|
|
|
|
return;
|
|
|
|
player_accelerate_message_t msg;
|
|
|
|
msg.msg_id = MESSAGE_PLAYER_ACCELERATE;
|
|
|
|
msg.player_id = game.local_player->id;
|
|
|
|
msg.x = x;
|
|
|
|
msg.y = y;
|
|
|
|
msg.z = z;
|
|
|
|
send_message((uint8_t*) &msg, sizeof(msg), &game);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::drop_bomb(double rx, double ry, double rz, double ttl) {
|
|
|
|
bomb_drop_meesage_t msg;
|
|
|
|
msg.msg_id = MESSAGE_BOMB_DROP;
|
|
|
|
msg.x = game.local_player->x + rx * 20;
|
|
|
|
msg.y = game.local_player->y + ry * 20;
|
|
|
|
msg.z = game.local_player->z + rz * 20;
|
|
|
|
msg.vx = game.local_player->vx + rx * 100;
|
|
|
|
msg.vy = game.local_player->vy + ry * 100;
|
|
|
|
msg.vz = game.local_player->vz + rz * 100;
|
|
|
|
msg.ttl = ttl;
|
|
|
|
send_message((uint8_t*) &msg, sizeof(msg), &game);
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-01-09 21:56:48 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
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
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
2011-01-08 17:53:45 +01:00
|
|
|
glLoadIdentity();
|
|
|
|
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f, 10000.0f);
|
|
|
|
|
|
|
|
// Select and setup the modelview matrix
|
2011-01-12 15:28:44 +01:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2011-01-08 17:53:45 +01:00
|
|
|
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;
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-12 22:05:22 +01:00
|
|
|
std::string arg1;
|
2011-01-05 00:07:08 +01:00
|
|
|
if (argc > 1) {
|
2011-01-12 22:05:22 +01:00
|
|
|
arg1 = argv[1];
|
|
|
|
if (arg1 == "server") {
|
|
|
|
server = 1;
|
|
|
|
} else {
|
|
|
|
server = 0;
|
|
|
|
}
|
2011-01-05 00:07:08 +01:00
|
|
|
} else {
|
2011-01-12 22:05:22 +01:00
|
|
|
server = 0;
|
|
|
|
arg1 = "forge.camijo.de";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise GLFW
|
|
|
|
if (!glfwInit()) {
|
|
|
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2011-01-09 21:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
game_setup(&game, server, explosion_callback);
|
2011-01-12 22:05:22 +01:00
|
|
|
|
2011-01-09 21:56:48 +01:00
|
|
|
setup_opengl();
|
|
|
|
setup_explosion();
|
2011-01-12 22:05:22 +01:00
|
|
|
quadratic = gluNewQuadric();
|
|
|
|
gluQuadricNormals(quadratic, GLU_SMOOTH);
|
|
|
|
gluQuadricTexture(quadratic, GL_TRUE);
|
2011-01-09 21:56:48 +01:00
|
|
|
|
|
|
|
if (server) {
|
2011-01-05 00:07:08 +01:00
|
|
|
setup_network(NULL);
|
2011-01-05 15:00:09 +01:00
|
|
|
team_t *team = game_team(&game, 0);
|
2011-01-12 22:05:22 +01:00
|
|
|
game.local_player = game_spawn_player(&game, team);
|
2011-01-09 21:56:48 +01:00
|
|
|
} else {
|
2011-01-12 22:05:22 +01:00
|
|
|
setup_network(arg1.c_str());
|
2011-01-05 00:07:08 +01:00
|
|
|
}
|
2011-01-09 21:56:48 +01:00
|
|
|
|
2011-01-05 15:00:09 +01:00
|
|
|
size_t i;
|
2011-01-04 18:11:59 +01:00
|
|
|
running = GL_TRUE;
|
2011-01-12 22:05:22 +01:00
|
|
|
last_time = PerformanceTimer::get();
|
2011-01-05 15:00:09 +01:00
|
|
|
last_player_update = last_time;
|
2011-01-09 21:56:48 +01:00
|
|
|
last_bomb = last_time - 5.;
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-12 22:05:22 +01:00
|
|
|
glfwGetMousePos(&last_x, &last_y);
|
2011-01-08 17:53:45 +01:00
|
|
|
accelerate_schudule.setExact(true);
|
|
|
|
accelerate_schudule.setInterval(0.05);
|
|
|
|
|
|
|
|
player_update_schudule.setExact(true);
|
2011-01-12 15:28:44 +01:00
|
|
|
player_update_schudule.setInterval(0.05);
|
2011-01-08 17:53:45 +01:00
|
|
|
|
|
|
|
loadConsoleFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::update() {
|
|
|
|
|
|
|
|
// Get time and mouse position
|
2011-01-12 22:05:22 +01:00
|
|
|
double time = PerformanceTimer::get();
|
2011-01-08 17:53:45 +01:00
|
|
|
double dt = time - last_time;
|
2011-01-12 22:05:22 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
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;
|
2011-01-06 13:51:52 +01:00
|
|
|
|
2011-01-10 23:02:38 +01:00
|
|
|
service_network(&game);
|
2011-01-12 22:05:22 +01:00
|
|
|
game_update(&game, dt);
|
2011-01-10 23:02:38 +01:00
|
|
|
if (server && player_update_schudule.next(time)) {
|
|
|
|
send_player_updates(&game);
|
|
|
|
}
|
2011-01-08 17:53:45 +01:00
|
|
|
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;
|
2011-01-05 00:07:08 +01:00
|
|
|
if (glfwGetKey('W')) {
|
2011-01-12 22:05:22 +01:00
|
|
|
accelerate(rx * v, ry * v, rz * v);
|
2011-01-05 00:07:08 +01:00
|
|
|
} else if (glfwGetKey('S')) {
|
2011-01-12 22:05:22 +01:00
|
|
|
accelerate(rx * -v, ry * -v, rz * -v);
|
2011-01-05 23:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (glfwGetKey('A')) {
|
2011-01-12 22:05:22 +01:00
|
|
|
accelerate(rz * v, 0, -rx * v);
|
2011-01-05 00:07:08 +01:00
|
|
|
} else if (glfwGetKey('D')) {
|
2011-01-12 22:05:22 +01:00
|
|
|
accelerate(-rz * v, 0, rx * v);
|
2011-01-05 23:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (glfwGetKey(GLFW_KEY_SPACE)) {
|
2011-01-12 22:05:22 +01:00
|
|
|
accelerate(game.local_player->vx * -0.1, game.local_player->vy
|
|
|
|
* -0.1, game.local_player->vz * -0.1);
|
2011-01-05 00:07:08 +01:00
|
|
|
}
|
2011-01-08 17:53:45 +01:00
|
|
|
}
|
|
|
|
if (glfwGetKey(GLFW_KEY_LCTRL)) {
|
2011-01-09 21:56:48 +01:00
|
|
|
if (time - last_bomb > 1.0) {
|
2011-01-08 17:53:45 +01:00
|
|
|
last_bomb = time;
|
2011-01-12 22:05:22 +01:00
|
|
|
drop_bomb(rx, ry, rz, 5.0);
|
2011-01-05 15:00:09 +01:00
|
|
|
}
|
2011-01-08 17:53:45 +01:00
|
|
|
}
|
2011-01-12 15:28:44 +01:00
|
|
|
|
2011-01-12 22:05:22 +01:00
|
|
|
if (game.local_player) {
|
|
|
|
camX = game.local_player->x;
|
|
|
|
camY = game.local_player->y;
|
|
|
|
camZ = game.local_player->z;
|
2011-01-12 15:28:44 +01:00
|
|
|
} else {
|
|
|
|
camX = 1000.0;
|
|
|
|
camY = 1000.0;
|
|
|
|
camZ = 1000.0;
|
|
|
|
}
|
2011-01-05 15:00:09 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
prepareFrame(rx, ry, rz);
|
2011-01-10 21:44:38 +01:00
|
|
|
setup_light();
|
2011-01-12 15:28:44 +01:00
|
|
|
glEnable(GL_LIGHT0);
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
2011-01-08 17:53:45 +01:00
|
|
|
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]);
|
|
|
|
|
2011-01-09 21:56:48 +01:00
|
|
|
for (size_t i = 0; i < GAME_POINT_COUNT; i++)
|
|
|
|
draw_point(&game.point[i]);
|
|
|
|
|
2011-01-10 23:14:56 +01:00
|
|
|
draw_box();
|
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
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);
|
2011-01-12 22:05:22 +01:00
|
|
|
if (game.local_player) {
|
2011-01-09 21:56:48 +01:00
|
|
|
std::stringstream sstr;
|
2011-01-12 22:05:22 +01:00
|
|
|
sstr << "Points: " << game.local_player->points;
|
2011-01-09 21:56:48 +01:00
|
|
|
oglf_print(&font, 10, 10, sstr.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) {
|
|
|
|
std::stringstream sstr;
|
2011-01-12 22:05:22 +01:00
|
|
|
if (game.local_player && &game.team[i] == game.local_player->team) {
|
2011-01-09 21:56:48 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
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);
|
2011-01-05 23:02:10 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
last_time = time;
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
}
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
void Application::shutdown() {
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-04 22:24:18 +01:00
|
|
|
gluDeleteQuadric(quadratic);
|
2011-01-08 17:53:45 +01:00
|
|
|
oglf_destroy(&font);
|
2011-01-12 22:05:22 +01:00
|
|
|
|
|
|
|
shutdown_network();
|
2011-01-04 18:11:59 +01:00
|
|
|
// Close OpenGL window and terminate GLFW
|
|
|
|
glfwTerminate();
|
2011-01-08 17:53:45 +01:00
|
|
|
}
|
2011-01-04 18:11:59 +01:00
|
|
|
|
2011-01-08 17:53:45 +01:00
|
|
|
bool Application::isRunning() {
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
Application app;
|
|
|
|
app.initialize(argc, argv);
|
|
|
|
while (app.isRunning())
|
|
|
|
app.update();
|
|
|
|
app.shutdown();
|
2011-01-05 23:02:10 +01:00
|
|
|
return 0;
|
2011-01-04 18:11:59 +01:00
|
|
|
}
|
|
|
|
|