added buttons to main state
This commit is contained in:
88
src/main.cpp
88
src/main.cpp
@ -26,6 +26,7 @@
|
||||
#include "states/State.h"
|
||||
#include "states/LoadingState.h"
|
||||
#include "states/MenuState.h"
|
||||
#include "states/OptionsState.h"
|
||||
#include "states/LevelSelectorState.h"
|
||||
#include "states/PlayingState.h"
|
||||
#include "audio/MenuWrappers.h"
|
||||
@ -652,10 +653,23 @@ int main(int, char **)
|
||||
ctx.showSettingsPopup = &showSettingsPopup;
|
||||
ctx.showExitConfirmPopup = &showExitConfirmPopup;
|
||||
ctx.exitPopupSelectedButton = &exitPopupSelectedButton;
|
||||
ctx.playerName = &playerName;
|
||||
ctx.fullscreenFlag = &isFullscreen;
|
||||
ctx.applyFullscreen = [window, &isFullscreen](bool enable) {
|
||||
SDL_SetWindowFullscreen(window, enable ? SDL_WINDOW_FULLSCREEN : 0);
|
||||
isFullscreen = enable;
|
||||
};
|
||||
ctx.queryFullscreen = [window]() -> bool {
|
||||
return (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) != 0;
|
||||
};
|
||||
ctx.requestQuit = [&running]() {
|
||||
running = false;
|
||||
};
|
||||
|
||||
// Instantiate state objects
|
||||
auto loadingState = std::make_unique<LoadingState>(ctx);
|
||||
auto menuState = std::make_unique<MenuState>(ctx);
|
||||
auto optionsState = std::make_unique<OptionsState>(ctx);
|
||||
auto levelSelectorState = std::make_unique<LevelSelectorState>(ctx);
|
||||
auto playingState = std::make_unique<PlayingState>(ctx);
|
||||
|
||||
@ -668,6 +682,10 @@ int main(int, char **)
|
||||
stateMgr.registerOnEnter(AppState::Menu, [&](){ menuState->onEnter(); });
|
||||
stateMgr.registerOnExit(AppState::Menu, [&](){ menuState->onExit(); });
|
||||
|
||||
stateMgr.registerHandler(AppState::Options, [&](const SDL_Event& e){ optionsState->handleEvent(e); });
|
||||
stateMgr.registerOnEnter(AppState::Options, [&](){ optionsState->onEnter(); });
|
||||
stateMgr.registerOnExit(AppState::Options, [&](){ optionsState->onExit(); });
|
||||
|
||||
stateMgr.registerHandler(AppState::LevelSelector, [&](const SDL_Event& e){ levelSelectorState->handleEvent(e); });
|
||||
stateMgr.registerOnEnter(AppState::LevelSelector, [&](){ levelSelectorState->onEnter(); });
|
||||
stateMgr.registerOnExit(AppState::LevelSelector, [&](){ levelSelectorState->onExit(); });
|
||||
@ -792,21 +810,30 @@ int main(int, char **)
|
||||
float btnCX = LOGICAL_W * 0.5f + contentOffsetX;
|
||||
const float btnYOffset = 40.0f; // must match MenuState offset
|
||||
float btnCY = LOGICAL_H * 0.86f + contentOffsetY + btnYOffset;
|
||||
SDL_FRect playBtn{btnCX - btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
SDL_FRect levelBtn{btnCX + btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
float spacing = isSmall ? btnW * 1.15f : btnW * 1.05f;
|
||||
std::array<SDL_FRect, 4> buttonRects{};
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
float center = btnCX + (static_cast<float>(i) - 1.5f) * spacing;
|
||||
buttonRects[i] = SDL_FRect{center - btnW / 2.0f, btnCY - btnH / 2.0f, btnW, btnH};
|
||||
}
|
||||
|
||||
if (lx >= playBtn.x && lx <= playBtn.x + playBtn.w && ly >= playBtn.y && ly <= playBtn.y + playBtn.h)
|
||||
{
|
||||
// Reset the game first with the chosen start level so HUD and
|
||||
// Playing state see the correct 0-based level immediately.
|
||||
auto pointInRect = [&](const SDL_FRect& r) {
|
||||
return lx >= r.x && lx <= r.x + r.w && ly >= r.y && ly <= r.y + r.h;
|
||||
};
|
||||
|
||||
if (pointInRect(buttonRects[0])) {
|
||||
game.reset(startLevelSelection);
|
||||
state = AppState::Playing;
|
||||
stateMgr.setState(state);
|
||||
}
|
||||
else if (lx >= levelBtn.x && lx <= levelBtn.x + levelBtn.w && ly >= levelBtn.y && ly <= levelBtn.y + levelBtn.h)
|
||||
{
|
||||
} else if (pointInRect(buttonRects[1])) {
|
||||
state = AppState::LevelSelector;
|
||||
stateMgr.setState(state);
|
||||
} else if (pointInRect(buttonRects[2])) {
|
||||
state = AppState::Options;
|
||||
stateMgr.setState(state);
|
||||
} else if (pointInRect(buttonRects[3])) {
|
||||
showExitConfirmPopup = true;
|
||||
exitPopupSelectedButton = 1;
|
||||
}
|
||||
|
||||
// Settings button (gear icon area - top right)
|
||||
@ -865,6 +892,32 @@ int main(int, char **)
|
||||
game.setPaused(false);
|
||||
}
|
||||
}
|
||||
else if (state == AppState::Menu && showExitConfirmPopup) {
|
||||
float contentW = LOGICAL_W * logicalScale;
|
||||
float contentH = LOGICAL_H * logicalScale;
|
||||
float contentOffsetX = (winW - contentW) * 0.5f / logicalScale;
|
||||
float contentOffsetY = (winH - contentH) * 0.5f / logicalScale;
|
||||
float popupW = 420.0f;
|
||||
float popupH = 230.0f;
|
||||
float popupX = (LOGICAL_W - popupW) * 0.5f + contentOffsetX;
|
||||
float popupY = (LOGICAL_H - popupH) * 0.5f + contentOffsetY;
|
||||
float btnW = 140.0f;
|
||||
float btnH = 50.0f;
|
||||
float yesX = popupX + popupW * 0.3f - btnW / 2.0f;
|
||||
float noX = popupX + popupW * 0.7f - btnW / 2.0f;
|
||||
float btnY = popupY + popupH - btnH - 30.0f;
|
||||
bool insidePopup = lx >= popupX && lx <= popupX + popupW && ly >= popupY && ly <= popupY + popupH;
|
||||
if (insidePopup) {
|
||||
if (lx >= yesX && lx <= yesX + btnW && ly >= btnY && ly <= btnY + btnH) {
|
||||
showExitConfirmPopup = false;
|
||||
running = false;
|
||||
} else if (lx >= noX && lx <= noX + btnW && ly >= btnY && ly <= btnY + btnH) {
|
||||
showExitConfirmPopup = false;
|
||||
}
|
||||
} else {
|
||||
showExitConfirmPopup = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (e.type == SDL_EVENT_MOUSE_MOTION)
|
||||
@ -886,15 +939,16 @@ int main(int, char **)
|
||||
float btnCX = LOGICAL_W * 0.5f + contentOffsetX;
|
||||
const float btnYOffset = 40.0f; // must match MenuState offset
|
||||
float btnCY = LOGICAL_H * 0.86f + contentOffsetY + btnYOffset;
|
||||
SDL_FRect playBtn{btnCX - btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
SDL_FRect levelBtn{btnCX + btnW * 0.6f - btnW/2.0f, btnCY - btnH/2.0f, btnW, btnH};
|
||||
|
||||
// Check menu button hovers (no level popup to handle anymore)
|
||||
float spacing = isSmall ? btnW * 1.15f : btnW * 1.05f;
|
||||
hoveredButton = -1;
|
||||
if (lx >= playBtn.x && lx <= playBtn.x + playBtn.w && ly >= playBtn.y && ly <= playBtn.y + playBtn.h)
|
||||
hoveredButton = 0;
|
||||
else if (lx >= levelBtn.x && lx <= levelBtn.x + levelBtn.w && ly >= levelBtn.y && ly <= levelBtn.y + levelBtn.h)
|
||||
hoveredButton = 1;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
float center = btnCX + (static_cast<float>(i) - 1.5f) * spacing;
|
||||
SDL_FRect rect{center - btnW / 2.0f, btnCY - btnH / 2.0f, btnW, btnH};
|
||||
if (lx >= rect.x && lx <= rect.x + rect.w && ly >= rect.y && ly <= rect.y + rect.h) {
|
||||
hoveredButton = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user