5 #include "SDLVideoOut.h"
12 SDLVideoOut
*Game::video
= 0;
16 lua_State
*Game::LUA
= 0;
18 std::map
<std::string
, ObjectClass
> Game::objectTypes
;
23 video
= new SDLVideoOut( 800, 600, 32 );
25 LUA
= lua_open(); //open scripting unit
29 LuaFuncs::Push( LUA
); //and load libraries
40 void Game::Initialize()
45 Game::map
.load( "map.txt" );
46 Game::player
.Teleport( 2,1 );
48 Game::map
.Initialize();
53 void Game::LoadContent()
58 int Game::LoadObjects()
60 lua_State
*L
= lua_newthread( LUA
);
62 int s
= luaL_loadfile( L
, "objects.lua" );
66 s
= lua_pcall( L
, 0, 0, 0 ); //run file
69 std::cout
<< "Error loading Objects:" << std::endl
70 << lua_tostring( L
, -1 ) << std::endl
;
76 std::cout
<< "Error loading file: objects.lua:" << std::endl
;
80 lua_getglobal( L
, "objects" );
84 while( lua_next( L
, -2 ) ) //each object type
87 if( lua_istable( L
, -1 ) )
93 while( lua_next( L
, -2 ) )
96 std::string
key( lua_tostring( L
, -2 ) );
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 );
113 lua_getfield( L
, -1, "length" );
115 temp
.anim_length
= lua_tonumber( L
, -1 );
119 lua_getfield( L
, -1, "time" );
121 temp
.anim_time
= lua_tonumber( L
, -1 );
125 lua_getfield( L
, -1, "loop" );
127 temp
.anim_loop
= (bool)lua_toboolean( L
, -1 );
132 else if( key
== "nuse" )
135 if( lua_isstring( L
, -1 ) )
137 std::string
nuse( lua_tostring( L
, -1 ) );
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
;
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
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
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
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
188 return luaL_error( L
, "Not a function!" );
195 objectTypes
[lua_tostring( L
, -2 )] = temp
;
209 void Game::DeInitialize()
218 while( SDL_PollEvent( &event
) )
220 HandleInput( &event
);
221 Game::player
.HandleInput( &event
);
224 if( Game::player
.isAlive() )
227 Game::player
.Update();
230 if( Game::player
.getLives() == 0 )
231 game_is_running
= false;
236 void Game::HandleInput( SDL_Event
*event
)
240 // exit if the window is closed
242 game_is_running
= false;
245 // check for keypresses
248 // exit if ESCAPE is pressed
249 if (event
->key
.keysym
.sym
== SDLK_ESCAPE
)
250 game_is_running
= false;
258 std::stringstream status
;
263 Game::video
->Clear( Game::video
->MapRGB( 255, 255, 255 ) );
267 if(Game::player
.isAlive())
268 Game::player
.Draw( );
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 );
290 game_is_running
= true;
291 clock_t nextUpdate
= clock();
293 while( game_is_running
)
297 while( clock() > nextUpdate
&& loops
< MAX_FRAMESKIP
)
301 nextUpdate
+= SKIP_FRAMES
;
314 SDLVideoOut
*Game::getVideo()
324 Player
*Game::getPlayer()
326 return &Game::player
;