Skip to content

Commit ae8a657

Browse files
committed
More cleanup. Moved required version to C++14
1 parent eca3337 commit ae8a657

File tree

8 files changed

+455
-327
lines changed

8 files changed

+455
-327
lines changed

CMakeLists.txt

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
#
2+
# Copyright (c) 2024 Sidhin S Thomas. All rights reserved
3+
#
4+
15
cmake_minimum_required(VERSION 3.20)
2-
project(gamemenu)
6+
project(Menu)
37

4-
set(VERSION_MAJOR 2)
8+
set(VERSION_MAJOR 3)
59
set(VERSION_MINOR 0)
610
set(VERSION_PATCH 0)
711

812
set(CMAKE_CXX_STANDARD 20)
913

14+
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
15+
1016
if (NOT DEFINED LINK_TYPE)
1117
message("Library link type not specified, using default - STATIC")
1218
set(LINK_TYPE STATIC)
@@ -22,48 +28,48 @@ FetchContent_Declare(SFML
2228

2329
FetchContent_MakeAvailable(SFML)
2430

25-
2631
set(SOURCES
27-
"public/game_menu/game_menu.h"
28-
"src/game_menu_impl.cpp"
32+
"public/game_menu/Menu.h"
33+
"src/MenuImpl.h"
34+
"src/MenuImpl.cpp"
2935
)
3036

31-
add_library(gamemenu ${LINK_TYPE} ${SOURCES})
32-
target_link_libraries(gamemenu PUBLIC sfml-graphics)
37+
add_library(Menu ${LINK_TYPE} ${SOURCES})
38+
target_link_libraries(Menu PUBLIC sfml-graphics)
3339
target_include_directories(
34-
gamemenu
40+
Menu
3541
PUBLIC
3642
"public"
3743
"include"
3844
)
3945

4046
set(GMENU_HEADERS
41-
"public/game_menu/game_menu.h"
47+
"public/game_menu/Menu.h"
4248
)
4349

44-
set_target_properties(gamemenu PROPERTIES
50+
set_target_properties(Menu PROPERTIES
4551
FRAMEWORK TRUE
4652
FRAMEWORK_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
47-
MACOSX_FRAMEWORK_IDENTIFIER in.sidhin.gamemenu
53+
MACOSX_FRAMEWORK_IDENTIFIER in.sidhin.Menu
4854
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
4955
MACOSX_FRAMEWORK_BUNDLE_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
5056
PUBLIC_HEADER "${GMENU_HEADERS}")
5157

5258
if(WIN32)
5359
add_custom_command(
54-
TARGET gamemenu
60+
TARGET Menu
5561
COMMENT "Copy OpenAL DLL"
56-
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:gamemenu>
62+
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<EQUAL:${CMAKE_SIZEOF_VOID_P},8>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:Menu>
5763
VERBATIM)
5864
endif()
5965

60-
SET(BUILD_WITH_EXAMPLE OFF CACHE BOOL "Build attached example")
66+
SET(BUILD_WITH_EXAMPLE ON CACHE BOOL "Build attached example")
6167

6268
if(BUILD_WITH_EXAMPLE)
6369
add_executable(example
6470
"examples/sample_menu.cpp"
6571
)
66-
target_link_libraries(example PRIVATE gamemenu)
72+
target_link_libraries(example PRIVATE Menu)
6773
add_custom_target(copy_example_font
6874
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/examples/sansation.ttf ${CMAKE_BINARY_DIR}
6975
)

examples/sample_menu.cpp

+108-53
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,126 @@
1-
#include "game_menu/game_menu.h"
1+
/*
2+
Copyright (c) 2024 Sidhin S Thomas. All rights reserved
3+
*/
24

3-
#include <SFML/Graphics.hpp>
45
#include <memory>
56
#include <vector>
67

7-
int main() {
8-
sf::RenderWindow w( sf::VideoMode( 800, 600 ), "Sample Title", sf::Style::Close);
8+
#include <SFML/Graphics.hpp>
9+
10+
#include "game_menu/Menu.h"
11+
12+
int main()
13+
{
14+
// Creating the SFML RenderWindow object for displaying the example.
15+
sf::RenderWindow window(sf::VideoMode(800, 600), "Sample Menu Example", sf::Style::Close);
916

17+
// Loading the font from the example font file.
1018
sf::Font font;
11-
font.loadFromFile( "sansation.ttf" );
12-
game_menu::Style style {
13-
.TitleFont = &font,
14-
.ItemFont = &font,
15-
.TitleFontSize = 36,
16-
.ItemFontSize = 24,
17-
.MenuTitleScaleFactor = 1,
18-
.MenuItemScaleFactor = 1.5,
19-
.colorScheme = {
20-
.titleColor = 0xFFFFFF,
21-
.itemColor = 0xFFFFFF,
22-
.selectedColor = 0xFF22F1
23-
},
24-
.PaddingTitle = {
25-
.top = 100,
26-
.left = 0,
27-
},
28-
.PaddingItems = {
29-
.top = 40,
30-
},
31-
.TitleAlign = game_menu::Align::Center,
32-
.ItemAlign = game_menu::Align::Center
33-
};
19+
font.loadFromFile("sansation.ttf");
3420

35-
bool is_exit_requested = false;
21+
// Variable to be captured by action functions below for the example.
22+
auto isExitRequested = false;
3623

37-
std::vector<game_menu::MenuItem> items {
38-
{ "New Game", [](sf::RenderTarget &target) {}},
39-
{ "Load Game", [](sf::RenderTarget &target) {}},
40-
{ "Leaderboard", [](sf::RenderTarget &target) {}},
41-
{ "Settings", [](sf::RenderTarget &target) {}},
42-
{ "Exit", [&is_exit_requested](sf::RenderTarget &target) {is_exit_requested = true;}}
43-
};
24+
// This configuration defines the layout and operation of a menu.
25+
Menu::MenuConfig menuConfig;
4426

45-
game_menu::MenuConfig config {
46-
.title = "My Game",
47-
.items = items,
48-
.style = style
27+
// The items list should have all selectable menu items in the list. Along with each string include a
28+
// function pointer to the action you want performed when the menu item is selected. In the example
29+
// below we have added 3 menu items with generic names. The third item "Exit" has an example exit method.
30+
menuConfig.Items =
31+
{
32+
// Text and function pointers.
33+
{ "Menu Item 1", [](sf::RenderTarget& target) {} },
34+
{ "Menu Item 2", [](sf::RenderTarget& target) {} },
35+
{ "Exit", [&](sf::RenderTarget& target) { isExitRequested = true; } },
4936
};
37+
38+
// The ItemStyle member defines the style to apply to all items in the menu.
5039

51-
auto menu_ptr = create_menu_context(w, config);
52-
std::unique_ptr<game_menu::MENU, decltype(&menu_destroy_context)> menu(menu_ptr, &menu_destroy_context);
53-
while (w.isOpen()) {
54-
if (is_exit_requested) {
55-
w.close();
40+
// Alignment Defines how the text of menu items is aligned. Center is the default.
41+
menuConfig.ItemStyle.Alignment = Menu::Align::Center;
42+
// Color to use for menu items that are not selected.
43+
menuConfig.ItemStyle.Color = sf::Color::Blue;
44+
45+
// Font to use for menu item text.
46+
menuConfig.ItemStyle.Font = font;
47+
48+
// Size of the menu item font in points.
49+
menuConfig.ItemStyle.FontSize = 24;
50+
51+
// Padding for menu item text in pixels.
52+
menuConfig.ItemStyle.Padding.Bottom = 10.0f;
53+
menuConfig.ItemStyle.Padding.Left = 10.0f;
54+
menuConfig.ItemStyle.Padding.Right = 10.0f;
55+
menuConfig.ItemStyle.Padding.Top = 10.0f;
56+
57+
// The color to use when a menu item is selected.
58+
menuConfig.SelectedItemColor = sf::Color::Cyan;
59+
60+
// The TitleStyle member defines the style to apply to the title.
61+
62+
// Gives the alignment of the title.
63+
menuConfig.TitleStyle.Alignment = Menu::Align::Center;
64+
65+
// Color to use for the title text.
66+
menuConfig.TitleStyle.Color = sf::Color::Green;
67+
68+
// Font to use for title text.
69+
menuConfig.TitleStyle.Font = font;
70+
71+
// Size of the title font in points.
72+
menuConfig.TitleStyle.FontSize = 36;
73+
74+
// Padding for title text in pixels.
75+
menuConfig.TitleStyle.Padding.Bottom = 20.0f;
76+
menuConfig.TitleStyle.Padding.Left = 20.0f;
77+
menuConfig.TitleStyle.Padding.Right = 20.0f;
78+
menuConfig.TitleStyle.Padding.Top = 20.0f;
79+
80+
// Test to use as the title of the menu.
81+
menuConfig.TitleText = "My Menu Title";
82+
83+
// Use the BuildMenu factory function in the IMenu header. This gives a fully built menu object.
84+
auto menu = Menu::BuildMenu(window.getSize(), menuConfig);
85+
86+
// This while loop ill control the menu operation.
87+
while (window.isOpen())
88+
{
89+
// The isExitRequested was passed into the "Exit" menu action by reference.
90+
if (isExitRequested)
91+
{
92+
// When the isExitRequested goes true the user pressed enter on the "Exit" action.
93+
window.close();
5694
break;
5795
}
58-
sf::Event event;
59-
while (w.pollEvent(event)) {
60-
if (event.type == sf::Event::Closed) {
61-
is_exit_requested = true;
96+
97+
// Poll the events from the window.
98+
sf::Event windowEvent;
99+
while (window.pollEvent(windowEvent))
100+
{
101+
// Handle the closed event here, but pass all other events to the menu since it's displayed.
102+
if (windowEvent.type == sf::Event::Closed)
103+
{
104+
// Just handle this as if the "Exit" entry had been seleced.
105+
isExitRequested = true;
106+
}
107+
else
108+
{
109+
// Pass the event to the menu
110+
menu->HandleEvent(windowEvent, window);
62111
}
63-
menu_handle_event(menu.get(), event);
64112
}
65-
w.clear();
66-
menu_render(menu.get());
67-
w.display();
113+
114+
// Clear the window.
115+
window.clear();
116+
117+
// Draw the menu on the window.
118+
menu->Draw(window);
119+
120+
// Tell the window to display.
121+
window.display();
68122
}
123+
124+
// Exit from the example.
69125
return 0;
70126
}
71-

0 commit comments

Comments
 (0)