Lots of changes. Good number of small fixes. Some new stuff.
[luagame.git] / funcs.cpp
blob117cb901f530b575963cc9fa07ad9fa631133204
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include "SDL/SDL.h"
8 #include "lua.hpp"
9 #include <iostream>
10 #include "funcs.h"
12 using namespace std;
14 //prints usage info
15 void print_usage(string progname){
16 cerr << "USAGE: " << progname << " [dir] | [-h|--help]\n";
17 cerr << " dir - directory where game data is\n";
18 cerr << " -h --help - displays this usage info\n\n";
21 //configuration loader
22 //loads config into lua, retrieves values and leaves them there
23 SDL_Surface *load_config(lua_State *L, string pname){
24 if(luaL_loadfile(L, "scripts/config.lua") || lua_pcall(L,0,0,0)){
25 cerr << "Cannot load configuration script!\n" << "Error: " <<lua_tostring(L, -1) << "\n";
26 print_usage(pname);
27 return 0;
30 //put the config values on the stack
31 lua_getglobal(L, "s_fullscreen");
32 lua_getglobal(L, "s_width");
33 lua_getglobal(L, "s_height");
34 lua_getglobal(L, "s_depth");
35 lua_getglobal(L, "s_gamename");
37 //check values for proper type
38 if(!lua_isnumber(L,-5)){
39 cerr << "Config Error: "<< "'s_fullscreen' should be a number\n";
40 return 0;
42 if(!lua_isnumber(L,-4)){
43 cerr << "Config Error: "<< "'s_width' should be a number\n";
44 return 0;
46 if(!lua_isnumber(L,-3)){
47 cerr << "Config Error: "<< "'s_height' should be a number\n";
48 return 0;
50 if(!lua_isnumber(L,-2)){
51 cerr << "Config Error: "<< "'s_depth' should be a number\n";
52 return 0;
54 if(!lua_isstring(L,-1)){
55 cerr << "Config Error: "<< "'s_gamename' should be a string\n";
56 return 0;
59 //set caption and return the surface
60 SDL_WM_SetCaption(lua_tostring(L,-1), lua_tostring(L, -1));
62 SDL_Surface *temp = SDL_SetVideoMode(lua_tointeger(L, -4), lua_tointeger(L, -3), lua_tointeger(L, -2), SDL_HWSURFACE | (lua_tointeger(L,-5)? SDL_FULLSCREEN : 0));
63 for(int i=0; i < 5; i++){lua_pop(L,-1);}
64 return temp;
67 //function for clearing out the image store
68 void clear_images(){
69 map<string, SDL_Surface *>::iterator im = image_store.begin();
71 for(; im != image_store.end(); im++){
72 SDL_FreeSurface(im->second);
75 image_store.clear();