Game main menu/retry scenes
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
zvon 2022-09-19 22:56:50 +02:00
parent 65edc9e8a0
commit 5201d10e20
8 changed files with 463 additions and 127 deletions

View File

@ -78,6 +78,11 @@ target_sources(mario
PRIVATE blocks/coinblock.cpp PRIVATE blocks/coinblock.cpp
PRIVATE blocks/mushroomblock.cpp PRIVATE blocks/mushroomblock.cpp
PRIVATE blocks/goombablock.cpp PRIVATE blocks/goombablock.cpp
PRIVATE scenes/load_scene.cpp
PRIVATE scenes/game_main_menu.cpp
PRIVATE editor_visitor.cpp # TODO
PRIVATE edit_box.cpp # TODO
PRIVATE tool_box.cpp # TODO
) )
target_sources(editor target_sources(editor

View File

@ -20,8 +20,11 @@
#include "mario.hpp" #include "mario.hpp"
#include "visitors/visitor_generator.hpp" #include "visitors/visitor_generator.hpp"
bool quit = false; // TODO make shared scenes
#include "scenes/game_scenes.hpp"
bool update = false; bool update = false;
bool newLoaded = false;
std::shared_ptr<Mario> mario = nullptr; std::shared_ptr<Mario> mario = nullptr;
std::shared_ptr<SDLPP::RectangleRender> leftStop = nullptr; std::shared_ptr<SDLPP::RectangleRender> leftStop = nullptr;
std::shared_ptr<SDLPP::Renderer> renderer = nullptr; std::shared_ptr<SDLPP::Renderer> renderer = nullptr;
@ -31,14 +34,13 @@ int coin_count = 0;
int global_frames = 0; int global_frames = 0;
std::vector<std::shared_ptr<MarioBlock>> moving_objects = {}; std::vector<std::shared_ptr<MarioBlock>> moving_objects = {};
std::vector<SceneStruct> game_scenes{};
std::mutex render_mutex; std::mutex render_mutex;
std::mutex gamescene_mutex;
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
switch (key) { switch (key) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_a: case SDLK_a:
mario->walkLeft(); mario->walkLeft();
break; break;
@ -66,6 +68,12 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
void handleKeyUp(SDL_Keycode key) { void handleKeyUp(SDL_Keycode key) {
switch (key) { switch (key) {
case SDLK_ESCAPE:
{
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.push_back(createGameMainMenuScene(renderer));
}
break;
case SDLK_a: case SDLK_a:
mario->walkRight(); mario->walkRight();
break; break;
@ -112,7 +120,7 @@ void pollEvents(SDLPP::Scene &scene) {
while (SDLPP::getSDLEvent(event)) { while (SDLPP::getSDLEvent(event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
quit = true; g_quit = true;
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (!event.key.repeat) { if (!event.key.repeat) {
@ -135,80 +143,84 @@ void pollEvents(SDLPP::Scene &scene) {
} }
} }
void doInput(std::shared_ptr<SDLPP::Scene> scene) { void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
pollEvents(*scene);
std::lock_guard<std::mutex> lock(render_mutex);
scene->updateScene();
auto prev_coin_count = coin_count;
auto rightmost_x = renderer->getDoubleDimensions().getX();
for (size_t i = 0; i < moving_objects.size(); i++) {
moving_objects[i]->checkVisibility(rightmost_x);
if (!moving_objects[i]->wasVisible()) {
continue;
}
auto visitor = getVisitor(*moving_objects[i], *scene, g_quit,
coin_count, moving_objects);
scene->visitCollisions(*moving_objects[i], *visitor);
moving_objects[i]->handleVisitor(*visitor);
auto rightmost_pos =
moving_objects[i]->getAbsolutePos().getX() +
moving_objects[i]->getDoubleRect().second.getX();
if (rightmost_pos < 0 && moving_objects[i] != mario) {
moving_objects[i]->destroy();
}
}
std::vector<uint64_t> killed_indices{};
for (size_t i = 0; i < moving_objects.size(); i++) {
if (moving_objects[i]->getKilled()) {
killed_indices.push_back(i);
}
}
std::reverse(killed_indices.begin(), killed_indices.end());
for (auto &index : killed_indices) {
moving_objects.erase(moving_objects.begin() + index);
}
if (coin_count != prev_coin_count) {
coins->changeText(std::to_string(coin_count) + " COINS");
}
// if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x;
auto width = scene->getWidth();
auto rightBarrier = width * 0.5;
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
scene->moveEverything((playerX > rightBarrier && rightmostX > width) *
(rightBarrier - playerX) / width,
0);
update = update || (playerX > rightBarrier && rightmostX > width);
global_frames++;
}
void doInput() {
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200); SDL_setFramerate(&gFPS, 200);
while (!quit) { while (!g_quit) {
SDL_framerateDelay(&gFPS); SDL_framerateDelay(&gFPS);
pollEvents(*scene); std::lock_guard<std::mutex> lock(gamescene_mutex);
std::lock_guard<std::mutex> lock(render_mutex); game_scenes.back().doInput(game_scenes.back().scene);
scene->updateScene(); game_scenes.back().scene->updateScene();
auto prev_coin_count = coin_count;
auto rightmost_x = renderer->getDoubleDimensions().getX();
for (size_t i = 0; i < moving_objects.size(); i++) {
moving_objects[i]->checkVisibility(rightmost_x);
if (!moving_objects[i]->wasVisible()) {
continue;
}
auto visitor = getVisitor(*moving_objects[i], *scene, quit,
coin_count, moving_objects);
scene->visitCollisions(*moving_objects[i], *visitor);
moving_objects[i]->handleVisitor(*visitor);
auto rightmost_pos =
moving_objects[i]->getAbsolutePos().getX() +
moving_objects[i]->getDoubleRect().second.getX();
if (rightmost_pos < 0 && moving_objects[i] != mario) {
moving_objects[i]->destroy();
}
}
std::vector<uint64_t> killed_indices{};
for (size_t i = 0; i < moving_objects.size(); i++) {
if (moving_objects[i]->getKilled()) {
killed_indices.push_back(i);
}
}
std::reverse(killed_indices.begin(), killed_indices.end());
for (auto &index : killed_indices) {
moving_objects.erase(moving_objects.begin() + index);
}
if (coin_count != prev_coin_count) {
coins->changeText(std::to_string(coin_count) + " COINS");
}
// if player is > 0.7 of playground, move everything left
auto playerX = mario->getRect().x;
auto width = scene->getWidth();
auto rightBarrier = width * 0.5;
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
scene->moveEverything((playerX > rightBarrier && rightmostX > width) *
(rightBarrier - playerX) / width,
0);
update = update || (playerX > rightBarrier && rightmostX > width);
global_frames++;
} }
} }
#ifdef _WIN32 void mainGameAdditional(std::shared_ptr<SDLPP::Scene> &/*UNUSED*/) {
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, static auto base = SDL_GetTicks();
PWSTR szCmdLine, int nCmdShow) { static int frames = 0;
#else mario->setStanding();
int main() { frames++;
#endif if (SDL_GetTicks() - base >= 1000) {
SDLPP::init(); if (global_frames < frames) {
SDLPP::Window w("Mario clone!"); frames = global_frames;
w.setResizable(true); }
global_frames = 0;
renderer = std::make_shared<SDLPP::Renderer>(w); fps->changeText(std::to_string(frames) + " fps");
renderer->setBlendMode(SDL_BLENDMODE_BLEND); frames = 0;
base = SDL_GetTicks();
// prepare global vars }
g_terrain_texture = std::make_shared<SDLPP::Texture>( }
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY);
g_enemies_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/enemies.png", MARIO_OVERWORLD_COLORKEY);
SceneStruct mainGameScene(const std::string &level_path) {
auto scene = std::make_shared<SDLPP::Scene>(renderer); auto scene = std::make_shared<SDLPP::Scene>(renderer);
g_playground = scene; g_playground = scene;
auto bg = std::make_shared<SDLPP::RectangleRender>( auto bg = std::make_shared<SDLPP::RectangleRender>(
@ -217,11 +229,7 @@ int main() {
bg->setStatic(); bg->setStatic();
bg->setId(1); bg->setId(1);
scene->addObject(bg); scene->addObject(bg);
g_mario_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY);
g_translucent_terrain_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY);
g_translucent_terrain_texture->setAlpha(100);
mario = std::make_shared<Mario>(renderer); mario = std::make_shared<Mario>(renderer);
scene->addObject(mario); scene->addObject(mario);
@ -247,7 +255,7 @@ int main() {
leftStop->setColiderColor("#FF00FF"); leftStop->setColiderColor("#FF00FF");
scene->addObject(leftStop); scene->addObject(leftStop);
loadMap(scene, mario, "test_binary2.bin"); loadMap(scene, mario, level_path);
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36); auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
fps = std::make_shared<SDLPP::TextRenderer>( fps = std::make_shared<SDLPP::TextRenderer>(
@ -266,15 +274,7 @@ int main() {
coins->setId(0); coins->setId(0);
coins->setPermanent(); coins->setPermanent();
scene->addObject(coins); scene->addObject(coins);
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
auto base = SDL_GetTicks();
int frames = 0;
scene->moveEverything(-mario->getDoubleRect().first.getX() + 0.2, 0); scene->moveEverything(-mario->getDoubleRect().first.getX() + 0.2, 0);
update = true;
std::unordered_set<uint64_t> background_ids = { std::unordered_set<uint64_t> background_ids = {
HILL_INCLINE_ID, HILL_DECLINE_ID, HILL_DOTS_RIGHT_ID, HILL_INCLINE_ID, HILL_DECLINE_ID, HILL_DOTS_RIGHT_ID,
@ -290,6 +290,7 @@ int main() {
scene->updateBackgroundObjectZIndex(); scene->updateBackgroundObjectZIndex();
// we want mario to be first because visiting isn't perfect // we want mario to be first because visiting isn't perfect
moving_objects.clear();
moving_objects.push_back(mario); moving_objects.push_back(mario);
std::unordered_set<int> moving_object_ids = { std::unordered_set<int> moving_object_ids = {
GOOMBA_ID, GOOMBA_ID,
@ -298,34 +299,96 @@ int main() {
moving_objects.push_back(std::dynamic_pointer_cast<MarioBlock>(obj)); moving_objects.push_back(std::dynamic_pointer_cast<MarioBlock>(obj));
} }
std::thread inputThread(doInput, scene); SceneStruct ret{};
ret.scene = scene;
ret.doInput = doInputMainGame;
ret.additionalRender = mainGameAdditional;
return ret;
}
void loadLevel(const std::string &level) {
//std::lock_guard<std::mutex> lock(render_mutex);
coin_count = 0;
std::lock_guard<std::mutex> lock(gamescene_mutex);
game_scenes.clear();
game_scenes.push_back(mainGameScene("levels/" + level));
game_scenes.back().scene->updateSizeAndPosition();
update = true;
newLoaded = true;
}
#ifdef _WIN32
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR szCmdLine, int nCmdShow) {
#else
int main() {
#endif
SDLPP::init();
SDLPP::Window w("Mario clone!");
w.setResizable(true);
renderer = std::make_shared<SDLPP::Renderer>(w);
renderer->setBlendMode(SDL_BLENDMODE_BLEND);
// prepare global vars
g_terrain_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY);
g_enemies_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/enemies.png", MARIO_OVERWORLD_COLORKEY);
g_mario_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY);
g_translucent_terrain_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY);
g_translucent_terrain_texture->setAlpha(100);
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
g_text_config = std::make_shared<SDLPP::FontConfiguration>(font, "#FFFFFF",
"#000000", 0.15);
game_scenes.push_back(createGameMainMenuScene(renderer, false));
std::thread inputThread(doInput);
SDL_PumpEvents(); SDL_PumpEvents();
scene->updateSizeAndPosition(); // TODO main menu
scene->renderScene(); /* game_scenes.push_back(mainGameScene("levels/test.marmap"));
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
renderer->presentRenderer();*/
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
renderer->presentRenderer(); renderer->presentRenderer();
update = true; update = true;
while (!quit) { while (!g_quit) {
SDL_framerateDelay(&gFPS); SDL_framerateDelay(&gFPS);
SDL_PumpEvents(); SDL_PumpEvents();
std::lock_guard<std::mutex> lock(render_mutex); std::lock_guard<std::mutex> lock(render_mutex);
mario->setStanding(); if (update || newLoaded) {
if (update) { for(auto &scene : game_scenes) {
scene->updateSizeAndPosition(); scene.scene->updateSizeAndPosition();
update = false; }
} if(newLoaded) {
scene->renderScene(); newLoaded = false;
renderer->presentRenderer(); } else {
frames++; update = false;
if (SDL_GetTicks() - base >= 1000) {
if (global_frames < frames) {
frames = global_frames;
} }
global_frames = 0;
fps->changeText(std::to_string(frames) + " fps");
frames = 0;
base = SDL_GetTicks();
} }
auto max_game_scenes = game_scenes.size();
renderer->clearRenderer();
for (size_t i = 0; i < max_game_scenes; i++) {
game_scenes[i].additionalRender(game_scenes[i].scene);
// additional renderer can remove scene from game_scenes, better
// check
if (i < game_scenes.size()) {
game_scenes[i].scene->renderScene(false);
}
}
renderer->presentRenderer();
} }
inputThread.join(); inputThread.join();

View File

@ -1052,6 +1052,13 @@ void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene,
} else { } else {
loadMap(scene, global_vars.mario, filename, global_vars.objects, true, loadMap(scene, global_vars.mario, filename, global_vars.objects, true,
MAP_WIDTH); MAP_WIDTH);
for(int i = 0; i < global_vars.objects.size(); i++) {
for(int j = 0; j < 16; j++) {
if(global_vars.objects[i][j].hasCharacter() && global_vars.objects[i][j].getCharacterId() == MARIO_ID) {
global_vars.mario_pos = {i,j};
}
}
}
} }
// map // map
@ -1368,4 +1375,4 @@ SceneStruct createEditorScene(std::shared_ptr<SDLPP::Renderer> &renderer) {
mainMenuScene = createEditorMainMenuScene(renderer); mainMenuScene = createEditorMainMenuScene(renderer);
// fileChoiceScene = createEditorFileChoiceScene(renderer); // fileChoiceScene = createEditorFileChoiceScene(renderer);
return ret; return ret;
} }

View File

@ -1,15 +1,10 @@
#ifndef EDITOR_MAIN_HPP #ifndef EDITOR_SCENES_HPP
#define EDITOR_MAIN_HPP #define EDITOR_SCENES_HPP
#include "../../sdlpp/sdlpp_scene.hpp" #include "../../sdlpp/sdlpp_scene.hpp"
#include "../gui/gui.hpp" #include "../gui/gui.hpp"
#include <functional> #include <functional>
#include "shared_scenes.hpp"
struct SceneStruct {
std::shared_ptr<SDLPP::Scene> scene;
std::function<void(std::shared_ptr<SDLPP::Scene> &)> doInput;
std::function<void(std::shared_ptr<SDLPP::Scene> &)> additionalRender;
};
extern std::mutex render_mutex; extern std::mutex render_mutex;
extern std::vector<SceneStruct> game_scenes; extern std::vector<SceneStruct> game_scenes;
@ -22,15 +17,5 @@ SceneStruct
createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer); createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer);
SceneStruct SceneStruct
createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer); createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer);
// TODO move to a shared header
SceneStruct createYesNoScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &text,
std::function<void(bool)> finalizer);
SceneStruct createOkScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &text,
std::function<void()> finalizer);
SceneStruct createLoadScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &path,
std::function<void(const std::string &)> finalizer);
#endif #endif

View File

@ -0,0 +1,229 @@
#include "editor_scenes.hpp"
#include "../../sdlpp/sdlpp.hpp"
#include "../../sdlpp/sdlpp_mouse.hpp"
#include "../global_vars.hpp"
#include "../objectids.hpp"
#include "../editor_visitor.hpp"
#include <functional>
bool __update_scenes_main_menu = false;
bool __quit_scenes_main_menu = false;
bool __started_main_menu = false;
uint64_t __cur_button_index_main_menu = -1;
uint64_t __cur_button_index_main_menu_down = -1;
std::vector<std::shared_ptr<Button>> __buttons_main_menu{};
std::shared_ptr<SDLPP::RectangleRender> __mouse_main_menu{};
extern void loadLevel(const std::string &level);
void quitMainMenu() {
g_quit = true;
}
void resumeMainMenu() {
__quit_scenes_main_menu = true;
}
void quitMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
quitMainMenu();
}
void resumeMainMenuCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
resumeMainMenu();
}
void resetGlobals() {
__update_scenes_main_menu = false;
__quit_scenes_main_menu = false;
__started_main_menu = false;
__cur_button_index_main_menu_down = -1;
__mouse_main_menu.reset();
__buttons_main_menu.clear();
}
void showLoadMenu(void */*UNUSED*/, Button */*UNUSED*/) {
auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), "levels", loadLevel, false);
std::lock_guard<std::mutex> lock(render_mutex);
game_scenes.pop_back();
resetGlobals();
game_scenes.push_back(loadMenu);
}
void __updateSelectedButton_MainMenu(uint64_t new_index) {
if (new_index != __cur_button_index_main_menu &&
new_index != (uint64_t)-1) {
__buttons_main_menu[new_index]->setHighlight();
if (__cur_button_index_main_menu != (uint64_t)-1) {
__buttons_main_menu[__cur_button_index_main_menu]
->unsetHighlight();
}
__cur_button_index_main_menu = new_index;
}
}
void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
switch (key) {
case SDLK_ESCAPE:
resumeMainMenu();
break;
case SDLK_DOWN:
case SDLK_s:
if(__cur_button_index_main_menu == __buttons_main_menu.size() - 1) {
__updateSelectedButton_MainMenu(0);
} else {
__updateSelectedButton_MainMenu(__cur_button_index_main_menu + 1);
}
break;
case SDLK_UP:
case SDLK_w:
if(__cur_button_index_main_menu == 0) {
__updateSelectedButton_MainMenu(__buttons_main_menu.size() - 1);
} else {
__updateSelectedButton_MainMenu(__cur_button_index_main_menu - 1);
}
break;
case SDLK_RETURN:
if(__cur_button_index_main_menu >= 0 && __cur_button_index_main_menu < __buttons_main_menu.size()) {
__buttons_main_menu[__cur_button_index_main_menu]
->performFunction();
}
break;
default:
break;
}
}
std::shared_ptr<SDLPP::Scene>
createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_resume) {
auto scene = std::make_shared<SDLPP::Scene>(renderer);
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
"#00000088", true);
bg->setPermanent();
bg->setId(1);
scene->addObject(bg);
__mouse_main_menu =
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
__mouse_main_menu->setMinWidth(1);
__mouse_main_menu->setMinHeight(1);
__mouse_main_menu->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
__mouse_main_menu->setId(EDITOR_MOUSE_ID);
__mouse_main_menu->setColiderColor("#00FF00");
__mouse_main_menu->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
scene->addObject(__mouse_main_menu);
ButtonConfig default_button_theme{};
default_button_theme.bg_color = "#FFFFFF88";
default_button_theme.bg_color_highlight = "#FFFFFFBB";
default_button_theme.bg_color_disabled = "#AAAAAA88";
default_button_theme.font_color = "#000000";
default_button_theme.font_color_highlight = "#000000";
default_button_theme.font_color_disabled = "#555555";
default_button_theme.font_outline_color = "#FFFFFF88";
default_button_theme.font_outline_color_highlight = "#FFFFFFAA";
default_button_theme.font_outline_color_disabled = "#787878";
default_button_theme.outline = 0.1;
// buttons
if(include_resume) {
__buttons_main_menu.emplace_back(std::make_shared<Button>(
0.2, 0.25, 0.6, 0.1, renderer, "RESUME", default_button_theme,
resumeMainMenuCallback, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
}
__buttons_main_menu.emplace_back(std::make_shared<Button>(
0.2, 0.4, 0.6, 0.1, renderer, "LOAD", default_button_theme,
showLoadMenu, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
__buttons_main_menu.emplace_back(std::make_shared<Button>(
0.2, 0.7, 0.6, 0.1, renderer, "QUIT", default_button_theme,
quitMainMenuCallback, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
for (auto &button : __buttons_main_menu) {
scene->addObject(button);
}
return scene;
}
void additionalRenderMainMenu(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
if (__update_scenes_main_menu) {
for (auto &_scene : game_scenes) {
_scene.scene->updateSizeAndPosition();
}
if (__started_main_menu) {
__update_scenes_main_menu = false;
} else {
__started_main_menu = true;
}
}
if (__quit_scenes_main_menu) {
game_scenes.pop_back();
resetGlobals();
}
}
void getMousePositionFlagsMainMenu(SDLPP::Scene &scene) {
auto mouse = scene.getObjects({ EDITOR_MOUSE_ID })[0];
// move mouse colider to mouse position
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
MouseVisitor visitor;
scene.visitCollisions(*mouse, visitor);
__updateSelectedButton_MainMenu(visitor.getCurButton());
}
void pollEventsMainMenu(std::shared_ptr<SDLPP::Scene> &scene) {
SDL_Event event;
while (SDLPP::getSDLEvent(event)) {
switch (event.type) {
case SDL_QUIT:
quitMainMenu();
break;
case SDL_KEYUP:
handleKeyUpMainMenu(event.key.keysym.sym, *scene);
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
__update_scenes_main_menu = true;
}
break;
case SDL_MOUSEMOTION:
getMousePositionFlagsMainMenu(*scene);
break;
case SDL_MOUSEBUTTONUP:
if (__cur_button_index_main_menu_down ==
__cur_button_index_main_menu &&
__cur_button_index_main_menu != (uint64_t)-1) {
__buttons_main_menu[__cur_button_index_main_menu]
->performFunction();
}
break;
case SDL_MOUSEBUTTONDOWN:
// store current mouse flags in previous mouse flags
__cur_button_index_main_menu_down = __cur_button_index_main_menu;
break;
default:
break;
}
}
}
SceneStruct
createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_resume) {
SceneStruct ret{};
ret.scene = createSceneMainMenu(renderer, include_resume);
ret.additionalRender = additionalRenderMainMenu;
ret.doInput = pollEventsMainMenu;
__update_scenes_main_menu = true;
__updateSelectedButton_MainMenu(0);
return ret;
}

View File

@ -0,0 +1,15 @@
#ifndef GAME_SCENES_HPP
#define GAME_SCENES_HPP
#include "../../sdlpp/sdlpp_scene.hpp"
#include "../gui/gui.hpp"
#include <functional>
#include "shared_scenes.hpp"
extern std::mutex render_mutex;
extern std::vector<SceneStruct> game_scenes;
SceneStruct createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_resume = true);
SceneStruct createGameRetryScene(std::shared_ptr<SDLPP::Renderer> &renderer);
#endif

View File

@ -1,3 +1,5 @@
#include <utility>
#include "SDL2/SDL_keycode.h" #include "SDL2/SDL_keycode.h"
#include "editor_scenes.hpp" #include "editor_scenes.hpp"
#include "../../sdlpp/sdlpp.hpp" #include "../../sdlpp/sdlpp.hpp"
@ -10,6 +12,7 @@
bool __update_scenes_load_dialog = false; bool __update_scenes_load_dialog = false;
bool __quit_scenes_load_dialog = false; bool __quit_scenes_load_dialog = false;
bool __started_load_dialog = false; bool __started_load_dialog = false;
bool __pop_at_finish_load_dialog = false;
uint64_t __cur_button_index_load_dialog = -1; uint64_t __cur_button_index_load_dialog = -1;
uint64_t __cur_button_index_load_dialog_down = -1; uint64_t __cur_button_index_load_dialog_down = -1;
std::vector<std::shared_ptr<Button>> __buttons_load_dialog{}; std::vector<std::shared_ptr<Button>> __buttons_load_dialog{};
@ -50,7 +53,7 @@ void __selectUpperButton() {
if (__cur_button_index_load_dialog > 0) { if (__cur_button_index_load_dialog > 0) {
__updateSelectedButton_LoadDialog(__cur_button_index_load_dialog - 1); __updateSelectedButton_LoadDialog(__cur_button_index_load_dialog - 1);
if (__buttons_load_dialog[__cur_button_index_load_dialog] if (__buttons_load_dialog[__cur_button_index_load_dialog]
->getPos() ->getPos()
.getY() < 0.49) { .getY() < 0.49) {
__moveButtons(0.25); __moveButtons(0.25);
} }
@ -172,6 +175,7 @@ void __resetGlobals_LoadDialog() {
__update_scenes_load_dialog = false; __update_scenes_load_dialog = false;
__quit_scenes_load_dialog = false; __quit_scenes_load_dialog = false;
__started_load_dialog = false; __started_load_dialog = false;
__pop_at_finish_load_dialog = false;
__cur_button_index_load_dialog = -1; __cur_button_index_load_dialog = -1;
__cur_button_index_load_dialog_down = -1; __cur_button_index_load_dialog_down = -1;
__buttons_load_dialog.clear(); __buttons_load_dialog.clear();
@ -192,7 +196,9 @@ void __additionalRender_LoadDialog(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
} }
if (__quit_scenes_load_dialog) { if (__quit_scenes_load_dialog) {
__load_dialog_finalizer(__selected_level_load_dialog); __load_dialog_finalizer(__selected_level_load_dialog);
game_scenes.pop_back(); if(__pop_at_finish_load_dialog) {
game_scenes.pop_back();
}
__resetGlobals_LoadDialog(); __resetGlobals_LoadDialog();
} }
} }
@ -256,8 +262,10 @@ void __pollEvents_LoadDialog(std::shared_ptr<SDLPP::Scene> &scene) {
SceneStruct SceneStruct
createLoadScene(std::shared_ptr<SDLPP::Renderer> renderer, createLoadScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &path, const std::string &path,
std::function<void(const std::string &)> finalizer) { std::function<void(const std::string &)> finalizer,
__load_dialog_finalizer = finalizer; bool pop_at_finish) {
__load_dialog_finalizer = std::move(finalizer);
__pop_at_finish_load_dialog = pop_at_finish;
SceneStruct ret{}; SceneStruct ret{};
ret.scene = createSceneLoadDialog(renderer, path); ret.scene = createSceneLoadDialog(renderer, path);
ret.additionalRender = __additionalRender_LoadDialog; ret.additionalRender = __additionalRender_LoadDialog;

View File

@ -0,0 +1,24 @@
#ifndef SHARED_SCENES_HPP
#define SHARED_SCENES_HPP
#include "../../sdlpp/sdlpp_scene.hpp"
#include <functional>
struct SceneStruct {
std::shared_ptr<SDLPP::Scene> scene;
std::function<void(std::shared_ptr<SDLPP::Scene> &)> doInput;
std::function<void(std::shared_ptr<SDLPP::Scene> &)> additionalRender;
};
SceneStruct createYesNoScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &text,
std::function<void(bool)> finalizer);
SceneStruct createOkScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &text,
std::function<void()> finalizer);
SceneStruct createLoadScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &path,
std::function<void(const std::string &)> finalizer,
bool pop_at_finish = true);
#endif