forked from adamwiggins/lua-pong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.c
156 lines (133 loc) · 3.45 KB
/
engine.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
#include <SDL.h>
#include <SDL_opengl.h>
#include <lua.h>
#include <lauxlib.h>
#define bool int
#define false 0
#define true 1
#define max(a, b) (a < b ? b : a)
lua_State *L;
static int draw_rectangle(lua_State *L)
{
/* get value from Lua */
float x = lua_tonumber(L, 1);
float y = lua_tonumber(L, 2);
float width = lua_tonumber(L, 3);
float height = lua_tonumber(L, 4);
float red = lua_tonumber(L, 5);
float green = lua_tonumber(L, 6);
float blue = lua_tonumber(L, 7);
glLoadIdentity();
glTranslatef(x, y, 0.0f);
glBegin(GL_QUADS);
glColor3f(red, green, blue);
glVertex2f(0.0f, 0.0f);
glVertex2f(width, 0.0f);
glColor3f(max(0, red - 0.7), max(0, green - 0.7), max(0, blue - 0.7));
glVertex2f(width, height);
glVertex2f(0.0f, height);
glEnd();
}
static int key_pressed(int key)
{
Uint8 *keystate = SDL_GetKeyState(NULL);
int pressed = 0;
if (keystate[key])
pressed = 1;
lua_pushboolean(L, pressed);
return 1;
}
static int up_pressed() { return key_pressed(SDLK_UP); }
static int down_pressed() { return key_pressed(SDLK_DOWN); }
static int right_pressed() { return key_pressed(SDLK_RIGHT); }
static int left_pressed() { return key_pressed(SDLK_LEFT); }
/* handle SDL key event */
void pulse_via_lua()
{
/* push the value of "pulse" to the Lua stack */
lua_getglobal(L, "pulse");
if (lua_pcall(L, 0, 0, 0) != 0) {
printf("error running function: %s\n", lua_tostring(L, -1));
exit(1);
}
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("usage: engine [script.lua]\n");
exit(1);
}
L = lua_open();
luaL_openlibs(L); /* Lua 5.2 replce the following to this */
/*luaopen_base(L);*/
/*luaopen_table(L);*/
/*luaopen_io(L);*/
/*luaopen_string(L);*/
/*luaopen_math(L);*/
/* register C Function in Lua */
lua_register(L, "draw_rectangle", draw_rectangle);
lua_register(L, "up_pressed", up_pressed);
lua_register(L, "down_pressed", down_pressed);
lua_register(L, "right_pressed", right_pressed);
lua_register(L, "left_pressed", left_pressed);
/* Initialize SDL and OpenGL */
atexit(SDL_Quit);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_Surface *screen = SDL_SetVideoMode(400, 400, 32, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_OPENGL);
glViewport(0, 0, 400, 400);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
/* read in script file */
FILE *fp = fopen(argv[1], "r");
if (!fp) {
printf("no such file as %s\n", argv[1]);
exit(1);
}
char buff[10000];
int size = fread(buff, 1, 10000, fp);
buff[size] = 0;
fclose(fp);
/* Loads a buffer as Lua chunk */
if (luaL_loadbuffer(L, buff, strlen(buff), "pong") != 0 ||
lua_pcall(L, 0, 0, 0) != 0) {
printf("error loading: %s\n", lua_tostring(L, -1));
exit(1);
}
/* Enter SDL event loop */
bool done = false;
while (!done)
{
SDL_Event event;
SDL_keysym key;
while (SDL_PollEvent(&event) && !done)
switch (event.type)
{
case SDL_KEYDOWN:
key = event.key.keysym;
switch (key.sym)
{
case SDLK_ESCAPE: done = true; break;
}
break;
case SDL_QUIT:
done = true;
break;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* handle key event */
pulse_via_lua();
SDL_GL_SwapBuffers();
/* change this value will affect the frame rate */
SDL_Delay(10);
}
lua_close(L);
return 0;
}