Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

wtfpl public license

  • Loading branch information...
commit cf03aca5f289c1a6195bff652299d205458dabd6 1 parent 4c53809
Dmytro Lytovchenko authored
6 CMakeLists.txt
View
@@ -3,6 +3,12 @@
# This is minimal game skeleton in HGE based on HGE_TUTORIAL 06
# to adapt this to your new game, search and replace HGESKEL with your project name
#
+# This program is free software. It comes without any warranty, to
+# the extent permitted by applicable law. You can redistribute it
+# and/or modify it under the terms of the Do What The Fuck You Want
+# To Public License, Version 2, as published by Sam Hocevar. See
+# http://sam.zoy.org/wtfpl/COPYING for more details.
+#
#-----------------------------------------------------------------------
project( HGESKEL )
cmake_minimum_required( VERSION 2.8 )
16 LICENSE.txt
View
@@ -0,0 +1,16 @@
+http://sam.zoy.org/wtfpl/COPYING
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ Version 2, December 2004
+
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
+
50 src/creature.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// base Creature class
// represents a walking or stationary creature which is able to kill player
// on touch.
@@ -9,44 +15,44 @@
WorldObject::WorldObject( World * owner, float x, float y ): m_world(owner)
{
- m_box.Set( x, y, x + World::CELL_BOX_SIZE - 1.0f, y + World::CELL_BOX_SIZE - 1.0f );
+ m_box.Set( x, y, x + World::CELL_BOX_SIZE - 1.0f, y + World::CELL_BOX_SIZE - 1.0f );
}
void WorldObject::Render()
{
- hgeSprite * spr = GetSprite();
- if( ! spr ) return; // can't draw a NULL sprite
-
- hgeRect box( m_box.x1 - m_world->m_camera_pos.x,
- m_box.y1 - m_world->m_camera_pos.y,
- m_box.x2 - m_world->m_camera_pos.x,
- m_box.y2 - m_world->m_camera_pos.y );
-
- spr->Render4V( box.x1, box.y1,
- box.x2, box.y1,
- box.x2, box.y2,
- box.x1, box.y2 );
+ hgeSprite * spr = GetSprite();
+ if( ! spr ) return; // can't draw a NULL sprite
+
+ hgeRect box( m_box.x1 - m_world->m_camera_pos.x,
+ m_box.y1 - m_world->m_camera_pos.y,
+ m_box.x2 - m_world->m_camera_pos.x,
+ m_box.y2 - m_world->m_camera_pos.y );
+
+ spr->Render4V( box.x1, box.y1,
+ box.x2, box.y1,
+ box.x2, box.y2,
+ box.x1, box.y2 );
}
WorldObject_Money::WorldObject_Money( World * owner, float x, float y ) : WorldObject( owner, x, y )
{
- m_sprite[0] = owner->m_sprite_manager.GetSprite( "textures/coin1.png" );
- m_sprite[1] = owner->m_sprite_manager.GetSprite( "textures/coin2.png" );
+ m_sprite[0] = owner->m_sprite_manager.GetSprite( "textures/coin1.png" );
+ m_sprite[1] = owner->m_sprite_manager.GetSprite( "textures/coin2.png" );
}
WorldObject_Money::~WorldObject_Money()
{
- delete m_sprite[0];
- delete m_sprite[1];
+ delete m_sprite[0];
+ delete m_sprite[1];
}
hgeSprite * WorldObject_Money::GetSprite()
{
- uint32_t milliseconds = GetTickCount();
- // we want frames to change every 250 msec from 0 to 1
- // total of 2 frames, hence the modulo of 2
- uint32_t f = (milliseconds / 250) % 2;
+ uint32_t milliseconds = GetTickCount();
+ // we want frames to change every 250 msec from 0 to 1
+ // total of 2 frames, hence the modulo of 2
+ uint32_t f = (milliseconds / 250) % 2;
- return m_sprite[f];
+ return m_sprite[f];
}
26 src/creature.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
@@ -16,29 +22,29 @@ class World;
class WorldObject
{
protected:
- World * m_world;
+ World * m_world;
public:
- hgeRect m_box;
+ hgeRect m_box;
- WorldObject( World * owner, float x, float y );
- virtual ~WorldObject() {}
+ WorldObject( World * owner, float x, float y );
+ virtual ~WorldObject() {}
- virtual hgeSprite * GetSprite() { return NULL; }
- void Render();
+ virtual hgeSprite * GetSprite() { return NULL; }
+ void Render();
};
class WorldObject_Money: public virtual WorldObject
{
protected:
- hgeSprite * m_sprite[2];
+ hgeSprite * m_sprite[2];
public:
- WorldObject_Money(World * owner, float x, float y );
- virtual ~WorldObject_Money();
+ WorldObject_Money(World * owner, float x, float y );
+ virtual ~WorldObject_Money();
- virtual hgeSprite * GetSprite();
+ virtual hgeSprite * GetSprite();
};
154 src/game.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
/*
** Based on: hge_tut06 - Creating menus
** Haaf's Game Engine 1.8 -- Copyright (C) 2003-2007, Relish Games http://hge.relishgames.com
@@ -18,51 +24,51 @@ MyGame * MyGame::m_game = NULL;
MyGame::MyGame()
- : m_hge(NULL), m_font(NULL), m_mouse_cursor_sprite(NULL), m_state(NULL)
- , m_sound_enabled(true)
+ : m_hge(NULL), m_font(NULL), m_mouse_cursor_sprite(NULL), m_state(NULL)
+ , m_sound_enabled(true)
{
- // remember pointer to 'this' in globally visible variable so that everyone
- // else can reach it (not very good example of software design, but this simplifies a lot)
- m_game = this;
+ // remember pointer to 'this' in globally visible variable so that everyone
+ // else can reach it (not very good example of software design, but this simplifies a lot)
+ m_game = this;
- // do nothing else here, unless it can run before HGE is started
+ // do nothing else here, unless it can run before HGE is started
}
bool MyGame::FrameFunc()
{
- // passing control to the current game state, so that game reacts to events
- // differently in different states
- return m_game->m_state->Think( m_game );
- // do not add more code here, all the action happens in GameState's child classes
+ // passing control to the current game state, so that game reacts to events
+ // differently in different states
+ return m_game->m_state->Think( m_game );
+ // do not add more code here, all the action happens in GameState's child classes
}
bool MyGame::RenderFunc()
{
- // letting the current game state to take care of rendering (different render for
- // main menu, for game, and for game over screen)
- m_game->m_hge->Gfx_BeginScene();
-
- m_game->m_state->Render( m_game );
-
- // Draw framerate and time since previous frame
- 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()
- );
-
- // print with black shadow
- 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 );
+ // letting the current game state to take care of rendering (different render for
+ // main menu, for game, and for game over screen)
+ m_game->m_hge->Gfx_BeginScene();
+
+ m_game->m_state->Render( m_game );
+
+ // Draw framerate and time since previous frame
+ 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()
+ );
+
+ // print with black shadow
+ 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();
+ m_game->m_hge->Gfx_EndScene();
- // do not add more code here, all the action happens in GameState's child classes
- return false;
+ // do not add more code here, all the action happens in GameState's child classes
+ return false;
}
@@ -70,8 +76,8 @@ 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_SHOWSPLASH, false);
+ m_hge->System_SetState(HGE_FPS, 60);
m_hge->System_SetState(HGE_LOGFILE, "hgeskel.log");
m_hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
@@ -110,85 +116,85 @@ bool MyGame::Startup()
m_background_quad.v[i].col=ARGB(0xFF, 0xFF, 0xFF, 0xFF);
}
- m_background_quad.v[0].x=0;
- m_background_quad.v[0].y=0;
+ m_background_quad.v[0].x=0;
+ m_background_quad.v[0].y=0;
- m_background_quad.v[1].x=800;
- m_background_quad.v[1].y=0;
+ m_background_quad.v[1].x=800;
+ m_background_quad.v[1].y=0;
- m_background_quad.v[2].x=800;
- m_background_quad.v[2].y=600;
+ m_background_quad.v[2].x=800;
+ m_background_quad.v[2].y=600;
- m_background_quad.v[3].x=0;
- m_background_quad.v[3].y=600;
+ m_background_quad.v[3].x=0;
+ m_background_quad.v[3].y=600;
- // Load the font, create the cursor sprite
- m_font = new hgeFont("font1.fnt");
- m_mouse_cursor_sprite = new hgeSprite(m_mouse_cursor_tex,0,0,32,32);
+ // Load the font, create the cursor sprite
+ m_font = new hgeFont("font1.fnt");
+ m_mouse_cursor_sprite = new hgeSprite(m_mouse_cursor_tex,0,0,32,32);
- m_state_options = new GameState_Options( this );
- m_state_mainmenu = new GameState_MainMenu( this );
- m_state_instructions = new GameState_Instructions( this );
- m_state_credits = new GameState_Credits( this );
- m_state_play = new GameState_Play();
+ m_state_options = new GameState_Options( this );
+ m_state_mainmenu = new GameState_MainMenu( this );
+ m_state_instructions = new GameState_Instructions( this );
+ m_state_credits = new GameState_Credits( this );
+ m_state_play = new GameState_Play();
- ShowMainMenuScreen();
+ ShowMainMenuScreen();
- return true; // all is fine
+ return true; // all is fine
}
void MyGame::Shutdown()
{
- delete m_state_options;
- delete m_state_mainmenu;
- delete m_state_instructions;
- delete m_state_credits;
- delete m_state_play;
-
- // Delete created objects and free loaded resources
- delete m_font;
- delete m_mouse_cursor_sprite;
-
- m_hge->Effect_Free(m_click_sound);
- m_hge->Texture_Free(m_mouse_cursor_tex);
- m_hge->Texture_Free(m_background_quad.tex);
-
- // Clean up and shutdown
- m_hge->System_Shutdown();
- m_hge->Release();
+ delete m_state_options;
+ delete m_state_mainmenu;
+ delete m_state_instructions;
+ delete m_state_credits;
+ delete m_state_play;
+
+ // Delete created objects and free loaded resources
+ delete m_font;
+ delete m_mouse_cursor_sprite;
+
+ m_hge->Effect_Free(m_click_sound);
+ m_hge->Texture_Free(m_mouse_cursor_tex);
+ m_hge->Texture_Free(m_background_quad.tex);
+
+ // Clean up and shutdown
+ m_hge->System_Shutdown();
+ m_hge->Release();
}
void MyGame::ShowMainMenuScreen()
{
- m_state = m_state_mainmenu;
+ m_state = m_state_mainmenu;
}
void MyGame::ShowOptionsScreen()
{
- m_state = m_state_options;
+ m_state = m_state_options;
}
void MyGame::ShowInstructionsScreen()
{
- m_state = m_state_instructions;
+ m_state = m_state_instructions;
}
void MyGame::ShowCreditsScreen()
{
- m_state = m_state_credits;
+ m_state = m_state_credits;
}
void MyGame::ShowPlayScreen()
{
- m_state = m_state_play;
- m_state_play->OnEnterState( this );
+ m_state = m_state_play;
+ m_state_play->OnEnterState( this );
}
66 src/game.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
@@ -16,43 +22,43 @@ class GameState;
class MyGame
{
public:
- // Globally visible pointer to this class (not a brilliant example of
- // software design, but it simplifies things)
- static MyGame * m_game;
+ // Globally visible pointer to this class (not a brilliant example of
+ // software design, but it simplifies things)
+ static MyGame * m_game;
- // Pointer to the HGE interface.
+ // Pointer to the HGE interface.
HGE * m_hge;
- // flag set to 0 or 1 to enable sound effects
- int m_sound_enabled;
+ // flag set to 0 or 1 to enable sound effects
+ int m_sound_enabled;
// Some resource handles
- HEFFECT m_click_sound;
- HTEXTURE m_mouse_cursor_tex;
+ HEFFECT m_click_sound;
+ HTEXTURE m_mouse_cursor_tex;
- // menu background quad. As well we can use it for sky during the gameplay
- hgeQuad m_background_quad;
+ // menu background quad. As well we can use it for sky during the gameplay
+ hgeQuad m_background_quad;
// Pointers to the HGE objects we will use
- hgeFont * m_font;
- hgeSprite * m_mouse_cursor_sprite;
+ hgeFont * m_font;
+ hgeSprite * m_mouse_cursor_sprite;
- // This will change the way game renders and reacts to user input
- // We will set this to different states when user starts or ends playing
- GameState * m_state;
+ // This will change the way game renders and reacts to user input
+ // We will set this to different states when user starts or ends playing
+ GameState * m_state;
- // premade game states to change to. Since its bad idea to delete them on the
- // fly while state change code is called from the running game state, we just
- // premake states and store them here.
- GameState * m_state_options; // shows options menu
- GameState * m_state_mainmenu; // shows main menu
- GameState * m_state_instructions; // shows 2 pages of instructions
- GameState * m_state_credits; // shows scrolling credits text
- GameState * m_state_play; // shows game world and allows to play
+ // premade game states to change to. Since its bad idea to delete them on the
+ // fly while state change code is called from the running game state, we just
+ // premake states and store them here.
+ GameState * m_state_options; // shows options menu
+ GameState * m_state_mainmenu; // shows main menu
+ GameState * m_state_instructions; // shows 2 pages of instructions
+ GameState * m_state_credits; // shows scrolling credits text
+ GameState * m_state_play; // shows game world and allows to play
public:
- MyGame();
+ MyGame();
static bool FrameFunc();
static bool RenderFunc();
@@ -64,10 +70,10 @@ class MyGame
// this is called to end the world, when game is about to quit
void Shutdown();
- // switches game state to a new state
- void ShowMainMenuScreen();
- void ShowOptionsScreen();
- void ShowInstructionsScreen();
- void ShowCreditsScreen();
- void ShowPlayScreen();
+ // switches game state to a new state
+ void ShowMainMenuScreen();
+ void ShowOptionsScreen();
+ void ShowInstructionsScreen();
+ void ShowCreditsScreen();
+ void ShowPlayScreen();
};
410 src/game_state.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// GameState class
// Represents current state of the application - is it in menu screen, or in the
// game world, or at the game over screen (showing high scores screen).
@@ -12,48 +18,48 @@
#include <math.h>
char * _opt_sound_text[2] = {
- "Sound: Disabled",
- "Sound: Enabled"
+ "Sound: Disabled",
+ "Sound: Enabled"
};
// array of strings of unknown length (NULL marks the end).
// You may delete few lines or add new lines before NULL if screen height allows.
const char * _page1_text[] = {
- "This is My Game of Doom",
- "You are to guide the character to the last",
- "sanctuary in this galaxy. or you die. That's about it. Yea.",
- "You will have to avoid obstacles, and kill stuff.",
- NULL
+ "This is My Game of Doom",
+ "You are to guide the character to the last",
+ "sanctuary in this galaxy. or you die. That's about it. Yea.",
+ "You will have to avoid obstacles, and kill stuff.",
+ NULL
};
// arrayof strings of unknown length (NULL marks the end)
// You may delete few lines or add new lines before NULL if screen height allows.
const char * _page2_text[] = {
- "You can move with A/D/arrows, jump with W or space",
- "and throw deadly rocks with mouse button",
- "Game slowly scrolls with time, if you get caught",
- "between the end of screen and the wall, you will die.",
- NULL
+ "You can move with A/D/arrows, jump with W or space",
+ "and throw deadly rocks with mouse button",
+ "Game slowly scrolls with time, if you get caught",
+ "between the end of screen and the wall, you will die.",
+ NULL
};
// array of strings of unknown length (NULL marks the end)
// You may delete few lines or add new lines before NULL if screen height allows.
const char * _credits_text[] = {
- "This brilliant masterpiece",
- "was brought to you by",
- "-",
- "big boss Haaf, the master and",
- "the maker of HGE Engine",
- "-",
- "and your humble slave, kvakvs,",
- "who made this ugly game",
- "***",
- "tremble, mortals, before it",
- "and feel free to edit to your liking",
- NULL
+ "This brilliant masterpiece",
+ "was brought to you by",
+ "-",
+ "big boss Haaf, the master and",
+ "the maker of HGE Engine",
+ "-",
+ "and your humble slave, kvakvs,",
+ "who made this ugly game",
+ "***",
+ "tremble, mortals, before it",
+ "and feel free to edit to your liking",
+ NULL
};
@@ -63,8 +69,8 @@ void GameState_MainMenu::Render( MyGame * game )
game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
m_gui->Render();
- // we are using GUI in this state, so it will draw the mouse cursor itself
- // no need to bother
+ // we are using GUI in this state, so it will draw the mouse cursor itself
+ // no need to bother
}
@@ -79,16 +85,16 @@ void AnimateBackground( float dt, MyGame * game )
ty = 50*sinf(t/60);
game->m_background_quad.v[0].tx=tx;
- game->m_background_quad.v[0].ty=ty;
+ game->m_background_quad.v[0].ty=ty;
game->m_background_quad.v[1].tx=tx+800/64;
- game->m_background_quad.v[1].ty=ty;
+ game->m_background_quad.v[1].ty=ty;
game->m_background_quad.v[2].tx=tx+800/64;
- game->m_background_quad.v[2].ty=ty+600/64;
+ game->m_background_quad.v[2].ty=ty+600/64;
game->m_background_quad.v[3].tx=tx;
- game->m_background_quad.v[3].ty=ty+600/64;
+ game->m_background_quad.v[3].ty=ty+600/64;
}
@@ -100,9 +106,9 @@ bool GameState_MainMenu::Think( MyGame * game )
// If ESCAPE was pressed, tell the GUI to finish
if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
- lastid = MAINMENU_ELEMENT_EXIT;
- m_gui->Leave();
- }
+ lastid = MAINMENU_ELEMENT_EXIT;
+ m_gui->Leave();
+ }
// We update the GUI and take an action if
// one of the menu items was selected
@@ -112,34 +118,34 @@ bool GameState_MainMenu::Think( MyGame * game )
switch(lastid)
{
case MAINMENU_ELEMENT_PLAY:
- game->ShowPlayScreen();
- m_gui->Enter();
+ game->ShowPlayScreen();
+ m_gui->Enter();
break;
- case MAINMENU_ELEMENT_OPTIONS:
- game->ShowOptionsScreen();
- m_gui->Enter();
+ case MAINMENU_ELEMENT_OPTIONS:
+ game->ShowOptionsScreen();
+ m_gui->Enter();
break;
- case MAINMENU_ELEMENT_INSTRUCTIONS:
- game->ShowInstructionsScreen();
- m_gui->Enter();
+ case MAINMENU_ELEMENT_INSTRUCTIONS:
+ game->ShowInstructionsScreen();
+ m_gui->Enter();
break;
- case MAINMENU_ELEMENT_CREDITS:
- game->ShowCreditsScreen();
- m_gui->Enter();
+ case MAINMENU_ELEMENT_CREDITS:
+ game->ShowCreditsScreen();
+ m_gui->Enter();
break;
case MAINMENU_ELEMENT_EXIT:
- return true;
+ return true;
}
} else if(id) {
lastid = id;
- m_gui->Leave();
+ m_gui->Leave();
}
- AnimateBackground( dt, game );
+ AnimateBackground( dt, game );
return false;
}
@@ -152,15 +158,15 @@ GameState_MainMenu::GameState_MainMenu( MyGame * game )
m_gui = new hgeGUI();
m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_PLAY, game->m_font, game->m_click_sound,
- 400, MAINMENU_TOP_ITEM_Y, 0.0f, "Play"));
+ 400, MAINMENU_TOP_ITEM_Y, 0.0f, "Play"));
m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_OPTIONS, game->m_font, game->m_click_sound,
- 400, MAINMENU_TOP_ITEM_Y+MAINMENU_ITEM_HEIGHT, 0.1f, "Options"));
+ 400, MAINMENU_TOP_ITEM_Y+MAINMENU_ITEM_HEIGHT, 0.1f, "Options"));
m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_INSTRUCTIONS, game->m_font, game->m_click_sound,
- 400, MAINMENU_TOP_ITEM_Y+2*MAINMENU_ITEM_HEIGHT, 0.2f, "Instructions"));
+ 400, MAINMENU_TOP_ITEM_Y+2*MAINMENU_ITEM_HEIGHT, 0.2f, "Instructions"));
m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_CREDITS, game->m_font, game->m_click_sound,
- 400, MAINMENU_TOP_ITEM_Y+3*MAINMENU_ITEM_HEIGHT, 0.3f, "Credits"));
+ 400, MAINMENU_TOP_ITEM_Y+3*MAINMENU_ITEM_HEIGHT, 0.3f, "Credits"));
m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_EXIT, game->m_font, game->m_click_sound,
- 400, MAINMENU_TOP_ITEM_Y+4*MAINMENU_ITEM_HEIGHT, 0.4f, "Exit"));
+ 400, MAINMENU_TOP_ITEM_Y+4*MAINMENU_ITEM_HEIGHT, 0.4f, "Exit"));
m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
m_gui->SetCursor( game->m_mouse_cursor_sprite );
@@ -171,7 +177,7 @@ GameState_MainMenu::GameState_MainMenu( MyGame * game )
GameState_MainMenu::~GameState_MainMenu()
{
- delete m_gui;
+ delete m_gui;
}
@@ -180,22 +186,22 @@ void GameState_Instructions::Render( MyGame * game )
game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
m_gui->Render();
- // we are using GUI in this state, so it will draw the mouse cursor itself
- // no need to bother
+ // we are using GUI in this state, so it will draw the mouse cursor itself
+ // no need to bother
}
bool GameState_Instructions::Think( MyGame * game )
{
- float dt = game->m_hge->Timer_GetDelta();
+ float dt = game->m_hge->Timer_GetDelta();
int id;
static int lastid = INSTRUC_ELEMENT_NONE_SELECTED;
// If ESCAPE was pressed, tell the GUI to finish
if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
- lastid = INSTRUC_ELEMENT_EXIT;
- m_gui->Leave();
- }
+ lastid = INSTRUC_ELEMENT_EXIT;
+ m_gui->Leave();
+ }
// We update the GUI and take an action if
// one of the menu items was selected
@@ -205,31 +211,31 @@ bool GameState_Instructions::Think( MyGame * game )
switch(lastid)
{
case INSTRUC_ELEMENT_PAGE1:
- PopulateInstructionsPage( game, _page1_text );
- m_gui->SetFocus(INSTRUC_ELEMENT_PAGE1);
+ PopulateInstructionsPage( game, _page1_text );
+ m_gui->SetFocus(INSTRUC_ELEMENT_PAGE1);
m_gui->Enter();
break;
- case INSTRUC_ELEMENT_PAGE2:
- PopulateInstructionsPage( game, _page2_text );
- m_gui->SetFocus(INSTRUC_ELEMENT_PAGE2);
+ case INSTRUC_ELEMENT_PAGE2:
+ PopulateInstructionsPage( game, _page2_text );
+ m_gui->SetFocus(INSTRUC_ELEMENT_PAGE2);
m_gui->Enter();
break;
case INSTRUC_ELEMENT_EXIT:
- game->ShowMainMenuScreen();
-// m_gui->SetFocus(INSTRUC_ELEMENT_EXIT);
-// m_gui->Enter();
+ game->ShowMainMenuScreen();
+// m_gui->SetFocus(INSTRUC_ELEMENT_EXIT);
+// m_gui->Enter();
break;
}
} else if(id) {
lastid = id;
- m_gui->Leave();
+ m_gui->Leave();
}
- AnimateBackground( dt, game );
+ AnimateBackground( dt, game );
- return false;
+ return false;
}
@@ -243,8 +249,8 @@ GameState_Instructions::GameState_Instructions( MyGame * game )
m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_PAGE2, game->m_font, game->m_click_sound,400,550,0.1f,"Page 2"));
m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_EXIT, game->m_font, game->m_click_sound,700,550,0.2f,"Done"));
- PopulateInstructionsPage( game, _page1_text );
-
+ PopulateInstructionsPage( game, _page1_text );
+
m_gui->SetNavMode(HGEGUI_LEFTRIGHT | HGEGUI_CYCLED);
m_gui->SetCursor( game->m_mouse_cursor_sprite );
m_gui->SetFocus(INSTRUC_ELEMENT_PAGE1);
@@ -254,25 +260,25 @@ GameState_Instructions::GameState_Instructions( MyGame * game )
void GameState_Instructions::PopulateInstructionsPage( MyGame * game, const char * text[] )
{
- // for each row in instructions text
- for( int textrow = 0; text[textrow]; textrow++ )
- {
- // attempt to retrieve and delete old text control
- if (m_gui->GetCtrl( 100+textrow )) {
- m_gui->DelCtrl( 100+textrow );
- }
-
- // add new text control with line from instructions
- hgeGUIText * c1 = new hgeGUIText(100+textrow, 0, 150.0f+40.0f*textrow, 800, 40, game->m_font);
- c1->SetMode( HGETEXT_CENTER );
- c1->SetText( text[textrow] );
- m_gui->AddCtrl(c1);
- }
+ // for each row in instructions text
+ for( int textrow = 0; text[textrow]; textrow++ )
+ {
+ // attempt to retrieve and delete old text control
+ if (m_gui->GetCtrl( 100+textrow )) {
+ m_gui->DelCtrl( 100+textrow );
+ }
+
+ // add new text control with line from instructions
+ hgeGUIText * c1 = new hgeGUIText(100+textrow, 0, 150.0f+40.0f*textrow, 800, 40, game->m_font);
+ c1->SetMode( HGETEXT_CENTER );
+ c1->SetText( text[textrow] );
+ m_gui->AddCtrl(c1);
+ }
}
GameState_Instructions::~GameState_Instructions()
{
- delete m_gui;
+ delete m_gui;
}
@@ -282,9 +288,9 @@ GameState_Credits::GameState_Credits( MyGame * game )
m_gui = new hgeGUI();
m_gui->AddCtrl(new hgeGUIMenuItem(CREDITS_ELEMENT_EXIT, game->m_font, game->m_click_sound,700,550,0.0f,"Done"));
- m_draw_credits_y = 600; // bottom of screen, will decrease to slide up
- //PopulateCredits( game );
-
+ m_draw_credits_y = 600; // bottom of screen, will decrease to slide up
+ //PopulateCredits( game );
+
//m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
m_gui->SetCursor( game->m_mouse_cursor_sprite );
//m_gui->SetFocus(1);
@@ -293,7 +299,7 @@ GameState_Credits::GameState_Credits( MyGame * game )
GameState_Credits::~GameState_Credits()
{
- delete m_gui;
+ delete m_gui;
}
@@ -301,27 +307,27 @@ GameState_Credits::~GameState_Credits()
// This shows how to fill array of hgeGUIText's with values
void GameState_Credits::PopulateCredits( MyGame * game )
{
- m_credits_row_count = 0;
-
- // for each row in credits text
- for( int textrow = 0; _credits_text[textrow]; textrow++ )
- {
- // attempt to retrieve and delete old text control
- if (m_gui->GetCtrl( 100+textrow )) {
- m_gui->DelCtrl( 100+textrow );
- }
-
- // add new text control with line from instructions
- // starting from under bottom of screen (600) and going deeper
- hgeGUIText * c1 = new hgeGUIText(100+textrow,
- 0, 600.0f + 40.0f*textrow,
- 800, 40, game->m_font);
-
- c1->SetMode( HGETEXT_CENTER );
- c1->SetText( _credits_text[textrow] );
- m_gui->AddCtrl(c1);
- m_credits_row_count++;
- }
+ m_credits_row_count = 0;
+
+ // for each row in credits text
+ for( int textrow = 0; _credits_text[textrow]; textrow++ )
+ {
+ // attempt to retrieve and delete old text control
+ if (m_gui->GetCtrl( 100+textrow )) {
+ m_gui->DelCtrl( 100+textrow );
+ }
+
+ // add new text control with line from instructions
+ // starting from under bottom of screen (600) and going deeper
+ hgeGUIText * c1 = new hgeGUIText(100+textrow,
+ 0, 600.0f + 40.0f*textrow,
+ 800, 40, game->m_font);
+
+ c1->SetMode( HGETEXT_CENTER );
+ c1->SetText( _credits_text[textrow] );
+ m_gui->AddCtrl(c1);
+ m_credits_row_count++;
+ }
}
*/
@@ -331,29 +337,29 @@ void GameState_Credits::Render( MyGame * game )
game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
m_gui->Render();
- game->m_font->SetColor(0xFFFFFFFF);
- for( int textrow = 0; _credits_text[textrow]; textrow++ )
- {
- game->m_font->printf( 400, m_draw_credits_y + CREDITS_ROW_HEIGHT * textrow,
- HGETEXT_CENTER, _credits_text[textrow] );
- }
+ game->m_font->SetColor(0xFFFFFFFF);
+ for( int textrow = 0; _credits_text[textrow]; textrow++ )
+ {
+ game->m_font->printf( 400, m_draw_credits_y + CREDITS_ROW_HEIGHT * textrow,
+ HGETEXT_CENTER, _credits_text[textrow] );
+ }
- // we are using GUI in this state, so it will draw the mouse cursor itself
- // no need to bother
+ // we are using GUI in this state, so it will draw the mouse cursor itself
+ // no need to bother
}
bool GameState_Credits::Think( MyGame * game )
{
- float dt = game->m_hge->Timer_GetDelta();
+ float dt = game->m_hge->Timer_GetDelta();
int id;
static int lastid = CREDITS_ELEMENT_NONE_SELECTED;
// If ESCAPE was pressed, tell the GUI to finish
if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
- lastid = CREDITS_ELEMENT_EXIT;
- m_gui->Leave();
- }
+ lastid = CREDITS_ELEMENT_EXIT;
+ m_gui->Leave();
+ }
// We update the GUI and take an action if
// one of the menu items was selected
@@ -363,33 +369,33 @@ bool GameState_Credits::Think( MyGame * game )
switch(lastid)
{
case CREDITS_ELEMENT_EXIT:
- game->ShowMainMenuScreen();
- //m_gui->SetFocus(CREDITS_ELEMENT_EXIT);
+ game->ShowMainMenuScreen();
+ //m_gui->SetFocus(CREDITS_ELEMENT_EXIT);
m_gui->Enter();
break;
}
} else if(id) {
lastid = id;
- m_gui->Leave();
+ m_gui->Leave();
}
- AnimateBackground( dt, game );
+ AnimateBackground( dt, game );
- // slide credits up
+ // slide credits up
/*
- for( int i = 100; i < 100+m_credits_row_count; i++ )
- {
- hgeGUIText * t = (hgeGUIText *)m_gui->GetCtrl( i );
- if( ! t ) continue;
-
- // this one doesn't move anything, at all. Might be my mistake of course.
- m_gui->MoveCtrl( i, t->rect.x1, t->rect.y1 - dt * 20.0f );
- }
+ for( int i = 100; i < 100+m_credits_row_count; i++ )
+ {
+ hgeGUIText * t = (hgeGUIText *)m_gui->GetCtrl( i );
+ if( ! t ) continue;
+
+ // this one doesn't move anything, at all. Might be my mistake of course.
+ m_gui->MoveCtrl( i, t->rect.x1, t->rect.y1 - dt * 20.0f );
+ }
*/
- // this is rough speed estimation, should be properly reworked with millisecond timer
- m_draw_credits_y -= dt * CREDITS_SPEED;
+ // this is rough speed estimation, should be properly reworked with millisecond timer
+ m_draw_credits_y -= dt * CREDITS_SPEED;
- return false;
+ return false;
}
@@ -399,9 +405,9 @@ GameState_Options::GameState_Options( MyGame * game )
m_gui = new hgeGUI();
m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_SOUND, game->m_font, game->m_click_sound,
- 400, OPTIONS_TOP_ITEM_Y, 0.0f, _opt_sound_text[game->m_sound_enabled] ));
+ 400, OPTIONS_TOP_ITEM_Y, 0.0f, _opt_sound_text[game->m_sound_enabled] ));
m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_EXIT, game->m_font, game->m_click_sound,
- 400, OPTIONS_TOP_ITEM_Y+OPTIONS_ITEM_HEIGHT, 0.1f, "Exit"));
+ 400, OPTIONS_TOP_ITEM_Y+OPTIONS_ITEM_HEIGHT, 0.1f, "Exit"));
m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
m_gui->SetCursor( game->m_mouse_cursor_sprite );
@@ -412,7 +418,7 @@ GameState_Options::GameState_Options( MyGame * game )
GameState_Options::~GameState_Options()
{
- delete m_gui;
+ delete m_gui;
}
@@ -420,105 +426,105 @@ void GameState_Options::Render( MyGame * game )
{
game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
m_gui->Render();
- // we are using GUI in this state, so it will draw the mouse cursor itself
- // no need to bother
+ // we are using GUI in this state, so it will draw the mouse cursor itself
+ // no need to bother
}
bool GameState_Options::Think( MyGame * game )
{
- float dt = game->m_hge->Timer_GetDelta();
+ float dt = game->m_hge->Timer_GetDelta();
int id;
static int lastid = OPTIONS_ELEMENT_NONE_SELECTED;
// If ESCAPE was pressed, tell the GUI to finish
if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
- lastid = OPTIONS_ELEMENT_EXIT;
- m_gui->Leave();
- }
+ lastid = OPTIONS_ELEMENT_EXIT;
+ m_gui->Leave();
+ }
// We update the GUI and take an action if
// one of the menu items was selected
- id = m_gui->Update(dt);
- if(id == -1)
- {
- switch(lastid)
- {
- case OPTIONS_ELEMENT_SOUND:
- game->m_sound_enabled = ! game->m_sound_enabled;
-
- // delete and re-add the menu element
- m_gui->DelCtrl(OPTIONS_ELEMENT_SOUND);
- m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_SOUND, game->m_font, game->m_click_sound,
- 400, OPTIONS_TOP_ITEM_Y, 0.0f, _opt_sound_text[game->m_sound_enabled] ));
-
- m_gui->SetFocus( OPTIONS_ELEMENT_SOUND );
- m_gui->Enter();
- break;
-
- case OPTIONS_ELEMENT_EXIT:
- game->ShowMainMenuScreen();
- m_gui->Enter();
- break;
- }
- } else if(id) {
- lastid = id;
- m_gui->Leave();
- }
-
- AnimateBackground( dt, game );
-
- return false;
+ id = m_gui->Update(dt);
+ if(id == -1)
+ {
+ switch(lastid)
+ {
+ case OPTIONS_ELEMENT_SOUND:
+ game->m_sound_enabled = ! game->m_sound_enabled;
+
+ // delete and re-add the menu element
+ m_gui->DelCtrl(OPTIONS_ELEMENT_SOUND);
+ m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_SOUND, game->m_font, game->m_click_sound,
+ 400, OPTIONS_TOP_ITEM_Y, 0.0f, _opt_sound_text[game->m_sound_enabled] ));
+
+ m_gui->SetFocus( OPTIONS_ELEMENT_SOUND );
+ m_gui->Enter();
+ break;
+
+ case OPTIONS_ELEMENT_EXIT:
+ game->ShowMainMenuScreen();
+ m_gui->Enter();
+ break;
+ }
+ } else if(id) {
+ lastid = id;
+ m_gui->Leave();
+ }
+
+ AnimateBackground( dt, game );
+
+ return false;
}
void GameState_Play::Render( MyGame * game )
{
- if( m_world ) m_world->Render();
- if( m_player ) m_player->Render( m_world );
+ if( m_world ) m_world->Render();
+ if( m_player ) m_player->Render( m_world );
- // 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 );
- game->m_mouse_cursor_sprite->Render( mx, my );
+ // 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 );
+ game->m_mouse_cursor_sprite->Render( mx, my );
}
bool GameState_Play::Think( MyGame * game )
{
- // give the World a chance to play its internal logic, like move monsters and animate things
- m_world->Think();
- // give the Player a chance to check keys, test powerup collisions etc
- m_player->Think();
+ // give the World a chance to play its internal logic, like move monsters and animate things
+ m_world->Think();
+ // give the Player a chance to check keys, test powerup collisions etc
+ m_player->Think();
- // TODO: Design a way to return events from the World::Think, like inform about player taking damage/dying
+ // TODO: Design a way to return events from the World::Think, like inform about player taking damage/dying
- if( m_world->Victory() ) {
- }
+ if( m_world->Victory() ) {
+ }
- return false;
+ return false;
}
void GameState_Play::OnEnterState( MyGame * game )
{
- delete m_player;
- delete m_world;
- m_player = new Player();
- m_world = new World( m_player, "level_01.txt" );
-
- m_player->EnterWorld( m_world );
+ delete m_player;
+ delete m_world;
+ m_player = new Player();
+ m_world = new World( m_player, "level_01.txt" );
+
+ m_player->EnterWorld( m_world );
}
GameState_Play::GameState_Play()
- : m_world(NULL), m_player(NULL)
+ : m_world(NULL), m_player(NULL)
{
}
GameState_Play::~GameState_Play()
{
- delete m_world;
- delete m_player;
+ delete m_world;
+ delete m_player;
}
178 src/game_state.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
@@ -13,22 +19,22 @@ class MyGame;
class GameState
{
public:
- virtual ~GameState() {}
-
- // This is called periodically to draw the main menu, or the world, or the gameover screen
- // Please no logic in this code, only drawing
- // Render returns void. Can't stop the game from here, unlike HGE's renderfunc.
- virtual void Render( MyGame * game ) = 0;
-
- // This is called periodically to do the thinking (or nothing) and check keyboard etc.
- // Please no drawing in this code, only logic
- // Think returns bool like HGE framefunc does, returning true will stop the game
- virtual bool Think( MyGame * game ) = 0;
-
- // 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)
- virtual void OnEnterState( MyGame * game ) {}
+ virtual ~GameState() {}
+
+ // This is called periodically to draw the main menu, or the world, or the gameover screen
+ // Please no logic in this code, only drawing
+ // Render returns void. Can't stop the game from here, unlike HGE's renderfunc.
+ virtual void Render( MyGame * game ) = 0;
+
+ // This is called periodically to do the thinking (or nothing) and check keyboard etc.
+ // Please no drawing in this code, only logic
+ // Think returns bool like HGE framefunc does, returning true will stop the game
+ virtual bool Think( MyGame * game ) = 0;
+
+ // 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)
+ virtual void OnEnterState( MyGame * game ) {}
};
@@ -36,27 +42,27 @@ class GameState
class GameState_MainMenu: public virtual GameState
{
protected:
- hgeGUI * m_gui;
+ hgeGUI * m_gui;
public:
- GameState_MainMenu( MyGame * game );
- virtual ~GameState_MainMenu();
-
- void Render( MyGame * game );
- bool Think( MyGame * game );
-
- static const int MAINMENU_TOP_ITEM_Y = 200;
- static const int MAINMENU_ITEM_HEIGHT = 40;
-
- // numeric codes for GUI elements
- enum {
- MAINMENU_ELEMENT_NONE_SELECTED = 0,
- MAINMENU_ELEMENT_PLAY = 1,
- MAINMENU_ELEMENT_OPTIONS,
- MAINMENU_ELEMENT_INSTRUCTIONS,
- MAINMENU_ELEMENT_CREDITS,
- MAINMENU_ELEMENT_EXIT
- };
+ GameState_MainMenu( MyGame * game );
+ virtual ~GameState_MainMenu();
+
+ void Render( MyGame * game );
+ bool Think( MyGame * game );
+
+ static const int MAINMENU_TOP_ITEM_Y = 200;
+ static const int MAINMENU_ITEM_HEIGHT = 40;
+
+ // numeric codes for GUI elements
+ enum {
+ MAINMENU_ELEMENT_NONE_SELECTED = 0,
+ MAINMENU_ELEMENT_PLAY = 1,
+ MAINMENU_ELEMENT_OPTIONS,
+ MAINMENU_ELEMENT_INSTRUCTIONS,
+ MAINMENU_ELEMENT_CREDITS,
+ MAINMENU_ELEMENT_EXIT
+ };
};
@@ -64,24 +70,24 @@ class GameState_MainMenu: public virtual GameState
class GameState_Instructions: public virtual GameState
{
protected:
- hgeGUI * m_gui;
+ hgeGUI * m_gui;
public:
- GameState_Instructions( MyGame * game );
- virtual ~GameState_Instructions();
+ GameState_Instructions( MyGame * game );
+ virtual ~GameState_Instructions();
- enum {
- INSTRUC_ELEMENT_NONE_SELECTED = 0,
- INSTRUC_ELEMENT_PAGE1 = 1,
- INSTRUC_ELEMENT_PAGE2,
- INSTRUC_ELEMENT_EXIT
- };
+ enum {
+ INSTRUC_ELEMENT_NONE_SELECTED = 0,
+ INSTRUC_ELEMENT_PAGE1 = 1,
+ INSTRUC_ELEMENT_PAGE2,
+ INSTRUC_ELEMENT_EXIT
+ };
- // fills m_gui with text labels containing lines of instructions page1 or page2
- void PopulateInstructionsPage( MyGame * game, const char * text[] );
+ // fills m_gui with text labels containing lines of instructions page1 or page2
+ void PopulateInstructionsPage( MyGame * game, const char * text[] );
- void Render( MyGame * game );
- bool Think( MyGame * game );
+ void Render( MyGame * game );
+ bool Think( MyGame * game );
};
@@ -90,30 +96,30 @@ class GameState_Instructions: public virtual GameState
class GameState_Credits: public virtual GameState
{
protected:
- hgeGUI * m_gui;
- //int m_credits_row_count;
+ hgeGUI * m_gui;
+ //int m_credits_row_count;
- // vertical coord to render credits, slowly decreases to show all text into screen
- float m_draw_credits_y;
+ // vertical coord to render credits, slowly decreases to show all text into screen
+ float m_draw_credits_y;
public:
- GameState_Credits( MyGame * game );
- virtual ~GameState_Credits();
+ GameState_Credits( MyGame * game );
+ virtual ~GameState_Credits();
- const static int CREDITS_ROW_HEIGHT = 40; // row height (distance between rows) in pixels
- const static int CREDITS_SPEED = 25; // pixels per second (roughly, bad estimation)
+ const static int CREDITS_ROW_HEIGHT = 40; // row height (distance between rows) in pixels
+ const static int CREDITS_SPEED = 25; // pixels per second (roughly, bad estimation)
- enum {
- CREDITS_ELEMENT_NONE_SELECTED = 0,
- CREDITS_ELEMENT_EXIT = 1
- };
+ enum {
+ CREDITS_ELEMENT_NONE_SELECTED = 0,
+ CREDITS_ELEMENT_EXIT = 1
+ };
- // fills m_gui with text labels containing lines of credits
- // (Called once)
- //void PopulateCredits( MyGame * game );
+ // fills m_gui with text labels containing lines of credits
+ // (Called once)
+ //void PopulateCredits( MyGame * game );
- void Render( MyGame * game );
- bool Think( MyGame * game );
+ void Render( MyGame * game );
+ bool Think( MyGame * game );
};
@@ -121,22 +127,22 @@ class GameState_Credits: public virtual GameState
class GameState_Options: public virtual GameState
{
protected:
- hgeGUI * m_gui;
+ hgeGUI * m_gui;
public:
- GameState_Options( MyGame * game );
- virtual ~GameState_Options();
+ GameState_Options( MyGame * game );
+ virtual ~GameState_Options();
- static const int OPTIONS_TOP_ITEM_Y = 200;
- static const int OPTIONS_ITEM_HEIGHT = 40;
+ static const int OPTIONS_TOP_ITEM_Y = 200;
+ static const int OPTIONS_ITEM_HEIGHT = 40;
- enum {
- OPTIONS_ELEMENT_NONE_SELECTED = 0,
- OPTIONS_ELEMENT_SOUND = 1,
- OPTIONS_ELEMENT_EXIT
- };
+ enum {
+ OPTIONS_ELEMENT_NONE_SELECTED = 0,
+ OPTIONS_ELEMENT_SOUND = 1,
+ OPTIONS_ELEMENT_EXIT
+ };
- void Render( MyGame * game );
- bool Think( MyGame * game );
+ void Render( MyGame * game );
+ bool Think( MyGame * game );
};
@@ -147,16 +153,16 @@ class Player;
class GameState_Play: virtual public GameState
{
protected:
- // We create and load world using this class. World is responsible for its
- // playing rules. World defines conditions when player wins.
- World * m_world;
- Player * m_player;
+ // We create and load world using this class. World is responsible for its
+ // playing rules. World defines conditions when player wins.
+ World * m_world;
+ Player * m_player;
public:
- GameState_Play();
- virtual ~GameState_Play();
+ GameState_Play();
+ virtual ~GameState_Play();
- virtual void Render( MyGame * game );
- virtual bool Think( MyGame * game );
- void OnEnterState( MyGame * game );
+ virtual void Render( MyGame * game );
+ virtual bool Think( MyGame * game );
+ void OnEnterState( MyGame * game );
};
6 src/menuitem.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
/*
** Haaf's Game Engine 1.7
** Copyright (C) 2003-2007, Relish Games
6 src/menuitem.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
480 src/player.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// Player class
// controls keyboard interaction and game rules
#include "player.h"
@@ -9,289 +15,289 @@
Player::Player()
- : m_lives(INITIAL_LIVES_COUNT)
- , m_position(0, 0, World::CELL_BOX_SIZE-1, World::CELL_BOX_SIZE-1), m_speed(0,0)
- , m_last_facing(FACING_RIGHT), m_is_dead(false), m_money(0)
+ : m_lives(INITIAL_LIVES_COUNT)
+ , m_position(0, 0, World::CELL_BOX_SIZE-1, World::CELL_BOX_SIZE-1), m_speed(0,0)
+ , m_last_facing(FACING_RIGHT), m_is_dead(false), m_money(0)
{
- m_character_right[0][0] = m_sprite_manager.GetSprite( "textures/mario_r1.png" );
- m_character_right[0][1] = m_sprite_manager.GetSprite( "textures/mario_r2.png" );
- m_character_right[1][0] = m_sprite_manager.GetSprite( "textures/mario_l1.png" );
- m_character_right[1][1] = m_sprite_manager.GetSprite( "textures/mario_l2.png" );
-
- // 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_character_right[0][0] = m_sprite_manager.GetSprite( "textures/mario_r1.png" );
+ m_character_right[0][1] = m_sprite_manager.GetSprite( "textures/mario_r2.png" );
+ m_character_right[1][0] = m_sprite_manager.GetSprite( "textures/mario_l1.png" );
+ m_character_right[1][1] = m_sprite_manager.GetSprite( "textures/mario_l2.png" );
+
+ // this will not create another HGE, instead we will get the global
+ // unique HGE object which is already started
+ m_hge = hgeCreate( HGE_VERSION );
}
Player::~Player()
{
- delete m_character_right[0][0];
- delete m_character_right[0][1];
- delete m_character_right[1][0];
- delete m_character_right[1][1];
+ delete m_character_right[0][0];
+ delete m_character_right[0][1];
+ delete m_character_right[1][0];
+ delete m_character_right[1][1];
}
void Player::EnterWorld( World * w )
{
- m_world = w;
-
- // we need a simple way to determine player starting position
- // we scan the world for '@' and use that as start
- for( uint32_t r = 0; r < w->m_world_height; ++r ) {
- for( uint32_t c = 0; c < w->m_world_width; ++c ) {
- if( w->At(r,c) == World::WORLD_CELL_PLAYER_START )
- {
- // found the @ character, set player position
- MoveTo( c * World::CELL_BOX_SIZE, r * World::CELL_BOX_SIZE );
- }
- }
- }
+ m_world = w;
+
+ // we need a simple way to determine player starting position
+ // we scan the world for '@' and use that as start
+ for( uint32_t r = 0; r < w->m_world_height; ++r ) {
+ for( uint32_t c = 0; c < w->m_world_width; ++c ) {
+ if( w->At(r,c) == World::WORLD_CELL_PLAYER_START )
+ {
+ // found the @ character, set player position
+ MoveTo( c * World::CELL_BOX_SIZE, r * World::CELL_BOX_SIZE );
+ }
+ }
+ }
}
hgeRect Player::GetScreenPosition()
{
- hgeRect spos = m_position;
- spos.x1 -= m_world->m_camera_pos.x;
- spos.x2 -= m_world->m_camera_pos.x;
- spos.y1 -= m_world->m_camera_pos.y;
- spos.y2 -= m_world->m_camera_pos.y;
- return spos;
+ hgeRect spos = m_position;
+ spos.x1 -= m_world->m_camera_pos.x;
+ spos.x2 -= m_world->m_camera_pos.x;
+ spos.y1 -= m_world->m_camera_pos.y;
+ spos.y2 -= m_world->m_camera_pos.y;
+ return spos;
}
hgeSprite * Player::GetSprite()
{
- // As an exercise for the reader:
- // Do not return animated frame, if horizontal speed is zero
+ // As an exercise for the reader:
+ // Do not return animated frame, if horizontal speed is zero
- uint32_t milliseconds = GetTickCount();
- // we want frames to change every 333 msec from 0 to 1
- // total of 2 frames, hence the modulo of 2
- uint32_t f = (milliseconds / 333) % 2;
+ uint32_t milliseconds = GetTickCount();
+ // we want frames to change every 333 msec from 0 to 1
+ // total of 2 frames, hence the modulo of 2
+ uint32_t f = (milliseconds / 333) % 2;
- return m_character_right[m_last_facing][f];
+ return m_character_right[m_last_facing][f];
}
// MyGame pointer is only needed to access the font, otherwise we don't need it
void Player::Render( World * world )
{
- MyGame * game = MyGame::m_game;
-
- // print with black shadow
- game->m_font->SetColor( ARGB(255, 0, 0, 0) );
- game->m_font->printfb( 2, 2, World::SCREEN_WIDTH, 40, HGETEXT_RIGHT, "Score: %d Lives: %d", m_money, m_lives );
- game->m_font->SetColor( ARGB(255, 255, 255, 255) );
- game->m_font->printfb( 0, 0, World::SCREEN_WIDTH, 40, HGETEXT_RIGHT, "Score: %d Lives: %d", m_money, m_lives );
-
- if( m_is_dead ) {
- // test for >0 instead of >=0 because respawn requires at least 1 life to resurrect
- if( m_lives > 0 ) {
- game->m_font->printfb( 0, World::SCREEN_HEIGHT/2,
- World::SCREEN_WIDTH, 40, HGETEXT_CENTER,
- "You have died. Press ENTER to respawn." );
- } else {
- game->m_font->printfb( 0, World::SCREEN_HEIGHT/2,
- World::SCREEN_WIDTH, 40, HGETEXT_CENTER,
- "You have died. Game Over." );
- }
- // no rendering when we're dead
- // should display blood and all those guts around the death place or the corpse
- return;
- }
-
- hgeSprite * spr = GetSprite();
- if( ! spr ) return;
-
- hgeRect pos = GetScreenPosition();
-
- spr->Render( pos.x1, pos.y1 );
-
- m_hge->Gfx_RenderLine( pos.x1, pos.y1, pos.x2, pos.y1 );
- m_hge->Gfx_RenderLine( pos.x2, pos.y1, pos.x2, pos.y2 );
- m_hge->Gfx_RenderLine( pos.x2, pos.y2, pos.x1, pos.y2 );
- m_hge->Gfx_RenderLine( pos.x1, pos.y1, pos.x1, pos.y2 );
+ MyGame * game = MyGame::m_game;
+
+ // print with black shadow
+ game->m_font->SetColor( ARGB(255, 0, 0, 0) );
+ game->m_font->printfb( 2, 2, World::SCREEN_WIDTH, 40, HGETEXT_RIGHT, "Score: %d Lives: %d", m_money, m_lives );
+ game->m_font->SetColor( ARGB(255, 255, 255, 255) );
+ game->m_font->printfb( 0, 0, World::SCREEN_WIDTH, 40, HGETEXT_RIGHT, "Score: %d Lives: %d", m_money, m_lives );
+
+ if( m_is_dead ) {
+ // test for >0 instead of >=0 because respawn requires at least 1 life to resurrect
+ if( m_lives > 0 ) {
+ game->m_font->printfb( 0, World::SCREEN_HEIGHT/2,
+ World::SCREEN_WIDTH, 40, HGETEXT_CENTER,
+ "You have died. Press ENTER to respawn." );
+ } else {
+ game->m_font->printfb( 0, World::SCREEN_HEIGHT/2,
+ World::SCREEN_WIDTH, 40, HGETEXT_CENTER,
+ "You have died. Game Over." );
+ }
+ // no rendering when we're dead
+ // should display blood and all those guts around the death place or the corpse
+ return;
+ }
+
+ hgeSprite * spr = GetSprite();
+ if( ! spr ) return;
+
+ hgeRect pos = GetScreenPosition();
+
+ spr->Render( pos.x1, pos.y1 );
+
+ m_hge->Gfx_RenderLine( pos.x1, pos.y1, pos.x2, pos.y1 );
+ m_hge->Gfx_RenderLine( pos.x2, pos.y1, pos.x2, pos.y2 );
+ m_hge->Gfx_RenderLine( pos.x2, pos.y2, pos.x1, pos.y2 );
+ m_hge->Gfx_RenderLine( pos.x1, pos.y1, pos.x1, pos.y2 );
}
void Player::Think()
{
- if( m_is_dead ) {
- // press ENTER when dead leads to respawn
- if( m_hge->Input_GetKeyState( HGEK_ENTER ) ) {
- Respawn();
- }
- return;
- }
- if( m_world->m_pause_flag ) {
- // check for pause key to unpause
- return;
- }
-
- float delta = m_hge->Timer_GetDelta();
-
- if( m_hge->Input_GetKeyState( HGEK_RIGHT )
- || m_hge->Input_GetKeyState( HGEK_D ) )
- {
- m_last_facing = FACING_RIGHT;
- m_speed.x = BASE_MOVING_SPEED;
- }
- else if( m_hge->Input_GetKeyState( HGEK_LEFT )
- || m_hge->Input_GetKeyState( HGEK_A ) )
- {
- m_last_facing = FACING_LEFT;
- m_speed.x = -BASE_MOVING_SPEED;
- }
- else {
- // stop moving if neither right nor left pressed, but preserve facing
- m_speed.x = 0;
- }
-
- float dy = m_speed.y * delta;
- float dx = m_speed.x * delta;
-
- bool solid_under_bottom_left = m_world->IsSolidAtXY( m_position.x1, m_position.y2+dy );
- bool solid_under_bottom_right = m_world->IsSolidAtXY( m_position.x2, m_position.y2+dy );
- bool standing_flag = solid_under_bottom_left || solid_under_bottom_right;
-
- if( standing_flag )
- {
- if (m_hge->Input_GetKeyState( HGEK_W )
- || m_hge->Input_GetKeyState( HGEK_SPACE )
- || m_hge->Input_GetKeyState( HGEK_UP ))
- {
- m_speed.y = -JumpSpeed();
- }
- }
-
- //-----------------------------------------------------------------
- // Get speed affected by gravity if our feet are not standing firm
- //-----------------------------------------------------------------
-
- // gravity drags us down, man (to the limit of MAX_FALL_SPEED)
- m_speed.y = std::min( m_speed.y + m_world->GravityAccel(), (float)MAX_FALL_SPEED );
- dy = m_speed.y * delta;
-
- // if we are falling
- if (m_speed.y > 0 )
- {
- // if we were falling but now we stand firmly on feet - stop falling
- if( standing_flag )
- {
- m_speed.y = 0;
- }
- else {
- hgeRect future_pos = m_position;
- future_pos.y1 += m_speed.y * delta;
- future_pos.y2 += m_speed.y * delta;
- if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
- }
- }
- else
- // jumping/flying up
- if( m_speed.y < 0) {
- // 2 is a magic number to allow sliding down/jumping through tight 1-block wide holes
- bool solid_above_top_left = m_world->IsSolidAtXY( m_position.x1+2, m_position.y1-dy );
- bool solid_above_top_right = m_world->IsSolidAtXY( m_position.x2-2, m_position.y1-dy );
- bool hitting_the_ceiling = solid_above_top_left || solid_above_top_right;
- if( hitting_the_ceiling ) {
- // stop flying, hit the ceiling
- m_speed.y = 0;
- // doesn't work: m_speed.y = -m_speed.y;
- } else {
- hgeRect future_pos = m_position;
- future_pos.y1 += m_speed.y * delta;
- future_pos.y2 += m_speed.y * delta;
- if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
- }
- }
-
- // moving left, test if we hit the wall
- if( m_speed.x < 0) {
- bool solid_top_left = m_world->IsSolidAtXY( m_position.x1-dx, m_position.y1 );
- bool solid_bottom_left = m_world->IsSolidAtXY( m_position.x1-dx, m_position.y2 );
- bool hitting_left_wall = solid_top_left || solid_bottom_left;
- if( hitting_left_wall ) {
- // stop moving, hit the wall
- m_speed.x = 0;
- } else {
- hgeRect future_pos = m_position;
- future_pos.x1 += m_speed.x * delta;
- future_pos.x2 += m_speed.x * delta;
- if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
- }
- }
- else
- // moving right, test if we hit the wall
- if( m_speed.x > 0) {
- bool solid_top_right = m_world->IsSolidAtXY( m_position.x2+dx, m_position.y1 );
- bool solid_bottom_right = m_world->IsSolidAtXY( m_position.x2+dx, m_position.y2 );
- bool hitting_right_wall = solid_top_right || solid_bottom_right;
- if( hitting_right_wall ) {
- // stop moving, hit the wall
- m_speed.x = 0;
- } else {
- hgeRect future_pos = m_position;
- future_pos.x1 += m_speed.x * delta;
- future_pos.x2 += m_speed.x * delta;
- if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
- }
- }
-
- // if we fall below the world
- if( m_position.y1 >= m_world->m_world_height * World::CELL_BOX_SIZE ) {
- Die();
- m_world->OnPlayerDied();
- }
-
- // if we step at least 25% cell deep into the spikes, we die
- World::CellType left_foot = m_world->AtXY( m_position.x1, m_position.y2 - World::CELL_BOX_SIZE*0.25f );
- World::CellType right_foot = m_world->AtXY( m_position.x2, m_position.y2 - World::CELL_BOX_SIZE*0.25f );
- if( m_world->IsKillOnTouch( left_foot )
- || m_world->IsKillOnTouch( right_foot ) )
- {
- Die();
- m_world->OnPlayerDied();
- }
+ if( m_is_dead ) {
+ // press ENTER when dead leads to respawn
+ if( m_hge->Input_GetKeyState( HGEK_ENTER ) ) {
+ Respawn();
+ }
+ return;
+ }
+ if( m_world->m_pause_flag ) {
+ // check for pause key to unpause
+ return;
+ }
+
+ float delta = m_hge->Timer_GetDelta();
+
+ if( m_hge->Input_GetKeyState( HGEK_RIGHT )
+ || m_hge->Input_GetKeyState( HGEK_D ) )
+ {
+ m_last_facing = FACING_RIGHT;
+ m_speed.x = BASE_MOVING_SPEED;
+ }
+ else if( m_hge->Input_GetKeyState( HGEK_LEFT )
+ || m_hge->Input_GetKeyState( HGEK_A ) )
+ {
+ m_last_facing = FACING_LEFT;
+ m_speed.x = -BASE_MOVING_SPEED;
+ }
+ else {
+ // stop moving if neither right nor left pressed, but preserve facing
+ m_speed.x = 0;
+ }
+
+ float dy = m_speed.y * delta;
+ float dx = m_speed.x * delta;
+
+ bool solid_under_bottom_left = m_world->IsSolidAtXY( m_position.x1, m_position.y2+dy );
+ bool solid_under_bottom_right = m_world->IsSolidAtXY( m_position.x2, m_position.y2+dy );
+ bool standing_flag = solid_under_bottom_left || solid_under_bottom_right;
+
+ if( standing_flag )
+ {
+ if (m_hge->Input_GetKeyState( HGEK_W )
+ || m_hge->Input_GetKeyState( HGEK_SPACE )
+ || m_hge->Input_GetKeyState( HGEK_UP ))
+ {
+ m_speed.y = -JumpSpeed();
+ }
+ }
+
+ //-----------------------------------------------------------------
+ // Get speed affected by gravity if our feet are not standing firm
+ //-----------------------------------------------------------------
+
+ // gravity drags us down, man (to the limit of MAX_FALL_SPEED)
+ m_speed.y = std::min( m_speed.y + m_world->GravityAccel(), (float)MAX_FALL_SPEED );
+ dy = m_speed.y * delta;
+
+ // if we are falling
+ if (m_speed.y > 0 )
+ {
+ // if we were falling but now we stand firmly on feet - stop falling
+ if( standing_flag )
+ {
+ m_speed.y = 0;
+ }
+ else {
+ hgeRect future_pos = m_position;
+ future_pos.y1 += m_speed.y * delta;
+ future_pos.y2 += m_speed.y * delta;
+ if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
+ }
+ }
+ else
+ // jumping/flying up
+ if( m_speed.y < 0) {
+ // 2 is a magic number to allow sliding down/jumping through tight 1-block wide holes
+ bool solid_above_top_left = m_world->IsSolidAtXY( m_position.x1+2, m_position.y1-dy );
+ bool solid_above_top_right = m_world->IsSolidAtXY( m_position.x2-2, m_position.y1-dy );
+ bool hitting_the_ceiling = solid_above_top_left || solid_above_top_right;
+ if( hitting_the_ceiling ) {
+ // stop flying, hit the ceiling
+ m_speed.y = 0;
+ // doesn't work: m_speed.y = -m_speed.y;
+ } else {
+ hgeRect future_pos = m_position;
+ future_pos.y1 += m_speed.y * delta;
+ future_pos.y2 += m_speed.y * delta;
+ if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
+ }
+ }
+
+ // moving left, test if we hit the wall
+ if( m_speed.x < 0) {
+ bool solid_top_left = m_world->IsSolidAtXY( m_position.x1-dx, m_position.y1 );
+ bool solid_bottom_left = m_world->IsSolidAtXY( m_position.x1-dx, m_position.y2 );
+ bool hitting_left_wall = solid_top_left || solid_bottom_left;
+ if( hitting_left_wall ) {
+ // stop moving, hit the wall
+ m_speed.x = 0;
+ } else {
+ hgeRect future_pos = m_position;
+ future_pos.x1 += m_speed.x * delta;
+ future_pos.x2 += m_speed.x * delta;
+ if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
+ }
+ }
+ else
+ // moving right, test if we hit the wall
+ if( m_speed.x > 0) {
+ bool solid_top_right = m_world->IsSolidAtXY( m_position.x2+dx, m_position.y1 );
+ bool solid_bottom_right = m_world->IsSolidAtXY( m_position.x2+dx, m_position.y2 );
+ bool hitting_right_wall = solid_top_right || solid_bottom_right;
+ if( hitting_right_wall ) {
+ // stop moving, hit the wall
+ m_speed.x = 0;
+ } else {
+ hgeRect future_pos = m_position;
+ future_pos.x1 += m_speed.x * delta;
+ future_pos.x2 += m_speed.x * delta;
+ if( m_world->TestBlockCollisionAt( future_pos ) ) m_position = future_pos;
+ }
+ }
+
+ // if we fall below the world
+ if( m_position.y1 >= m_world->m_world_height * World::CELL_BOX_SIZE ) {
+ Die();
+ m_world->OnPlayerDied();
+ }
+
+ // if we step at least 25% cell deep into the spikes, we die
+ World::CellType left_foot = m_world->AtXY( m_position.x1, m_position.y2 - World::CELL_BOX_SIZE*0.25f );
+ World::CellType right_foot = m_world->AtXY( m_position.x2, m_position.y2 - World::CELL_BOX_SIZE*0.25f );
+ if( m_world->IsKillOnTouch( left_foot )
+ || m_world->IsKillOnTouch( right_foot ) )
+ {
+ Die();
+ m_world->OnPlayerDied();
+ }
}
void Player::MoveTo( float x, float y )
{
- m_position.Set(
- x, y,
- x + World::CELL_BOX_SIZE - 1.0f,
- y + World::CELL_BOX_SIZE - 1.0f );
+ m_position.Set(
+ x, y,
+ x + World::CELL_BOX_SIZE - 1.0f,
+ y + World::CELL_BOX_SIZE - 1.0f );
}
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_is_dead = true;
+ // 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_is_dead = true;
}
void Player::Respawn()
{
- m_lives--;
- if( m_lives >= 0 ) {
- // Here add actions to move player to start location,
- // to scroll window slightly back to allow playing, to give temporary immunity
- // to monsters and strip the player of all buffs
- m_is_dead = false;
- m_world->m_pause_flag = false;
-
- EnterWorld( m_world );
- m_world->m_camera_pos.x = 0; // reset the camera to the start
- }
- else {
- // out of spare lives - Game over
- // As an exercise for the reader: replace this game over text with a proper
- // gamestate which will show some animation or the Game Over text or show hi-scores
- MyGame::m_game->ShowMainMenuScreen();
- }
+ m_lives--;
+ if( m_lives >= 0 ) {
+ // Here add actions to move player to start location,
+ // to scroll window slightly back to allow playing, to give temporary immunity
+ // to monsters and strip the player of all buffs
+ m_is_dead = false;
+ m_world->m_pause_flag = false;
+
+ EnterWorld( m_world );
+ m_world->m_camera_pos.x = 0; // reset the camera to the start
+ }
+ else {
+ // out of spare lives - Game over
+ // As an exercise for the reader: replace this game over text with a proper
+ // gamestate which will show some animation or the Game Over text or show hi-scores
+ MyGame::m_game->ShowMainMenuScreen();
+ }
}
126 src/player.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
@@ -17,88 +23,88 @@ class MyGame;
class Player
{
protected:
- // Horizontal and vertical speeds of player.
- // You can use acceleration to affect this value when player presses buttons
- // to simulate inertia. Or you can write exact speeds when player presses buttons
- // to do instant turns and direction changes.
- hgeVector m_speed;
+ // Horizontal and vertical speeds of player.
+ // You can use acceleration to affect this value when player presses buttons
+ // to simulate inertia. Or you can write exact speeds when player presses buttons
+ // to do instant turns and direction changes.
+ hgeVector m_speed;
- // pixels per second when player is moving (remember 64 is cell size)
- static const int BASE_MOVING_SPEED = 120;
+ // pixels per second when player is moving (remember 64 is cell size)
+ static const int BASE_MOVING_SPEED = 120;
- // Hack this value for infinite lives
- int m_lives;
- // Hack this value for infinite gold, u jelly american treasury?
- int m_money;
+ // Hack this value for infinite lives
+ int m_lives;
+ // Hack this value for infinite gold, u jelly american treasury?
+ int m_money;
- // flag to render death animation/effects/blood splash when player died
- bool m_is_dead;
+ // flag to render death animation/effects/blood splash when player died
+ bool m_is_dead;
- // world we have entered, must not be NULL when game is active
- World * m_world;
+ // world we have entered, must not be NULL when game is active
+ World * m_world;
- enum {
- FACING_RIGHT = 0,
- FACING_LEFT = 1
- };
+ enum {
+ FACING_RIGHT = 0,
+ FACING_LEFT = 1
+ };
- // make box slightly smaller for the player
- //static const int PADDING_LEFT = 12;
- //static const int PADDING_RIGHT = 12;
- static const int MAX_FALL_SPEED = 300;
+ // make box slightly smaller for the player
+ //static const int PADDING_LEFT = 12;
+ //static const int PADDING_RIGHT = 12;
+ static const int MAX_FALL_SPEED = 300;
- // set this to 0 to render sprite facing right, 1 to render sprite facing left
- // used in GetSprite to return the correct facing
- int m_last_facing;
+ // set this to 0 to render sprite facing right, 1 to render sprite facing left
+ // used in GetSprite to return the correct facing
+ int m_last_facing;
- SpriteManager m_sprite_manager;
+ SpriteManager m_sprite_manager;
- hgeSprite * m_character_right[2][2];
+ hgeSprite * m_character_right[2][2];
- HGE * m_hge;
+ HGE * m_hge;
public:
- // A bounding box for character sprite used for rendering and collisions
- // for simplicity player has same size as world cells
- hgeRect m_position;
+ // A bounding box for character sprite used for rendering and collisions
+ // for simplicity player has same size as world cells
+ hgeRect m_position;
- const static int INITIAL_LIVES_COUNT = 3;
+ const static int INITIAL_LIVES_COUNT = 3;
public:
- Player();
- virtual ~Player();
+ Player();
+ virtual ~Player();
- // player's vertical speed will be instantly set to this value on jump, and then
- // will be gradually reduced by World::GravityAccel every frame to simulate proper
- // jumping.
- // This can be modified to return different values depending if player picks up
- // some bonus for higher jumps or eats some weakening mushroom
- float JumpSpeed() {
- return 420.0f;
- }
+ // player's vertical speed will be instantly set to this value on jump, and then
+ // will be gradually reduced by World::GravityAccel every frame to simulate proper
+ // jumping.
+ // This can be modified to return different values depending if player picks up
+ // some bonus for higher jumps or eats some weakening mushroom
+ float JumpSpeed() {
+ return 420.0f;
+ }
- //hgeVector GetPos();
- // returns position projected from world to screen coordinates
- hgeRect GetScreenPosition();
+ //hgeVector GetPos();
+ // returns position projected from world to screen coordinates
+ hgeRect GetScreenPosition();
- // this one should reduce lives count
- virtual void Die();
+ // this one should reduce lives count
+ virtual void Die();
- // Calculates current sprite to render, using last direction and time to find the frame
- virtual hgeSprite * GetSprite();
+ // Calculates current sprite to render, using last direction and time to find the frame
+ virtual hgeSprite * GetSprite();
- // Draws character over the world, using World's camera to calculate positions
- // MyGame pointer is only needed to access the font, otherwise we don't need it
- virtual void Render( World * world );
+ // Draws character over the world, using World's camera to calculate positions
+ // MyGame pointer is only needed to access the font, otherwise we don't need it
+ virtual void Render( World * world );
- // called by gamestate when player enters the world and game begins
- virtual void EnterWorld( World * w );
+ // called by gamestate when player enters the world and game begins
+ virtual void EnterWorld( World * w );
- // Here check keyboard
- virtual void Think();
+ // Here check keyboard
+ virtual void Think();
- void MoveTo( float x, float y );
+ void MoveTo( float x, float y );
- // Recovery after death
- virtual void Respawn();
+ // Recovery after death
+ virtual void Respawn();
};
6 src/projectile.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// Projectile class
// a thrown or shot object with defined speed and straight path
// hits creatures hostile to the shooter, ends its life at solid world objects
6 src/projectile.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
64 src/sprite_manager.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// base Creature class
// represents a walking or stationary creature which is able to kill player
// on touch.
@@ -9,44 +15,44 @@
SpriteManager::SpriteManager()
{
- // this will not create another HGE, instead we will get the global
- // unique HGE object which is already started
- m_hge = hgeCreate( HGE_VERSION );
+ // this will not create another HGE, instead we will get the global
+ // unique HGE object which is already started
+ m_hge = hgeCreate( HGE_VERSION );
}
hgeSprite * SpriteManager::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;
+ // 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;
+ 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;
+ hgeSprite * spr = new hgeSprite(
+ t, 0.0f, 0.0f,
+ (float)m_hge->Texture_GetWidth(t), (float)m_hge->Texture_GetHeight(t)
+ );
+ return spr;
}
SpriteManager::~SpriteManager()
{
- // 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 );
- }
+ // 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();
+ m_hge->Release();
}
20 src/sprite_manager.h
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// to prevent multiple compiling of this header
#pragma once
@@ -15,15 +21,15 @@ class hgeSprite;
class SpriteManager
{
protected:
- // 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;
+ // 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;
- HGE * m_hge;
+ HGE * m_hge;
public:
- SpriteManager();
- virtual ~SpriteManager();
+ SpriteManager();
+ virtual ~SpriteManager();
- hgeSprite * GetSprite( const std::string & name );
+ hgeSprite * GetSprite( const std::string & name );
};
292 src/world.cpp
View
@@ -1,3 +1,9 @@
+/* This program is free software. It comes without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify it under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
+ */
// World class
// contains the world and its physical rules (collisions, gravity etc)
// basically its a level of the game, other levels can be different child of world
@@ -12,197 +18,197 @@
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)
+ : m_player(plr), m_world_width(0), m_world_height(0)
+ , m_camera_pos(0, 0), m_pause_flag(false)
{
- LoadWorld( filename );
+ 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 );
+ // 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 = m_sprite_manager.GetSprite("textures/brick1.png");
- m_sprite_sky = m_sprite_manager.GetSprite("textures/sky1.png");
- m_sprite_spikes = m_sprite_manager.GetSprite("textures/spikes.png");
+ m_sprite_brick1 = m_sprite_manager.GetSprite("textures/brick1.png");
+ m_sprite_sky = m_sprite_manager.GetSprite("textures/sky1.png");
+ m_sprite_spikes = m_sprite_manager.GetSprite("textures/spikes.png");
}
World::~World()
{
- delete m_sprite_spikes;
- delete m_sprite_brick1;
- delete m_sprite_sky;
+ delete m_sprite_spikes;
+ delete m_sprite_brick1;
+ delete m_sprite_sky;
- // clear world contents
- for( object_list_t::iterator i = m_objects.begin(); i != m_objects.end(); ++i ) {
- delete (*i);
- }
- m_objects.clear();
+ // clear world contents
+ for( object_list_t::iterator i = m_objects.begin(); i != m_objects.end(); ++i ) {
+ delete (*i);
+ }
+ m_objects.clear();
- m_hge->Release();
+ m_hge->Release();
}
void World::LoadWorld( const std::string & filename )
{
- // Since we need to know exact world size to resize the m_world_cells array
- // first we read the level file to determine the max length of all lines
- // second we read it again and put values in the m_world_cells
- std::ifstream f;
- f.open( filename );
-
- uint32_t row = 0;
- size_t max_line_width = 0;
- char line_buf[MAX_WORLD_WIDTH];
-
- while( row < VISIBLE_ROWS || f.eof() || f.fail() )
- {
- f.getline( line_buf, sizeof line_buf );
- size_t line_width = strlen( line_buf );
- if( line_width > max_line_width ) {
- max_line_width = line_width;
- }
- row++;
- }
- f.close();
-
- // this can be actually more than 9 if your game can also scroll vertically
- // but for now we scroll horizontally only
- m_world_height = VISIBLE_ROWS;
- m_world_width = max_line_width;
-
- // resize the world
- m_world_cells.resize( m_world_height * m_world_width );
-
- // fill the world with empties (ASCII space, value 32)