-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.c
143 lines (119 loc) · 2.74 KB
/
events.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
/*
Theron Rabe
10/7/2012
events.c
Contains all the event functions for the framework that can be bound to user-defined scripts.
*/
#include <game.h>
int main(int argc, char **argv) {
//Graphics Stuff
glutInit(&argc, argv);
//Framework Stuff
createHeadInst();
startGame(argc, argv);
glutKeyboardFunc(KeyDown);
glutKeyboardUpFunc(KeyUp);
glutMouseFunc(Mouse);
glutTimerFunc(GAME.STEPTIME, Step, 0);
glutDisplayFunc(Draw);
glutTimerFunc(GAME.FRAMERATE, FPS, 0);
glutPassiveMotionFunc(moveMouse);
//glutIdleFunc(Draw);
//Aaaaand... we're off!
loadShaders("gameMachine/vertShader", "gameMachine/fragShader");
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutMainLoop();
return 0;
}
void moveMouse(int x, int y) {
GAME.mouseX = x;
GAME.mouseY = y;
}
void Mouse(int button, int state, int x, int y) {
Object *OBJ = GAME.headInst->OBJ;
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
triggerEvent(onMouseLeft, GAME.headInst);
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
triggerEvent(onMouseRight, GAME.headInst);
}
void Step(int x) {
glutTimerFunc(GAME.STEPTIME, Step, 0);
triggerEvent(onStep, GAME.headInst);
}
void Draw() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
triggerEvent(onDraw, GAME.headInst);
glFlush();
glutSwapBuffers();
//if(flagDraw) {
Instance *i = GAME.headInst->NEXT;
while(i) {
i->X += i->xVelocity;
i->Y += i->yVelocity;
i = i->NEXT;
}
//}
flagDraw = 0;
//Instance *i = GAME.headInst->NEXT;
i = GAME.headInst->NEXT;
while(i) {
i->X += i->xVelocity;
i->Y += i->yVelocity;
i = i->NEXT;
}
}
void FPS(int x) {
glutTimerFunc(GAME.FRAMERATE, FPS, 0);
//flagDraw = 1;
//triggerEvent(onIdle, GAME.headInst);
glutPostRedisplay();
}
void Idle() {
triggerEvent(onIdle, GAME.headInst);
}
void KeyUp(unsigned char key, int x, int y) {
GAME.KEYS[key] = 0;
triggerEvent(onKeyUp, GAME.headInst);
}
void KeyDown(unsigned char key, int x, int y) {
GAME.KEYS[key] = 1;
triggerEvent(onKeyDown, GAME.headInst);
}
Script onCreate(Object *obj) {
return (obj->onCreate);
}
Script onDraw(Object *obj) {
return obj->onDraw;
}
Script onStep(Object *obj) {
return obj->onStep;
}
Script onKeyUp(Object *obj) {
return obj->onKeyUp;
}
Script onKeyDown(Object *obj) {
return obj->onKeyDown;
}
Script onMouseLeft(Object *obj) {
return obj->onMouseLeft;
}
Script onMouseRight(Object *obj) {
return obj->onMouseRight;
}
Script onDestroy(Object *obj) {
return obj->onDestroy;
}
Script onIdle(Object *obj) {
return obj->onIdle;
}
void endGame() {
cleanUpInstances();
cleanUpObjects();
exit(0);
}