game/mario/main.cpp

513 lines
16 KiB
C++
Raw Normal View History

2021-04-25 20:42:55 +00:00
#include "../sdlpp/sdlpp.hpp"
2022-09-24 18:44:52 +00:00
#include "SDL2/SDL_keycode.h"
2021-04-25 20:42:55 +00:00
#include "sprites.hpp"
2021-08-08 19:45:50 +00:00
#include <memory>
2021-04-25 20:42:55 +00:00
#ifdef _WIN32
#include "../sdlpp/SDL2/SDL2_framerate.h"
#include <ctime>
#include <string>
#include <windows.h>
#else
#include <SDL2/SDL2_framerate.h>
#endif // UNIX
#include <thread>
2021-05-27 14:33:00 +00:00
#include <mutex>
2021-08-07 19:59:06 +00:00
#include <unordered_set>
2021-04-25 20:42:55 +00:00
#include "global_vars.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "maploader.hpp"
2021-05-22 21:13:26 +00:00
#include "mario.hpp"
#include "visitors/visitor_generator.hpp"
2021-04-25 20:42:55 +00:00
2022-09-19 20:56:50 +00:00
// TODO make shared scenes
#include "scenes/game_scenes.hpp"
2021-05-25 18:21:02 +00:00
bool update = false;
int update_count = 0;
2022-09-19 20:56:50 +00:00
bool newLoaded = false;
2021-10-18 07:08:35 +00:00
std::shared_ptr<Mario> mario = nullptr;
std::shared_ptr<SDLPP::RectangleRender> leftStop = nullptr;
std::shared_ptr<SDLPP::Renderer> renderer = nullptr;
std::shared_ptr<SDLPP::TextRenderer> fps = nullptr;
std::shared_ptr<SDLPP::TextRenderer> coins = nullptr;
int coin_count = 0;
int global_frames = 0;
std::string last_load_level = "";
bool __right_pressed = false;
bool __left_pressed = false;
2021-04-25 20:42:55 +00:00
2021-10-18 07:08:35 +00:00
std::vector<std::shared_ptr<MarioBlock>> moving_objects = {};
2022-09-19 20:56:50 +00:00
std::vector<SceneStruct> game_scenes{};
2022-11-13 18:52:39 +00:00
std::string _teleport_level = "";
2021-05-27 14:33:00 +00:00
std::mutex render_mutex;
2022-09-19 20:56:50 +00:00
std::mutex gamescene_mutex;
2021-05-27 14:33:00 +00:00
2021-10-18 07:08:35 +00:00
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
switch (key) {
2021-04-25 20:42:55 +00:00
case SDLK_a:
2022-09-24 18:44:52 +00:00
case SDLK_LEFT:
__left_pressed = true;
2021-05-22 21:13:26 +00:00
mario->walkLeft();
2021-04-25 20:42:55 +00:00
break;
case SDLK_d:
2022-09-24 18:44:52 +00:00
case SDLK_RIGHT:
__right_pressed = true;
2021-05-22 21:13:26 +00:00
mario->walkRight();
2021-04-25 20:42:55 +00:00
break;
case SDLK_SPACE:
case SDLK_w:
2022-09-24 18:44:52 +00:00
case SDLK_UP:
2021-05-22 21:54:01 +00:00
mario->jump();
2021-04-25 20:42:55 +00:00
break;
case SDLK_s:
2022-09-24 18:44:52 +00:00
case SDLK_DOWN:
2022-11-13 18:52:39 +00:00
mario->crouch();
2021-04-25 20:42:55 +00:00
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(
2021-10-18 07:08:35 +00:00
!scene.getRenderer().getRenderColiders());
2021-05-25 18:21:02 +00:00
break;
case SDLK_f:
2021-10-18 07:08:35 +00:00
if (fps) {
fps->setHidden(!fps->getHidden());
}
2022-11-12 20:32:18 +00:00
case SDLK_RETURN:
mario->fire();
break;
2021-04-25 20:42:55 +00:00
default:
break;
}
}
2021-10-18 07:08:35 +00:00
void handleKeyUp(SDL_Keycode key) {
switch (key) {
2022-09-23 14:46:50 +00:00
case SDLK_ESCAPE: {
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;
2022-09-23 14:46:50 +00:00
game_scenes.push_back(
createGameMainMenuScene(renderer, false, true, true));
} break;
2021-04-25 20:42:55 +00:00
case SDLK_a:
2022-09-24 18:44:52 +00:00
case SDLK_LEFT:
if (__left_pressed) {
mario->walkRight();
__left_pressed = false;
}
2021-04-25 20:42:55 +00:00
break;
case SDLK_d:
2022-09-24 18:44:52 +00:00
case SDLK_RIGHT:
if (__right_pressed) {
mario->walkLeft();
__right_pressed = false;
}
2021-04-25 20:42:55 +00:00
break;
2021-05-22 21:54:01 +00:00
case SDLK_SPACE:
2021-04-25 20:42:55 +00:00
case SDLK_w:
2022-09-24 18:44:52 +00:00
case SDLK_UP:
2021-05-22 21:54:01 +00:00
mario->stopJump();
2022-11-13 18:52:39 +00:00
break;
case SDLK_s:
case SDLK_DOWN:
mario->uncrouch();
if(__left_pressed) {
mario->walkLeft();
}
if(__right_pressed) {
mario->walkRight();
}
break;
2021-04-25 20:42:55 +00:00
default:
break;
}
}
2021-10-18 07:08:35 +00:00
void moveToMarioPosition(SDLPP::Scene &scene,
SDLPP::Vec2D<double> &prev_mario) {
auto rendDims = renderer->getDoubleDimensions();
2021-10-18 07:08:35 +00:00
if (leftStop) {
auto left =
2021-10-18 07:08:35 +00:00
rendDims.getX() < 2.0 ? -(rendDims.getX() - 1) / 2.0 - 0.1 : -0.5;
leftStop->setPos(left, 0);
}
auto mario_pos_difference = prev_mario - mario->getAbsolutePos();
// sometimes there is a concurrency problem and prev_pos == cur_pos, in
// that case move everything so Mario is standing on the left edge of the
// screen
2021-10-18 07:08:35 +00:00
if (mario_pos_difference.getX() < 0.01 &&
mario_pos_difference.getX() > -0.01) {
// 0.01 is the width of visible leftStop
scene.moveEverything(-(mario->getAbsolutePos().getX() - 0.01), 0);
} else {
2021-10-18 07:08:35 +00:00
scene.moveEverything(mario_pos_difference.getX(), 0);
}
scene.updateSizeAndPosition();
auto left_stop_rightmost = leftStop->getDoubleRect().first.getX() +
leftStop->getDoubleRect().second.getX();
2021-10-18 07:08:35 +00:00
if (mario->getPos().getX() < left_stop_rightmost) {
mario->setPos(left_stop_rightmost, mario->getPos().getY());
}
}
2021-10-18 07:08:35 +00:00
void pollEvents(SDLPP::Scene &scene) {
2021-04-25 20:42:55 +00:00
SDL_Event event;
2021-10-18 07:08:35 +00:00
while (SDLPP::getSDLEvent(event)) {
switch (event.type) {
2021-04-25 20:42:55 +00:00
case SDL_QUIT:
2022-09-19 20:56:50 +00:00
g_quit = true;
2021-04-25 20:42:55 +00:00
break;
case SDL_KEYDOWN:
2021-10-18 07:08:35 +00:00
if (!event.key.repeat) {
handleKeyDown(event.key.keysym.sym, scene);
}
2021-04-25 20:42:55 +00:00
break;
case SDL_KEYUP:
2021-10-18 07:08:35 +00:00
handleKeyUp(event.key.keysym.sym);
2021-04-25 20:42:55 +00:00
break;
case SDL_WINDOWEVENT:
2021-10-18 07:08:35 +00:00
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
auto prev_mario_pos = mario->getAbsolutePos();
2021-04-25 20:42:55 +00:00
scene.updateSizeAndPosition();
2021-10-18 07:08:35 +00:00
moveToMarioPosition(scene, prev_mario_pos);
2021-08-08 19:45:50 +00:00
update = true;
update_count = 2;
2021-04-26 19:59:21 +00:00
}
2021-04-25 20:42:55 +00:00
default:
break;
}
}
}
2022-09-19 20:56:50 +00:00
void doInputMainGame(std::shared_ptr<SDLPP::Scene> scene) {
2022-09-23 14:46:50 +00:00
if (newLoaded) {
auto prev_mario_pos = mario->getAbsolutePos();
scene->updateSizeAndPosition();
moveToMarioPosition(*scene, prev_mario_pos);
update = true;
update_count = 2;
newLoaded = false;
}
2022-09-23 14:46:50 +00:00
if (g_death) {
game_scenes.push_back(
createGameMainMenuScene(renderer, true, false, true));
g_death = false;
}
2022-09-19 20:56:50 +00:00
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;
}
2022-09-25 17:44:28 +00:00
auto visitor = getVisitor(*moving_objects[i], *scene,
2022-09-23 14:46:50 +00:00
coin_count, moving_objects);
2022-09-19 20:56:50 +00:00
scene->visitCollisions(*moving_objects[i], *visitor);
moving_objects[i]->handleVisitor(*visitor);
2022-09-23 14:46:50 +00:00
auto rightmost_pos = moving_objects[i]->getAbsolutePos().getX() +
moving_objects[i]->getDoubleRect().second.getX();
2022-09-19 20:56:50 +00:00
if (rightmost_pos < 0 && moving_objects[i] != mario) {
moving_objects[i]->destroy();
}
2022-09-19 20:56:50 +00:00
}
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);
}
2022-09-19 20:56:50 +00:00
}
std::reverse(killed_indices.begin(), killed_indices.end());
for (auto &index : killed_indices) {
moving_objects.erase(moving_objects.begin() + index);
}
2021-05-22 21:13:26 +00:00
2022-09-19 20:56:50 +00:00
if (coin_count != prev_coin_count) {
coins->changeText(std::to_string(coin_count) + " COINS");
update = true;
update_count = 2;
2021-04-25 20:42:55 +00:00
}
2022-09-19 20:56:50 +00:00
// 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) *
2022-09-23 14:46:50 +00:00
(rightBarrier - playerX) / width,
0);
2022-09-19 20:56:50 +00:00
update = update || (playerX > rightBarrier && rightmostX > width);
global_frames++;
2022-09-23 14:46:50 +00:00
if (mario->isDead()) {
g_death = true;
}
2021-04-25 20:42:55 +00:00
}
2022-09-19 20:56:50 +00:00
void doInput() {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
while (!g_quit) {
SDL_framerateDelay(&gFPS);
std::lock_guard<std::mutex> lock(gamescene_mutex);
game_scenes.back().doInput(game_scenes.back().scene);
game_scenes.back().scene->updateScene();
}
}
2021-04-26 19:59:21 +00:00
2022-09-23 14:46:50 +00:00
void mainGameAdditional(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
2022-09-19 20:56:50 +00:00
static auto base = SDL_GetTicks();
static int frames = 0;
mario->setStanding();
frames++;
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();
}
}
2021-04-26 19:59:21 +00:00
2022-11-12 20:32:18 +00:00
void addObjectToScene(std::shared_ptr<MarioBlock> &object, bool moving) {
game_scenes.back().scene->addObject(object);
object->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
if(moving) {
moving_objects.push_back(object);
}
}
void addObjectToSceneTop(std::shared_ptr<MarioBlock> &object, bool moving) {
addObjectToScene(object, moving);
game_scenes.back().scene->moveZTop(object);
}
void addObjectToSceneBack(std::shared_ptr<MarioBlock> &object, bool moving) {
addObjectToScene(object, moving);
game_scenes.back().scene->moveZJustAboveBackground(object);
}
2022-09-19 20:56:50 +00:00
SceneStruct mainGameScene(const std::string &level_path) {
2021-10-18 07:08:35 +00:00
auto scene = std::make_shared<SDLPP::Scene>(renderer);
2021-05-26 16:24:09 +00:00
g_playground = scene;
2021-10-18 07:08:35 +00:00
auto bg = std::make_shared<SDLPP::RectangleRender>(
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true);
bg->setPermanent();
2021-04-26 19:59:21 +00:00
bg->setStatic();
2021-10-18 07:08:35 +00:00
bg->setId(1);
scene->addObject(bg);
2022-09-19 20:56:50 +00:00
mario.reset();
2022-11-12 20:32:18 +00:00
mario = std::make_shared<Mario>(renderer, addObjectToSceneTop);
2021-10-18 07:08:35 +00:00
scene->addObject(mario);
2021-04-26 19:59:21 +00:00
auto defeat =
2021-10-18 07:08:35 +00:00
std::make_shared<SDLPP::RectangleRender>(0, 1.01, 0, 0, renderer);
defeat->setId(DEATH_ID);
defeat->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
2021-04-26 19:59:21 +00:00
defeat->setPermanent();
2021-10-18 07:08:35 +00:00
auto defeatCol = SDLPP::RectColider(-1, 0, -1, -1);
2021-04-25 20:42:55 +00:00
defeatCol.setInfinite();
2021-10-18 07:08:35 +00:00
defeat->addCollision(defeatCol);
2021-04-26 19:59:21 +00:00
2021-10-18 07:08:35 +00:00
scene->addObject(defeat);
2021-04-25 20:42:55 +00:00
2021-10-18 07:08:35 +00:00
leftStop =
std::make_shared<SDLPP::RectangleRender>(-0.1, 0, 0.11, 0, renderer);
leftStop->setId(STOP_MOVEMENT);
leftStop->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
2021-04-26 19:59:21 +00:00
leftStop->setPermanent();
2021-10-18 07:08:35 +00:00
auto leftStopCol = SDLPP::RectColider(0, -1, 1, -1);
2021-04-26 19:59:21 +00:00
leftStopCol.setInfinite();
2021-10-18 07:08:35 +00:00
leftStop->addCollision(leftStopCol);
leftStop->setColiderColor("#FF00FF");
scene->addObject(leftStop);
2021-04-25 20:42:55 +00:00
2022-09-19 20:56:50 +00:00
loadMap(scene, mario, level_path);
2021-04-25 20:42:55 +00:00
2021-10-18 07:08:35 +00:00
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
fps = std::make_shared<SDLPP::TextRenderer>(
0.2, 0, 0.78, 0.1, renderer, font, "0fps", "#FFFFFF", "#000000", 0.1,
2021-10-18 07:08:35 +00:00
SDLPP_TEXT_RIGHT);
fps->setAlignment(SDLPP::OBJ_END, SDLPP::OBJ_START);
fps->setId(0);
2021-05-25 18:21:02 +00:00
fps->setPermanent();
2021-10-18 07:08:35 +00:00
fps->setHidden(true);
scene->addObject(fps);
2021-05-25 18:21:02 +00:00
2021-10-18 07:08:35 +00:00
coins = std::make_shared<SDLPP::TextRenderer>(
0.2, 0, 0.78, 0.1, renderer, font, "0 COINS", "#FFFFFF", "#000000", 0.1,
2021-10-18 07:08:35 +00:00
SDLPP_TEXT_RIGHT);
coins->setAlignment(SDLPP::OBJ_START, SDLPP::OBJ_START);
coins->setId(0);
coins->setPermanent();
2021-10-18 07:08:35 +00:00
scene->addObject(coins);
scene->moveEverything(-mario->getDoubleRect().first.getX() + 0.2, 0);
2021-08-07 19:59:06 +00:00
std::unordered_set<uint64_t> background_ids = {
2021-10-18 07:08:35 +00:00
HILL_INCLINE_ID, HILL_DECLINE_ID, HILL_DOTS_RIGHT_ID,
HILL_DOTS_LEFT_ID, HILL_FILL_ID, HILL_TOP_ID,
BUSH_LEFT_ID, BUSH_MIDDLE_ID, BUSH_RIGHT_ID,
2021-08-07 19:59:06 +00:00
CLOUD_LEFT_BOTTOM_ID, CLOUD_MIDDLE_BOTTOM_ID, CLOUD_RIGHT_BOTTOM_ID,
2021-10-18 07:08:35 +00:00
CLOUD_LEFT_TOP_ID, CLOUD_MIDDLE_TOP_ID, CLOUD_RIGHT_TOP_ID,
CASTLE_LEFT_ID, CASTLE_RIGHT_ID, CASTLE_BLACK_ID,
CASTLE_ENTRY_ID, CASTLE_TOWER_ID, CASTLE_TOWER_FILLED_ID,
WATER_TOP_ID, WATER_FILL_ID
};
2021-08-07 19:59:06 +00:00
scene->setBackgroundObjectIDs(background_ids);
scene->updateBackgroundObjectZIndex();
2021-08-08 19:45:50 +00:00
// we want mario to be first because visiting isn't perfect
2022-09-19 20:56:50 +00:00
moving_objects.clear();
2021-08-08 19:45:50 +00:00
moving_objects.push_back(mario);
std::unordered_set<int> moving_object_ids = {
GOOMBA_ID,
};
2021-10-18 07:08:35 +00:00
for (auto &obj : scene->getObjects(moving_object_ids)) {
2021-08-08 19:45:50 +00:00
moving_objects.push_back(std::dynamic_pointer_cast<MarioBlock>(obj));
}
2022-09-19 20:56:50 +00:00
SceneStruct ret{};
ret.scene = scene;
ret.doInput = doInputMainGame;
ret.additionalRender = mainGameAdditional;
return ret;
}
2022-11-13 18:52:39 +00:00
void setTeleportLevelMain(const std::string &level) {
_teleport_level = level;
}
void loadLevel(const std::string &level, bool reset) {
2022-09-23 14:46:50 +00:00
// std::lock_guard<std::mutex> lock(render_mutex);
2022-11-13 18:52:39 +00:00
if(reset) {
coin_count = 0;
}
2022-09-19 20:56:50 +00:00
std::lock_guard<std::mutex> lock(gamescene_mutex);
2022-09-23 14:46:50 +00:00
for (auto &scene : game_scenes) {
scene.scene->resetScene();
}
2022-09-19 20:56:50 +00:00
game_scenes.clear();
2022-11-13 18:52:39 +00:00
int marioBig = 0;
if(!reset) {
if(mario->hasFire()) {
marioBig = 2;
} else if (mario->isBig()) {
marioBig = 1;
}
}
2022-09-19 20:56:50 +00:00
game_scenes.push_back(mainGameScene("levels/" + level));
2022-11-13 18:52:39 +00:00
if(!reset) {
for(int i = 0; i < marioBig; i++) {
mario->setBig();
}
}
2022-09-19 20:56:50 +00:00
game_scenes.back().scene->updateSizeAndPosition();
update = true;
newLoaded = true;
update_count = 2;
2022-11-13 18:52:39 +00:00
if(reset) {
last_load_level = level;
}
g_death = false;
}
2022-11-13 18:52:39 +00:00
void loadLevel(const std::string &level) {
loadLevel(level, true);
}
void loadLastLevel() {
2022-09-23 14:46:50 +00:00
if (last_load_level != "") {
loadLevel(last_load_level);
}
2022-09-19 20:56:50 +00:00
}
#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);
2022-11-12 20:32:18 +00:00
g_items_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/items.png", MARIO_OVERWORLD_COLORKEY);
2022-09-19 20:56:50 +00:00
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);
2022-09-23 14:46:50 +00:00
game_scenes.push_back(
createGameMainMenuScene(renderer, false, false, false));
2022-09-19 20:56:50 +00:00
std::thread inputThread(doInput);
2021-08-08 19:45:50 +00:00
SDL_PumpEvents();
2022-09-19 20:56:50 +00:00
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
2021-08-08 19:45:50 +00:00
renderer->presentRenderer();
update = true;
2022-09-19 20:56:50 +00:00
while (!g_quit) {
2021-10-18 07:08:35 +00:00
SDL_framerateDelay(&gFPS);
2021-08-08 19:45:50 +00:00
SDL_PumpEvents();
2021-10-18 07:08:35 +00:00
std::lock_guard<std::mutex> lock(render_mutex);
2022-11-13 18:52:39 +00:00
if (!_teleport_level.empty()) {
loadLevel(_teleport_level, false);
_teleport_level = "";
}
if (update) {
2022-09-23 14:46:50 +00:00
for (auto &scene : game_scenes) {
2022-09-19 20:56:50 +00:00
scene.scene->updateSizeAndPosition();
}
2022-09-23 14:46:50 +00:00
if (update_count > 0) {
update_count--;
2022-09-19 20:56:50 +00:00
} else {
update = false;
}
2021-05-25 18:21:02 +00:00
}
2022-09-19 20:56:50 +00:00
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);
}
2021-04-25 20:42:55 +00:00
}
2022-09-19 20:56:50 +00:00
renderer->presentRenderer();
2021-04-25 20:42:55 +00:00
}
inputThread.join();
2021-04-25 20:42:55 +00:00
return 0;
}