Browse files

instructions work now

  • Loading branch information...
1 parent f171bd6 commit 2862f56d287d1997de0f05322f37f89e35a3484f Dmytro Lytovchenko committed Sep 28, 2011
Showing with 208 additions and 84 deletions.
  1. +24 −26 src/game.cpp
  2. +0 −12 src/game.h
  3. +146 −46 src/game_state.cpp
  4. +38 −0 src/game_state.h
View
50 src/game.cpp
@@ -9,25 +9,20 @@
// and bass.dll to the same folder.
-#include "menuitem.h"
#include "game.h"
#include "game_state.h"
MyGame * MyGame::m_game = NULL;
MyGame::MyGame()
- : m_hge(NULL), m_gui(NULL), m_font(NULL), m_mouse_cursor_sprite(NULL), m_state(NULL)
+ : m_hge(NULL), m_font(NULL), m_mouse_cursor_sprite(NULL), m_state(NULL)
{
// 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;
- m_state_options = new GameState_Options();
- m_state_mainmenu = new GameState_MainMenu();
- m_state_instructions = new GameState_Instructions();
- m_state_credits = new GameState_Credits();
- ShowMainMenuScreen();
+ // do nothing else here, unless it can run before HGE is started
}
@@ -44,7 +39,18 @@ 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
+ 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() );
+
+ m_game->m_hge->Gfx_EndScene();
+
// do not add more code here, all the action happens in GameState's child classes
return false;
}
@@ -108,28 +114,25 @@ bool MyGame::Startup()
m_font = new hgeFont("font1.fnt");
m_mouse_cursor_sprite = new hgeSprite(m_mouse_cursor_tex,0,0,32,32);
- // Create and initialize the GUI
- m_gui=new hgeGUI();
-
- m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_PLAY,m_font,m_click_sound,400,200,0.0f,"Play"));
- m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_OPTIONS,m_font,m_click_sound,400,240,0.1f,"Options"));
- m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_INSTRUCTIONS,m_font,m_click_sound,400,280,0.2f,"Instructions"));
- m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_CREDITS,m_font,m_click_sound,400,320,0.3f,"Credits"));
- m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_EXIT,m_font,m_click_sound,400,360,0.4f,"Exit"));
+ m_state_options = new GameState_Options();
+ m_state_mainmenu = new GameState_MainMenu( this );
+ m_state_instructions = new GameState_Instructions( this );
+ m_state_credits = new GameState_Credits();
- m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
- m_gui->SetCursor(m_mouse_cursor_sprite);
- m_gui->SetFocus(1);
- m_gui->Enter();
+ ShowMainMenuScreen();
return true; // all is fine
}
void MyGame::Shutdown()
{
- // Delete created objects and free loaded resources
- delete m_gui;
+ delete m_state_options;
+ delete m_state_mainmenu;
+ delete m_state_instructions;
+ delete m_state_credits;
+
+ // Delete created objects and free loaded resources
delete m_font;
delete m_mouse_cursor_sprite;
@@ -140,11 +143,6 @@ void MyGame::Shutdown()
// 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;
}
View
12 src/game.h
@@ -3,7 +3,6 @@
#include <hge.h>
#include <hgefont.h>
-#include <hgegui.h>
// telling the compiler that GameState class exists. Somewhere.
class GameState;
@@ -29,7 +28,6 @@ class MyGame
hgeQuad m_background_quad;
// Pointers to the HGE objects we will use
- hgeGUI * m_gui;
hgeFont * m_font;
hgeSprite * m_mouse_cursor_sprite;
@@ -46,16 +44,6 @@ class MyGame
GameState * m_state_credits;
- // 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
- };
-
public:
MyGame();
View
192 src/game_state.cpp
@@ -3,28 +3,40 @@
// game world, or at the game over screen (showing high scores screen).
#include "game_state.h"
#include "game.h"
+#include "menuitem.h"
+
+#include <hgeguictrls.h>
#include <math.h>
+// 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
+};
+// 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 * _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
+};
+
// This should render main menu
void GameState_MainMenu::Render( MyGame * game )
{
- game->m_hge->Gfx_BeginScene();
game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
- game->m_gui->Render();
-
- // Draw framerate and time since previous frame
- game->m_font->SetColor(0xFFFFFFFF);
-
- game->m_font->printf( 5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d",
- game->m_hge->Timer_GetDelta(),
- game->m_hge->Timer_GetFPS() );
+ m_gui->Render();
// we are using GUI in this state, so it will draw the mouse cursor itself
// no need to bother
-
- game->m_hge->Gfx_EndScene();
}
@@ -56,50 +68,50 @@ bool GameState_MainMenu::Think( MyGame * game )
{
float dt = game->m_hge->Timer_GetDelta();
int id;
- static int lastid = MyGame::MAINMENU_ELEMENT_NONE_SELECTED;
+ static int lastid = MAINMENU_ELEMENT_NONE_SELECTED;
// If ESCAPE was pressed, tell the GUI to finish
if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
- lastid = MyGame::MAINMENU_ELEMENT_EXIT;
- game->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
- id = game->m_gui->Update(dt);
+ id = m_gui->Update(dt);
if(id == -1)
{
switch(lastid)
{
- case MyGame::MAINMENU_ELEMENT_PLAY:
- game->m_gui->SetFocus(1);
- game->m_gui->Enter();
+ case MAINMENU_ELEMENT_PLAY:
+ m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
+ m_gui->Enter();
break;
- case MyGame::MAINMENU_ELEMENT_OPTIONS:
+ case MAINMENU_ELEMENT_OPTIONS:
game->ShowOptionsScreen();
- game->m_gui->SetFocus(1);
- game->m_gui->Enter();
+ m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
+ m_gui->Enter();
break;
- case MyGame::MAINMENU_ELEMENT_INSTRUCTIONS:
+ case MAINMENU_ELEMENT_INSTRUCTIONS:
game->ShowInstructionsScreen();
- game->m_gui->SetFocus(1);
- game->m_gui->Enter();
+ m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
+ m_gui->Enter();
break;
- case MyGame::MAINMENU_ELEMENT_CREDITS:
+ case MAINMENU_ELEMENT_CREDITS:
game->ShowCreditsScreen();
- game->m_gui->SetFocus(1);
- game->m_gui->Enter();
+ m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
+ m_gui->Enter();
break;
- case MyGame::MAINMENU_ELEMENT_EXIT:
+ case MAINMENU_ELEMENT_EXIT:
return true;
}
} else if(id) {
lastid = id;
- game->m_gui->Leave();
+ m_gui->Leave();
}
AnimateBackground( dt, game );
@@ -109,42 +121,130 @@ bool GameState_MainMenu::Think( MyGame * game )
-void GameState_Instructions::Render( MyGame * game )
+GameState_MainMenu::GameState_MainMenu( MyGame * game )
{
- game->m_hge->Gfx_BeginScene();
- game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
+ // Create and initialize the GUI
+ m_gui = new hgeGUI();
+
+ m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_PLAY, game->m_font, game->m_click_sound,400,200,0.0f,"Play"));
+ m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_OPTIONS, game->m_font, game->m_click_sound,400,240,0.1f,"Options"));
+ m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_INSTRUCTIONS, game->m_font, game->m_click_sound,400,280,0.2f,"Instructions"));
+ m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_CREDITS, game->m_font, game->m_click_sound,400,320,0.3f,"Credits"));
+ m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_EXIT, game->m_font, game->m_click_sound,400,360,0.4f,"Exit"));
+
+ m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
+ m_gui->SetCursor( game->m_mouse_cursor_sprite );
+ m_gui->SetFocus(1);
+ m_gui->Enter();
+}
- // draw instructions
- game->m_font->SetColor(0xFFFFFFFF);
- game->m_font->printf( 400, 150, HGETEXT_CENTER, "This is My Game of Doom" );
- game->m_font->printf( 400, 180, HGETEXT_CENTER, "You are to guide the character" );
- game->m_font->printf( 400, 210, HGETEXT_CENTER, "to the last sanctuary in this galaxy" );
- game->m_font->printf( 400, 240, HGETEXT_CENTER, "or you die. That's about it. Yea." );
+GameState_MainMenu::~GameState_MainMenu()
+{
+ delete m_gui;
+}
- game->m_font->printf( 400, 300, HGETEXT_CENTER, "Click or press SPACE to return" );
+
+void GameState_Instructions::Render( MyGame * game )
+{
+ game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
+ m_gui->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 );
game->m_mouse_cursor_sprite->Render( mx, my );
-
- game->m_hge->Gfx_EndScene();
}
bool GameState_Instructions::Think( MyGame * game )
{
float dt = game->m_hge->Timer_GetDelta();
+ int id;
+ static int lastid = INSTRUC_ELEMENT_NONE_SELECTED;
- if( game->m_hge->Input_GetKeyState(HGEK_SPACE)
- || game->m_hge->Input_GetKeyState(HGEK_LBUTTON) )
- {
- game->ShowMainMenuScreen();
- return false;
+ // If ESCAPE was pressed, tell the GUI to finish
+ if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
+ lastid = INSTRUC_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 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);
+ m_gui->Enter();
+ break;
+
+ case INSTRUC_ELEMENT_EXIT:
+ game->ShowMainMenuScreen();
+ m_gui->SetFocus(INSTRUC_ELEMENT_EXIT);
+ m_gui->Enter();
+ break;
+ }
+ } else if(id) {
+ lastid = id;
+ m_gui->Leave();
+ }
AnimateBackground( dt, game );
return false;
}
+
+
+
+GameState_Instructions::GameState_Instructions( MyGame * game )
+{
+ // Create and initialize the GUI
+ m_gui = new hgeGUI();
+
+ m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_PAGE1, game->m_font, game->m_click_sound,100,550,0.0f,"Page 1"));
+ 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.4f,"Done"));
+
+ PopulateInstructionsPage( game, _page1_text );
+
+ //m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
+ m_gui->SetCursor( game->m_mouse_cursor_sprite );
+ //m_gui->SetFocus(1);
+ m_gui->Enter();
+}
+
+
+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] );
+ //c1->bVisible = true;
+ m_gui->AddCtrl(c1);
+ }
+}
+
+GameState_Instructions::~GameState_Instructions()
+{
+ delete m_gui;
+}
+
View
38 src/game_state.h
@@ -5,12 +5,16 @@
// 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 <hgegui.h>
+
// telling the compiler that MyGame class exists. Somewhere.
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.
@@ -26,16 +30,50 @@ class GameState
// This game state represents main menu and reacts on keypresses like a main menu would
class GameState_MainMenu: public virtual GameState
{
+protected:
+ hgeGUI * m_gui;
+
public:
+ GameState_MainMenu( MyGame * game );
+ virtual ~GameState_MainMenu();
+
void Render( MyGame * game );
bool Think( MyGame * game );
+
+ // 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
+ };
};
// This game state represents instructions screen and only draws text until click or key pressed
class GameState_Instructions: public virtual GameState
{
+protected:
+ // Instructions pages 1 and 2 (we will move elements to slide into the view
+ // when user changes pages)
+ hgeGUI * m_gui;
+
public:
+ GameState_Instructions( MyGame * game );
+ virtual ~GameState_Instructions();
+
+ 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[] );
+
void Render( MyGame * game );
bool Think( MyGame * game );
};

0 comments on commit 2862f56

Please sign in to comment.