Fixed symlinks. Added quit event to demos.
[luagame.git] / main.cpp
blobd2d3c70d4528928a90f1683c51313c6f2051cbbd
1 /*
2 LuaGame Game Execution Environment
3 ----------------------------------
5 Copyright (c)2006-2007 - Brett Lajzer
7 See LICENSE for license information.
8 */
10 #include <iostream>
11 #include <list>
12 #include <string>
13 #include "lua.hpp"
14 #include "SDL/SDL.h"
15 #include "globals.h"
16 #include "SoundEngine.h"
17 #include "funcs.h"
18 #include "funcs_input.h"
19 #include "funcs_video.h"
20 #include "funcs_sound.h"
21 #include "funcs_font.h"
22 #include "funcs_draw.h"
24 using namespace std; //don't cry about this. really.
26 int main(int argc, char *argv[]){
28 //get path from the command line
29 //or print usage info if the arg is --help or -h
30 if(argc == 2){
31 if(string(argv[1])=="-h" || string(argv[1])=="--help"){
32 print_usage(string(argv[0]));
33 return 0;
34 }else{
35 if(chdir(argv[1])==-1){
36 perror("");
37 return 1;
42 //init Lua
43 lua_State *L = luaL_newstate();
44 luaL_openlibs(L);
46 //init SDL
47 SDL_Init(SDL_INIT_EVERYTHING);
49 //initialize audio
50 SoundEngine::open();
52 //initialize fonts
53 TTF_Init();
55 //load up configuration file and create screen surface
56 screen = load_config(L, string(argv[0]));
57 if(!screen){ //die
58 cerr << "Error: Cannot create video surface. \n";
59 SDL_Quit();
60 lua_close(L);
61 return 1;
64 //register functions with lua
65 lua_pushcfunction(L, l_getimage);
66 lua_setglobal(L, "get_image");
68 lua_pushcfunction(L, l_releaseimage);
69 lua_setglobal(L, "release_image");
71 lua_pushcfunction(L, l_flip);
72 lua_setglobal(L, "update_screen");
74 lua_pushcfunction(L, l_blit);
75 lua_setglobal(L, "blit");
77 lua_pushcfunction(L, l_blitframe);
78 lua_setglobal(L, "blit_frame");
80 lua_pushcfunction(L, l_mouse_state);
81 lua_setglobal(L, "mouse_state");
83 lua_pushcfunction(L, l_show_cursor);
84 lua_setglobal(L, "show_cursor");
86 lua_pushcfunction(L, l_fill_screen);
87 lua_setglobal(L, "fill_screen");
89 lua_pushcfunction(L, l_delete_image);
90 lua_setglobal(L, "delete_image");
92 lua_pushcfunction(L, l_rotzoom);
93 lua_setglobal(L, "rotzoom");
95 lua_pushcfunction(L, l_playsample);
96 lua_setglobal(L, "play_sample");
98 lua_pushcfunction(L, l_playmusic);
99 lua_setglobal(L, "play_music");
101 lua_pushcfunction(L, l_stopmusic);
102 lua_setglobal(L, "stop_music");
104 lua_pushcfunction(L, l_load_font);
105 lua_setglobal(L, "load_font");
107 lua_pushcfunction(L, l_close_font);
108 lua_setglobal(L, "unload_font");
110 lua_pushcfunction(L, l_font_string_metrics);
111 lua_setglobal(L, "rendered_string_size");
113 lua_pushcfunction(L, l_render_string);
114 lua_setglobal(L, "render_string");
116 lua_pushcfunction(L, l_get_event);
117 lua_setglobal(L, "get_event");
119 lua_pushcfunction(L, l_getticks);
120 lua_setglobal(L, "get_ticks");
122 lua_pushcfunction(L, l_delay);
123 lua_setglobal(L, "delay");
125 lua_pushcfunction(L, l_num_joysticks);
126 lua_setglobal(L, "num_joysticks");
128 //draw functions
129 lua_pushcfunction(L, l_draw_pixel);
130 lua_setglobal(L, "draw_pixel");
132 lua_pushcfunction(L, l_draw_line);
133 lua_setglobal(L, "draw_line");
135 lua_pushcfunction(L, l_draw_rect);
136 lua_setglobal(L, "draw_rect");
138 lua_pushcfunction(L, l_draw_frect);
139 lua_setglobal(L, "draw_filled_rect");
141 lua_pushcfunction(L, l_draw_circle);
142 lua_setglobal(L, "draw_circle");
144 lua_pushcfunction(L, l_draw_fcircle);
145 lua_setglobal(L, "draw_filled_circle");
147 lua_pushcfunction(L, l_draw_ellipse);
148 lua_setglobal(L, "draw_ellipse");
150 lua_pushcfunction(L, l_draw_fellipse);
151 lua_setglobal(L, "draw_filled_ellipse");
154 //force joystick event handling on
155 SDL_JoystickEventState(SDL_ENABLE);
157 //open up all available joysticks
158 std::list<SDL_Joystick*> joystick_list;
159 for(int i=0; i < SDL_NumJoysticks(); i++){
160 joystick_list.push_back(SDL_JoystickOpen(i));
161 std::cout << "Opened Joystick: " << SDL_JoystickName(i) << std::endl;
164 //nil bits of the os table for safety reasons
165 luaL_loadstring(L, "os.execute=nil os.exit=nil os.remove=nil os.rename=nil os.tmpname=nil");
166 lua_pcall(L, 0, 0, 0);
168 //load up the main script into the lua environment
169 //and make it all happen
170 int error_main = luaL_loadfile(L, "base/init.lua") ||
171 lua_pcall(L, 0, 0, 0);
173 if ( error_main) { // die a horrible death b/c we can't run without that file
174 cerr << lua_tostring(L, -1) << "\n";
175 lua_pop(L,1);
177 //close any opened joysticks
178 for(std::list<SDL_Joystick*>::iterator it=joystick_list.begin(); it != joystick_list.end(); it++){
179 SDL_JoystickClose(*it);
182 //end it all
183 TTF_Quit();
184 SoundEngine::close();
185 clear_images();
186 SDL_Quit();
187 lua_close(L);
188 return 1;
191 //close any opened joysticks
192 for(std::list<SDL_Joystick*>::iterator it=joystick_list.begin(); it != joystick_list.end(); it++){
193 SDL_JoystickClose(*it);
197 //end fonts
198 TTF_Quit();
200 //close audio
201 SoundEngine::close();
203 //cleanup image store
204 clear_images();
206 //kill sdl
207 SDL_Quit();
209 //kill lua
210 lua_close(L);
212 return 0;