initial network code
This commit is contained in:
parent
abb75acb91
commit
2adf7c3671
262
src/main.cpp
262
src/main.cpp
@ -1,26 +1,40 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <enet/enet.h>
|
||||||
#include <GL/glfw.h>
|
#include <GL/glfw.h>
|
||||||
|
|
||||||
#define MAX_PLAYERS_PER_TEAM 8
|
#define MAX_PLAYERS_PER_TEAM 8
|
||||||
#define MAX_TEAMS 8
|
#define MAX_TEAMS 8
|
||||||
|
|
||||||
typedef struct _player {
|
typedef struct team_t team_t;
|
||||||
|
|
||||||
|
typedef struct player_t player_t;
|
||||||
|
|
||||||
|
struct player_t {
|
||||||
unsigned int session;
|
unsigned int session;
|
||||||
double x, y, z;
|
double x, y, z;
|
||||||
double rx, ry, rz, rw;
|
double rx, ry, rz, rw;
|
||||||
double vx, vy, vz;
|
double vx, vy, vz;
|
||||||
} player;
|
team_t *team;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct _team {
|
struct team_t {
|
||||||
player players[MAX_PLAYERS_PER_TEAM];
|
player_t players[MAX_PLAYERS_PER_TEAM];
|
||||||
double x, y, z;
|
double x, y, z;
|
||||||
GLfloat color[4];
|
GLfloat color[4];
|
||||||
} team;
|
};
|
||||||
|
|
||||||
GLUquadricObj *quadratic;
|
GLUquadricObj *quadratic;
|
||||||
|
ENetHost *host;
|
||||||
|
ENetPeer *client_peer;
|
||||||
|
player_t *local_player;
|
||||||
|
|
||||||
|
void key_callback(int key, int state) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void setup_opengl() {
|
void setup_opengl() {
|
||||||
// Initialise GLFW
|
// Initialise GLFW
|
||||||
@ -30,14 +44,14 @@ void setup_opengl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Open OpenGL window
|
// Open OpenGL window
|
||||||
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_FULLSCREEN)) {
|
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) {
|
||||||
fprintf(stderr, "Failed to open GLFW window\n");
|
fprintf(stderr, "Failed to open GLFW window\n");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
glfwDisable(GLFW_MOUSE_CURSOR);
|
//glfwDisable(GLFW_MOUSE_CURSOR);
|
||||||
glfwSetWindowTitle("Trilinear interpolation");
|
glfwSetWindowTitle("Gremlin Lan Party Game");
|
||||||
|
glfwSetKeyCallback(key_callback);
|
||||||
// Enable sticky keys
|
// Enable sticky keys
|
||||||
glfwEnable(GLFW_STICKY_KEYS);
|
glfwEnable(GLFW_STICKY_KEYS);
|
||||||
|
|
||||||
@ -66,7 +80,7 @@ void setup_opengl() {
|
|||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_team(team *team, size_t id) {
|
void setup_team(team_t *team, size_t id) {
|
||||||
team->color[0] = .5f + .5f * (id & (1 << 0));
|
team->color[0] = .5f + .5f * (id & (1 << 0));
|
||||||
team->color[1] = .5f + .5f * (id & (1 << 1));
|
team->color[1] = .5f + .5f * (id & (1 << 1));
|
||||||
team->color[2] = .5f + .5f * (id & (1 << 2));
|
team->color[2] = .5f + .5f * (id & (1 << 2));
|
||||||
@ -88,7 +102,7 @@ void setup_team(team *team, size_t id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_team(team *team, double dt) {
|
void update_team(team_t *team, double dt) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
||||||
if (team->players[i].session == 0)
|
if (team->players[i].session == 0)
|
||||||
@ -99,7 +113,7 @@ void update_team(team *team, double dt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_team(team *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);
|
||||||
@ -119,39 +133,217 @@ void draw_team(team *team) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
void spawn_player(team_t *team, size_t player) {
|
||||||
|
team->players[player].session = 1;
|
||||||
|
team->players[player].x = team->x + 100 + player * 50.;
|
||||||
|
team->players[player].y = team->y;
|
||||||
|
team->players[player].z = team->z;
|
||||||
|
team->players[player].vx = 0.;
|
||||||
|
team->players[player].vy = 0.;
|
||||||
|
team->players[player].vz = 0.;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t active_players(team_t *team) {
|
||||||
|
size_t i, count = 0;
|
||||||
|
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
||||||
|
if (team->players[i].session != 0)
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t team_with_least_players(team_t *teams) {
|
||||||
|
size_t i, count = MAX_PLAYERS_PER_TEAM + 1;
|
||||||
|
size_t team = MAX_TEAMS + 1;
|
||||||
|
for (i = 0; i < MAX_TEAMS; i++) {
|
||||||
|
size_t players = active_players(&teams[i]);
|
||||||
|
if (players < count) {
|
||||||
|
count = players;
|
||||||
|
team = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return team;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t free_player(team_t *team) {
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
||||||
|
if (team->players[i].session == 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return MAX_PLAYERS_PER_TEAM + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void service_network(team_t *teams) {
|
||||||
|
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);
|
||||||
|
{
|
||||||
|
size_t team = team_with_least_players(teams);
|
||||||
|
size_t player = free_player(&teams[team]);
|
||||||
|
printf("Spwan as %d.%d\n", team, player);
|
||||||
|
/* Store any relevant client information here. */
|
||||||
|
event.peer->data = &teams[team].players[player];
|
||||||
|
spawn_player(&teams[team], player);
|
||||||
|
// send team and player
|
||||||
|
// send state
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ENET_EVENT_TYPE_RECEIVE:
|
||||||
|
printf(
|
||||||
|
"A packet of length %u containing %s was received from %s on channel %u.\n",
|
||||||
|
event.packet->dataLength, event.packet -> data,
|
||||||
|
event.peer->data, event.channelID);
|
||||||
|
|
||||||
|
/* Clean up the packet now that we're done using it. */
|
||||||
|
// receive team and player
|
||||||
|
// receive update
|
||||||
|
// receive state
|
||||||
|
enet_packet_destroy(event.packet);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ENET_EVENT_TYPE_DISCONNECT:
|
||||||
|
printf("%s disconected.\n", event.peer -> data);
|
||||||
|
|
||||||
|
/* Reset the peer's client information. */
|
||||||
|
free(event.peer->data);
|
||||||
|
event.peer->data = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void accelerate(double x, double y, double z) {
|
||||||
|
local_player->vx += x;
|
||||||
|
local_player->vy += y;
|
||||||
|
local_player->vz += z;
|
||||||
|
// send update
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
int width, height, x, y, last_x, last_y;
|
int width, height, x, y, last_x, last_y;
|
||||||
double time, last_time, phi = 0.0, theta = 0.0;
|
double time, last_time, phi = 0.0, theta = 0.0;
|
||||||
GLboolean running;
|
GLboolean running;
|
||||||
|
int server = 0;
|
||||||
|
|
||||||
quadratic = gluNewQuadric();
|
quadratic = gluNewQuadric();
|
||||||
gluQuadricNormals(quadratic, GLU_SMOOTH);
|
gluQuadricNormals(quadratic, GLU_SMOOTH);
|
||||||
gluQuadricTexture(quadratic, GL_TRUE);
|
gluQuadricTexture(quadratic, GL_TRUE);
|
||||||
|
|
||||||
team teams[MAX_TEAMS];
|
team_t teams[MAX_TEAMS];
|
||||||
|
|
||||||
setup_opengl();
|
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < MAX_TEAMS; i++)
|
for (i = 0; i < MAX_TEAMS; i++)
|
||||||
setup_team(&teams[i], i);
|
setup_team(&teams[i], i);
|
||||||
|
|
||||||
|
setup_opengl();
|
||||||
|
|
||||||
|
if (argc > 1) {
|
||||||
|
server = 0;
|
||||||
|
setup_network(argv[1]);
|
||||||
|
} else {
|
||||||
|
server = 1;
|
||||||
|
setup_network(NULL);
|
||||||
|
local_player = &teams[0].players[0];
|
||||||
|
spawn_player(&teams[0], 0);
|
||||||
|
}
|
||||||
|
|
||||||
running = GL_TRUE;
|
running = GL_TRUE;
|
||||||
last_time = glfwGetTime();
|
last_time = glfwGetTime();
|
||||||
glfwGetMousePos(&last_x, &last_y);
|
glfwGetMousePos(&last_x, &last_y);
|
||||||
while (running) {
|
while (running) {
|
||||||
// Get time and mouse position
|
// Get time and mouse position
|
||||||
time = glfwGetTime();
|
time = glfwGetTime();
|
||||||
|
double dt = time - last_time;
|
||||||
glfwGetMousePos(&x, &y);
|
glfwGetMousePos(&x, &y);
|
||||||
phi += (x - last_x) * 0.001;
|
if (glfwGetMouseButton(GLFW_MOUSE_BUTTON_2) == GLFW_PRESS) {
|
||||||
theta += (y - last_y) * -0.001;
|
phi += (x - last_x) * 0.01;
|
||||||
if (theta > 1.5)
|
theta += (y - last_y) * -0.01;
|
||||||
theta = 1.5;
|
if (theta > 1.5)
|
||||||
if (theta < -1.5)
|
theta = 1.5;
|
||||||
theta = -1.5;
|
if (theta < -1.5)
|
||||||
|
theta = -1.5;
|
||||||
|
}
|
||||||
last_x = x;
|
last_x = x;
|
||||||
last_y = y;
|
last_y = y;
|
||||||
|
|
||||||
|
double rx = cos(phi) * cos(theta);
|
||||||
|
double ry = sin(theta);
|
||||||
|
double rz = sin(phi) * cos(theta);
|
||||||
|
|
||||||
|
if (glfwGetKey('W')) {
|
||||||
|
accelerate(rx * 10.0f * dt, ry * 10.0f * dt, rz * 10.0f * dt);
|
||||||
|
} else if (glfwGetKey('S')) {
|
||||||
|
accelerate(rx * -10.0f * dt, ry * -10.0f * dt, rz * -10.0f * dt);
|
||||||
|
} else if (glfwGetKey('A')) {
|
||||||
|
accelerate(rz * 10.0f * dt, 0, -rx * 10.0f * dt);
|
||||||
|
} else if (glfwGetKey('D')) {
|
||||||
|
accelerate(-rz * 10.0f * dt, 0, rx * 10.0f * dt);
|
||||||
|
}
|
||||||
|
|
||||||
// Get window size (may be different than the requested size)
|
// Get window size (may be different than the requested size)
|
||||||
glfwGetWindowSize(&width, &height);
|
glfwGetWindowSize(&width, &height);
|
||||||
height = height > 0 ? height : 1;
|
height = height > 0 ? height : 1;
|
||||||
@ -172,10 +364,19 @@ int main(void) {
|
|||||||
// Select and setup the modelview matrix
|
// Select and setup the modelview matrix
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluLookAt(100.0f, 100.0f, 100.0f, // Eye-position
|
if (local_player) {
|
||||||
100.0f + cos(phi) * cos(theta) * 10.0f, 100.0f + sin(theta)
|
gluLookAt(local_player->x, local_player->y,
|
||||||
* 10.0f, 100.0f + sin(phi) * cos(theta) * 10.0f, // View-point
|
local_player->z, // Eye-position
|
||||||
0.0f, 1.0f, 0.0f); // Up-vector
|
local_player->x + cos(phi) * cos(theta) * 10.0f,
|
||||||
|
local_player->y + sin(theta) * 10.0f, local_player->z
|
||||||
|
+ sin(phi) * cos(theta) * 10.0f, // View-point
|
||||||
|
0.0f, 1.0f, 0.0f); // Up-vector
|
||||||
|
} else {
|
||||||
|
gluLookAt(100.0f, 100.0f, 100.0f, // Eye-position
|
||||||
|
100.0f + cos(phi) * cos(theta) * 10.0f, 100.0f + sin(theta)
|
||||||
|
* 10.0f, 100.0f + sin(phi) * cos(theta) * 10.0f, // View-point
|
||||||
|
0.0f, 1.0f, 0.0f); // Up-vector
|
||||||
|
}
|
||||||
|
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, teams[0].color);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, teams[0].color);
|
||||||
// Draw a textured quad
|
// Draw a textured quad
|
||||||
@ -187,7 +388,7 @@ int main(void) {
|
|||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
for (i = 0; i < MAX_TEAMS; i++)
|
for (i = 0; i < MAX_TEAMS; i++)
|
||||||
update_team(&teams[i], time - last_time);
|
update_team(&teams[i], dt);
|
||||||
|
|
||||||
for (i = 0; i < MAX_TEAMS; i++)
|
for (i = 0; i < MAX_TEAMS; i++)
|
||||||
draw_team(&teams[i]);
|
draw_team(&teams[i]);
|
||||||
@ -195,6 +396,8 @@ int main(void) {
|
|||||||
// Swap buffers
|
// Swap buffers
|
||||||
glfwSwapBuffers();
|
glfwSwapBuffers();
|
||||||
|
|
||||||
|
service_network(teams);
|
||||||
|
|
||||||
// Check if the ESC key was pressed or the window was closed
|
// Check if the ESC key was pressed or the window was closed
|
||||||
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
|
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
|
||||||
|
|
||||||
@ -203,6 +406,9 @@ int main(void) {
|
|||||||
|
|
||||||
gluDeleteQuadric(quadratic);
|
gluDeleteQuadric(quadratic);
|
||||||
|
|
||||||
|
enet_host_destroy(host);
|
||||||
|
enet_deinitialize();
|
||||||
|
|
||||||
// Close OpenGL window and terminate GLFW
|
// Close OpenGL window and terminate GLFW
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user