Fixed some little errors with the drawing functions.
[luagame.git] / main.cpp
blob923cc9c8567ad1a1a3b24c21bf71e9cb97fd0845
1 /*
2 LuaGame Game Execution Environment
3 ----------------------------------
5 Copyright (c)2006-2008 - 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;
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_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");
92 //sound functions
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");
114 //font functions
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");
127 //misc functions
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");
140 //draw functions
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";
187 lua_pop(L,1);
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);
194 //end it all
195 TTF_Quit();
196 sound_close();
197 clear_images();
198 SDL_Quit();
199 lua_close(L);
200 return 1;
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);
209 //end fonts
210 TTF_Quit();
212 //close audio
213 sound_close();
215 //cleanup image store
216 clear_images();
218 //kill sdl
219 SDL_Quit();
221 //kill lua
222 lua_close(L);
224 return 0;