10 char * tex_name
[ NUM_TEXTURES
] = {
13 GLuint tex_id
[ NUM_TEXTURES
];
14 static void LoadTextures( void )
18 // Generate texture objects
19 glGenTextures( NUM_TEXTURES
, tex_id
);
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
]);
46 static int draw_getFrameSize(lua_State
*L
) {
48 glfwGetWindowSize( &width
, &height
);
49 lua_pushnumber(L
,width
);
50 lua_pushnumber(L
,height
);
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));
58 static int hasfield (lua_State
*L
,int idx
, const char *name
) {
60 lua_getfield(L
,idx
,name
);
61 res
= lua_isnil(L
,-1);
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);
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);
87 glTexCoord2f( 0.0f
, 1.0f
);
89 glTexCoord2f( 1.0f
, 1.0f
);
91 glTexCoord2f( 1.0f
, 0.0f
);
93 glTexCoord2f( 0.0f
, 0.0f
);
99 static int framefunc (lua_State
*L
) {
100 static int frame
= 0;
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
);
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
);
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)) {
131 lua_getfield(L
, -1, "traceback");
132 if (!lua_isfunction(L
, -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 */
143 static int docall (lua_State
*L
, int narg
, int clear
) {
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);
169 static const luaL_reg drawlib
[] = {
170 {"getFrameSize",draw_getFrameSize
},
171 {"glViewport",draw_glViewport
},
172 {"rect",draw_glRect
},
173 {"framefunc",framefunc
},
177 luaL_openlib(L
, "draw", drawlib
, 0);
180 lua_setglobal(L
,"engine");
181 if (luaL_loadfile(L
,"main.lua")) {
182 printf("Syntax error: %s\n",lua_tostring(L
,-1));
187 printf("LUA SCRIPT ERROR:\n%s",lua_tostring(L
,-1));
195 if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW
) )
201 glfwSetWindowTitle("GLFW Application");
208 // exit if ESC was pressed or window was closed
209 running
= !glfwGetKey(GLFW_KEY_ESC
) && glfwGetWindowParam( GLFW_OPENED
);
212 int fps
= frame
*100 / (t2
-t
);
214 sprintf(out
,"fps: %i",fps
);
215 glfwSetWindowTitle(out
);
221 glfwGetWindowSize( &width
, &height
);
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
);
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
);
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,
248 0.0f, 1.0f, 0.0f );*/
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
) {
256 printf("LUA SCRIPT ERROR:\n%s",lua_tostring(L
,-1));
262 //glTranslatef( 1.0f, 1.0f, 0.0f );