-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_client.c
188 lines (148 loc) · 3.82 KB
/
main_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <math.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <GL/glut.h>
#include <AL/al.h>
#include <AL/alc.h>
#endif
#include <enet/enet.h>
#include "vector2.h"
#include "client.h"
vector2 movin = {0,0};
int lastFrameTime = 0;
ALCdevice * device;
ALCcontext * context;
ALenum error;
ENetHost * enet_client;
ENetAddress address;
ENetEvent event;
client tclient;
void leave(int sig);
void alCleanUp(void){
context = alcGetCurrentContext();
device = alcGetContextsDevice(context);
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);
}
void clean_up(void){
enet_host_destroy(enet_client);
client_free(tclient);
}
int init(void){
device = alcOpenDevice(NULL);
if(device) {
context = alcCreateContext(device, NULL);
}
alcMakeContextCurrent(context);
if (enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit (enet_deinitialize);
atexit (alCleanUp);
atexit (clean_up);
enet_client = enet_host_create (NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
0 /* allow up 4 channels to be used, 0 and 1 */,
0 /* 56K modem with 56 Kbps downstream bandwidth */,
0 /* 56K modem with 14 Kbps upstream bandwidth */);
if (enet_client == NULL)
{
fprintf (stderr,
"An error occurred while trying to create an ENet client host.\n");
exit (EXIT_FAILURE);
}
tclient = client_init(enet_client);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
}
void processNormalKeys(unsigned char key, int xx, int yy) {
client_keys(tclient, key);
}
void releaseNormalKeys(unsigned char key, int xx, int yy) {
client_rkeys(tclient, key);
}
void numbers(void){
int now = glutGet(GLUT_ELAPSED_TIME);
int elapsedMilliseconds = now - lastFrameTime;
float elapsedTime = elapsedMilliseconds / 1000.0f;
lastFrameTime = now;
float h = elapsedTime;
client_update(tclient, h);
while (enet_host_service (enet_client, & event, 10) > 0 && client_connection(tclient))
{
switch (event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
client_process_packets(tclient, &event);
break;
case ENET_EVENT_TYPE_DISCONNECT:
client_disconnect(tclient);
break;
}
}
}
void display(void) {
int i,k;
numbers();
//-----This is the stuff involved with drawing the screen----//
glClearColor (0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
client_render(tclient);
glutSwapBuffers();
}
void pressKey(int key, int xx, int yy) {
client_skeys(tclient, key);
}
void releaseKey(int key, int xx, int yy) {
client_rskeys(tclient, key);
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
double ratio = glutGet(GLUT_WINDOW_WIDTH)/(double)glutGet(GLUT_WINDOW_HEIGHT);
vector2 w = clnt_gm_size(tclient);
double wx = ratio * w.y;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, wx, 0, w.y);
glMatrixMode(GL_MODELVIEW);
}
void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char** argv)
{
(void) signal(SIGINT,leave);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Trail Blazer");
init();
glutIgnoreKeyRepeat(1);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(processNormalKeys);
glutKeyboardUpFunc(releaseNormalKeys);
glutMainLoop();
return EXIT_SUCCESS;
}
void leave(int sig) {
printf("\nProgram interupted. Cleaning up.\n");
exit(sig);
}