objekt hihi
[sdlbotor.git] / Game.cpp
blobcba424bff74e8475642f197de0581c7d9b5f8a27
1 #include <time.h>
2 #include <iostream>
3 #include <sstream>
4 #include <assert.h>
5 #include "SDLVideoOut.h"
6 #include "Game.h"
7 #include "luafuncs.h"
9 namespace botor
12 SDLVideoOut *Game::video = 0;
13 Map Game::map;
14 Player Game::player;
16 lua_State *Game::LUA = 0;
18 std::map<std::string, ObjectClass> Game::objectTypes;
21 Game::Game()
23 video = new SDLVideoOut( 800, 600, 32 );
25 LUA = lua_open(); //open scripting unit
27 luaopen_base( LUA );
29 LuaFuncs::Push( LUA ); //and load libraries
33 Game::~Game()
35 lua_close( LUA );
37 delete video;
40 void Game::Initialize()
45 Game::map.load( "map.txt" );
46 Game::player.Teleport( 2,1 );
48 Game::map.Initialize();
53 void Game::LoadContent()
55 LoadObjects();
58 int Game::LoadObjects()
60 lua_State *L = lua_newthread( LUA );
62 int s = luaL_loadfile( L, "objects.lua" );
64 if( !s )
66 s = lua_pcall( L, 0, 0, 0 ); //run file
67 if( s )
69 std::cout << "Error loading Objects:" << std::endl
70 << lua_tostring( L, -1 ) << std::endl;
71 exit(1);
74 else
76 std::cout << "Error loading file: objects.lua:" << std::endl;
77 exit(1);
80 lua_getglobal( L, "objects" );
82 lua_pushnil( L );
84 while( lua_next( L, -2 ) ) //each object type
87 if( lua_istable( L, -1 ) )
89 ObjectClass temp;
91 lua_pushnil( L );
93 while( lua_next( L, -2 ) )
96 std::string key( lua_tostring( L, -2 ) );
98 if( key == "tile" )
100 temp.graphic = ObjectClass::GRA_TILE;
101 temp.tile_id = lua_tonumber( L, -1 );
103 else if( key == "animation" )
105 temp.graphic = ObjectClass::GRA_ANIMATION;
107 lua_getfield( L, -1 ,"id" );
109 temp.tile_id = lua_tonumber( L, -1 );
111 lua_pop( L, 1 );
113 lua_getfield( L, -1, "length" );
115 temp.anim_length = lua_tonumber( L, -1 );
117 lua_pop( L, 1 );
119 lua_getfield( L, -1, "time" );
121 temp.anim_time = lua_tonumber( L, -1 );
123 lua_pop( L, 1 );
125 lua_getfield( L, -1, "loop" );
127 temp.anim_loop = (bool)lua_toboolean( L, -1 );
129 lua_pop( L, 1 );
132 else if( key == "nuse" )
135 if( lua_isstring( L, -1 ) )
137 std::string nuse( lua_tostring( L, -1 ) );
139 if( nuse == "Once" )
140 temp.nUse = ObjectClass::NUseOnce;
141 else if( nuse == "Multi" )
142 temp.nUse = ObjectClass::NUseMulti;
143 else if( nuse == "Drop" )
144 temp.nUse = ObjectClass::NUseDrop;
146 else
147 return luaL_error( L, "Not a string!" );
150 else if( key == "Activation" )
152 if( lua_isfunction( L, -1 ) )
154 temp.OnActivation = luaL_ref( L, LUA_REGISTRYINDEX );
155 lua_pushnil( L ); // so the pop doesn't get confused
157 else
158 return luaL_error( L, "Not a function!" );
160 else if( key == "PlayerOnTile" )
162 if( lua_isfunction( L, -1 ) )
164 temp.OnPlayerOnTile = luaL_ref( L, LUA_REGISTRYINDEX );
165 lua_pushnil( L ); // so the pop doesn't get confused
167 else
168 return luaL_error( L, "Not a function!" );
170 else if( key == "Pickup" )
172 if( lua_isfunction( L, -1 ) )
174 temp.OnPickup = luaL_ref( L, LUA_REGISTRYINDEX );
175 lua_pushnil( L ); // so the pop doesn't get confused
177 else
178 return luaL_error( L, "Not a function!" );
180 else if( key == "Drop" )
182 if( lua_isfunction( L, -1 ) )
184 temp.OnDrop = luaL_ref( L, LUA_REGISTRYINDEX );
185 lua_pushnil( L ); // so the pop doesn't get confused
187 else
188 return luaL_error( L, "Not a function!" );
191 lua_pop( L, 1 );
195 objectTypes[lua_tostring( L, -2 )] = temp;
200 lua_pop( L, 1 );
204 return 0;
209 void Game::DeInitialize()
211 Game::map.unload();
214 void Game::Update()
216 SDL_Event event;
218 while( SDL_PollEvent( &event ) )
220 HandleInput( &event );
221 Game::player.HandleInput( &event );
224 if( Game::player.isAlive() )
226 Game::map.Update();
227 Game::player.Update();
230 if( Game::player.getLives() == 0 )
231 game_is_running = false;
236 void Game::HandleInput( SDL_Event *event )
238 switch (event->type)
240 // exit if the window is closed
241 case SDL_QUIT:
242 game_is_running = false;
243 break;
245 // check for keypresses
246 case SDL_KEYDOWN:
248 // exit if ESCAPE is pressed
249 if (event->key.keysym.sym == SDLK_ESCAPE)
250 game_is_running = false;
251 break;
253 } // end switch
256 void Game::Draw()
258 std::stringstream status;
260 if( !Game::video )
261 return;
263 Game::video->Clear( Game::video->MapRGB( 255, 255, 255 ) );
265 Game::map.Draw( );
267 if(Game::player.isAlive())
268 Game::player.Draw( );
269 else
270 Game::video->DrawString( "You are dead! Press [Enter] to continue!", 0, 0, 255, 0, 0 );
272 status << "Leben: " << Game::player.getLives() << " Pos: " << (int)Game::player.getMapX() << "/" << (int)Game::player.getMapY();
274 Game::video->DrawString( status.str().c_str(), 0, 20, 0, 0, 0 );
277 Game::video->Flip();
281 void Game::run()
283 int loops;
285 LoadContent();
288 Initialize();
290 game_is_running = true;
291 clock_t nextUpdate = clock();
293 while( game_is_running )
295 loops = 0;
297 while( clock() > nextUpdate && loops < MAX_FRAMESKIP )
299 Update();
301 nextUpdate += SKIP_FRAMES;
303 loops++;
306 Draw();
310 DeInitialize();
314 SDLVideoOut *Game::getVideo()
316 return Game::video;
319 Map *Game::getMap()
321 return &Game::map;
324 Player *Game::getPlayer()
326 return &Game::player;