|
1 |
| -#include "game_menu/game_menu.h" |
| 1 | +/* |
| 2 | + Copyright (c) 2024 Sidhin S Thomas. All rights reserved |
| 3 | +*/ |
2 | 4 |
|
3 |
| -#include <SFML/Graphics.hpp> |
4 | 5 | #include <memory>
|
5 | 6 | #include <vector>
|
6 | 7 |
|
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); |
9 | 16 |
|
| 17 | + // Loading the font from the example font file. |
10 | 18 | 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"); |
34 | 20 |
|
35 |
| - bool is_exit_requested = false; |
| 21 | + // Variable to be captured by action functions below for the example. |
| 22 | + auto isExitRequested = false; |
36 | 23 |
|
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; |
44 | 26 |
|
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; } }, |
49 | 36 | };
|
| 37 | + |
| 38 | + // The ItemStyle member defines the style to apply to all items in the menu. |
50 | 39 |
|
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(); |
56 | 94 | break;
|
57 | 95 | }
|
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); |
62 | 111 | }
|
63 |
| - menu_handle_event(menu.get(), event); |
64 | 112 | }
|
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(); |
68 | 122 | }
|
| 123 | + |
| 124 | + // Exit from the example. |
69 | 125 | return 0;
|
70 | 126 | }
|
71 |
| - |
|
0 commit comments