summaryrefslogtreecommitdiffstats
path: root/sdl/platform.c
diff options
context:
space:
mode:
Diffstat (limited to 'sdl/platform.c')
-rw-r--r--sdl/platform.c112
1 files changed, 107 insertions, 5 deletions
diff --git a/sdl/platform.c b/sdl/platform.c
index aba5789..a44bfd4 100644
--- a/sdl/platform.c
+++ b/sdl/platform.c
@@ -6,11 +6,17 @@
*
*/
+#include <lauxlib.h>
+#include <lualib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/glew.h>
+#include <string.h>
+#include "gldraw.h"
#include "gltools.h"
+#include "luatools.h"
#include "platform.h"
+#include "sdltools.h"
/* few light arrays */
const GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 1.0f };
@@ -23,13 +29,109 @@ const GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
/* variables that should be already defined and declared for us by main program */
-extern const unsigned int xres_w;
-extern const unsigned int yres_w;
-extern const unsigned int bpp_w;
-extern const unsigned int af_w;
-extern const unsigned int sdl_video_flags;
+extern unsigned int xres_w;
+extern unsigned int yres_w;
+extern unsigned int bpp_w;
+extern unsigned int af_w;
extern const char *window_caption;
extern const char *window_icon_path;
+extern const unsigned int sdl_video_flags;
+extern unsigned int maxfps_w;
+
+void platform_init(struct platform *p)
+{
+ memset(p, 0, sizeof(struct platform));
+
+ /* Lua */
+ /* create new lua state */
+ p->L = luaL_newstate();
+
+ /* load lua libraries */
+ luaL_openlibs(p->L);
+
+ /* ToDo: how would you make platform more generic? */
+ /* note, this should be already zero'ed out */
+ memset(&p->config_table, 0, sizeof(p->config_table));
+
+ /* initialize SDL, GLEW, and OpenGL */
+ load_config(p);
+ p->screen = setup_sdl();
+ setup_glew();
+ setup_opengl();
+
+ /* set the camera to <0,0,0> */
+ glframe_reset(&p->camera);
+ p->textures[0] = gltLoadTGATexture("stone.tga");
+ p->textures[1] = gltLoadTGATexture("grass.tga");
+
+ /* display list, precompile commands */
+ p->ground_list = glGenLists(2);
+ p->triangle_list = p->ground_list + 1;
+
+ /* a triangle with a texture */
+ glNewList(p->triangle_list, GL_COMPILE);
+ glBindTexture(GL_TEXTURE_2D, p->textures[0]);
+ /* glBindTexture(GL_TEXTURE_2D, p->textures[1]); */
+ glColor3f(1.0f, 1.0f, 1.0f);
+ glDrawTriangle();
+ glEndList();
+
+ /* a ground drawn using magneta lines */
+ glNewList(p->ground_list, GL_COMPILE);
+ glColor3ub(255, 0, 255);
+ glDrawGround();
+ glEndList();
+
+ /* add a simple timer / callback function */
+ p->timer_id = SDL_AddTimer(5000, sdlTimerCallback, &(p->camera));
+
+ /* apply a custom cursor */
+ p->my_cursor = sdlInitCursor(arrow);
+ SDL_SetCursor(p->my_cursor);
+}
+
+void platform_destroy(struct platform *p)
+{
+ glDeleteTextures(2, p->textures);
+ glDeleteLists(p->ground_list, 2);
+
+ SDL_FreeSurface(p->screen);
+
+ lua_close(p->L);
+}
+
+/* load Lua config file from the disk */
+void load_config(struct platform *p)
+{
+ if (luaLoadConfig(p->L, "config.lua") ||
+ luaFillTablePlatform(p->L, &p->config_table) != -1)
+ {
+ /* print was was loaded from config.lua to stdout */
+ luaPrintTablePlatform(&p->config_table);
+
+ /* override any globals that config.lua redefines */
+ if (p->config_table.xres)
+ xres_w = p->config_table.xres;
+
+ if (p->config_table.yres)
+ yres_w = p->config_table.yres;
+
+ if (p->config_table.bpp)
+ bpp_w = p->config_table.bpp;
+
+ if (p->config_table.af)
+ af_w = p->config_table.af;
+
+ if (p->config_table.name)
+ window_caption = p->config_table.name;
+
+ if (p->config_table.icon)
+ window_icon_path = p->config_table.icon;
+
+ if (p->config_table.maxfps)
+ maxfps_w = p->config_table.maxfps;
+ }
+}
void setup_opengl(void)
{