group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
MainMenu.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "IScreen.hpp"
7#include "app/AppContext.hpp"
17
18#include <atomic>
19#include <memory>
20#include <mutex>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <thread>
25#include <vector>
26
29{
30 std::string serverIp;
31 uint16_t serverPort;
32 uint32_t globalServerId = 0;
33 std::string serverName;
34};
35
37class MainMenu : public IScreen
38{
39public:
43
44 SDL_AppResult event(SDL_Event* event) override;
45 SDL_AppResult iterate() override;
46 void quit() override;
47
50 std::optional<JoinRequest> consumeJoinRequest();
51
54 bool consumeHostRequest();
55
59
61 bool consumeExitRequest();
62
64 void setJoinError(const std::string& error);
65
67 void setLaunchError(const std::string& error);
68
70 void setJoinInProgress(bool joining, const std::string& label = {});
71
73 void setPopupMessage(const std::string& message);
74
77
80
83
86
87private:
94
96 bool canManageCurrentServer() const;
97
99 bool hasUnsavedServerChanges() const;
100
103
106
109
112
114 SDL_Window* window = nullptr;
115 Client* client = nullptr;
120 std::string_view settingsPath;
124 std::string lastHostError;
125 std::optional<MatchConfig> lastSyncedMatchConfig;
126 std::optional<DiscoverySettings> lastSyncedDiscoverySettings;
129 std::optional<JoinRequest>
131 bool pendingHostRequest = false;
132 bool pendingLaunch = false;
133 bool pendingShutdown = false;
134 bool pendingGoToLobby = false;
136 false;
137 bool pendingExitRequest = false;
138 std::string joinError;
139 std::string popupMessage;
140 bool openPopupMessage = false;
141
142 std::unique_ptr<DiscoveryClient> localDiscoveryClient = std::make_unique<DiscoveryClient>();
143
145 void startGlobalRefresh(bool force = false);
146
149
150 std::vector<net::discovery::ServerInfo> globalServers;
151 std::vector<DiscoveryClient::DiscoveredServer> localServers;
152 std::string browserError;
153 std::mutex browserMutex;
154 std::thread browserThread;
155 std::atomic<bool> browserRefreshing{false};
156 uint64_t lastBrowserRefreshMs = 0;
157};
Borrowed dependencies shared by client screens.
Runtime host-controlled discovery advertisement settings.
Client-side global server browser and NAT-assist helper.
Abstract interface for top-level application screens (lobby, in-game).
ImGui widget for the main menu server join form.
ServerBrowserTab
Tabs available on the server browser screen.
Definition MainMenuUI.hpp:16
@ LocalListing
Definition MainMenuUI.hpp:17
Network configuration loaded from config.toml at startup.
Work-in-progress SDL3 GPU renderer.
TCP stream client — sends input to the server and receives state updates.
Definition Client.hpp:69
Definition ConfirmModal.hpp:22
Starts, monitors, and shuts down a server process spawned by the client.
Definition HostedServer.hpp:37
Interface implemented by each full-screen mode (Lobby, Game).
Definition IScreen.hpp:16
IScreen implementation for the main menu/join screen; hosts the server join form.
Definition MainMenu.hpp:38
HostConfigState draftConfig() const
Current host-screen draft settings, clamped and sanitized.
Definition MainMenu.cpp:524
uint64_t lastBrowserRefreshMs
SDL tick timestamp of the last global refresh.
Definition MainMenu.hpp:156
std::thread browserThread
Worker used for global server-browser requests.
Definition MainMenu.hpp:154
std::unique_ptr< DiscoveryClient > localDiscoveryClient
Definition MainMenu.hpp:142
bool hasUnsavedServerChanges() const
True if the current draft differs from the last settings sent to or received from the server.
Definition MainMenu.cpp:509
bool consumeExitRequest()
True if the user requested closing the application, then clear that request.
Definition MainMenu.cpp:454
std::atomic< bool > browserRefreshing
True while browserThread is fetching.
Definition MainMenu.hpp:155
std::vector< net::discovery::ServerInfo > globalServers
Latest directory-server browser snapshot.
Definition MainMenu.hpp:150
bool updateServerSettings()
Send the current host-managed settings to the hosted server.
Definition MainMenu.cpp:551
SDL_AppResult iterate() override
Update the screen state and render the next frame.
Definition MainMenu.cpp:207
HostConfigState consumeDraftConfig() const
Current host-screen draft settings.
Definition MainMenu.cpp:489
std::optional< DiscoverySettings > lastSyncedDiscoverySettings
Last discovery settings acknowledged locally.
Definition MainMenu.hpp:126
SDL_Window * window
Application window; not owned.
Definition MainMenu.hpp:114
bool consumeHostRequest()
Take the pending host request set when the user clicks "Host", clearing it.
Definition MainMenu.cpp:407
HostedServer * hostedServer
Hosted server owned by App; not owned.
Definition MainMenu.hpp:116
bool pendingHostRequest
Set when the user clicks "Host", cleared on App transition.
Definition MainMenu.hpp:131
PendingConfirmAction
Definition MainMenu.hpp:89
@ ShutdownServer
Definition MainMenu.hpp:92
@ DiscardMatchChanges
Definition MainMenu.hpp:91
@ None
Definition MainMenu.hpp:90
void setPopupMessage(const std::string &message)
Display a modal message on the next main menu screen frame.
Definition MainMenu.cpp:483
void requestDiscardMatchChangesConfirm()
Ask the host whether to discard unsaved match setting changes before leaving this screen.
Definition MainMenu.cpp:586
void setJoinInProgress(bool joining, const std::string &label={})
Show or clear the in-progress connection indicator.
Definition MainMenu.cpp:477
std::string popupMessage
Modal message shown once after returning to the main menu.
Definition MainMenu.hpp:139
bool canManageCurrentServer() const
True if the client is connected and is the current lobby host.
Definition MainMenu.cpp:494
bool pendingGoToLobby
Set when the user clicks "Go to Lobby", cleared by App.
Definition MainMenu.hpp:134
HostConfigState * draft
Persistent draft state owned by App; not owned.
Definition MainMenu.hpp:117
UserSettings * settings
Live user settings; not owned.
Definition MainMenu.hpp:119
bool pendingShutdown
Set when the user clicks "Shutdown", cleared by App.
Definition MainMenu.hpp:133
std::optional< JoinRequest > consumeJoinRequest()
Take the pending join request set when the user clicks "Join", clearing it.
Definition MainMenu.cpp:396
std::mutex browserMutex
Guards global browser results from worker thread.
Definition MainMenu.hpp:153
std::optional< MatchConfig > lastSyncedMatchConfig
Last match config acknowledged locally as server state.
Definition MainMenu.hpp:125
bool consumeGoToLobbyRequest()
True if the user requested entering the hosted lobby, then clear that request.
Definition MainMenu.cpp:435
SystemMenuOverlay systemMenu_
Shared Escape menu for front-end screens.
Definition MainMenu.hpp:121
NewRenderer * renderer
Renderer; not owned.
Definition MainMenu.hpp:113
GlobalDiscoveryConfig discoveryConfig
Definition MainMenu.hpp:122
bool pendingReturnToTitleScreenRequest
Set when the user clicks "Return to Title Screen", cleared on transition.
Definition MainMenu.hpp:135
NetworkConfig * networkConfig
Runtime network config owned by App; not owned.
Definition MainMenu.hpp:118
bool pendingExitRequest
Set when the user confirms "Exit to Desktop", cleared by App.
Definition MainMenu.hpp:137
bool pendingLaunch
Set when the user clicks "Launch", cleared by App.
Definition MainMenu.hpp:132
bool openPopupMessage
True when the modal should be opened next frame.
Definition MainMenu.hpp:140
void setLaunchError(const std::string &error)
Display a launch or connection error on the host tab.
Definition MainMenu.cpp:470
void requestShutdownConfirm()
Ask the host to confirm server shutdown.
Definition MainMenu.cpp:596
ConfirmModal confirm_
Reusable confirmation modal for host tab actions.
Definition MainMenu.hpp:127
void quit() override
Perform any necessary cleanup before the screen is destroyed.
Definition MainMenu.cpp:196
SDL_AppResult event(SDL_Event *event) override
Handle an incoming SDL event.
Definition MainMenu.cpp:187
JoinMenuState joinMenuState
Mutable state backing the join form widgets.
Definition MainMenu.hpp:123
bool consumeReturnToTitleScreenRequest()
Take the pending title-screen request set when the user clicks "Return to Title Screen",...
Definition MainMenu.cpp:444
std::string_view settingsPath
Save path for user settings.
Definition MainMenu.hpp:120
Client * client
Network client owned by App; not owned.
Definition MainMenu.hpp:115
bool consumeLaunchRequest()
True if the user requested server launch, then clear that request.
Definition MainMenu.cpp:417
void setJoinError(const std::string &error)
Display an error string on the join form (e.g. from a failed connection attempt).
Definition MainMenu.cpp:463
bool consumeShutdownRequest()
True if the user requested hosted-server shutdown, then clear that request.
Definition MainMenu.cpp:426
std::vector< DiscoveryClient::DiscoveredServer > localServers
Latest LAN browser snapshot.
Definition MainMenu.hpp:151
std::string joinError
Error message shown on the join form; empty when no error.
Definition MainMenu.hpp:138
PendingConfirmAction pendingConfirmAction
Action to run after modal confirm.
Definition MainMenu.hpp:128
void joinRefreshThreadIfFinished()
Join the global browser worker once it has finished.
Definition MainMenu.cpp:642
std::string browserError
Last global browser error, empty when none.
Definition MainMenu.hpp:152
void startGlobalRefresh(bool force=false)
Start an asynchronous global server-browser refresh when allowed by throttling.
Definition MainMenu.cpp:606
bool init(AppContext &ctx, ServerBrowserTab initialTab=ServerBrowserTab::LocalListing)
Bind renderer, window, and discovery configuration; must be called before iterate().
Definition MainMenu.cpp:144
std::optional< JoinRequest > pendingJoinRequest
Set when the user clicks "Join", cleared on App transition to Lobby.
Definition MainMenu.hpp:130
std::string lastHostError
Error message shown on the host form; empty when no error.
Definition MainMenu.hpp:124
Graphics-team's work-in-progress SDL3 GPU renderer.
Definition NewRenderer.hpp:83
Shared Escape menu for non-gameplay screens.
Definition SystemMenuOverlay.hpp:20
Non-owning view of App-owned services and configuration.
Definition AppContext.hpp:20
Global server browser / directory-service settings.
Definition NetworkConfig.hpp:121
Persistent host-screen options used when launching a local server.
Definition HostedServer.hpp:17
Mutable widget state for the server join form.
Definition MainMenuUI.hpp:25
Server address and port entered by the user on the main menu screen.
Definition MainMenu.hpp:29
uint32_t globalServerId
Directory server id, or 0 for manual join.
Definition MainMenu.hpp:32
uint16_t serverPort
TCP port of the target server.
Definition MainMenu.hpp:31
std::string serverName
Display name shown in the lobby after joining.
Definition MainMenu.hpp:33
std::string serverIp
Hostname or IP address of the target server.
Definition MainMenu.hpp:30
Runtime network connection parameters.
Definition NetworkConfig.hpp:141
Per-user gameplay settings loaded from SDL's pref-path TOML file.
Definition UserSettings.hpp:25