Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

this still works. With game states now

  • Loading branch information...
commit 0f41c16664dac3ab8744eb75e24011ed2f38c7cc 1 parent 6d02e08
@kvakvs authored
View
1  CMakeLists.txt
@@ -22,6 +22,7 @@ add_definitions( "-D_CRT_SECURE_NO_WARNINGS" "-D_CRT_NONSTDC_NO_WARNINGS" "-D_SC
#------ sources ------
set( HGESKEL_SRC
src/game.cpp src/game.h
+ src/game_state.cpp src/game_state.h
src/menuitem.cpp src/menuitem.h
src/player.cpp src/player.h
src/world.cpp src/world.h
View
BIN  linklib/hge.lib
Binary file not shown
View
80 src/game.cpp
@@ -3,85 +3,40 @@
** Haaf's Game Engine 1.8 -- Copyright (C) 2003-2007, Relish Games http://hge.relishgames.com
** Reworked by Dmytro Lytovchenko kvakvs@yandex.ru https://github.com/kvakvs/hge_skel
*/
-
-
// Copy the files "menu.wav", "font1.fnt", "font1.png",
// "bg.png" and "cursor.png" from the folder "precompiled"
// to the folder with executable file. Also copy hge.dll
// and bass.dll to the same folder.
-#include "game.h"
-#include "menuitem.h"
-#include <math.h>
+#include "menuitem.h"
+#include "game.h"
+#include "game_state.h"
-// for the class definition check "game.h" file
-HGE * MyGame::hge = NULL;
-HEFFECT MyGame::snd;
-HTEXTURE MyGame::tex;
-hgeQuad MyGame::quad;
-hgeGUI * MyGame::gui = NULL;
-hgeFont * MyGame::fnt = NULL;
-hgeSprite * MyGame::spr = NULL;
+MyGame * MyGame::m_game = NULL;
-bool MyGame::FrameFunc()
+MyGame::MyGame() : hge(NULL), gui(NULL), fnt(NULL), spr(NULL)
{
- float dt=hge->Timer_GetDelta();
- static float t=0.0f;
- float tx,ty;
- int id;
- static int lastid=0;
-
- // If ESCAPE was pressed, tell the GUI to finish
- if(hge->Input_GetKeyState(HGEK_ESCAPE)) { lastid=5; gui->Leave(); }
-
- // We update the GUI and take an action if
- // one of the menu items was selected
- id=gui->Update(dt);
- if(id == -1)
- {
- switch(lastid)
- {
- case 1:
- case 2:
- case 3:
- case 4:
- gui->SetFocus(1);
- gui->Enter();
- break;
-
- case 5: return true;
- }
- }
- else if(id) {
- lastid=id; gui->Leave();
- }
+ // 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;
- // Here we update our background animation
- t+=dt;
- tx=50*cosf(t/60);
- ty=50*sinf(t/60);
+ m_state = new GameState_MainMenu();
+}
- quad.v[0].tx=tx; quad.v[0].ty=ty;
- quad.v[1].tx=tx+800/64; quad.v[1].ty=ty;
- quad.v[2].tx=tx+800/64; quad.v[2].ty=ty+600/64;
- quad.v[3].tx=tx; quad.v[3].ty=ty+600/64;
- return false;
+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 );
}
bool MyGame::RenderFunc()
{
- // Render graphics
- hge->Gfx_BeginScene();
- hge->Gfx_RenderQuad(&quad);
- gui->Render();
- fnt->SetColor(0xFFFFFFFF);
- fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d", hge->Timer_GetDelta(), hge->Timer_GetFPS());
- hge->Gfx_EndScene();
-
+ m_game->m_state->Render( m_game );
return false;
}
@@ -169,13 +124,12 @@ void MyGame::Shutdown()
}
-
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
MyGame game;
if( game.Startup() )
{
- MyGame::hge->System_Start(); // Let's rock now!
+ game.hge->System_Start(); // Let's rock now!
game.Shutdown();
}
}
View
23 src/game.h
@@ -5,25 +5,34 @@
#include <hgefont.h>
#include <hgegui.h>
+// telling the compiler that GameState class exists. Somewhere.
+class GameState;
+
//
// Main application class, does the job to start up the graphics and the game
//
class MyGame
{
public:
+ static MyGame * m_game;
// Pointer to the HGE interface.
// Helper classes require this to work.
- static HGE * hge;
+ HGE * hge;
// Some resource handles
- static HEFFECT snd;
- static HTEXTURE tex;
- static hgeQuad quad;
+ HEFFECT snd;
+ HTEXTURE tex;
+ hgeQuad quad;
// Pointers to the HGE objects we will use
- static hgeGUI * gui;
- static hgeFont * fnt;
- static hgeSprite * spr;
+ hgeGUI * gui;
+ hgeFont * fnt;
+ hgeSprite * spr;
+
+ GameState * m_state;
+
+public:
+ MyGame();
static bool FrameFunc();
static bool RenderFunc();
View
78 src/game_state.cpp
@@ -1,3 +1,81 @@
// 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).
+#include "game_state.h"
+#include "game.h"
+
+#include <math.h>
+
+
+// This should render main menu
+void GameState_MainMenu::Render( MyGame * game )
+{
+ game->hge->Gfx_BeginScene();
+ game->hge->Gfx_RenderQuad( &game->quad );
+ game->gui->Render();
+
+ // Draw framerate and time since previous frame
+ game->fnt->SetColor(0xFFFFFFFF);
+ game->fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d",
+ game->hge->Timer_GetDelta(),
+ game->hge->Timer_GetFPS());
+
+ game->hge->Gfx_EndScene();
+}
+
+
+bool GameState_MainMenu::Think( MyGame * game )
+{
+ float dt = game->hge->Timer_GetDelta();
+ static float t = 0.0f;
+ float tx, ty;
+ int id;
+ static int lastid = 0;
+
+ // If ESCAPE was pressed, tell the GUI to finish
+ if (game->hge->Input_GetKeyState(HGEK_ESCAPE)) {
+ lastid = 5;
+ game->gui->Leave();
+ }
+
+ // We update the GUI and take an action if
+ // one of the menu items was selected
+ id = game->gui->Update(dt);
+ if(id == -1)
+ {
+ switch(lastid)
+ {
+ case 1:
+ case 2:
+ case 3:
+ case 4:
+ game->gui->SetFocus(1);
+ game->gui->Enter();
+ break;
+
+ case 5: return true;
+ }
+ } else if(id) {
+ lastid = id;
+ game->gui->Leave();
+ }
+
+ // Here we update our background animation
+ t += dt;
+ tx = 50*cosf(t/60);
+ ty = 50*sinf(t/60);
+
+ game->quad.v[0].tx=tx;
+ game->quad.v[0].ty=ty;
+
+ game->quad.v[1].tx=tx+800/64;
+ game->quad.v[1].ty=ty;
+
+ game->quad.v[2].tx=tx+800/64;
+ game->quad.v[2].ty=ty+600/64;
+
+ game->quad.v[3].tx=tx;
+ game->quad.v[3].ty=ty+600/64;
+
+ return false;
+}
View
18 src/game_state.h
@@ -5,14 +5,28 @@
// 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).
+// telling the compiler that MyGame class exists. Somewhere.
+class MyGame;
+
class GameState
{
public:
// This is called periodically to draw the main menu, or the world, or the gameover screen
// Please no logic in this code, only drawing
- virtual void Render() = 0;
+ // 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
- virtual void Think() = 0;
+ // Think returns bool like HGE framefunc does, returning true will stop the game
+ virtual bool Think( MyGame * game ) = 0;
+};
+
+
+// This game state represents main menu and reacts on keypresses like a main menu would
+class GameState_MainMenu: public virtual GameState
+{
+public:
+ void Render( MyGame * game );
+ bool Think( MyGame * game );
};
Please sign in to comment.
Something went wrong with that request. Please try again.