Skip to content

Commit ec9803b

Browse files
authored
Merge pull request #4 from ParadoxZero/refactor
Refactored code for better readability
2 parents 10467d3 + e8db2fd commit ec9803b

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

include/GameMenu/GameMenu.h

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,76 @@ namespace gmenu {
3333
sf::Font font;
3434
};
3535

36-
/* Generic Menu - can be instantiated to generate a custom menu as needed over a sf::RenderWindow */
3736
class Menu {
37+
/* Generic Menu - can be instantiated to generate a custom menu as needed over a sf::RenderWindow */
38+
39+
public:
40+
41+
/* Only accesible constructor */
42+
Menu(sf::RenderWindow *window, std::string title,sf::Font titleFont, MenuItem* items, int8_t length)
43+
: Menu(window, title, titleFont) {
44+
setMenuItems(items, length);
45+
}
46+
47+
48+
49+
/* This method is will start the menu and handover the screen control to it.
50+
The Event loop will be controlled by this function after the call, and
51+
only after Back/exit on the menu will the control be returned.
52+
53+
The control is returned when an "gmenu::Action" object is called
54+
whose start return "false" */
55+
void createMenu();
56+
57+
/* In case menu items needs to be changed */
58+
void setMenuItems(MenuItem *, int8_t);
59+
60+
/* In case the title needs to be changed */
61+
void setTitle(std::string title, sf::Font font);
62+
63+
3864
private:
65+
66+
Menu( sf::RenderWindow *wnd ) {
67+
window = wnd;
68+
}
69+
70+
71+
Menu( sf::RenderWindow *window, std::string title, sf::Font titleFont ) : Menu( window ) {
72+
setTitle( title, titleFont );
73+
}
74+
75+
void writeText( std::string string, sf::Font font, unsigned int size, float x, float y,
76+
const sf::Color &color = sf::Color::White );
77+
78+
void setMenu();
79+
80+
void drawMenu();
81+
82+
83+
/*==================================================*
84+
* Internal structuers *
85+
*===================================================*/
3986

40-
/* structures to hold the menu item informtion */
4187
struct {
4288
MenuItem *entries;
4389
int8_t size;
4490
} menu_items;
4591

4692
struct coordinates {
47-
coordinates() { x = y = 0.f; size = 0; }
93+
coordinates() {
94+
x = y = 0.f; size = 0;
95+
}
4896
float x;
4997
float y;
5098
int size;
5199
} *menu_location, title_location;
52100

53101

102+
/*==================================================*
103+
* Data Members *
104+
*===================================================*/
105+
54106
int currently_selected_item = 0;
55107

56108
sf::Font MenuItemFont;
@@ -62,36 +114,6 @@ namespace gmenu {
62114
float MenuTitleScaleFactor = 0.125;
63115
float MenuItemScaleFactor = 0.25;
64116

65-
66-
void writeText(std::string string, sf::Font font, unsigned int size, float x, float y,
67-
const sf::Color &color = sf::Color::White);
68-
69-
void setMenu();
70-
71-
void drawMenu();
72-
73-
74-
public:
75-
Menu(sf::RenderWindow *wnd) {
76-
window = wnd;
77-
}
78-
79-
Menu(sf::RenderWindow *window, std::string title, sf::Font titleFont) : Menu(window) {
80-
setTitle(title,titleFont);
81-
}
82-
83-
Menu(sf::RenderWindow *window, std::string title,sf::Font titleFont, MenuItem* items, int8_t length)
84-
: Menu(window, title, titleFont) {
85-
setMenuItems(items, length);
86-
}
87-
88-
void setMenuItems(MenuItem *, int8_t);
89-
90-
void setTitle(std::string title, sf::Font font);
91-
92-
void createMenu();
93-
94-
95117
}; // Menu
96118

97119
} // namespace sui

lib/libGameMenu.a

322 Bytes
Binary file not shown.

src/GameMenu/GameMenu.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
namespace gmenu {
1010

11-
/* Public function definitions*/
11+
12+
/*==================================================*
13+
* public functions *
14+
*===================================================*/
15+
1216
void Menu::setTitle(std::string title, sf::Font font) {
1317
menu_title = title;
1418
MenuTitleFont = font;
@@ -19,6 +23,11 @@ namespace gmenu {
1923
menu_items.size = length;
2024
}
2125

26+
27+
/*
28+
This function constains the main event loop for the menu
29+
The actions performed : drawMenu() -> pollEvent() -> prefromAction()
30+
*/
2231
void Menu::createMenu() {
2332
setMenu();
2433
bool cont = true;
@@ -50,8 +59,13 @@ namespace gmenu {
5059
} //create menu
5160

5261

53-
/* Private function definitions */
5462

63+
/*==================================================*
64+
* Private Functions *
65+
*===================================================*/
66+
67+
68+
/* Utility function */
5569
void Menu::writeText(std::string str, sf::Font font, unsigned int size, float x, float y, const sf::Color &color) {
5670
sf::Text text;
5771
text.setString(str);

0 commit comments

Comments
 (0)