Compare commits
2 Commits
aa5043a811
...
09cb13195c
Author | SHA1 | Date | |
---|---|---|---|
09cb13195c | |||
9e4cc97f79 |
@ -35,6 +35,8 @@ std::shared_ptr<SDLPP::TextRenderer> coins = nullptr;
|
|||||||
int coin_count = 0;
|
int coin_count = 0;
|
||||||
int global_frames = 0;
|
int global_frames = 0;
|
||||||
std::string last_load_level = "";
|
std::string last_load_level = "";
|
||||||
|
bool __right_pressed = false;
|
||||||
|
bool __left_pressed = false;
|
||||||
|
|
||||||
std::vector<std::shared_ptr<MarioBlock>> moving_objects = {};
|
std::vector<std::shared_ptr<MarioBlock>> moving_objects = {};
|
||||||
std::vector<SceneStruct> game_scenes{};
|
std::vector<SceneStruct> game_scenes{};
|
||||||
@ -46,10 +48,12 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
|
|||||||
switch (key) {
|
switch (key) {
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
|
__left_pressed = true;
|
||||||
mario->walkLeft();
|
mario->walkLeft();
|
||||||
break;
|
break;
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
|
__right_pressed = true;
|
||||||
mario->walkRight();
|
mario->walkRight();
|
||||||
break;
|
break;
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
@ -77,16 +81,26 @@ void handleKeyUp(SDL_Keycode key) {
|
|||||||
switch (key) {
|
switch (key) {
|
||||||
case SDLK_ESCAPE: {
|
case SDLK_ESCAPE: {
|
||||||
std::lock_guard<std::mutex> lock(render_mutex);
|
std::lock_guard<std::mutex> lock(render_mutex);
|
||||||
|
mario->setMovement(0, mario->getMovement().getY());
|
||||||
|
game_scenes.back().scene->pauseScene();
|
||||||
|
__right_pressed = false;
|
||||||
|
__left_pressed = false;
|
||||||
game_scenes.push_back(
|
game_scenes.push_back(
|
||||||
createGameMainMenuScene(renderer, false, true, true));
|
createGameMainMenuScene(renderer, false, true, true));
|
||||||
} break;
|
} break;
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
|
if (__left_pressed) {
|
||||||
mario->walkRight();
|
mario->walkRight();
|
||||||
|
__left_pressed = false;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
|
if (__right_pressed) {
|
||||||
mario->walkLeft();
|
mario->walkLeft();
|
||||||
|
__right_pressed = false;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
|
@ -56,7 +56,8 @@ void resetGlobals() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) {
|
||||||
auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), "levels", loadLevel, false, false);
|
auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(),
|
||||||
|
"levels", loadLevel, false, false);
|
||||||
std::lock_guard<std::mutex> lock(render_mutex);
|
std::lock_guard<std::mutex> lock(render_mutex);
|
||||||
game_scenes.pop_back();
|
game_scenes.pop_back();
|
||||||
resetGlobals();
|
resetGlobals();
|
||||||
@ -67,9 +68,9 @@ void __updateSelectedButton_MainMenu(uint64_t new_index) {
|
|||||||
if (new_index != (uint64_t)-1) {
|
if (new_index != (uint64_t)-1) {
|
||||||
__buttons_main_menu[new_index]->setHighlight();
|
__buttons_main_menu[new_index]->setHighlight();
|
||||||
|
|
||||||
if (__cur_button_index_main_menu != (uint64_t)-1 && new_index != __cur_button_index_main_menu) {
|
if (__cur_button_index_main_menu != (uint64_t)-1 &&
|
||||||
__buttons_main_menu[__cur_button_index_main_menu]
|
new_index != __cur_button_index_main_menu) {
|
||||||
->unsetHighlight();
|
__buttons_main_menu[__cur_button_index_main_menu]->unsetHighlight();
|
||||||
}
|
}
|
||||||
__cur_button_index_main_menu = new_index;
|
__cur_button_index_main_menu = new_index;
|
||||||
}
|
}
|
||||||
@ -101,7 +102,8 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
if(__cur_button_index_main_menu >= 0 && __cur_button_index_main_menu < __buttons_main_menu.size()) {
|
if (__cur_button_index_main_menu >= 0 &&
|
||||||
|
__cur_button_index_main_menu < __buttons_main_menu.size()) {
|
||||||
__buttons_main_menu[__cur_button_index_main_menu]
|
__buttons_main_menu[__cur_button_index_main_menu]
|
||||||
->performFunction();
|
->performFunction();
|
||||||
}
|
}
|
||||||
@ -112,9 +114,12 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<SDLPP::Scene>
|
std::shared_ptr<SDLPP::Scene>
|
||||||
createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_restart, bool include_resume, bool transparent_bg) {
|
createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer,
|
||||||
|
bool include_restart, bool include_resume,
|
||||||
|
bool transparent_bg) {
|
||||||
auto scene = std::make_shared<SDLPP::Scene>(renderer);
|
auto scene = std::make_shared<SDLPP::Scene>(renderer);
|
||||||
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
|
auto bg = std::make_shared<SDLPP::RectangleRender>(
|
||||||
|
0, 0, 10, 10, renderer,
|
||||||
transparent_bg ? "#00000088" : MARIO_OVERWORLD_COLORKEY, true);
|
transparent_bg ? "#00000088" : MARIO_OVERWORLD_COLORKEY, true);
|
||||||
bg->setPermanent();
|
bg->setPermanent();
|
||||||
bg->setId(1);
|
bg->setId(1);
|
||||||
@ -148,7 +153,8 @@ createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_res
|
|||||||
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
||||||
SDLPP::OBJ_CENTER);
|
SDLPP::OBJ_CENTER);
|
||||||
__buttons_main_menu.back()->setPermanent();
|
__buttons_main_menu.back()->setPermanent();
|
||||||
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
|
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() -
|
||||||
|
1);
|
||||||
}
|
}
|
||||||
if (include_restart || include_resume) {
|
if (include_restart || include_resume) {
|
||||||
__buttons_main_menu.emplace_back(std::make_shared<Button>(
|
__buttons_main_menu.emplace_back(std::make_shared<Button>(
|
||||||
@ -157,11 +163,12 @@ createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_res
|
|||||||
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
||||||
SDLPP::OBJ_CENTER);
|
SDLPP::OBJ_CENTER);
|
||||||
__buttons_main_menu.back()->setPermanent();
|
__buttons_main_menu.back()->setPermanent();
|
||||||
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
|
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() -
|
||||||
|
1);
|
||||||
}
|
}
|
||||||
__buttons_main_menu.emplace_back(std::make_shared<Button>(
|
__buttons_main_menu.emplace_back(
|
||||||
0.2, 0.55, 0.6, 0.1, renderer, "LOAD", default_button_theme,
|
std::make_shared<Button>(0.2, 0.55, 0.6, 0.1, renderer, "LOAD",
|
||||||
showLoadMenu, nullptr));
|
default_button_theme, showLoadMenu, nullptr));
|
||||||
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
|
||||||
SDLPP::OBJ_CENTER);
|
SDLPP::OBJ_CENTER);
|
||||||
__buttons_main_menu.back()->setPermanent();
|
__buttons_main_menu.back()->setPermanent();
|
||||||
@ -192,6 +199,7 @@ void additionalRenderMainMenu(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
|
|||||||
}
|
}
|
||||||
if (__quit_scenes_main_menu) {
|
if (__quit_scenes_main_menu) {
|
||||||
game_scenes.pop_back();
|
game_scenes.pop_back();
|
||||||
|
game_scenes.back().scene->unpauseScene();
|
||||||
if (__restart_scenes_main_menu) {
|
if (__restart_scenes_main_menu) {
|
||||||
loadLastLevel();
|
loadLastLevel();
|
||||||
}
|
}
|
||||||
@ -246,11 +254,13 @@ void pollEventsMainMenu(std::shared_ptr<SDLPP::Scene> &scene) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SceneStruct
|
SceneStruct createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer,
|
||||||
createGameMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer, bool include_restart, bool include_resume, bool transparent_background) {
|
bool include_restart, bool include_resume,
|
||||||
|
bool transparent_background) {
|
||||||
__include_resume = include_resume;
|
__include_resume = include_resume;
|
||||||
SceneStruct ret{};
|
SceneStruct ret{};
|
||||||
ret.scene = createSceneMainMenu(renderer, include_restart, include_resume, transparent_background);
|
ret.scene = createSceneMainMenu(renderer, include_restart, include_resume,
|
||||||
|
transparent_background);
|
||||||
ret.additionalRender = additionalRenderMainMenu;
|
ret.additionalRender = additionalRenderMainMenu;
|
||||||
ret.doInput = pollEventsMainMenu;
|
ret.doInput = pollEventsMainMenu;
|
||||||
__update_scenes_main_menu = true;
|
__update_scenes_main_menu = true;
|
||||||
|
@ -90,6 +90,9 @@ Scene::getObjects( const std::unordered_set< int > &objectIDs ) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
void Scene::updateScene() {
|
void Scene::updateScene() {
|
||||||
|
if(paused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// check for objects that should be removed
|
// check for objects that should be removed
|
||||||
checkKilled();
|
checkKilled();
|
||||||
std::lock_guard< std::mutex > lock( render_mutex );
|
std::lock_guard< std::mutex > lock( render_mutex );
|
||||||
@ -102,6 +105,14 @@ void Scene::updateScene() {
|
|||||||
}
|
}
|
||||||
prev_ticks = now_ticks;
|
prev_ticks = now_ticks;
|
||||||
}
|
}
|
||||||
|
void Scene::pauseScene() {
|
||||||
|
paused = true;
|
||||||
|
diff_ticks = SDL_GetTicks() - prev_ticks;
|
||||||
|
}
|
||||||
|
void Scene::unpauseScene() {
|
||||||
|
paused = false;
|
||||||
|
prev_ticks = SDL_GetTicks() - diff_ticks;
|
||||||
|
}
|
||||||
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
|
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
|
||||||
Scene::getCollisions( RenderObject &r ) {
|
Scene::getCollisions( RenderObject &r ) {
|
||||||
if ( r.getHidden() )
|
if ( r.getHidden() )
|
||||||
|
@ -28,6 +28,8 @@ public:
|
|||||||
std::vector< std::shared_ptr< RenderObject > >
|
std::vector< std::shared_ptr< RenderObject > >
|
||||||
getObjects( const std::unordered_set< int > &objectIDs );
|
getObjects( const std::unordered_set< int > &objectIDs );
|
||||||
void updateScene();
|
void updateScene();
|
||||||
|
void pauseScene();
|
||||||
|
void unpauseScene();
|
||||||
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
|
std::vector< std::pair< uint64_t, std::shared_ptr< RenderObject > > >
|
||||||
getCollisions( RenderObject &r );
|
getCollisions( RenderObject &r );
|
||||||
void visitCollisions( RenderObject &r, Visitor &v );
|
void visitCollisions( RenderObject &r, Visitor &v );
|
||||||
@ -63,12 +65,14 @@ private:
|
|||||||
std::shared_ptr< Renderer > renderer;
|
std::shared_ptr< Renderer > renderer;
|
||||||
std::shared_ptr< Texture > background;
|
std::shared_ptr< Texture > background;
|
||||||
int prev_ticks = 0;
|
int prev_ticks = 0;
|
||||||
|
int diff_ticks = 0;
|
||||||
std::shared_ptr< RenderObject > leftmost_obj;
|
std::shared_ptr< RenderObject > leftmost_obj;
|
||||||
std::shared_ptr< RenderObject > rightmost_obj;
|
std::shared_ptr< RenderObject > rightmost_obj;
|
||||||
uint64_t max_object_id = 0;
|
uint64_t max_object_id = 0;
|
||||||
std::mutex render_mutex;
|
std::mutex render_mutex;
|
||||||
std::unordered_set<uint64_t> background_ids;
|
std::unordered_set<uint64_t> background_ids;
|
||||||
uint64_t first_non_background_index = 1;
|
uint64_t first_non_background_index = 1;
|
||||||
|
bool paused = false;
|
||||||
};
|
};
|
||||||
} // end of namespace SDLPP
|
} // end of namespace SDLPP
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user