First commit of LuaGame with an OpenGL backend.
[luagame.git] / funcs.cpp
blob3e671fead4ae8904e416a0908fc9c412ad68bcd7
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include <GL/gl.h>
8 #include <GL/glu.h>
9 #include <SDL/SDL.h>
10 #include <lua.hpp>
11 #include <iostream>
12 #include "funcs.h"
14 using namespace std;
16 //prints usage info
17 void print_usage(string progname){
18 cerr << "USAGE: " << progname << " [dir] | [-h|--help]\n";
19 cerr << " dir - directory where game data is\n";
20 cerr << " -h --help - displays this usage info\n\n";
23 //configuration loader
24 //loads config into lua, retrieves values and leaves them there
25 SDL_Surface *load_config(lua_State *L, string pname){
26 if(luaL_loadfile(L, "scripts/config.lua") || lua_pcall(L,0,0,0)){
27 cerr << "Cannot load configuration script!\n" << "Error: " <<lua_tostring(L, -1) << "\n";
28 print_usage(pname);
29 return 0;
32 //put the config values on the stack
33 lua_getglobal(L, "s_fullscreen");
34 lua_getglobal(L, "s_width");
35 lua_getglobal(L, "s_height");
36 lua_getglobal(L, "s_depth");
37 lua_getglobal(L, "s_gamename");
39 //check values for proper type
40 if(!lua_isnumber(L,-5)){
41 cerr << "Config Error: "<< "'s_fullscreen' should be a number\n";
42 return 0;
44 if(!lua_isnumber(L,-4)){
45 cerr << "Config Error: "<< "'s_width' should be a number\n";
46 return 0;
48 if(!lua_isnumber(L,-3)){
49 cerr << "Config Error: "<< "'s_height' should be a number\n";
50 return 0;
52 if(!lua_isnumber(L,-2)){
53 cerr << "Config Error: "<< "'s_depth' should be a number\n";
54 return 0;
56 if(!lua_isstring(L,-1)){
57 cerr << "Config Error: "<< "'s_gamename' should be a string\n";
58 return 0;
61 //set caption and return the surface
62 SDL_WM_SetCaption(lua_tostring(L,-1), lua_tostring(L, -1));
64 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
66 SDL_Surface *temp = SDL_SetVideoMode(lua_tointeger(L, -4), lua_tointeger(L, -3), lua_tointeger(L, -2), SDL_OPENGL | (lua_tointeger(L,-5)? SDL_FULLSCREEN : 0));
68 //initialize OpenGL
69 glShadeModel(GL_FLAT);
70 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
71 glEnable(GL_TEXTURE_RECTANGLE_ARB);
72 glEnable(GL_BLEND);
73 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
76 //setup projection
77 glViewport(0, 0, (GLsizei)lua_tointeger(L, -4), (GLsizei)lua_tointeger(L, -3));
79 glMatrixMode(GL_PROJECTION);
80 glLoadIdentity();
82 glOrtho(0.0f, (GLfloat)lua_tointeger(L, -4), (GLfloat)lua_tointeger(L, -3), 0.0f, -1.0f, 1.0f);
84 glMatrixMode(GL_MODELVIEW);
85 glLoadIdentity();
87 glClear(GL_COLOR_BUFFER_BIT);
89 //cleanup
90 for(int i=0; i < 5; i++){lua_pop(L,-1);}
92 return temp;
95 //function for clearing out the image store
96 void clear_images(){
97 map<string, GLSurface *>::iterator im = image_store.begin();
99 for(; im != image_store.end(); im++){
100 glDeleteTextures(1, &(im->second->texture));
101 delete im->second;
104 image_store.clear();