2 LuaGame Game Execution Environment
3 ----------------------------------
5 Copyright (c)2006-2008 - Brett Lajzer
7 See LICENSE for license information.
17 #include "funcs_input.h"
18 #include "funcs_video.h"
19 #include "funcs_sound.h"
20 #include "funcs_font.h"
21 #include "funcs_draw.h"
25 int main(int argc
, char *argv
[]){
27 //get path from the command line
28 //or print usage info if the arg is --help or -h
30 if(string(argv
[1])=="-h" || string(argv
[1])=="--help"){
31 print_usage(string(argv
[0]));
34 if(chdir(argv
[1])==-1){
42 lua_State
*L
= luaL_newstate();
46 SDL_Init(SDL_INIT_EVERYTHING
);
54 //load up configuration file and create screen surface
55 screen
= load_config(L
, string(argv
[0]));
57 cerr
<< "Error: Cannot create video surface. \n";
63 //register functions with lua
64 lua_pushcfunction(L
, l_getimage
);
65 lua_setglobal(L
, "get_image");
67 lua_pushcfunction(L
, l_releaseimage
);
68 lua_setglobal(L
, "release_image");
70 lua_pushcfunction(L
, l_flip
);
71 lua_setglobal(L
, "update_screen");
73 lua_pushcfunction(L
, l_display
);
74 lua_setglobal(L
, "display");
76 lua_pushcfunction(L
, l_displayframe
);
77 lua_setglobal(L
, "display_frame");
79 lua_pushcfunction(L
, l_mouse_state
);
80 lua_setglobal(L
, "mouse_state");
82 lua_pushcfunction(L
, l_show_cursor
);
83 lua_setglobal(L
, "show_cursor");
85 lua_pushcfunction(L
, l_fill_screen
);
86 lua_setglobal(L
, "fill_screen");
88 lua_pushcfunction(L
, l_delete_image
);
89 lua_setglobal(L
, "delete_image");
93 lua_pushcfunction(L
, l_playsample
);
94 lua_setglobal(L
, "play_sample");
96 lua_pushcfunction(L
, l_stopsamples
);
97 lua_setglobal(L
, "stop_samples");
99 lua_pushcfunction(L
, l_loadsample
);
100 lua_setglobal(L
, "load_sample");
102 lua_pushcfunction(L
, l_unloadsample
);
103 lua_setglobal(L
, "unload_sample");
105 lua_pushcfunction(L
, l_clearsamples
);
106 lua_setglobal(L
, "clear_samples");
108 lua_pushcfunction(L
, l_playmusic
);
109 lua_setglobal(L
, "play_music");
111 lua_pushcfunction(L
, l_stopmusic
);
112 lua_setglobal(L
, "stop_music");
115 lua_pushcfunction(L
, l_load_font
);
116 lua_setglobal(L
, "load_font");
118 lua_pushcfunction(L
, l_close_font
);
119 lua_setglobal(L
, "unload_font");
121 lua_pushcfunction(L
, l_font_string_metrics
);
122 lua_setglobal(L
, "rendered_string_size");
124 lua_pushcfunction(L
, l_render_string
);
125 lua_setglobal(L
, "render_string");
128 lua_pushcfunction(L
, l_get_event
);
129 lua_setglobal(L
, "get_event");
131 lua_pushcfunction(L
, l_getticks
);
132 lua_setglobal(L
, "get_ticks");
134 lua_pushcfunction(L
, l_delay
);
135 lua_setglobal(L
, "delay");
137 lua_pushcfunction(L
, l_num_joysticks
);
138 lua_setglobal(L
, "num_joysticks");
141 lua_pushcfunction(L
, l_draw_pixel
);
142 lua_setglobal(L
, "draw_pixel");
144 lua_pushcfunction(L
, l_draw_line
);
145 lua_setglobal(L
, "draw_line");
147 lua_pushcfunction(L
, l_draw_rect
);
148 lua_setglobal(L
, "draw_rect");
150 lua_pushcfunction(L
, l_draw_frect
);
151 lua_setglobal(L
, "draw_filled_rect");
153 lua_pushcfunction(L
, l_draw_circle
);
154 lua_setglobal(L
, "draw_circle");
156 lua_pushcfunction(L
, l_draw_fcircle
);
157 lua_setglobal(L
, "draw_filled_circle");
159 lua_pushcfunction(L
, l_draw_ellipse
);
160 lua_setglobal(L
, "draw_ellipse");
162 lua_pushcfunction(L
, l_draw_fellipse
);
163 lua_setglobal(L
, "draw_filled_ellipse");
166 //force joystick event handling on
167 SDL_JoystickEventState(SDL_ENABLE
);
169 //open up all available joysticks
170 std::list
<SDL_Joystick
*> joystick_list
;
171 for(int i
=0; i
< SDL_NumJoysticks(); i
++){
172 joystick_list
.push_back(SDL_JoystickOpen(i
));
173 std::cout
<< "Opened Joystick: " << SDL_JoystickName(i
) << std::endl
;
176 //nil bits of the os table for safety reasons
177 luaL_loadstring(L
, "os.execute=nil os.exit=nil os.remove=nil os.rename=nil os.tmpname=nil");
178 lua_pcall(L
, 0, 0, 0);
180 //load up the main script into the lua environment
181 //and make it all happen
182 int error_main
= luaL_loadfile(L
, "base/init.lua") ||
183 lua_pcall(L
, 0, 0, 0);
185 if ( error_main
) { // die a horrible death b/c we can't run without that file
186 cerr
<< lua_tostring(L
, -1) << "\n";
189 //close any opened joysticks
190 for(std::list
<SDL_Joystick
*>::iterator it
=joystick_list
.begin(); it
!= joystick_list
.end(); it
++){
191 SDL_JoystickClose(*it
);
203 //close any opened joysticks
204 for(std::list
<SDL_Joystick
*>::iterator it
=joystick_list
.begin(); it
!= joystick_list
.end(); it
++){
205 SDL_JoystickClose(*it
);
215 //cleanup image store