structure changes
This commit is contained in:
19
src/client/CMakeLists.txt
Normal file
19
src/client/CMakeLists.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
include_directories (${GREMLIN_SOURCE_DIR}/src/common)
|
||||
|
||||
# define executable
|
||||
add_executable( gremlin
|
||||
Client
|
||||
Explosion
|
||||
main
|
||||
oglfont
|
||||
)
|
||||
|
||||
add_dependencies( gremlin
|
||||
common
|
||||
glfw
|
||||
spark
|
||||
)
|
||||
|
||||
target_link_libraries(gremlin
|
||||
common enet glfw spark ${PLATFORM_LIBRARIES} ${OPENGL_LIBRARIES}
|
||||
)
|
516
src/client/Client.cpp
Normal file
516
src/client/Client.cpp
Normal file
@@ -0,0 +1,516 @@
|
||||
/*
|
||||
* Client.cpp
|
||||
*
|
||||
* Created on: 14.01.2011
|
||||
* Author: gmueller
|
||||
*/
|
||||
|
||||
#include "Client.h"
|
||||
#include "Network.h"
|
||||
#include "Explosion.h"
|
||||
#include "Time.h"
|
||||
|
||||
#include <GL/glfw.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <sstream>
|
||||
|
||||
GLUquadricObj *quadratic;
|
||||
Explosion explosion;
|
||||
|
||||
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() {
|
||||
|
||||
// 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");
|
||||
// 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(0);
|
||||
}
|
||||
|
||||
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_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, void *data) {
|
||||
explosion.add(x, y, z);
|
||||
}
|
||||
|
||||
GLuint wallTex = 0;
|
||||
|
||||
void draw_box() {
|
||||
|
||||
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_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_LINEAR);
|
||||
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 };
|
||||
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 = 3000.0f, t = 10.0f;
|
||||
// Just in case we set all vertices to white.
|
||||
glColor4f(1, 1, 1, 1);
|
||||
|
||||
// Render the front quad
|
||||
glBegin( GL_QUADS);
|
||||
glTexCoord2f(0, t);
|
||||
glNormal3f(0.0, 0.0, 1.0);
|
||||
glVertex3f(-s, s, -s);
|
||||
|
||||
glTexCoord2f(0, 0);
|
||||
glNormal3f(0.0, 0.0, 1.0);
|
||||
glVertex3f(-s, -s, -s);
|
||||
|
||||
glTexCoord2f(t, 0);
|
||||
glNormal3f(0.0, 0.0, 1.0);
|
||||
glVertex3f(s, -s, -s);
|
||||
|
||||
glTexCoord2f(t, t);
|
||||
glNormal3f(0.0, 0.0, 1.0);
|
||||
glVertex3f(s, s, -s);
|
||||
glEnd();
|
||||
|
||||
// Render the left quad
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex3f(s, -s, s);
|
||||
|
||||
glTexCoord2f(0, t);
|
||||
glVertex3f(s, s, s);
|
||||
|
||||
glTexCoord2f(t, t);
|
||||
glVertex3f(s, s, -s);
|
||||
|
||||
glTexCoord2f(t, 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, t);
|
||||
glVertex3f(-s, s, s);
|
||||
glTexCoord2f(t, t);
|
||||
glVertex3f(s, s, s);
|
||||
glTexCoord2f(t, 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, t);
|
||||
glVertex3f(-s, s, -s);
|
||||
glTexCoord2f(t, t);
|
||||
glVertex3f(-s, s, s);
|
||||
glTexCoord2f(t, 0);
|
||||
glVertex3f(-s, -s, s);
|
||||
glEnd();
|
||||
|
||||
// Render the top quad
|
||||
//glBindTexture(GL_TEXTURE_2D, _skybox[4]);
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0, t);
|
||||
glVertex3f(-s, s, -s);
|
||||
|
||||
glTexCoord2f(t, t);
|
||||
glVertex3f(s, s, -s);
|
||||
|
||||
glTexCoord2f(t, 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, t);
|
||||
glVertex3f(-s, -s, s);
|
||||
glTexCoord2f(t, t);
|
||||
glVertex3f(s, -s, s);
|
||||
glTexCoord2f(t, 0);
|
||||
glVertex3f(s, -s, -s);
|
||||
glEnd();
|
||||
|
||||
// Restore enable bits and matrix
|
||||
glPopAttrib();
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void Client::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;
|
||||
network.sendMessage((uint8_t*) &msg, sizeof(msg));
|
||||
}
|
||||
|
||||
void Client::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;
|
||||
network.sendMessage((uint8_t*) &msg, sizeof(msg));
|
||||
}
|
||||
|
||||
void Client::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 Client::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 Client::initialize(Arguments &arg) {
|
||||
Application::initialize(arg);
|
||||
|
||||
// Initialise GLFW
|
||||
if (!glfwInit()) {
|
||||
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
game_setup_explosion_callback(&game, explosion_callback, 0);
|
||||
setup_opengl();
|
||||
setup_explosion();
|
||||
quadratic = gluNewQuadric();
|
||||
gluQuadricNormals(quadratic, GLU_SMOOTH);
|
||||
gluQuadricTexture(quadratic, GL_TRUE);
|
||||
|
||||
if (isMaster()) {
|
||||
team_t *team = game_team(&game, 0);
|
||||
game.local_player = game_spawn_player(&game, team);
|
||||
}
|
||||
|
||||
size_t i;
|
||||
last_bomb = time - 5.;
|
||||
|
||||
glfwGetMousePos(&last_x, &last_y);
|
||||
accelerate_schudule.setExact(true);
|
||||
accelerate_schudule.setInterval(0.05);
|
||||
|
||||
loadConsoleFont();
|
||||
}
|
||||
|
||||
void Client::update() {
|
||||
|
||||
Application::update();
|
||||
|
||||
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;
|
||||
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(rx * v, ry * v, rz * v);
|
||||
} else if (glfwGetKey('S')) {
|
||||
accelerate(rx * -v, ry * -v, rz * -v);
|
||||
}
|
||||
|
||||
if (glfwGetKey('A')) {
|
||||
accelerate(rz * v, 0, -rx * v);
|
||||
} else if (glfwGetKey('D')) {
|
||||
accelerate(-rz * v, 0, rx * v);
|
||||
}
|
||||
|
||||
if (glfwGetKey(GLFW_KEY_SPACE)) {
|
||||
accelerate(game.local_player->vx * -0.1, game.local_player->vy
|
||||
* -0.1, game.local_player->vz * -0.1);
|
||||
}
|
||||
}
|
||||
if (glfwGetKey(GLFW_KEY_LCTRL)) {
|
||||
if (time - last_bomb > 1.0) {
|
||||
last_bomb = time;
|
||||
drop_bomb(rx, ry, rz, 5.0);
|
||||
}
|
||||
}
|
||||
|
||||
if (game.local_player) {
|
||||
camX = game.local_player->x;
|
||||
camY = game.local_player->y;
|
||||
camZ = game.local_player->z;
|
||||
} else {
|
||||
camX = 1000.0;
|
||||
camY = 1000.0;
|
||||
camZ = 1000.0;
|
||||
}
|
||||
prepareFrame(rx, ry, rz);
|
||||
setup_light();
|
||||
glEnable( GL_LIGHT0);
|
||||
glEnable( GL_LIGHTING);
|
||||
glEnable( GL_CULL_FACE);
|
||||
glDisable( GL_TEXTURE_2D);
|
||||
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);
|
||||
size_t fy = 1;
|
||||
if (game.local_player) {
|
||||
std::stringstream sstr;
|
||||
sstr << "Points: " << game.local_player->points;
|
||||
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
|
||||
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < GAME_TEAM_COUNT; i++) {
|
||||
std::stringstream sstr;
|
||||
if (game.local_player && &game.team[i] == game.local_player->team) {
|
||||
sstr << "Team " << i << " (yours) : " << game.team[i].points;
|
||||
} else {
|
||||
sstr << "Team " << i << " (other) : " << game.team[i].points;
|
||||
}
|
||||
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
|
||||
}
|
||||
|
||||
{
|
||||
std::stringstream sstr;
|
||||
sstr << "FPS: " << (int) (1 / dt) << " Time: " << floor(dt * 10000.0
|
||||
+ 0.5) / 10.0;
|
||||
oglf_print(&font, 10, 25 * fy++, sstr.str().c_str());
|
||||
}
|
||||
|
||||
oglf_end();
|
||||
|
||||
// Swap buffers
|
||||
glfwSwapBuffers();
|
||||
|
||||
// Check if the ESC key was pressed or the window was closed
|
||||
if (glfwGetKey(GLFW_KEY_ESC))
|
||||
stop();
|
||||
|
||||
if (glfwGetWindowParam(GLFW_OPENED) != GL_TRUE)
|
||||
stop();
|
||||
}
|
||||
|
||||
void Client::shutdown() {
|
||||
|
||||
Application::shutdown();
|
||||
|
||||
gluDeleteQuadric(quadratic);
|
||||
oglf_destroy(&font);
|
||||
glfwTerminate();
|
||||
}
|
37
src/client/Client.h
Normal file
37
src/client/Client.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Client.h
|
||||
*
|
||||
* Created on: 14.01.2011
|
||||
* Author: gmueller
|
||||
*/
|
||||
|
||||
#ifndef CLIENT_H_
|
||||
#define CLIENT_H_
|
||||
|
||||
#include "Application.h"
|
||||
#include "Schedule.h"
|
||||
#include "oglfont.h"
|
||||
#include "game.h"
|
||||
|
||||
class Client: public Application {
|
||||
public:
|
||||
void initialize(Arguments &arg);
|
||||
void update();
|
||||
void shutdown();
|
||||
private:
|
||||
double last_bomb; //TODO: use bomb schedule;
|
||||
int last_x, last_y;
|
||||
double phi, theta;
|
||||
double camX, camY, camZ;
|
||||
Schedule accelerate_schudule;
|
||||
int width, height;
|
||||
//TrueTyeFont font;
|
||||
oglf_font_t font;
|
||||
|
||||
void prepareFrame(double rx, double ry, double rz);
|
||||
void accelerate(double x, double y, double z);
|
||||
void drop_bomb(double rx, double ry, double rz, double ttl);
|
||||
void loadConsoleFont();
|
||||
};
|
||||
|
||||
#endif /* CLIENT_H_ */
|
351
src/client/Explosion.cpp
Normal file
351
src/client/Explosion.cpp
Normal file
@@ -0,0 +1,351 @@
|
||||
/*
|
||||
* Explosion.cpp
|
||||
*
|
||||
* Created on: 05.01.2011
|
||||
* Author: gmueller
|
||||
*/
|
||||
|
||||
#include "Explosion.h"
|
||||
|
||||
using namespace SPK;
|
||||
using namespace SPK::GL;
|
||||
|
||||
Explosion::~Explosion() {
|
||||
SPKFactory::getInstance().destroyAll();
|
||||
}
|
||||
|
||||
void Explosion::initialize(GLuint textureExplosion, GLuint textureFlash,
|
||||
GLuint textureSpark, GLuint texturePoint, GLuint textureWave) {
|
||||
///////////////
|
||||
// Renderers //
|
||||
///////////////
|
||||
double scale = 50.0;
|
||||
|
||||
// smoke renderer
|
||||
GLQuadRenderer* smokeRenderer = GLQuadRenderer::create();
|
||||
smokeRenderer->setTexturingMode(TEXTURE_2D);
|
||||
smokeRenderer->setTexture(textureExplosion);
|
||||
smokeRenderer->setTextureBlending(GL_MODULATE);
|
||||
smokeRenderer->setAtlasDimensions(2, 2); // uses 4 different patterns in the texture
|
||||
smokeRenderer->setBlending(BLENDING_ALPHA);
|
||||
smokeRenderer->enableRenderingHint(DEPTH_WRITE, false);
|
||||
smokeRenderer->setShared(true);
|
||||
smokeRenderer->setScale(scale, scale);
|
||||
|
||||
// flame renderer
|
||||
GLQuadRenderer* flameRenderer = GLQuadRenderer::create();
|
||||
flameRenderer->setTexturingMode(TEXTURE_2D);
|
||||
flameRenderer->setTexture(textureExplosion);
|
||||
flameRenderer->setTextureBlending(GL_MODULATE);
|
||||
flameRenderer->setAtlasDimensions(2, 2);
|
||||
flameRenderer->setBlending(BLENDING_ADD);
|
||||
flameRenderer->enableRenderingHint(DEPTH_WRITE, false);
|
||||
flameRenderer->setShared(true);
|
||||
flameRenderer->setScale(scale, scale);
|
||||
|
||||
// flash renderer
|
||||
GLQuadRenderer* flashRenderer = GLQuadRenderer::create();
|
||||
flashRenderer->setTexturingMode(TEXTURE_2D);
|
||||
flashRenderer->setTexture(textureFlash);
|
||||
flashRenderer->setTextureBlending(GL_REPLACE);
|
||||
flashRenderer->setBlending(BLENDING_ADD);
|
||||
flashRenderer->enableRenderingHint(DEPTH_WRITE, false);
|
||||
flashRenderer->setShared(true);
|
||||
flashRenderer->setScale(scale,scale);
|
||||
|
||||
// spark 1 renderer
|
||||
GLQuadRenderer* spark1Renderer = GLQuadRenderer::create();
|
||||
spark1Renderer->setTexturingMode(TEXTURE_2D);
|
||||
spark1Renderer->setTexture(textureSpark);
|
||||
spark1Renderer->setTextureBlending(GL_REPLACE);
|
||||
spark1Renderer->setBlending(BLENDING_ADD);
|
||||
spark1Renderer->enableRenderingHint(DEPTH_WRITE, false);
|
||||
spark1Renderer->setOrientation(DIRECTION_ALIGNED); // sparks are oriented function o their velocity
|
||||
spark1Renderer->setScale(0.05f * scale, 1.0f * scale); // thin rectangles
|
||||
spark1Renderer->setShared(true);
|
||||
|
||||
// spark 2 renderer
|
||||
GLRenderer* spark2Renderer = NULL;
|
||||
if (GLPointRenderer::loadGLExtPointSprite()
|
||||
&& GLPointRenderer::loadGLExtPointParameter()) // uses point sprite if possible
|
||||
{
|
||||
//GLPointRenderer::setPixelPerUnit(45.0f * PI / 180.f, scale * 1024);
|
||||
GLPointRenderer* pointRenderer = GLPointRenderer::create();
|
||||
pointRenderer->setType(POINT_SPRITE);
|
||||
pointRenderer->setTexture(texturePoint);
|
||||
pointRenderer->setTextureBlending(GL_MODULATE);
|
||||
pointRenderer->enableWorldSize(true);
|
||||
pointRenderer->setSize(0.02f * scale);
|
||||
spark2Renderer = pointRenderer;
|
||||
} else {
|
||||
GLQuadRenderer* quadRenderer = GLQuadRenderer::create();
|
||||
quadRenderer->setTexturingMode(TEXTURE_2D);
|
||||
quadRenderer->setTexture(texturePoint);
|
||||
quadRenderer->setTextureBlending(GL_MODULATE);
|
||||
quadRenderer->setScale(0.02f * scale, 0.02f * scale);
|
||||
spark2Renderer = quadRenderer;
|
||||
}
|
||||
spark2Renderer->setBlending(BLENDING_ADD);
|
||||
spark2Renderer->enableRenderingHint(DEPTH_WRITE, false);
|
||||
spark2Renderer->setShared(true);
|
||||
|
||||
// wave renderer
|
||||
GLQuadRenderer* waveRenderer = GLQuadRenderer::create();
|
||||
waveRenderer->setTexturingMode(TEXTURE_2D);
|
||||
waveRenderer->setTexture(textureWave);
|
||||
waveRenderer->setTextureBlending(GL_MODULATE);
|
||||
waveRenderer->setBlending(BLENDING_ALPHA);
|
||||
waveRenderer->enableRenderingHint(DEPTH_WRITE, false);
|
||||
waveRenderer->enableRenderingHint(ALPHA_TEST, true); // uses the alpha test
|
||||
waveRenderer->setAlphaTestThreshold(0.0f);
|
||||
waveRenderer->setOrientation(FIXED_ORIENTATION); // the orientatin is fixed
|
||||
waveRenderer->lookVector.set(0.0f, 1.0f, 0.0f);
|
||||
waveRenderer->upVector.set(1.0f, 0.0f, 0.0f); // we dont really care about the up axis
|
||||
waveRenderer->setShared(true);
|
||||
waveRenderer->setScale(scale, scale);
|
||||
|
||||
////////////
|
||||
// Models //
|
||||
////////////
|
||||
|
||||
Interpolator* interpolator = NULL; // pointer to an interpolator that is used to retrieve interpolators
|
||||
|
||||
// smoke model
|
||||
Model* smokeModel = Model::create(FLAG_RED | FLAG_GREEN | FLAG_BLUE
|
||||
| FLAG_ALPHA | FLAG_SIZE | FLAG_ANGLE | FLAG_TEXTURE_INDEX,
|
||||
FLAG_SIZE | FLAG_ANGLE,
|
||||
FLAG_SIZE | FLAG_ANGLE | FLAG_TEXTURE_INDEX, FLAG_ALPHA);
|
||||
smokeModel->setParam(PARAM_RED, 0.2f);
|
||||
smokeModel->setParam(PARAM_GREEN, 0.2f);
|
||||
smokeModel->setParam(PARAM_BLUE, 0.2f);
|
||||
smokeModel->setParam(PARAM_SIZE, 0.6f, 0.8f, 1.0f, 1.4f);
|
||||
smokeModel->setParam(PARAM_TEXTURE_INDEX, 0.0f, 4.0f);
|
||||
smokeModel->setParam(PARAM_ANGLE, 0.0f, M_PI * 0.5f, 0.0f, M_PI * 0.5f);
|
||||
smokeModel->setLifeTime(2.5f, 3.0f);
|
||||
smokeModel->setShared(true);
|
||||
|
||||
interpolator = smokeModel->getInterpolator(PARAM_ALPHA);
|
||||
interpolator->addEntry(0.0f, 0.0f);
|
||||
interpolator->addEntry(0.4f, 0.4f, 0.6f);
|
||||
interpolator->addEntry(0.6f, 0.4f, 0.6f);
|
||||
interpolator->addEntry(1.0f, 0.0f);
|
||||
|
||||
// flame model
|
||||
Model* flameModel = Model::create(FLAG_RED | FLAG_GREEN | FLAG_BLUE
|
||||
| FLAG_ALPHA | FLAG_SIZE | FLAG_ANGLE | FLAG_TEXTURE_INDEX,
|
||||
FLAG_ANGLE | FLAG_RED | FLAG_GREEN | FLAG_BLUE, FLAG_ANGLE
|
||||
| FLAG_TEXTURE_INDEX, FLAG_SIZE | FLAG_ALPHA);
|
||||
flameModel->setParam(PARAM_RED, 1.0f, 0.2f);
|
||||
flameModel->setParam(PARAM_GREEN, 0.5f, 0.2f);
|
||||
flameModel->setParam(PARAM_BLUE, 0.2f, 0.2f);
|
||||
flameModel->setParam(PARAM_TEXTURE_INDEX, 0.0f, 4.0f);
|
||||
flameModel->setParam(PARAM_ANGLE, 0.0f, M_PI * 0.5f, 0.0f, M_PI * 0.5f);
|
||||
flameModel->setLifeTime(1.5f, 2.0f);
|
||||
flameModel->setShared(true);
|
||||
|
||||
interpolator = flameModel->getInterpolator(PARAM_SIZE);
|
||||
interpolator->addEntry(0.0f, 0.25f);
|
||||
interpolator->addEntry(0.02f, 0.6f, 0.8f);
|
||||
interpolator->addEntry(1.0f, 1.0f, 1.4f);
|
||||
|
||||
interpolator = flameModel->getInterpolator(PARAM_ALPHA);
|
||||
interpolator->addEntry(0.5f, 1.0f);
|
||||
interpolator->addEntry(1.0f, 0.0f);
|
||||
|
||||
// flash model
|
||||
Model* flashModel = Model::create(FLAG_ALPHA | FLAG_SIZE | FLAG_ANGLE,
|
||||
FLAG_NONE, FLAG_ANGLE, FLAG_ALPHA | FLAG_SIZE);
|
||||
flashModel->setParam(PARAM_ANGLE, 0.0f, 2.0f * M_PI);
|
||||
flashModel->setLifeTime(0.5f, 0.5f);
|
||||
flashModel->setShared(true);
|
||||
|
||||
interpolator = flashModel->getInterpolator(PARAM_SIZE);
|
||||
interpolator->addEntry(0.0f, 0.25f);
|
||||
interpolator->addEntry(0.1f, 1.0f, 2.0f);
|
||||
|
||||
interpolator = flashModel->getInterpolator(PARAM_ALPHA);
|
||||
interpolator->addEntry(0.0f, 1.0f);
|
||||
interpolator->addEntry(0.4f, 0.0f);
|
||||
|
||||
// spark 1 model
|
||||
Model* spark1Model = Model::create(FLAG_SIZE | FLAG_ALPHA, FLAG_ALPHA,
|
||||
FLAG_SIZE);
|
||||
spark1Model->setParam(PARAM_ALPHA, 1.0f, 0.0f);
|
||||
spark1Model->setParam(PARAM_SIZE, 0.2f, 0.4f);
|
||||
spark1Model->setLifeTime(0.2f, 1.0f);
|
||||
spark1Model->setShared(true);
|
||||
|
||||
// spark 2 model
|
||||
Model* spark2Model = Model::create(FLAG_RED | FLAG_GREEN | FLAG_BLUE
|
||||
| FLAG_ALPHA, FLAG_GREEN | FLAG_BLUE | FLAG_ALPHA, FLAG_GREEN);
|
||||
spark2Model->setParam(PARAM_ALPHA, 1.0f, 0.0f);
|
||||
spark2Model->setParam(PARAM_RED, 1.0f);
|
||||
spark2Model->setParam(PARAM_GREEN, 1.0f, 1.0f, 0.3f, 1.0f);
|
||||
spark2Model->setParam(PARAM_BLUE, 0.7f, 0.3f);
|
||||
spark2Model->setLifeTime(1.0f, 3.0f);
|
||||
spark2Model->setShared(true);
|
||||
|
||||
// wave model
|
||||
Model* waveModel = Model::create(FLAG_ALPHA | FLAG_SIZE, FLAG_SIZE
|
||||
| FLAG_ALPHA);
|
||||
waveModel->setParam(PARAM_SIZE, 0.0f, 4.0f);
|
||||
waveModel->setParam(PARAM_ALPHA, 0.2f, 0.0f);
|
||||
waveModel->setLifeTime(0.8f, 0.8f);
|
||||
waveModel->setShared(true);
|
||||
|
||||
//////////////
|
||||
// Emitters //
|
||||
//////////////
|
||||
|
||||
// This zone will be used by several emitters
|
||||
Sphere* explosionSphere = Sphere::create(Vector3D(0.0f, 0.0f, 0.0f), 0.4f * scale);
|
||||
Sphere* explosionSphere2 = Sphere::create(Vector3D(0.0f, 0.0f, 0.0f), 0.4f * 2.5* scale);
|
||||
|
||||
// smoke emitter
|
||||
RandomEmitter* smokeEmitter = RandomEmitter::create();
|
||||
smokeEmitter->setZone(Sphere::create(Vector3D(0.0f, 0.0f, 0.0f), 0.6f),
|
||||
false);
|
||||
smokeEmitter->setFlow(-1);
|
||||
smokeEmitter->setTank(1);
|
||||
smokeEmitter->setForce(0.02f, 10.04f);
|
||||
|
||||
// flame emitter
|
||||
NormalEmitter* flameEmitter = NormalEmitter::create();
|
||||
flameEmitter->setZone(explosionSphere);
|
||||
flameEmitter->setFlow(-1);
|
||||
flameEmitter->setTank(1);
|
||||
flameEmitter->setForce(0.06f, 10.1f);
|
||||
|
||||
// flash emitter
|
||||
StaticEmitter* flashEmitter = StaticEmitter::create();
|
||||
flashEmitter->setZone(Sphere::create(Vector3D(0.0f, 0.0f, 0.0f), 0.1f));
|
||||
flashEmitter->setFlow(-1);
|
||||
flashEmitter->setTank(2);
|
||||
|
||||
// spark 1 emitter
|
||||
NormalEmitter* spark1Emitter = NormalEmitter::create();
|
||||
spark1Emitter->setZone(explosionSphere2);
|
||||
spark1Emitter->setFlow(-1);
|
||||
spark1Emitter->setTank(5);
|
||||
spark1Emitter->setForce(2.0f, 3.0f);
|
||||
|
||||
// spark 2 emitter
|
||||
NormalEmitter* spark2Emitter = NormalEmitter::create();
|
||||
spark2Emitter->setZone(explosionSphere);
|
||||
spark2Emitter->setFlow(-1);
|
||||
spark2Emitter->setTank(2);
|
||||
spark2Emitter->setForce(0.4f, 0.8f);
|
||||
|
||||
// wave emitter
|
||||
StaticEmitter* waveEmitter = StaticEmitter::create();
|
||||
waveEmitter->setZone(Point::create());
|
||||
waveEmitter->setFlow(-1);
|
||||
waveEmitter->setTank(1);
|
||||
|
||||
////////////
|
||||
// Groups //
|
||||
////////////
|
||||
|
||||
// smoke group
|
||||
Group* smokeGroup = Group::create(smokeModel, 15);
|
||||
smokeGroup->addEmitter(smokeEmitter);
|
||||
smokeGroup->setRenderer(smokeRenderer);
|
||||
|
||||
// flame group
|
||||
Group* flameGroup = Group::create(flameModel, 15);
|
||||
flameGroup->addEmitter(flameEmitter);
|
||||
flameGroup->setRenderer(flameRenderer);
|
||||
|
||||
// flash group
|
||||
Group* flashGroup = Group::create(flashModel, 3);
|
||||
flashGroup->addEmitter(flashEmitter);
|
||||
flashGroup->setRenderer(flashRenderer);
|
||||
|
||||
// spark 1 group
|
||||
Group* spark1Group = Group::create(spark1Model, 20);
|
||||
spark1Group->addEmitter(spark1Emitter);
|
||||
spark1Group->setRenderer(spark1Renderer);
|
||||
|
||||
// spark 2 group
|
||||
Group* spark2Group = Group::create(spark2Model, 400);
|
||||
spark2Group->addEmitter(spark2Emitter);
|
||||
spark2Group->setRenderer(spark2Renderer);
|
||||
|
||||
// wave group
|
||||
Group* waveGroup = Group::create(waveModel, 1);
|
||||
waveGroup->addEmitter(waveEmitter);
|
||||
waveGroup->setRenderer(waveRenderer);
|
||||
|
||||
////////////
|
||||
// System //
|
||||
////////////
|
||||
|
||||
System* system = System::create();
|
||||
system->addGroup(waveGroup);
|
||||
system->addGroup(smokeGroup);
|
||||
system->addGroup(flameGroup);
|
||||
system->addGroup(flashGroup);
|
||||
system->addGroup(spark1Group);
|
||||
system->addGroup(spark2Group);
|
||||
|
||||
// Gets a pointer to the base
|
||||
system_id = system->getSPKID();
|
||||
|
||||
// Sets the update step
|
||||
// clamp the step to 100 ms
|
||||
System::setClampStep(true, 0.1f);
|
||||
// use an adaptive step from 1ms to 10ms (1000fps to 100fps)
|
||||
System::useAdaptiveStep(0.001f, 0.01f);
|
||||
}
|
||||
|
||||
// Renders the scene
|
||||
void Explosion::render() {
|
||||
GLRenderer::saveGLStates();
|
||||
// Renders all the particle systems
|
||||
for (std::list<System*>::const_iterator it = particleSystems.begin(); it
|
||||
!= particleSystems.end(); ++it)
|
||||
(*it)->render();
|
||||
GLRenderer::restoreGLStates();
|
||||
}
|
||||
|
||||
void Explosion::add(double x, double y, double z) {
|
||||
Vector3D pos(x, y, z);
|
||||
// Creates a copy of the base system
|
||||
System* system = SPK_Copy(System,system_id);
|
||||
|
||||
// Locates the system at the given position
|
||||
system->setTransformPosition(pos);
|
||||
|
||||
// updates the world transform of system and its children
|
||||
system->updateTransform();
|
||||
particleSystems.push_back(system);
|
||||
}
|
||||
|
||||
// This function is used to sort the systems from front to back
|
||||
struct cmpDistToCamera {
|
||||
Vector3D camera;
|
||||
bool operator()(System* &system0, System* &system1) {
|
||||
return getSqrDist(system0->getWorldTransformPos(), camera)
|
||||
> getSqrDist(system1->getWorldTransformPos(), camera);
|
||||
}
|
||||
};
|
||||
|
||||
void Explosion::update(double dt, double cx, double cy, double cz) {
|
||||
cmpDistToCamera cmp;
|
||||
cmp.camera.x = cx;
|
||||
cmp.camera.y = cy;
|
||||
cmp.camera.z = cz;
|
||||
particleSystems.sort(cmp);
|
||||
|
||||
std::list<System*>::iterator it = particleSystems.begin();
|
||||
while (it != particleSystems.end()) {
|
||||
// Updates the particle systems
|
||||
if (!(*it)->update(dt * 0.001f)) {
|
||||
// If a system is sleeping, destroys it
|
||||
SPK_Destroy(*it);
|
||||
// And erases its entry in the container
|
||||
it = particleSystems.erase(it);
|
||||
} else
|
||||
++it;
|
||||
}
|
||||
}
|
27
src/client/Explosion.h
Normal file
27
src/client/Explosion.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Explosion.h
|
||||
*
|
||||
* Created on: 05.01.2011
|
||||
* Author: gmueller
|
||||
*/
|
||||
|
||||
#ifndef EXPLOSION_H_
|
||||
#define EXPLOSION_H_
|
||||
|
||||
#include "SPK.h"
|
||||
#include "SPK_GL.h"
|
||||
|
||||
class Explosion {
|
||||
public:
|
||||
~Explosion();
|
||||
void initialize(GLuint textureExplosion, GLuint textureFlash,
|
||||
GLuint textureSpark, GLuint texturePoint, GLuint textureWave);
|
||||
void add(double x, double y, double z);
|
||||
void update(double dt, double cx, double cy, double cz);
|
||||
void render();
|
||||
private:
|
||||
SPK::SPK_ID system_id;
|
||||
std::list<SPK::System*> particleSystems;
|
||||
};
|
||||
|
||||
#endif /* EXPLOSION_H_ */
|
22
src/client/main.cpp
Normal file
22
src/client/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "Client.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
try {
|
||||
Client app;
|
||||
Arguments args(argc, argv);
|
||||
app.initialize(args);
|
||||
while (app.isRunning())
|
||||
app.update();
|
||||
app.shutdown();
|
||||
return 0;
|
||||
} catch (const char *str) {
|
||||
std::cerr << "Exception: " << str << std::endl;
|
||||
return 1;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
275
src/client/oglfont.cpp
Normal file
275
src/client/oglfont.cpp
Normal file
@@ -0,0 +1,275 @@
|
||||
#include "oglfont.h"
|
||||
#ifdef _MSC_VER
|
||||
#define LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define GL_Y_FIX(f) (1 - (f))
|
||||
static void _compile(oglf_font_t *font) {
|
||||
int chr;
|
||||
|
||||
font->base_list = glGenLists(OGLF_MAX_ASCII);
|
||||
glBindTexture(GL_TEXTURE_2D, font->fontTex);
|
||||
|
||||
for (chr = 0; chr < OGLF_MAX_ASCII; chr++) {
|
||||
glNewList(font->base_list + chr - 1, GL_COMPILE);
|
||||
|
||||
if (font->chars[chr].w) {
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(
|
||||
(float) font->chars[chr].x / (float) font->w,
|
||||
(float) GL_Y_FIX((font->chars[chr].y+font->chars[chr].h) / (float) font->h));
|
||||
glVertex2d((float) font->chars[chr].x_ofs,
|
||||
(float) font->chars[chr].h + font->chars[chr].y_ofs);
|
||||
|
||||
glTexCoord2f(
|
||||
(float) (font->chars[chr].x + font->chars[chr].w)
|
||||
/ (float) font->w,
|
||||
(float) GL_Y_FIX((font->chars[chr].y + font->chars[chr].h) / (float) font->h));
|
||||
glVertex2d((float) font->chars[chr].w + font->chars[chr].x_ofs,
|
||||
(float) font->chars[chr].h + font->chars[chr].y_ofs);
|
||||
|
||||
glTexCoord2f((float) (font->chars[chr].x + font->chars[chr].w)
|
||||
/ (float) font->w,
|
||||
(float) GL_Y_FIX( font->chars[chr].y / (float) font->h));
|
||||
glVertex2d((float) font->chars[chr].w + font->chars[chr].x_ofs,
|
||||
(float) font->chars[chr].y_ofs);
|
||||
|
||||
glTexCoord2f((float) font->chars[chr].x / (float) font->w,
|
||||
(float) GL_Y_FIX( font->chars[chr].y / (float) font->h));
|
||||
glVertex2d((float) font->chars[chr].x_ofs,
|
||||
(float) font->chars[chr].y_ofs);
|
||||
glEnd();
|
||||
glTranslatef((float) (font->chars[chr].x_advance
|
||||
), 0, 0);
|
||||
} else {
|
||||
// if char has no width, treat it like a space
|
||||
glTranslatef((float) font->base, 0, 0);
|
||||
}
|
||||
|
||||
glEndList();
|
||||
}
|
||||
}
|
||||
|
||||
static int _starts_with(const char *buffer, const char *text) {
|
||||
const char *text_ptr = text;
|
||||
const char *buffer_ptr = buffer;
|
||||
while (*text_ptr != 0) {
|
||||
|
||||
if (*text_ptr != *buffer_ptr)
|
||||
return 0;
|
||||
|
||||
text_ptr++;
|
||||
buffer_ptr++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _parse_line(oglf_font_t *font, char *buffer) {
|
||||
|
||||
if (_starts_with(buffer, "common")) {
|
||||
int count = sscanf(buffer,
|
||||
"common lineHeight=%d base=%d scaleW=%d scaleH=%d pages=%d\n",
|
||||
&font->line_h, &font->base, &font->w, &font->h, &font->pages);
|
||||
|
||||
if (count != 5)
|
||||
return 0;
|
||||
} else if (_starts_with(buffer, "char ")) {
|
||||
oglf_char_t temp_char;
|
||||
int id;
|
||||
int
|
||||
count =
|
||||
sscanf(
|
||||
buffer,
|
||||
"char id=%d x=%d y=%d width=%d height=%d xoffset=%d yoffset=%d xadvance=%d page=%d chnl=%*d\n",
|
||||
&id, &temp_char.x, &temp_char.y, &temp_char.w,
|
||||
&temp_char.h, &temp_char.x_ofs,
|
||||
&temp_char.y_ofs, &temp_char.x_advance,
|
||||
&temp_char.page);
|
||||
font->chars[id] = temp_char;
|
||||
|
||||
if (count != 9)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int oglf_load(oglf_font_t *font, const char *bmf_path, unsigned int texture) {
|
||||
FILE *file;
|
||||
char buffer[256];
|
||||
|
||||
file = fopen(bmf_path, "r"); // could not open file for read
|
||||
if (file == 0)
|
||||
return 0;
|
||||
|
||||
font->chars = (oglf_char_t *) malloc(OGLF_MAX_ASCII * sizeof(oglf_char_t));
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), file) != 0) {
|
||||
int result = _parse_line(font, buffer);
|
||||
if (result == 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
fclose(file); // close the source file
|
||||
|
||||
font->fontTex = texture;
|
||||
font->scale = 1.f;
|
||||
|
||||
_compile(font);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// calculate the approx. width of a string of text
|
||||
// note: no kerning info is currently evaluated
|
||||
int oglf_width(oglf_font_t *font, const char *text) {
|
||||
int w, l, i;
|
||||
w = 0;
|
||||
l = strlen(text);
|
||||
for (i = 0; i < l; i++) {
|
||||
//w += font->chars[text[i]].w;
|
||||
w += font->chars[text[i]].x_advance;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
// destroy the charset
|
||||
void oglf_destroy(oglf_font_t *font) {
|
||||
glDeleteLists(font->base_list, OGLF_MAX_ASCII);
|
||||
glDeleteTextures(1, &font->fontTex);
|
||||
free(font->chars);
|
||||
}
|
||||
/*
|
||||
void ogl___font_in(ogl_font *font);
|
||||
|
||||
void bmf___font_in(ogl_font *font) {
|
||||
ogl___font_in(font);
|
||||
}
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
void oglf_begin(oglf_font_t *font, int width, int height) {
|
||||
glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT
|
||||
| GL_TRANSFORM_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_LIGHTING);
|
||||
glDisable(GL_CULL_FACE);
|
||||
//glNormal3f( 0.0, 0.0, 1.0);
|
||||
|
||||
// set projection matrix
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
//int newheight = (_width / 16.0) * 9.0;
|
||||
glOrtho(0.0,
|
||||
(double)width,
|
||||
(double)height,
|
||||
0.0,
|
||||
-1.0,
|
||||
1.0);
|
||||
// prepare model matrix
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
//glScalef(font->scale, font->scale, 1.f);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, font->fontTex);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
glAlphaFunc(GL_GEQUAL,0.1);
|
||||
glEnable(GL_ALPHA_TEST);
|
||||
}
|
||||
|
||||
void oglf_print_l(oglf_font_t *font, float x, float y, const char *text, int l) {
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glTranslatef(x, y, 0);
|
||||
glCallLists(l, GL_UNSIGNED_BYTE, text);
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void oglf_end() {
|
||||
glPopAttrib();
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPopMatrix();
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
void oglf_print(oglf_font_t *font, float x, float y, const char *text) {
|
||||
oglf_print_l(font, x, y, text, strlen(text));
|
||||
}
|
||||
|
||||
void oglf_printf(oglf_font_t *font, int x, int y, const char *fmt, ...) {
|
||||
char text[OGLF_MAX_LINE], *line_start, *c;
|
||||
float h;
|
||||
va_list ap;
|
||||
int row;
|
||||
|
||||
if (strlen(fmt) == 0)
|
||||
return;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsprintf(text, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
line_start = text;
|
||||
row = 0;
|
||||
h = font->h / .63f;
|
||||
|
||||
NextL: c = strchr(line_start, '\n');
|
||||
if (c) {
|
||||
row++;
|
||||
oglf_print_l(font, (float) x, (float) (y - h * row), line_start, c
|
||||
- line_start);
|
||||
line_start = c + 1;
|
||||
goto NextL;
|
||||
}
|
||||
|
||||
oglf_print_l(font, (float) x, (float) (y - h * row), line_start, strlen(
|
||||
line_start));
|
||||
}
|
||||
|
||||
/*
|
||||
#define OGLF_CENTER_TEXT(rect, txtw, ofs) ((float)((rect[ofs+_W] - txtw - rect[ofs])/2 + rect[ofs]))
|
||||
|
||||
// printf function which takes in account the alignment
|
||||
void ogl_printf_al(oglf_font_t *font, alignment align, GLuint *rect,
|
||||
const char *fmt, ...) {
|
||||
char text[BMF_MAX_LINE];
|
||||
va_list ap;
|
||||
|
||||
if (!strlen(fmt))
|
||||
return;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsprintf(text, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
switch (align) {
|
||||
case al_left:
|
||||
ogl_print(font, (float) rect[_X],
|
||||
OGLF_CENTER_TEXT(rect, font->line_h, _Y), text);
|
||||
break;
|
||||
case al_right:
|
||||
ogl_print(font, ((float) rect[_W] - ogl__width(font, text)),
|
||||
OGLF_CENTER_TEXT(rect, font->line_h, _Y), text);
|
||||
break;
|
||||
case al_center:
|
||||
ogl_print(font, OGLF_CENTER_TEXT(rect, ogl__width(font, text), _X),
|
||||
OGLF_CENTER_TEXT(rect, font->line_h, _Y), text);
|
||||
break;
|
||||
}
|
||||
}
|
||||
*/
|
49
src/client/oglfont.h
Normal file
49
src/client/oglfont.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifndef OGLF_H_
|
||||
#define OGLF_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define OGLF_MAX_ASCII 256
|
||||
#define OGLF_MAX_LINE 1024
|
||||
#define OGLF_MAX_PATH 256
|
||||
|
||||
typedef struct oglf_char_t {
|
||||
unsigned int x, y, w, h;
|
||||
unsigned int x_ofs, y_ofs;
|
||||
unsigned int x_advance, page;
|
||||
} oglf_char_t;
|
||||
|
||||
typedef struct oglf_kerninfo_t {
|
||||
unsigned short first, second;
|
||||
short kerning;
|
||||
} oglf_kerninfo_t;
|
||||
|
||||
typedef struct oglf_font_t {
|
||||
float scale;
|
||||
int base, line_h, w, h, pages, kerninfo_count;
|
||||
unsigned int fontTex;
|
||||
int base_list;
|
||||
oglf_kerninfo_t *kerninfo;
|
||||
oglf_char_t *chars;
|
||||
float mmat[16];
|
||||
} oglf_font_t;
|
||||
|
||||
int oglf_load(oglf_font_t *font, const char *bmf_path, unsigned int texture);
|
||||
int oglf_width(oglf_font_t *font, const char *text);
|
||||
void oglf_destroy(oglf_font_t *font);
|
||||
|
||||
void oglf_begin(oglf_font_t *font, int width, int height);
|
||||
void oglf_end();
|
||||
|
||||
void oglf_print(oglf_font_t *font, float x, float y, const char *text);
|
||||
void oglf_printf_al(oglf_font_t *font, int align, unsigned int *rect,
|
||||
const char *fmt, ...);
|
||||
void oglf_printf(oglf_font_t *font, int x, int y, const char *fmt, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _FONT_H_
|
Reference in New Issue
Block a user