Menu in C++ using Switches and Arrow keys.

Hi, I'm a very beginner programmer and was given code that could be used to help make a menu for my game. I am not sure how to implement into my program at all. I have two seperate .cpp files, once for the menu, and one for the game.

My question is, how do I implement this method into my menu.cpp file.


This is the menu code I was given to use;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	

enum STATE
{
	MENU,
	GAME,
	INSTRUCTIONS,
	HIGHSCORES,
	EXIT
};

/*
bool statename
*/

bool Menu;
bool Game;

STATE currentstate;

        ////////////////////////////////
	//MENU SWITCHING AND SUCH
	{
		currentstate = MENU;
		bool bcontinueGame = true;
	
		while (bcontinueGame)
		{
			switch(currentstate)
			{
			case MENU:
				bcontinueGame = Menu;
			case GAME:
				bcontinueGame = Game;
			case EXIT:
				break;
			default:
				break;
			};
		}
	}
	////////////////////////////////// 


This is what I have done so far;
I'm not sure at all what to do from now.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SDL_App.cpp : Defines the entry point for our Project
//
//#include "stdafx.h"
#include <Windows.h>
#include "GL_Functions.h"
#include "pctimer.h"
#include <string>
#include <cmath>
#include <stdio.h>
#include <crtdbg.h>

enum STATE
{
	MENU,
	GAME,
	INSTRUCTIONS,
	HIGHSCORES,
	EXIT
};

/*
bool statename
*/

bool Menu;
bool Game;

STATE currentstate;

void main(int argc, char* argv[])
{
	//Opens the window with respective width and height
	InitGL(1024, 768);

	//The selectors texture load and X and Y values
	int Select = LoadTexture("./images/select.png");
	int SelectY = 278;

	float Rot = 0;
	
	////////////////////////////////
	//MENU SWITCHING AND SUCH
	{
		currentstate = MENU;
		bool bcontinueGame = true;
	
		while (bcontinueGame)
		{
			switch(currentstate)
			{
			case MENU:
				bcontinueGame = Menu;
			case GAME:
				bcontinueGame = Game;
			case EXIT:
				break;
			default:
				break;
			};
		}
	}
	//////////////////////////////////
	
	do

	{
		//Clear Screen
		ClearScreen();

		//Headings and menu items
		DrawString("PONG - The Game", 64, 64, 5.0f);
		DrawString("1. Start Game", 64, 256, 3.0f);
		DrawString("2. Instructions", 64, 384, 3.0f);
		DrawString("3. High Scores", 64, 512, 3.0f);
		DrawString("4. Exit", 64, 640, 3.0f);

		//The selector
		DrawSpriteRotated(Select, 32, SelectY, 32, 32, Rot += 200.0f);

		//Moving the selector
		if (IsKeyDown(KEY_DOWN))
		{
			SelectY = SelectY + 128;
		}

		if (SelectY > 700)
		{
			SelectY = 278;
		}

		if (IsKeyDown(KEY_UP))
		{
			SelectY = SelectY - 128;
		}

		if (SelectY < 255)
		{
			SelectY = 278;
		}
			



		//Slows the game
		Sleep(75);
	} 

	//Do some secret stuff
	while (FrameworkUpdate()); 

	//Clean up after yourself
	FreeTexture(Select);

	//Close down
	CloseDown(); 

	_CrtDumpMemoryLeaks();
}



My second question is, how do I use the arrow keys to make selections straight from the menu? I have a rough idea of a way to maybe get it to work, although I'm not sure how I can transition from one screen to another.(e.g. from the Menu.cpp to the game.cpp)

Here is a bit of pseudocode for what I think I might be able to use that should be compatible with my current code;

Pseudocode;
if (selectY == 256)
then move from menu.cpp to game.cpp

Is this okay? Or can I just use the switch menu way straight up?

Thankyou guys in advance.
I will check this post frequently. (although it is currently 1:34AM where I am, so for now, maybe not for the next 5-6 hours, but if you need any more clarification, please do post a question and I will do my best to answer)
Last edited on
hi! so heres what you do.
1) make the main file (commonly called main.cpp) and then link it to your own made header file
2) in that header file include all enums and prototypes
3) make a second cpp file and write across the top #include "my_header.h" or whatever its called.
4) then in the second cpp file include all definitions
@Aramil
Hello~

Yeah, I read over that a few times and like I said, I'm a very beginner C++ programmer, I only started literally last week, so to be blatantly honest, I have NO idea what you said :P I'm still getting a handle on the terms and other such things as well.
SORRY!!

Could you be a little more specific or show me an example? :(
Sorry for troubling you.
Anyone have any ideas?
Really need some help! :(
Can I please get some help?
sorry been busy so make the main file(once again usually called main.cpp) and this will include the main code. At the top of your page with the addition of any header files you would normally #include (i.e. iostream, string, cmath, etc.) and then write #include "my_header.h". it doesn't need to be called my_header that is just for my example. Real quick a function prototype is like this:
1
2
3
4
5
6
7
8
//prototype: data_type function_name (arg_type); for example
int sqr_root(int, int);

//then actual function
int sqr_root(int base, int pow)
{
     /*perform magic*/
}


so then open up a new text document and save it as my_header.h in the same folder as main.cpp in the header file you will include enums, classes, structs, and function prototypes. then make another file called my_header.cpp and at the top write #include "my_header.h" this file is where you put variables and functions. you can now implement it in your game. as to your second question, that would require a graphics library
Ahh, I kind of see, I'll give it a test and see what happens.
Thankyou so much, definitely a big help!

Thanks again! :)
Topic archived. No new replies allowed.