Skip to content
Browse files

world render

  • Loading branch information...
1 parent 7570f20 commit 2e402a735923d7d829216d57c0b1b9588c5d7945 Dmytro Lytovchenko committed Sep 30, 2011
Showing with 192 additions and 9 deletions.
  1. +19 −0 bin/level_01.txt
  2. BIN bin/textures/brick1.png
  3. BIN bin/textures/sky1.png
  4. +14 −4 src/game.cpp
  5. +2 −0 src/game_state.cpp
  6. +1 −1 src/game_state.h
  7. +14 −0 src/player.cpp
  8. +6 −0 src/player.h
  9. +100 −2 src/world.cpp
  10. +36 −2 src/world.h
View
19 bin/level_01.txt
@@ -0,0 +1,19 @@
+# ##################### #####################
+# # #
+# $ A A $$A A A ! #
+# ################ $ ####### ############# ##########
+# $ # ############ #### ### #
+# @ ######## A ## $ # #### #
+# ### ########### ### ########### #### # #
+# ######### $ A $ $ A $ # ### $ ### $ A #
+######################################^^^^^^^^^^^##########^^^^^^^######################^^^^^^^^##############################
+
+* * *
+ only first 9 lines of this file will be read
+ # - the wall
+ @ - player start
+ $ - moneys
+ ! - the goal, exit door
+ ^ - god damn deadly spikes
+ A - monster type A
+* * *
View
BIN bin/textures/brick1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN bin/textures/sky1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
18 src/game.cpp
@@ -12,6 +12,8 @@
#include "game.h"
#include "game_state.h"
+#include <stdio.h>
+
MyGame * MyGame::m_game = NULL;
@@ -45,10 +47,15 @@ bool MyGame::RenderFunc()
m_game->m_state->Render( m_game );
// Draw framerate and time since previous frame
- m_game->m_font->SetColor(0xFFFFFFFF);
-
- m_game->m_font->printf( 5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d",
- m_game->m_hge->Timer_GetDelta(), m_game->m_hge->Timer_GetFPS() );
+ char fps_text[64]; // some safer length so FPS text will fit
+ _snprintf( fps_text, sizeof(fps_text)-1,
+ "dt:%.3f\nFPS:%d",
+ m_game->m_hge->Timer_GetDelta(), m_game->m_hge->Timer_GetFPS()
+ );
+ m_game->m_font->SetColor(ARGB(255,0,0,0)); // black
+ m_game->m_font->printf( 7, 7, HGETEXT_LEFT, fps_text );
+ m_game->m_font->SetColor(ARGB(255,255,255,255)); // white
+ m_game->m_font->printf( 5, 5, HGETEXT_LEFT, fps_text );
m_game->m_hge->Gfx_EndScene();
@@ -61,6 +68,9 @@ bool MyGame::Startup()
{
m_hge = hgeCreate(HGE_VERSION);
+ m_hge->System_SetState(HGE_SHOWSPLASH, false);
+ m_hge->System_SetState(HGE_FPS, 60);
+
m_hge->System_SetState(HGE_LOGFILE, "hgeskel.log");
m_hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
m_hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
View
2 src/game_state.cpp
@@ -474,6 +474,8 @@ bool GameState_Options::Think( MyGame * game )
void GameState_Play::Render( MyGame * game )
{
+ if( m_world ) m_world->Render();
+
// as we are not using GUI in this state, we have to draw cursor ourself
float mx, my;
game->m_hge->Input_GetMousePos( & mx, & my );
View
2 src/game_state.h
@@ -28,7 +28,7 @@ class GameState
// This is called when mygame enters the state. Not used anywhere except the GameState_Play
// class, where it is used to reset game to level 1
// (initial lives count and reset the score, stuff like that)
- void OnEnterState( MyGame * game ) {}
+ virtual void OnEnterState( MyGame * game ) {}
};
View
14 src/player.cpp
@@ -1,2 +1,16 @@
// Player class
// controls keyboard interaction and game rules
+#include "player.h"
+
+hgeVector Player::GetPos()
+{
+ return hgeVector(m_position.x1, m_position.y1);
+}
+
+
+void Player::Die()
+{
+ // TODO: invent the way for player to inform the gamestate or the world about
+ // level game restart or scroll back to allow player to continue
+ m_lives--;
+}
View
6 src/player.h
@@ -3,6 +3,7 @@
#include <hge.h>
#include <hgerect.h>
+#include <hgevector.h>
// Player class
// controls keyboard interaction and game rules
@@ -36,4 +37,9 @@ class Player
float JumpAccel() {
return 50.0f;
}
+
+ hgeVector GetPos();
+
+ // this one should reduce lives count
+ void Die();
};
View
102 src/world.cpp
@@ -7,11 +7,38 @@
#include <fstream>
+#include <hgesprite.h>
+
World::World( Player * plr, const std::string & filename )
: m_player(plr), m_world_width(0), m_world_height(0)
+ , m_camera_pos(0, 0), m_pause_flag(false)
{
LoadWorld( filename );
+
+ // this will not create another HGE, instead we will get the global
+ // unique HGE object which is already started
+ m_hge = hgeCreate( HGE_VERSION );
+
+ m_sprite_brick1 = GetSprite("textures/brick1.png");
+ m_sprite_sky = GetSprite("textures/sky1.png");
+}
+
+
+World::~World()
+{
+ delete m_sprite_brick1;
+ delete m_sprite_sky;
+
+ // to free all textures we might have loaded during this world
+ for( string_to_htexture_map_t::iterator iter = m_tex_map.begin();
+ iter != m_tex_map.end();
+ ++iter )
+ {
+ m_hge->Texture_Free( iter->second );
+ }
+
+ m_hge->Release();
}
@@ -27,7 +54,7 @@ void World::LoadWorld( const std::string & filename )
size_t max_line_width = 0;
char line_buf[MAX_WORLD_WIDTH];
- while( line < WORLD_HEIGHT || f.eof() || f.fail() )
+ while( line < VISIBLE_ROWS || f.eof() || f.fail() )
{
f.getline( line_buf, sizeof line_buf );
size_t line_width = strlen( line_buf );
@@ -40,7 +67,7 @@ void World::LoadWorld( const std::string & filename )
// this can be actually more than 9 if your game can also scroll vertically
// but for now we scroll horizontally only
- m_world_height = WORLD_HEIGHT;
+ m_world_height = VISIBLE_ROWS;
m_world_width = max_line_width;
// resize the world
@@ -68,7 +95,16 @@ void World::LoadWorld( const std::string & filename )
void World::Think()
{
+ if( m_pause_flag ) return;
+
+ float d = m_hge->Timer_GetDelta();
+ m_camera_pos.x += d * 16;
+ // test if player was pushed out of screen fully (to be pushed out partially is allowed)
+ if (m_player->GetPos().x < CELL_BOX_SIZE) {
+ m_player->Die();
+ m_pause_flag = true;
+ }
}
@@ -78,3 +114,65 @@ World::CellType & World::At( uint32_t row, uint32_t col )
_ASSERTE( col >= 0 && col < m_world_width );
return m_world_cells[ row * m_world_width + col ];
}
+
+
+void World::Render()
+{
+ // draw the sky, clearing is not needed anymore
+ //m_hge->Gfx_Clear( ARGB(255, 80, 160, 190 ) );
+ m_sprite_sky->Render( 0, 0 );
+
+ // Assume world does not scroll vertically so we render 9 visible rows wholly,
+ // and carefully calculate visible columns to allow them to seamlessly slide as
+ // the camera moves
+
+ // calculate leftmost visible world column
+ const uint32_t vis_column = (int)m_camera_pos.x / CELL_BOX_SIZE;
+
+ // render one extra column incase if leftmost is partially visible, to avoid gaps
+ // on the right side
+ const uint32_t right_end_column = vis_column + VISIBLE_COLS + 1;
+
+ for( uint32_t r = 0; r < VISIBLE_ROWS; ++r )
+ {
+ for( uint32_t c = vis_column; c <= right_end_column; ++c )
+ {
+ CellType cell_contents = this->At( r, c );
+ switch( cell_contents )
+ {
+ case WORLD_CELL_SOLID:
+ // find position in world and render it
+ m_sprite_brick1->Render(
+ c * CELL_BOX_SIZE - m_camera_pos.x,
+ r * CELL_BOX_SIZE - m_camera_pos.y
+ );
+ break;
+ } // end switch
+ } // end for columns
+ } // end for rows
+}
+
+
+hgeSprite * World::GetSprite( const std::string & name )
+{
+ // attempt to find the requested texture in cache
+ string_to_htexture_map_t::iterator iter = m_tex_map.find( name );
+ HTEXTURE t = 0;
+
+ if( iter != m_tex_map.end() ) {
+ // use texture from cache
+ t = iter->second;
+ } else {
+ // attempt to load the texture
+ t = m_hge->Texture_Load( name.c_str() );
+ }
+
+ // if loading failed
+ if( ! t ) return NULL;
+
+ hgeSprite * spr = new hgeSprite(
+ t, 0.0f, 0.0f,
+ (float)m_hge->Texture_GetWidth(t), (float)m_hge->Texture_GetHeight(t)
+ );
+ return spr;
+}
View
38 src/world.h
@@ -3,6 +3,10 @@
#include <string>
#include <vector>
+#include <map>
+
+#include <hge.h>
+#include <hgevector.h>
// this is for standard integer types like uint32_t (very useful)
#include <stdint.h>
@@ -12,6 +16,7 @@
#include <crtdbg.h>
class Player;
+class hgeSprite;
// World class
// contains the world and its physical rules (collisions, gravity etc)
@@ -39,12 +44,17 @@ class World
uint32_t m_world_width;
uint32_t m_world_height;
+ // pixel size of world cells
+ static const int CELL_BOX_SIZE = 64;
+
// world visible height will be 9 rows
// roughly 600 pixels screen height divided by 64 pix cell size
// this can be actually more than 9 if your game can also scroll vertically, but you
// will be able to draw only currently visible 9 rows, but this also will require
// writing code to auto detect map height in the input file
- static const int WORLD_HEIGHT = 9;
+ static const int VISIBLE_ROWS = 600/CELL_BOX_SIZE;
+
+ static const int VISIBLE_COLS = 800/CELL_BOX_SIZE;
// this also delimits max world width in cells, increase if your world grows wider
// actual world will be as wide as the widest line in your level file
@@ -57,10 +67,28 @@ class World
WORLD_CELL_MONEY = '$'
};
+ // Textures collection, this will be filled on world loading and freed on world end
+ typedef std::map <std::string, HTEXTURE> string_to_htexture_map_t;
+ string_to_htexture_map_t m_tex_map;
+
+ // Leaving this as exercise for the reader - to organize sprites better
+ hgeSprite * m_sprite_brick1;
+ hgeSprite * m_sprite_sky;
+
+ HGE * m_hge;
+
+ // This represents camera position, actually this is top-left corner of the visible
+ // window to the game world. Camera slowly moves right increasing X
+ // making the world "slide" left
+ hgeVector m_camera_pos;
+
+ // is the game running or paused (pause to animate player death for example)
+ bool m_pause_flag;
+
public:
// Loads the default world from the filename provided
World( Player * plr, const std::string & filename );
- virtual ~World() {}
+ virtual ~World();
// Returns a read/writable reference to a world cell. You can read and write to it
// like if it was a real array element
@@ -80,6 +108,12 @@ class World
// This can be modified to return different values depending if player picks up something
// or flips a gravity switch (if you wish)
virtual float GravityAccel() { return 20.0f; }
+
+ // Draws the world in its current state. This function must not call or perform
+ // any other game logic, only drawing
+ virtual void Render();
+
+ hgeSprite * GetSprite( const std::string & name );
};

0 comments on commit 2e402a7

Please sign in to comment.
Something went wrong with that request. Please try again.