Initial Commit.
[luagame.git] / main.cpp
blob2f9283a00cf0cf1f9247a62f6e2fc6047dd58ac1
1 //Lua Game Engine Framework
2 //WIP
4 #include <iostream>
5 #include <list>
6 #include "lua.hpp"
7 #include "SDL/SDL.h"
8 #include "globals.h"
9 #include "funcs.h"
10 #include "funcs_input.h"
11 #include "funcs_video.h"
12 #include "funcs_sound.h"
13 #include "funcs_font.h"
15 using namespace std;
17 int main(int argc, char *argv[]){
19 //init Lua
20 lua_State *L = luaL_newstate();
21 luaL_openlibs(L);
23 //init SDL
24 SDL_Init(SDL_INIT_EVERYTHING);
26 //initialize audio
27 SoundEngine::open();
29 //initialize fonts
30 TTF_Init();
32 //load up configuration file and create screen surface
33 screen = load_config(L);
34 if(!screen){ //die
35 cerr << "Error: Cannot create video surface. \n";
36 SDL_Quit();
37 lua_close(L);
38 return 1;
41 //register functions with lua
42 lua_pushcfunction(L, l_getimage);
43 lua_setglobal(L, "get_image");
45 lua_pushcfunction(L, l_releaseimage);
46 lua_setglobal(L, "release_image");
48 lua_pushcfunction(L, l_flip);
49 lua_setglobal(L, "update_screen");
51 lua_pushcfunction(L, l_blit);
52 lua_setglobal(L, "blit");
54 lua_pushcfunction(L, l_blitframe);
55 lua_setglobal(L, "blit_frame");
57 lua_pushcfunction(L, l_mouse_state);
58 lua_setglobal(L, "mouse_state");
60 lua_pushcfunction(L, l_show_cursor);
61 lua_setglobal(L, "show_cursor");
63 lua_pushcfunction(L, l_fill_screen);
64 lua_setglobal(L, "fill_screen");
66 lua_pushcfunction(L, l_delete_image);
67 lua_setglobal(L, "delete_image");
69 lua_pushcfunction(L, l_rotzoom);
70 lua_setglobal(L, "rotzoom");
72 lua_pushcfunction(L, l_playsample);
73 lua_setglobal(L, "play_sample");
75 lua_pushcfunction(L, l_playmusic);
76 lua_setglobal(L, "play_music");
78 lua_pushcfunction(L, l_stopmusic);
79 lua_setglobal(L, "stop_music");
81 lua_pushcfunction(L, l_load_font);
82 lua_setglobal(L, "load_font");
84 lua_pushcfunction(L, l_close_font);
85 lua_setglobal(L, "unload_font");
87 lua_pushcfunction(L, l_font_string_metrics);
88 lua_setglobal(L, "rendered_string_size");
90 lua_pushcfunction(L, l_render_string);
91 lua_setglobal(L, "render_string");
93 lua_pushcfunction(L, l_get_event);
94 lua_setglobal(L, "get_event");
96 lua_pushcfunction(L, l_getticks);
97 lua_setglobal(L, "get_ticks");
99 lua_pushcfunction(L, l_delay);
100 lua_setglobal(L, "delay");
102 lua_pushcfunction(L, l_num_joysticks);
103 lua_setglobal(L, "num_joysticks");
105 //force joystick event handling on
106 SDL_JoystickEventState(SDL_ENABLE);
108 //open up all available joysticks
109 std::list<SDL_Joystick*> joystick_list;
110 for(int i=0; i < SDL_NumJoysticks(); i++){
111 joystick_list.push_back(SDL_JoystickOpen(i));
112 std::cout << "Opened Joystick: " << SDL_JoystickName(i) << std::endl;
115 //load up the main script into the lua environment
116 //and make it all happen
117 int error_main = luaL_loadfile(L, "base/init.lua") ||
118 lua_pcall(L, 0, 0, 0);
120 if ( error_main) { // die a horrible death b/c we can't run without that file
121 cerr << lua_tostring(L, -1) << "\n";
122 lua_pop(L,1);
124 //close any opened joysticks
125 for(std::list<SDL_Joystick*>::iterator it=joystick_list.begin(); it != joystick_list.end(); it++){
126 SDL_JoystickClose(*it);
129 //end it all
130 TTF_Quit();
131 SoundEngine::close();
132 clear_images();
133 SDL_Quit();
134 lua_close(L);
135 return 1;
138 //close any opened joysticks
139 for(std::list<SDL_Joystick*>::iterator it=joystick_list.begin(); it != joystick_list.end(); it++){
140 SDL_JoystickClose(*it);
144 //end fonts
145 TTF_Quit();
147 //close audio
148 SoundEngine::close();
150 //cleanup image store
151 clear_images();
153 //kill sdl
154 SDL_Quit();
156 //kill lua
157 lua_close(L);
159 return 0;