2 LuaGame Game Execution Environment
3 ----------------------------------
5 Copyright (c)2006-2007 - Brett Lajzer
7 See LICENSE for license information.
16 #include "SoundEngine.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
31 if(string(argv
[1])=="-h" || string(argv
[1])=="--help"){
32 print_usage(string(argv
[0]));
35 if(chdir(argv
[1])==-1){
43 lua_State
*L
= luaL_newstate();
47 SDL_Init(SDL_INIT_EVERYTHING
);
55 //load up configuration file and create screen surface
56 screen
= load_config(L
, string(argv
[0]));
58 cerr
<< "Error: Cannot create video surface. \n";
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");
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";
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
);
184 SoundEngine::close();
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
);
201 SoundEngine::close();
203 //cleanup image store