Lots of changes. Good number of small fixes. Some new stuff.
[luagame.git] / main.cpp
blobbe86fca3b8eb2f6d223c27a55257c3c7cc328cb8
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 "funcs.h"
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"
23 using namespace std; //don't cry about this. really.
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
29 if(argc == 2){
30 if(string(argv[1])=="-h" || string(argv[1])=="--help"){
31 print_usage(string(argv[0]));
32 return 0;
33 }else{
34 if(chdir(argv[1])==-1){
35 perror("");
36 return 1;
41 //init Lua
42 lua_State *L = luaL_newstate();
43 luaL_openlibs(L);
45 //init SDL
46 SDL_Init(SDL_INIT_EVERYTHING);
48 //initialize audio
49 sound_open();
51 //initialize fonts
52 TTF_Init();
54 //load up configuration file and create screen surface
55 screen = load_config(L, string(argv[0]));
56 if(!screen){ //die
57 cerr << "Error: Cannot create video surface. \n";
58 SDL_Quit();
59 lua_close(L);
60 return 1;
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_blit);
74 lua_setglobal(L, "blit");
76 lua_pushcfunction(L, l_blitframe);
77 lua_setglobal(L, "blit_frame");
79 lua_pushcfunction(L, l_blit_i);
80 lua_setglobal(L, "blit_i");
82 lua_pushcfunction(L, l_blitframe_i);
83 lua_setglobal(L, "blit_frame_i");
85 lua_pushcfunction(L, l_mouse_state);
86 lua_setglobal(L, "mouse_state");
88 lua_pushcfunction(L, l_show_cursor);
89 lua_setglobal(L, "show_cursor");
91 lua_pushcfunction(L, l_fill_screen);
92 lua_setglobal(L, "fill_screen");
94 lua_pushcfunction(L, l_create_intermediate);
95 lua_setglobal(L, "create_intermediate");
97 lua_pushcfunction(L, l_delete_image);
98 lua_setglobal(L, "delete_image");
100 lua_pushcfunction(L, l_rotzoom);
101 lua_setglobal(L, "rotzoom");
103 //sound functions
104 lua_pushcfunction(L, l_playsample);
105 lua_setglobal(L, "play_sample");
107 lua_pushcfunction(L, l_stopsamples);
108 lua_setglobal(L, "stop_samples");
110 lua_pushcfunction(L, l_loadsample);
111 lua_setglobal(L, "load_sample");
113 lua_pushcfunction(L, l_unloadsample);
114 lua_setglobal(L, "unload_sample");
116 lua_pushcfunction(L, l_clearsamples);
117 lua_setglobal(L, "clear_samples");
119 lua_pushcfunction(L, l_playmusic);
120 lua_setglobal(L, "play_music");
122 lua_pushcfunction(L, l_stopmusic);
123 lua_setglobal(L, "stop_music");
125 //font functions
126 lua_pushcfunction(L, l_load_font);
127 lua_setglobal(L, "load_font");
129 lua_pushcfunction(L, l_close_font);
130 lua_setglobal(L, "unload_font");
132 lua_pushcfunction(L, l_font_string_metrics);
133 lua_setglobal(L, "rendered_string_size");
135 lua_pushcfunction(L, l_render_string);
136 lua_setglobal(L, "render_string");
138 //misc functions
139 lua_pushcfunction(L, l_get_event);
140 lua_setglobal(L, "get_event");
142 lua_pushcfunction(L, l_getticks);
143 lua_setglobal(L, "get_ticks");
145 lua_pushcfunction(L, l_delay);
146 lua_setglobal(L, "delay");
148 lua_pushcfunction(L, l_num_joysticks);
149 lua_setglobal(L, "num_joysticks");
151 //draw functions
152 lua_pushcfunction(L, l_draw_pixel);
153 lua_setglobal(L, "draw_pixel");
155 lua_pushcfunction(L, l_draw_line);
156 lua_setglobal(L, "draw_line");
158 lua_pushcfunction(L, l_draw_rect);
159 lua_setglobal(L, "draw_rect");
161 lua_pushcfunction(L, l_draw_frect);
162 lua_setglobal(L, "draw_filled_rect");
164 lua_pushcfunction(L, l_draw_circle);
165 lua_setglobal(L, "draw_circle");
167 lua_pushcfunction(L, l_draw_fcircle);
168 lua_setglobal(L, "draw_filled_circle");
170 lua_pushcfunction(L, l_draw_ellipse);
171 lua_setglobal(L, "draw_ellipse");
173 lua_pushcfunction(L, l_draw_fellipse);
174 lua_setglobal(L, "draw_filled_ellipse");
176 //intermediate draw functions
177 lua_pushcfunction(L, l_draw_pixel_i);
178 lua_setglobal(L, "draw_pixel_i");
180 lua_pushcfunction(L, l_draw_line_i);
181 lua_setglobal(L, "draw_line_i");
183 lua_pushcfunction(L, l_draw_rect_i);
184 lua_setglobal(L, "draw_rect_i");
186 lua_pushcfunction(L, l_draw_frect_i);
187 lua_setglobal(L, "draw_filled_rect_i");
189 lua_pushcfunction(L, l_draw_circle_i);
190 lua_setglobal(L, "draw_circle_i");
192 lua_pushcfunction(L, l_draw_fcircle_i);
193 lua_setglobal(L, "draw_filled_circle_i");
195 lua_pushcfunction(L, l_draw_ellipse_i);
196 lua_setglobal(L, "draw_ellipse_i");
198 lua_pushcfunction(L, l_draw_fellipse_i);
199 lua_setglobal(L, "draw_filled_ellipse_i");
202 //force joystick event handling on
203 SDL_JoystickEventState(SDL_ENABLE);
205 //open up all available joysticks
206 std::list<SDL_Joystick*> joystick_list;
207 for(int i=0; i < SDL_NumJoysticks(); i++){
208 joystick_list.push_back(SDL_JoystickOpen(i));
209 std::cout << "Opened Joystick: " << SDL_JoystickName(i) << std::endl;
212 //nil bits of the os table for safety reasons
213 luaL_loadstring(L, "os.execute=nil os.exit=nil os.remove=nil os.rename=nil os.tmpname=nil");
214 lua_pcall(L, 0, 0, 0);
216 //load up the main script into the lua environment
217 //and make it all happen
218 int error_main = luaL_loadfile(L, "base/init.lua") ||
219 lua_pcall(L, 0, 0, 0);
221 if ( error_main) { // die a horrible death b/c we can't run without that file
222 cerr << lua_tostring(L, -1) << "\n";
223 lua_pop(L,1);
225 //close any opened joysticks
226 for(std::list<SDL_Joystick*>::iterator it=joystick_list.begin(); it != joystick_list.end(); it++){
227 SDL_JoystickClose(*it);
230 //end it all
231 TTF_Quit();
232 sound_close();
233 clear_images();
234 SDL_Quit();
235 lua_close(L);
236 return 1;
239 //close any opened joysticks
240 for(std::list<SDL_Joystick*>::iterator it=joystick_list.begin(); it != joystick_list.end(); it++){
241 SDL_JoystickClose(*it);
245 //end fonts
246 TTF_Quit();
248 //close audio
249 sound_close();
251 //cleanup image store
252 clear_images();
254 //kill sdl
255 SDL_Quit();
257 //kill lua
258 lua_close(L);
260 return 0;