yay
[wtsg.git] / main.cpp
blob0fadbc3a6d96170212bba23349605cadf0fff3ac
1 #include <GL/gl.h>
2 #include <GL/glu.h>
3 #include <GL/glfw.h>
4 #include <iostream>
5 #include <vector>
7 using namespace std;
9 #define NUM_TEXTURES 1
10 char * tex_name[ NUM_TEXTURES ] = {
11 "particle.tga",
13 GLuint tex_id[ NUM_TEXTURES ];
14 static void LoadTextures( void )
16 int i;
18 // Generate texture objects
19 glGenTextures( NUM_TEXTURES, tex_id );
21 // Load textures
22 for( i = 0; i < NUM_TEXTURES; i ++ )
24 // Select texture object
25 glBindTexture( GL_TEXTURE_2D, tex_id[ i ] );
27 // Set texture parameters
28 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
29 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
30 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
31 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
33 // Upload texture from file to texture memory
34 if (!glfwLoadTexture2D( tex_name[ i ], 0 )) {
35 printf("Error loading %s\n",tex_name[i]);
41 extern "C" {
42 #include <lua.h>
43 #include <lauxlib.h>
44 #include <lualib.h>
46 static int draw_getFrameSize(lua_State *L) {
47 int width,height;
48 glfwGetWindowSize( &width, &height );
49 lua_pushnumber(L,width);
50 lua_pushnumber(L,height);
51 return 2;
54 static int draw_glViewport (lua_State *L) {
55 glViewport( luaL_checkint(L,1), luaL_checkint(L,2), luaL_checkint(L,3), luaL_checkint(L,4));
56 return 0;
58 static int hasfield (lua_State *L,int idx, const char *name) {
59 int res;
60 lua_getfield(L,idx,name);
61 res = lua_isnil(L,-1);
62 lua_pop(L,1);
63 return res;
65 static int draw_glClearColor (lua_State *L) {
66 int bits = GL_COLOR_BUFFER_BIT;
67 glClearColor( (float)luaL_checknumber(L,1), (float)luaL_checknumber(L,2), (float)luaL_checknumber(L,3), (float)luaL_checknumber(L,4));
68 if (lua_istable(L,5)) {
69 bits = (hasfield(L,5,"color")?GL_COLOR_BUFFER_BIT:0)|
70 (hasfield(L,5,"depth")?GL_DEPTH_BUFFER_BIT:0);
72 glClear( bits );
73 return 0;
76 static int draw_glRect (lua_State *L) {
77 double x = luaL_checknumber(L,1);
78 double y = luaL_checknumber(L,2);
79 double w = luaL_checknumber(L,3);
80 double h = luaL_checknumber(L,4);
81 double r = luaL_optnumber(L,5,1);
82 double g = luaL_optnumber(L,6,1);
83 double b = luaL_optnumber(L,7,1);
84 double a = luaL_optnumber(L,8,1);
85 glColor4d(r,g,b,a);
86 glBegin( GL_QUADS );
87 glTexCoord2f( 0.0f, 1.0f );
88 glVertex2d(x,y);
89 glTexCoord2f( 1.0f, 1.0f );
90 glVertex2d(x+w,y);
91 glTexCoord2f( 1.0f, 0.0f );
92 glVertex2d(x+w,y+h);
93 glTexCoord2f( 0.0f, 0.0f );
94 glVertex2d(x,y+h);
95 glEnd();
96 return 0;
99 static int framefunc (lua_State *L) {
100 static int frame = 0;
101 frame += 1;
102 glRotatef(frame*.01f, 0.25f, 1.0f, 0.75f);
103 glBegin( GL_TRIANGLES );
104 glColor3f(0.1f, 0.0f, 0.0f );
105 glVertex3f(0.0f, 3.0f, -4.0f);
106 glColor3f(0.0f, 1.0f, 0.0f );
107 glVertex3f(3.0f, -2.0f, -4.0f);
108 glColor3f(0.0f, 0.0f, 1.0f );
109 glVertex3f(-3.0f, -2.0f, -4.0f);
110 glEnd();
111 glBegin( GL_TRIANGLES );
112 glColor3f(0.0f, 0.1f, 0.0f );
113 glVertex3f(0.0f, 3.0f, -3.0f);
114 glColor3f(0.0f, 0.0f, 1.0f );
115 glVertex3f(3.0f, -2.0f, -2.0f);
116 glColor3f(1.0f, 0.0f, 0.0f );
117 glVertex3f(-3.0f, -2.0f, 2.0f);
118 glEnd();
120 return 0;
123 static int traceback (lua_State *L) {
124 if (!lua_isstring(L, 1)) /* 'message' not a string? */
125 return 1; /* keep it intact */
126 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
127 if (!lua_istable(L, -1)) {
128 lua_pop(L, 1);
129 return 1;
131 lua_getfield(L, -1, "traceback");
132 if (!lua_isfunction(L, -1)) {
133 lua_pop(L, 2);
134 return 1;
136 lua_pushvalue(L, 1); /* pass error message */
137 lua_pushinteger(L, 2); /* skip this function and traceback */
138 lua_call(L, 2, 1); /* call debug.traceback */
139 return 1;
143 static int docall (lua_State *L, int narg, int clear) {
144 int status;
145 int base = lua_gettop(L) - narg; /* function index */
146 lua_pushcfunction(L, traceback); /* push traceback function */
147 lua_insert(L, base); /* put it under chunk and args */
148 status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
149 lua_remove(L, base); /* remove traceback function */
150 /* force a complete garbage collection in case of errors */
151 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
152 return status;
158 int main()
160 int width, height;
161 int frame = 0;
162 bool running = true;
164 lua_State *L;
165 L = luaL_newstate();
167 luaL_openlibs(L);
169 static const luaL_reg drawlib[] = {
170 {"getFrameSize",draw_getFrameSize},
171 {"glViewport",draw_glViewport},
172 {"rect",draw_glRect},
173 {"framefunc",framefunc},
174 {NULL,NULL}
177 luaL_openlib(L, "draw", drawlib, 0);
179 lua_newtable(L);
180 lua_setglobal(L,"engine");
181 if (luaL_loadfile(L,"main.lua")) {
182 printf("Syntax error: %s\n",lua_tostring(L,-1));
183 lua_pop(L,1);
185 else {
186 if (docall(L,0,1)) {
187 printf("LUA SCRIPT ERROR:\n%s",lua_tostring(L,-1));
188 lua_pop(L,1);
193 glfwInit();
195 if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
197 glfwTerminate();
198 return 0;
201 glfwSetWindowTitle("GLFW Application");
202 LoadTextures();
204 int errorfree = 1;
205 int t = clock();
206 while(running)
208 // exit if ESC was pressed or window was closed
209 running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
210 int t2 = clock();
211 if (t2-t>1000) {
212 int fps = frame*100 / (t2-t);
213 char out[20];
214 sprintf(out,"fps: %i",fps);
215 glfwSetWindowTitle(out);
216 frame = 0;
217 t = t2;
219 frame++;
221 glfwGetWindowSize( &width, &height );
223 if (errorfree) {
224 height = height > 0 ? height : 1;
226 glViewport( 0, 0, width, height );
228 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
229 glClear( GL_COLOR_BUFFER_BIT );
231 glMatrixMode( GL_PROJECTION );
232 glLoadIdentity();
233 //gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );
234 gluOrtho2D(0,width,height,0);
236 // Draw some rotating garbage
237 glMatrixMode( GL_MODELVIEW );
238 glLoadIdentity();
240 glEnable( GL_TEXTURE_2D );
241 glEnable( GL_BLEND );
242 //glEnable( GL_COLOR_MATERIAL );
243 glBlendFunc( GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA );
244 glBindTexture( GL_TEXTURE_2D, tex_id[ 0 ] );
246 /*gluLookAt(0.0f, 0.0f, -10.0f,
247 0.0f, 0.0f, 0.0f,
248 0.0f, 1.0f, 0.0f );*/
250 lua_settop(L,0);
251 lua_getglobal(L,"engine");
252 if (lua_type(L,-1) == LUA_TTABLE) {
253 lua_getfield(L,-1,"frame");
254 if (lua_type(L,-1) == LUA_TFUNCTION) {
255 if (docall(L,0,1)) {
256 printf("LUA SCRIPT ERROR:\n%s",lua_tostring(L,-1));
257 lua_pop(L,1);
258 errorfree = 0;
262 //glTranslatef( 1.0f, 1.0f, 0.0f );
265 glfwSwapBuffers();
269 glfwTerminate();
271 return 0;