Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code & Build Cleanup & Enhancements #18

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#
# Copyright (c) 2024 Sidhin S Thomas. All rights reserved
#

cmake_minimum_required(VERSION 3.20)
project(gamemenu)
project(Menu)

set(VERSION_MAJOR 2)
set(VERSION_MAJOR 3)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)

set(CMAKE_CXX_STANDARD 20)

option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

if (NOT DEFINED LINK_TYPE)
message("Library link type not specified, using default - STATIC")
set(LINK_TYPE STATIC)
Expand All @@ -22,38 +28,38 @@ FetchContent_Declare(SFML

FetchContent_MakeAvailable(SFML)


set(SOURCES
"public/game_menu/game_menu.h"
"src/game_menu_impl.cpp"
"public/game_menu/Menu.h"
"src/MenuImpl.h"
"src/MenuImpl.cpp"
)

add_library(gamemenu ${LINK_TYPE} ${SOURCES})
target_link_libraries(gamemenu PUBLIC sfml-graphics)
add_library(Menu ${LINK_TYPE} ${SOURCES})
target_link_libraries(Menu PUBLIC sfml-graphics)
target_include_directories(
gamemenu
Menu
PUBLIC
"public"
"include"
)

set(GMENU_HEADERS
"public/game_menu/game_menu.h"
"public/game_menu/Menu.h"
)

set_target_properties(gamemenu PROPERTIES
set_target_properties(Menu PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
MACOSX_FRAMEWORK_IDENTIFIER in.sidhin.gamemenu
MACOSX_FRAMEWORK_IDENTIFIER in.sidhin.Menu
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
MACOSX_FRAMEWORK_BUNDLE_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
PUBLIC_HEADER "${GMENU_HEADERS}")

if(WIN32)
add_custom_command(
TARGET CMakeSFMLProject
TARGET Menu
COMMENT "Copy OpenAL DLL"
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:CMakeSFMLProject>
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>
VERBATIM)
endif()

Expand All @@ -63,7 +69,7 @@ if(BUILD_WITH_EXAMPLE)
add_executable(example
"examples/sample_menu.cpp"
)
target_link_libraries(example PRIVATE gamemenu)
target_link_libraries(example PRIVATE Menu)
add_custom_target(copy_example_font
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/examples/sansation.ttf ${CMAKE_BINARY_DIR}
)
Expand Down
164 changes: 111 additions & 53 deletions examples/sample_menu.cpp
Original file line number Diff line number Diff line change
@@ -1,71 +1,129 @@
#include "game_menu/game_menu.h"
/*
Copyright (c) 2024 Sidhin S Thomas. All rights reserved
*/

#include <SFML/Graphics.hpp>
#include <memory>
#include <vector>

int main() {
sf::RenderWindow w( sf::VideoMode( 800, 600 ), "Sample Title", sf::Style::Close);
#include <SFML/Graphics.hpp>

#include "game_menu/Menu.h"

int main()
{
// Creating the SFML RenderWindow object for displaying the example.
sf::RenderWindow window(sf::VideoMode(800, 600), "Sample Menu Example", sf::Style::Close);

// Loading the font from the example font file.
sf::Font font;
font.loadFromFile( "sansation.ttf" );
game_menu::Style style {
.TitleFont = &font,
.ItemFont = &font,
.TitleFontSize = 36,
.ItemFontSize = 24,
.MenuTitleScaleFactor = 1,
.MenuItemScaleFactor = 1.5,
.colorScheme = {
.titleColor = 0xFFFFFF,
.itemColor = 0xFFFFFF,
.selectedColor = 0xFF22F1
},
.PaddingTitle = {
.top = 100,
.left = 0,
},
.PaddingItems = {
.top = 40,
},
.TitleAlign = game_menu::Align::Center,
.ItemAlign = game_menu::Align::Center
};
font.loadFromFile("sansation.ttf");

bool is_exit_requested = false;
// Variable to be captured by action functions below for the example.
auto isExitRequested = false;

std::vector<game_menu::MenuItem> items {
{ "New Game", [](sf::RenderTarget &target) {}},
{ "Load Game", [](sf::RenderTarget &target) {}},
{ "Leaderboard", [](sf::RenderTarget &target) {}},
{ "Settings", [](sf::RenderTarget &target) {}},
{ "Exit", [&is_exit_requested](sf::RenderTarget &target) {is_exit_requested = true;}}
};
// This configuration defines the layout and operation of a menu.
Menu::MenuConfig menuConfig;

game_menu::MenuConfig config {
.title = "My Game",
.items = items,
.style = style
// The items list should have all selectable menu items in the list. Along with each string include a
// function pointer to the action you want performed when the menu item is selected. In the example
// below we have added 3 menu items with generic names. The third item "Exit" has an example exit method.
menuConfig.Items =
{
// Text and function pointers.
{ "Menu Item 1", [](sf::RenderTarget& target) {} },
{ "Menu Item 2", [](sf::RenderTarget& target) {} },
{ "Exit", [&](sf::RenderTarget& target) { isExitRequested = true; } },
};

// The ItemStyle member defines the style to apply to all items in the menu.

// Alignment Defines how the text of menu items is aligned. Center is the default.
menuConfig.ItemStyle.Alignment = Menu::Align::Center;
// Color to use for menu items that are not selected.
menuConfig.ItemStyle.Color = sf::Color::Blue;

// Font to use for menu item text.
menuConfig.ItemStyle.Font = font;

// Size of the menu item font in points.
menuConfig.ItemStyle.FontSize = 24;

// Padding for menu item text in pixels.
menuConfig.ItemStyle.Padding.Bottom = 10.0f;
menuConfig.ItemStyle.Padding.Left = 10.0f;
menuConfig.ItemStyle.Padding.Right = 10.0f;
menuConfig.ItemStyle.Padding.Top = 10.0f;

// The color to use when a menu item is selected.
menuConfig.SelectedItemColor = sf::Color::Cyan;

// The TitleStyle member defines the style to apply to the title.

// Gives the alignment of the title.
menuConfig.TitleStyle.Alignment = Menu::Align::Center;

// Color to use for the title text.
menuConfig.TitleStyle.Color = sf::Color::Green;

// Font to use for title text.
menuConfig.TitleStyle.Font = font;

// Size of the title font in points.
menuConfig.TitleStyle.FontSize = 36;

// Padding for title text in pixels.
menuConfig.TitleStyle.Padding.Bottom = 20.0f;
menuConfig.TitleStyle.Padding.Left = 20.0f;
menuConfig.TitleStyle.Padding.Right = 20.0f;
menuConfig.TitleStyle.Padding.Top = 20.0f;

// Test to use as the title of the menu.
menuConfig.TitleText = "My Menu Title";

auto menu_ptr = create_menu_context(w, config);
std::unique_ptr<game_menu::MENU, decltype(&menu_destroy_context)> menu(menu_ptr, &menu_destroy_context);
while (w.isOpen()) {
if (is_exit_requested) {
w.close();
// Scaling factor to apply to the whole menu.
menuConfig.ScalingFactor = 1.0;

// Use the BuildMenu factory function in the IMenu header. This gives a fully built menu object.
auto menu = Menu::BuildMenu(window.getSize(), menuConfig);

// This while loop ill control the menu operation.
while (window.isOpen())
{
// The isExitRequested was passed into the "Exit" menu action by reference.
if (isExitRequested)
{
// When the isExitRequested goes true the user pressed enter on the "Exit" action.
window.close();
break;
}
sf::Event event;
while (w.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
is_exit_requested = true;

// Poll the events from the window.
sf::Event windowEvent;
while (window.pollEvent(windowEvent))
{
// Handle the closed event here, but pass all other events to the menu since it's displayed.
if (windowEvent.type == sf::Event::Closed)
{
// Just handle this as if the "Exit" entry had been seleced.
isExitRequested = true;
}
else
{
// Pass the event to the menu
menu->HandleEvent(windowEvent, window);
}
menu_handle_event(menu.get(), event);
}
w.clear();
menu_render(menu.get());
w.display();

// Clear the window.
window.clear();

// Draw the menu on the window.
menu->Draw(window);

// Tell the window to display.
window.display();
}

// Exit from the example.
return 0;
}

Loading