Skip to content
Browse files

instructions work now with mouse cursor too

  • Loading branch information...
1 parent 9ad522d commit f171bd68b765afc41c02cf16a7c0d15b585eb3c0 Dmytro Lytovchenko committed Sep 29, 2011
Showing with 267 additions and 44 deletions.
  1. +62 −0 TASK.txt
  2. +54 −17 src/game.cpp
  3. +32 −3 src/game.h
  4. +92 −24 src/game_state.cpp
  5. +27 −0 src/game_state.h
View
62 TASK.txt
@@ -0,0 +1,62 @@
+hge_tut06 GUI options function as their text describes:
+
+* Play; Enters the game at level 1
+* Options; Allows the sound effects to be turned on/off via some method,
+ preferably a click box (Suggestion: sets global boolean to false, and all
+ subsequent sound effects perform a check on this bollean before playing?)
+ Must offer some methos to return to main menu
+* Instructions; Displays some placeholder text, over two pages, with a next
+ and previous button to go tot he next page of instructions (must offer some
+ method of returning to main menu).
+* Credits; Displays placeholder scrolling text from bottom to top.
+* Exit remains the same
+
+When level 1 is entered, the background must change (any place holder image will
+suffice).
+
+Player's character must be created in the middle of the screen on the x axis,
+but resting on the y axis.
+
+The screen must then scrolling forcably scroll from right to left, with
+boundaries so that the players character will be dragged along if they do not
+move.
+
+Function must be written to end the level, with two arguments: Level_Ended(bool
+passed, int next_level); The function must then do the following:
+
+Code:
+
+if (passed == false)
+{
+ //nothing, I will write this code
+}
+else
+{
+ //write the code to clean up the current level, releasing the resources etc
+ //Write code to start level 2 (level 2 should be the same as level 1, but different background)
+}
+
+int level; should be a global variable.
+
+I would also like a placeholder character sprite created (any placeholder will
+do really, as long as it demostrates it is working)
+
+I also want a player class written (and used for the character that is spawned),
+that handles (left arrow || w) walk left, (right arrow|| d) walk right, (space
+bar|| w) makes them jump (respecting their direction, so the jump is not
+staticically vertical unless they are stationary), and left mouse click fires
+some placeholder projectile sprite.
+
+It should also detect collision so as to kill it, and handle the killed event by
+simply moving the player's character back at the starting position, to give the
+illusion of death.
+
+Conditions:
+
+* hge_tut06 must be the basis of the changes.
+* It must be performed by modifying hge_tut06 only, not by making changes to hge itself.
+* Source must be free to use for any reason.
+* Source can be recycled from previous projects, or someone else's work, permission to do so pending.
+* Must be delivered by Oct5th, but earlier is much preferred.
+* Code must function as described, and be written so as to perform as best it might.
+* I would not object to the code being made public, I would in fact encourage it.
View
71 src/game.cpp
@@ -16,13 +16,18 @@
MyGame * MyGame::m_game = NULL;
-MyGame::MyGame() : m_hge(NULL), m_gui(NULL), m_font(NULL), spr(NULL)
+MyGame::MyGame()
+ : m_hge(NULL), m_gui(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();
- m_state = new GameState_MainMenu();
+ ShowMainMenuScreen();
}
@@ -61,10 +66,11 @@ bool MyGame::Startup()
if( !m_hge->System_Initiate() ) return false;
// Load sound and textures
- m_background_quad.tex=m_hge->Texture_Load("bg.png");
- m_menu_tex=m_hge->Texture_Load("cursor.png");
- m_click_sound=m_hge->Effect_Load("menu.wav");
- if(!m_background_quad.tex || !m_menu_tex || !m_click_sound)
+ m_background_quad.tex = m_hge->Texture_Load("bg.png");
+ m_mouse_cursor_tex = m_hge->Texture_Load("cursor.png");
+ m_click_sound = m_hge->Effect_Load("menu.wav");
+
+ if(!m_background_quad.tex || !m_mouse_cursor_tex || !m_click_sound)
{
// If one of the data files is not found, display
// an error message and shutdown.
@@ -99,39 +105,70 @@ bool MyGame::Startup()
// Load the font, create the cursor sprite
- m_font=new hgeFont("font1.fnt");
- spr=new hgeSprite(m_menu_tex,0,0,32,32);
+ 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(1,m_font,m_click_sound,400,200,0.0f,"Play"));
- m_gui->AddCtrl(new hgeGUIMenuItem(2,m_font,m_click_sound,400,240,0.1f,"Options"));
- m_gui->AddCtrl(new hgeGUIMenuItem(3,m_font,m_click_sound,400,280,0.2f,"Instructions"));
- m_gui->AddCtrl(new hgeGUIMenuItem(4,m_font,m_click_sound,400,320,0.3f,"Credits"));
- m_gui->AddCtrl(new hgeGUIMenuItem(5,m_font,m_click_sound,400,360,0.4f,"Exit"));
+ 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_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
- m_gui->SetCursor(spr);
+ m_gui->SetCursor(m_mouse_cursor_sprite);
m_gui->SetFocus(1);
m_gui->Enter();
return true; // all is fine
}
+
void MyGame::Shutdown()
{
// Delete created objects and free loaded resources
delete m_gui;
delete m_font;
- delete spr;
- m_hge->Effect_Free(m_click_sound);
- m_hge->Texture_Free(m_menu_tex);
+ 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;
+}
+
+
+void MyGame::ShowMainMenuScreen()
+{
+ m_state = m_state_mainmenu;
+}
+
+
+void MyGame::ShowOptionsScreen()
+{
+ m_state = m_state_options;
+}
+
+
+void MyGame::ShowInstructionsScreen()
+{
+ m_state = m_state_instructions;
+}
+
+
+void MyGame::ShowCreditsScreen()
+{
+ m_state = m_state_credits;
}
View
35 src/game.h
@@ -14,14 +14,16 @@ 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;
- // Pointer to the HGE interface.
- // Helper classes require this to work.
+
+ // Pointer to the HGE interface.
HGE * m_hge;
// Some resource handles
HEFFECT m_click_sound;
- HTEXTURE m_menu_tex;
+ HTEXTURE m_mouse_cursor_tex;
// menu background quad. As well we can use it for sky during the gameplay
hgeQuad m_background_quad;
@@ -31,8 +33,29 @@ class MyGame
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;
+ // 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;
+ GameState * m_state_mainmenu;
+ GameState * m_state_instructions;
+ 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();
@@ -45,4 +68,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();
};
View
116 src/game_state.cpp
@@ -21,21 +21,46 @@ void GameState_MainMenu::Render( MyGame * game )
game->m_hge->Timer_GetDelta(),
game->m_hge->Timer_GetFPS() );
+ // we are using GUI in this state, so it will draw the mouse cursor itself
+ // no need to bother
+
game->m_hge->Gfx_EndScene();
}
-bool GameState_MainMenu::Think( MyGame * game )
+void AnimateBackground( float dt, MyGame * game )
{
- float dt = game->m_hge->Timer_GetDelta();
static float t = 0.0f;
float tx, ty;
+
+ // Here we update our background animation
+ t += dt;
+ tx = 50*cosf(t/60);
+ 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[1].tx=tx+800/64;
+ 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[3].tx=tx;
+ game->m_background_quad.v[3].ty=ty+600/64;
+}
+
+
+bool GameState_MainMenu::Think( MyGame * game )
+{
+ float dt = game->m_hge->Timer_GetDelta();
int id;
- static int lastid = 0;
+ static int lastid = MyGame::MAINMENU_ELEMENT_NONE_SELECTED;
// If ESCAPE was pressed, tell the GUI to finish
if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
- lastid = 5;
+ lastid = MyGame::MAINMENU_ELEMENT_EXIT;
game->m_gui->Leave();
}
@@ -46,37 +71,80 @@ bool GameState_MainMenu::Think( MyGame * game )
{
switch(lastid)
{
- case 1:
- case 2:
- case 3:
- case 4:
- game->m_gui->SetFocus(1);
+ case MyGame::MAINMENU_ELEMENT_PLAY:
+ game->m_gui->SetFocus(1);
+ game->m_gui->Enter();
+ break;
+
+ case MyGame::MAINMENU_ELEMENT_OPTIONS:
+ game->ShowOptionsScreen();
+ game->m_gui->SetFocus(1);
+ game->m_gui->Enter();
+ break;
+
+ case MyGame::MAINMENU_ELEMENT_INSTRUCTIONS:
+ game->ShowInstructionsScreen();
+ game->m_gui->SetFocus(1);
+ game->m_gui->Enter();
+ break;
+
+ case MyGame::MAINMENU_ELEMENT_CREDITS:
+ game->ShowCreditsScreen();
+ game->m_gui->SetFocus(1);
game->m_gui->Enter();
break;
- case 5: return true;
+ case MyGame::MAINMENU_ELEMENT_EXIT:
+ return true;
}
} else if(id) {
lastid = id;
game->m_gui->Leave();
}
- // Here we update our background animation
- t += dt;
- tx = 50*cosf(t/60);
- ty = 50*sinf(t/60);
+ AnimateBackground( dt, game );
- game->m_background_quad.v[0].tx=tx;
- game->m_background_quad.v[0].ty=ty;
+ return false;
+}
- game->m_background_quad.v[1].tx=tx+800/64;
- 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[3].tx=tx;
- game->m_background_quad.v[3].ty=ty+600/64;
+void GameState_Instructions::Render( MyGame * game )
+{
+ game->m_hge->Gfx_BeginScene();
+ game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
- return false;
-}
+ // 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." );
+
+ game->m_font->printf( 400, 300, HGETEXT_CENTER, "Click or press SPACE to return" );
+
+ // 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();
+
+ if( game->m_hge->Input_GetKeyState(HGEK_SPACE)
+ || game->m_hge->Input_GetKeyState(HGEK_LBUTTON) )
+ {
+ game->ShowMainMenuScreen();
+ return false;
+ }
+
+ AnimateBackground( dt, game );
+
+ return false;
+}
View
27 src/game_state.h
@@ -30,3 +30,30 @@ class GameState_MainMenu: public virtual GameState
void Render( MyGame * game );
bool Think( MyGame * game );
};
+
+
+// This game state represents instructions screen and only draws text until click or key pressed
+class GameState_Instructions: public virtual GameState
+{
+public:
+ void Render( MyGame * game );
+ bool Think( MyGame * game );
+};
+
+
+// This game state represents instructions screen and only draws text until click or key pressed
+class GameState_Credits: public virtual GameState
+{
+public:
+ void Render( MyGame * game ) {}
+ bool Think( MyGame * game ) { return false; }
+};
+
+
+// This game state represents instructions screen and only draws text until click or key pressed
+class GameState_Options: public virtual GameState
+{
+public:
+ void Render( MyGame * game ) {}
+ bool Think( MyGame * game ) { return false; }
+};

0 comments on commit f171bd6

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