Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

options

  • Loading branch information...
commit f2be0c760994a2550f1e7e9447aa45a44a8d565f 1 parent b496c10
Dmytro Lytovchenko authored
3  src/game.cpp
@@ -17,6 +17,7 @@ MyGame * MyGame::m_game = NULL;
17 17
18 18 MyGame::MyGame()
19 19 : m_hge(NULL), m_font(NULL), m_mouse_cursor_sprite(NULL), m_state(NULL)
  20 + , m_sound_enabled(true)
20 21 {
21 22 // remember pointer to 'this' in globally visible variable so that everyone
22 23 // else can reach it (not very good example of software design, but this simplifies a lot)
@@ -114,7 +115,7 @@ bool MyGame::Startup()
114 115 m_font = new hgeFont("font1.fnt");
115 116 m_mouse_cursor_sprite = new hgeSprite(m_mouse_cursor_tex,0,0,32,32);
116 117
117   - m_state_options = new GameState_Options();
  118 + m_state_options = new GameState_Options( this );
118 119 m_state_mainmenu = new GameState_MainMenu( this );
119 120 m_state_instructions = new GameState_Instructions( this );
120 121 m_state_credits = new GameState_Credits( this );
3  src/game.h
@@ -20,6 +20,9 @@ class MyGame
20 20 // Pointer to the HGE interface.
21 21 HGE * m_hge;
22 22
  23 + // flag set to 0 or 1 to enable sound effects
  24 + int m_sound_enabled;
  25 +
23 26 // Some resource handles
24 27 HEFFECT m_click_sound;
25 28 HTEXTURE m_mouse_cursor_tex;
142 src/game_state.cpp
@@ -9,6 +9,11 @@
9 9
10 10 #include <math.h>
11 11
  12 +char * _opt_sound_text[2] = {
  13 + "Sound: Disabled",
  14 + "Sound: Enabled"
  15 +};
  16 +
12 17
13 18 // array of strings of unknown length (NULL marks the end).
14 19 // You may delete few lines or add new lines before NULL if screen height allows.
@@ -19,7 +24,9 @@ const char * _page1_text[] = {
19 24 "You will have to avoid obstacles, and kill stuff.",
20 25 NULL
21 26 };
22   -// array of strings of unknown length (NULL marks the end)
  27 +
  28 +
  29 +// arrayof strings of unknown length (NULL marks the end)
23 30 // You may delete few lines or add new lines before NULL if screen height allows.
24 31 const char * _page2_text[] = {
25 32 "You can move with A/D/arrows, jump with W or space",
@@ -28,23 +35,26 @@ const char * _page2_text[] = {
28 35 "between the end of screen and the wall, you will die.",
29 36 NULL
30 37 };
  38 +
  39 +
31 40 // array of strings of unknown length (NULL marks the end)
32 41 // You may delete few lines or add new lines before NULL if screen height allows.
33 42 const char * _credits_text[] = {
34 43 "This brilliant masterpiece",
35 44 "was brought to you by",
36   - "",
  45 + "-",
37 46 "big boss Haaf, the master and",
38 47 "the maker of HGE Engine",
39   - "",
  48 + "-",
40 49 "and your humble slave, kvakvs,",
41 50 "who made this ugly game",
42   - "",
  51 + "***",
43 52 "tremble, mortals, before it",
44 53 "and feel free to edit to your liking",
45 54 NULL
46 55 };
47 56
  57 +
48 58 // This should render main menu
49 59 void GameState_MainMenu::Render( MyGame * game )
50 60 {
@@ -100,26 +110,26 @@ bool GameState_MainMenu::Think( MyGame * game )
100 110 switch(lastid)
101 111 {
102 112 case MAINMENU_ELEMENT_PLAY:
103   - m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
104   - m_gui->Enter();
  113 +// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
  114 + m_gui->Enter();
105 115 break;
106 116
107 117 case MAINMENU_ELEMENT_OPTIONS:
108 118 game->ShowOptionsScreen();
109   - m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
110   - m_gui->Enter();
  119 +// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
  120 + m_gui->Enter();
111 121 break;
112 122
113 123 case MAINMENU_ELEMENT_INSTRUCTIONS:
114 124 game->ShowInstructionsScreen();
115   - m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
116   - m_gui->Enter();
  125 +// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
  126 + m_gui->Enter();
117 127 break;
118 128
119 129 case MAINMENU_ELEMENT_CREDITS:
120 130 game->ShowCreditsScreen();
121   - m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
122   - m_gui->Enter();
  131 +// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
  132 + m_gui->Enter();
123 133 break;
124 134
125 135 case MAINMENU_ELEMENT_EXIT:
@@ -142,15 +152,20 @@ GameState_MainMenu::GameState_MainMenu( MyGame * game )
142 152 // Create and initialize the GUI
143 153 m_gui = new hgeGUI();
144 154
145   - m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_PLAY, game->m_font, game->m_click_sound,400,200,0.0f,"Play"));
146   - m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_OPTIONS, game->m_font, game->m_click_sound,400,240,0.1f,"Options"));
147   - m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_INSTRUCTIONS, game->m_font, game->m_click_sound,400,280,0.2f,"Instructions"));
148   - m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_CREDITS, game->m_font, game->m_click_sound,400,320,0.3f,"Credits"));
149   - m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_EXIT, game->m_font, game->m_click_sound,400,360,0.4f,"Exit"));
  155 + m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_PLAY, game->m_font, game->m_click_sound,
  156 + 400, MAINMENU_TOP_ITEM_Y, 0.0f, "Play"));
  157 + m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_OPTIONS, game->m_font, game->m_click_sound,
  158 + 400, MAINMENU_TOP_ITEM_Y+MAINMENU_ITEM_HEIGHT, 0.1f, "Options"));
  159 + m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_INSTRUCTIONS, game->m_font, game->m_click_sound,
  160 + 400, MAINMENU_TOP_ITEM_Y+2*MAINMENU_ITEM_HEIGHT, 0.2f, "Instructions"));
  161 + m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_CREDITS, game->m_font, game->m_click_sound,
  162 + 400, MAINMENU_TOP_ITEM_Y+3*MAINMENU_ITEM_HEIGHT, 0.3f, "Credits"));
  163 + m_gui->AddCtrl(new hgeGUIMenuItem(MAINMENU_ELEMENT_EXIT, game->m_font, game->m_click_sound,
  164 + 400, MAINMENU_TOP_ITEM_Y+4*MAINMENU_ITEM_HEIGHT, 0.4f, "Exit"));
150 165
151 166 m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
152 167 m_gui->SetCursor( game->m_mouse_cursor_sprite );
153   - m_gui->SetFocus(1);
  168 + m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
154 169 m_gui->Enter();
155 170 }
156 171
@@ -206,8 +221,8 @@ bool GameState_Instructions::Think( MyGame * game )
206 221
207 222 case INSTRUC_ELEMENT_EXIT:
208 223 game->ShowMainMenuScreen();
209   - m_gui->SetFocus(INSTRUC_ELEMENT_EXIT);
210   - m_gui->Enter();
  224 +// m_gui->SetFocus(INSTRUC_ELEMENT_EXIT);
  225 +// m_gui->Enter();
211 226 break;
212 227 }
213 228 } else if(id) {
@@ -229,13 +244,13 @@ GameState_Instructions::GameState_Instructions( MyGame * game )
229 244
230 245 m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_PAGE1, game->m_font, game->m_click_sound,100,550,0.0f,"Page 1"));
231 246 m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_PAGE2, game->m_font, game->m_click_sound,400,550,0.1f,"Page 2"));
232   - m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_EXIT, game->m_font, game->m_click_sound,700,550,0.4f,"Done"));
  247 + m_gui->AddCtrl(new hgeGUIMenuItem(INSTRUC_ELEMENT_EXIT, game->m_font, game->m_click_sound,700,550,0.2f,"Done"));
233 248
234 249 PopulateInstructionsPage( game, _page1_text );
235 250
236 251 //m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
237 252 m_gui->SetCursor( game->m_mouse_cursor_sprite );
238   - //m_gui->SetFocus(1);
  253 + m_gui->SetFocus(INSTRUC_ELEMENT_PAGE1);
239 254 m_gui->Enter();
240 255 }
241 256
@@ -268,7 +283,7 @@ GameState_Credits::GameState_Credits( MyGame * game )
268 283 {
269 284 // Create and initialize the GUI
270 285 m_gui = new hgeGUI();
271   - m_gui->AddCtrl(new hgeGUIMenuItem(CREDITS_ELEMENT_EXIT, game->m_font, game->m_click_sound,700,550,0.4f,"Done"));
  286 + m_gui->AddCtrl(new hgeGUIMenuItem(CREDITS_ELEMENT_EXIT, game->m_font, game->m_click_sound,700,550,0.0f,"Done"));
272 287
273 288 m_draw_credits_y = 600; // bottom of screen, will decrease to slide up
274 289 //PopulateCredits( game );
@@ -286,6 +301,7 @@ GameState_Credits::~GameState_Credits()
286 301
287 302
288 303 /*
  304 +// This shows how to fill array of hgeGUIText's with values
289 305 void GameState_Credits::PopulateCredits( MyGame * game )
290 306 {
291 307 m_credits_row_count = 0;
@@ -348,7 +364,7 @@ bool GameState_Credits::Think( MyGame * game )
348 364 {
349 365 case CREDITS_ELEMENT_EXIT:
350 366 game->ShowMainMenuScreen();
351   - m_gui->SetFocus(CREDITS_ELEMENT_EXIT);
  367 + //m_gui->SetFocus(CREDITS_ELEMENT_EXIT);
352 368 m_gui->Enter();
353 369 break;
354 370 }
@@ -366,11 +382,87 @@ bool GameState_Credits::Think( MyGame * game )
366 382 hgeGUIText * t = (hgeGUIText *)m_gui->GetCtrl( i );
367 383 if( ! t ) continue;
368 384
369   - m_gui->MoveCtrl( i, 0, 0 ); // t->rect.x1, t->rect.y1 - dt * 20.0f );
  385 + // this one doesn't move anything, at all. Might be my mistake of course.
  386 + m_gui->MoveCtrl( i, t->rect.x1, t->rect.y1 - dt * 20.0f );
370 387 }
371 388 */
372 389 // this is rough speed estimation, should be properly reworked with millisecond timer
373 390 m_draw_credits_y -= dt * CREDITS_SPEED;
374 391
375 392 return false;
  393 +}
  394 +
  395 +
  396 +GameState_Options::GameState_Options( MyGame * game )
  397 +{
  398 + // Create and initialize the GUI
  399 + m_gui = new hgeGUI();
  400 +
  401 + m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_SOUND, game->m_font, game->m_click_sound,
  402 + 400, OPTIONS_TOP_ITEM_Y, 0.0f, _opt_sound_text[game->m_sound_enabled] ));
  403 + m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_EXIT, game->m_font, game->m_click_sound,
  404 + 400, OPTIONS_TOP_ITEM_Y+OPTIONS_ITEM_HEIGHT, 0.1f, "Exit"));
  405 +
  406 + //m_gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
  407 + m_gui->SetCursor( game->m_mouse_cursor_sprite );
  408 + m_gui->SetFocus( OPTIONS_ELEMENT_SOUND );
  409 + m_gui->Enter();
  410 +}
  411 +
  412 +
  413 +GameState_Options::~GameState_Options()
  414 +{
  415 + delete m_gui;
  416 +}
  417 +
  418 +void GameState_Options::Render( MyGame * game )
  419 +{
  420 + game->m_hge->Gfx_RenderQuad( &game->m_background_quad );
  421 + m_gui->Render();
  422 +}
  423 +
  424 +bool GameState_Options::Think( MyGame * game )
  425 +{
  426 + float dt = game->m_hge->Timer_GetDelta();
  427 + int id;
  428 + static int lastid = OPTIONS_ELEMENT_NONE_SELECTED;
  429 +
  430 + // If ESCAPE was pressed, tell the GUI to finish
  431 + if (game->m_hge->Input_GetKeyState(HGEK_ESCAPE)) {
  432 + lastid = OPTIONS_ELEMENT_EXIT;
  433 + m_gui->Leave();
  434 + }
  435 +
  436 + // We update the GUI and take an action if
  437 + // one of the menu items was selected
  438 + id = m_gui->Update(dt);
  439 + if(id == -1)
  440 + {
  441 + switch(lastid)
  442 + {
  443 + case OPTIONS_ELEMENT_SOUND:
  444 + game->m_sound_enabled = ! game->m_sound_enabled;
  445 +
  446 + // delete and re-add the menu element
  447 + m_gui->DelCtrl(OPTIONS_ELEMENT_SOUND);
  448 + m_gui->AddCtrl(new hgeGUIMenuItem(OPTIONS_ELEMENT_SOUND, game->m_font, game->m_click_sound,
  449 + 400, OPTIONS_TOP_ITEM_Y, 0.0f, _opt_sound_text[game->m_sound_enabled] ));
  450 +
  451 + m_gui->SetFocus( OPTIONS_ELEMENT_SOUND );
  452 + m_gui->Enter();
  453 + break;
  454 +
  455 + case OPTIONS_ELEMENT_EXIT:
  456 + game->ShowMainMenuScreen();
  457 + m_gui->Enter();
  458 + break;
  459 + }
  460 + } else if(id) {
  461 + lastid = id;
  462 + m_gui->Leave();
  463 + }
  464 +
  465 + AnimateBackground( dt, game );
  466 +
  467 + return false;
376 468 }
28 src/game_state.h
@@ -40,6 +40,9 @@ class GameState_MainMenu: public virtual GameState
40 40 void Render( MyGame * game );
41 41 bool Think( MyGame * game );
42 42
  43 + static const int MAINMENU_TOP_ITEM_Y = 200;
  44 + static const int MAINMENU_ITEM_HEIGHT = 40;
  45 +
43 46 // numeric codes for GUI elements
44 47 enum {
45 48 MAINMENU_ELEMENT_NONE_SELECTED = 0,
@@ -52,7 +55,7 @@ class GameState_MainMenu: public virtual GameState
52 55 };
53 56
54 57
55   -// This game state represents instructions screen and only draws text until click or key pressed
  58 +// This game state represents instructions screen and displays 2 pages of options
56 59 class GameState_Instructions: public virtual GameState
57 60 {
58 61 protected:
@@ -77,7 +80,8 @@ class GameState_Instructions: public virtual GameState
77 80 };
78 81
79 82
80   -// This game state represents instructions screen and only draws text until click or key pressed
  83 +// This game state represents credits screen displaying slowly scrolling credits from
  84 +// outside the bottom of screen
81 85 class GameState_Credits: public virtual GameState
82 86 {
83 87 protected:
@@ -108,10 +112,24 @@ class GameState_Credits: public virtual GameState
108 112 };
109 113
110 114
111   -// This game state represents instructions screen and only draws text until click or key pressed
  115 +// This game state represents options screen with a single option: Sound, and Exit button
112 116 class GameState_Options: public virtual GameState
113 117 {
  118 +protected:
  119 + hgeGUI * m_gui;
114 120 public:
115   - void Render( MyGame * game ) {}
116   - bool Think( MyGame * game ) { return false; }
  121 + GameState_Options( MyGame * game );
  122 + virtual ~GameState_Options();
  123 +
  124 + static const int OPTIONS_TOP_ITEM_Y = 200;
  125 + static const int OPTIONS_ITEM_HEIGHT = 40;
  126 +
  127 + enum {
  128 + OPTIONS_ELEMENT_NONE_SELECTED = 0,
  129 + OPTIONS_ELEMENT_SOUND = 1,
  130 + OPTIONS_ELEMENT_EXIT
  131 + };
  132 +
  133 + void Render( MyGame * game );
  134 + bool Think( MyGame * game );
117 135 };

0 comments on commit f2be0c7

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