game/mario/editor.cpp

118 lines
3.7 KiB
C++
Raw Normal View History

2021-04-30 19:02:14 +00:00
#include "../sdlpp/sdlpp.hpp"
#include "global_vars.hpp"
2022-06-21 12:52:36 +00:00
#include "gui/gui.hpp"
2021-04-30 19:02:14 +00:00
#include "sprites.hpp"
#include <memory>
2021-04-30 19:02:14 +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
2021-05-07 06:35:01 +00:00
#include <array>
2021-04-30 19:02:14 +00:00
#include <thread>
2021-05-02 12:14:11 +00:00
#include <mutex>
2021-04-30 19:02:14 +00:00
#include "objectids.hpp"
#include "blocks.hpp"
#include "maploader.hpp"
#include "../sdlpp/sdlpp_mouse.hpp"
2021-05-02 12:14:11 +00:00
#include "edit_box.hpp"
#include "editor_visitor.hpp"
2021-05-07 07:43:57 +00:00
#include "tool_box.hpp"
#include "blocks/coineditorblock.hpp"
#include "scenes/editor_scenes.hpp"
2021-04-30 19:02:14 +00:00
std::vector<SceneStruct> game_scenes{};
2021-05-31 12:03:11 +00:00
void doInput() {
2021-04-30 19:02:14 +00:00
FPSmanager gFPS;
2021-10-18 07:08:35 +00:00
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
while (!g_quit) {
2021-10-18 07:08:35 +00:00
SDL_framerateDelay(&gFPS);
game_scenes.back().doInput(game_scenes.back().scene);
game_scenes.back().scene->updateScene();
}
}
2021-04-30 19:02:14 +00:00
#ifdef _WIN32
2021-10-18 07:08:35 +00:00
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR szCmdLine, int nCmdShow) {
2021-04-30 19:02:14 +00:00
#else
int main() {
#endif
SDLPP::init();
2021-10-18 07:08:35 +00:00
SDLPP::Window w("Mario editor!");
w.setResizable(true);
2021-06-01 08:18:33 +00:00
BLOCK_SIZE = 1.0 / 26;
2021-04-30 19:02:14 +00:00
2021-10-18 07:08:35 +00:00
auto font = std::make_shared<SDLPP::Font>("testfont.ttf", 36);
g_text_config = std::make_shared<SDLPP::FontConfiguration>(font, "#FFFFFF",
"#000000", 0.15);
2021-10-18 07:08:35 +00:00
auto renderer = std::make_shared<SDLPP::Renderer>(w);
renderer->setBlendMode(SDL_BLENDMODE_BLEND);
2021-04-30 19:02:14 +00:00
// prepare global vars
2021-10-18 07:08:35 +00:00
g_terrain_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY);
g_mario_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY);
g_mod_texture =
2021-10-18 07:08:35 +00:00
std::make_shared<SDLPP::Texture>(renderer, "sprites/mods.png");
g_enemies_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/enemies.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);
g_translucent_mod_texture =
2021-10-18 07:08:35 +00:00
std::make_shared<SDLPP::Texture>(renderer, "sprites/mods.png");
g_translucent_mod_texture->setAlpha(100);
g_translucent_enemies_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/enemies.png", MARIO_OVERWORLD_COLORKEY);
g_translucent_enemies_texture->setAlpha(100);
game_scenes.push_back(createEditorScene(renderer));
2022-06-21 12:52:36 +00:00
2021-04-30 19:02:14 +00:00
auto base = SDL_GetTicks();
int frames = 0;
std::thread inputThread(doInput);
2021-08-08 19:22:00 +00:00
SDL_PumpEvents();
game_scenes.back().scene->updateSizeAndPosition();
game_scenes.back().scene->renderScene();
2021-08-08 19:22:00 +00:00
renderer->presentRenderer();
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
while (!g_quit) {
2021-10-18 07:08:35 +00:00
SDL_framerateDelay(&gFPS);
2021-08-08 19:22:00 +00:00
SDL_PumpEvents();
std::lock_guard<std::mutex> lock(render_mutex);
auto max_game_scenes = game_scenes.size();
for (size_t i = 0; i < max_game_scenes; i++) {
2022-07-22 10:23:16 +00:00
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();
2021-04-30 19:02:14 +00:00
frames++;
2021-10-18 07:08:35 +00:00
if (SDL_GetTicks() - base >= 1000) {
2021-04-30 19:02:14 +00:00
std::cout << "FPS: " << frames << std::endl;
frames = 0;
base = SDL_GetTicks();
}
}
inputThread.join();
2021-04-30 19:02:14 +00:00
return 0;
}