game/mario/editor.cpp

1185 lines
43 KiB
C++
Raw Normal View History

2021-04-30 19:02:14 +00:00
#include "../sdlpp/sdlpp.hpp"
#include "sprites.hpp"
#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 "global_vars.hpp"
#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"
2021-04-30 19:02:14 +00:00
2021-06-01 08:18:33 +00:00
#define MAP_WIDTH 24
2021-05-09 18:45:52 +00:00
#define MAP_HEIGHT 16
#define QUIT_FLAG 0x00000001
#define UPDATE_FLAG 0x00000002
#define MAP_LEFT_ENABLED_FLAG 0x00000004
#define MAP_RIGHT_ENABLED_FLAG 0x00000008
#define TOOL_LEFT_ENABLED_FLAG 0x00000010
#define TOOL_RIGHT_ENABLED_FLAG 0x00000020
2021-05-28 17:51:02 +00:00
#define MOD_LEFT_ENABLED_FLAG 0x00000040
#define MOD_RIGHT_ENABLED_FLAG 0x00000080
#define CHARACTER_LEFT_ENABLED_FLAG 0x00000100
#define CHARACTER_RIGHT_ENABLED_FLAG 0x00000200
#define TOOLS_WIDTH 4
2021-06-01 08:18:33 +00:00
#define CHARACTER_WIDTH 3
2021-05-31 12:03:11 +00:00
#define MOD_WIDTH 2
#define OVERWORLD_WIDTH 2
2021-05-28 17:51:02 +00:00
struct ToolType {
enum Value {
BLOCK,
MOD,
CHARACTER,
2021-05-31 12:03:11 +00:00
WORLD,
2021-05-28 17:51:02 +00:00
};
};
2021-05-08 22:43:53 +00:00
struct MouseInfo {
2021-08-07 10:13:23 +00:00
uint64_t cur_flags{};
uint64_t prev_flags{};
2021-10-18 07:08:35 +00:00
SDLPP::Vec2D<int> edit_box;
SDLPP::Vec2D<int> tool_box;
2021-08-07 10:13:23 +00:00
ToolType::Value tool_type{};
2021-05-08 22:43:53 +00:00
};
2021-05-09 18:21:53 +00:00
struct MapInfo {
int cur_page;
int max_page;
2021-05-09 18:21:53 +00:00
};
struct ToolInfo {
2021-05-28 17:51:02 +00:00
ToolType::Value type;
uint64_t index;
SDL_Rect texture_src;
2021-05-28 17:51:02 +00:00
int cur_page_tools;
int max_page_tools;
int cur_page_mods;
int max_page_mods;
int cur_page_characters;
int max_page_characters;
2021-05-09 18:21:53 +00:00
};
struct GlobalVars {
2021-08-07 10:13:23 +00:00
MouseInfo mouse{};
MapInfo map{};
ToolInfo tool{};
uint64_t flags{};
2021-10-18 07:08:35 +00:00
std::vector<mapColumnType> objects;
std::vector<std::shared_ptr<MarioBlock>> tools;
std::vector<std::shared_ptr<MarioBlock>> mods;
std::vector<std::shared_ptr<MarioBlock>> characters;
std::vector<std::shared_ptr<SDLPP::RenderObject>> tool_boxes;
std::vector<std::shared_ptr<SDLPP::RenderObject>> mod_boxes;
std::vector<std::shared_ptr<SDLPP::RenderObject>> character_boxes;
2021-08-07 10:13:23 +00:00
enum LandType::Value current_world_type{};
2021-10-18 07:08:35 +00:00
std::shared_ptr<MarioBlock> coin_tool;
std::shared_ptr<MarioBlock> generic_tool;
std::shared_ptr<MarioBlock> current_tool;
std::shared_ptr<SDLPP::Texture> translucent_terrain_texture;
std::shared_ptr<SDLPP::Texture> translucent_mario_texture;
std::shared_ptr<SDLPP::Texture> translucent_mod_texture;
std::shared_ptr<SDLPP::Texture> translucent_enemies_texture;
std::shared_ptr<SDLPP::RenderObject> mario;
SDLPP::Vec2D<int> mario_pos;
};
GlobalVars global_vars;
std::mutex destruction_mutex;
2021-05-02 12:28:17 +00:00
2021-05-08 22:43:53 +00:00
void updateTool() {
auto tool_index = global_vars.tool.index;
2021-05-28 17:51:02 +00:00
uint64_t tool_id = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.tool.type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
tool_id = possibleBlocks[tool_index];
break;
case ToolType::MOD:
tool_id = possibleMods[tool_index];
break;
case ToolType::CHARACTER:
tool_id = possibleCharacters[tool_index];
default:
break;
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
auto tool_role = getBlockRole(tool_id);
std::shared_ptr<SDLPP::Texture> target_texture = nullptr;
SDL_Rect target_src;
2021-10-18 07:08:35 +00:00
switch (tool_role) {
2021-05-08 22:46:10 +00:00
case BlockRole::TERRAIN:
target_texture = global_vars.translucent_terrain_texture;
2021-10-18 07:08:35 +00:00
target_src =
global_vars.tools[global_vars.tool.index]->getTextureSourceRect();
2021-05-08 22:46:10 +00:00
break;
case BlockRole::MARIO:
target_texture = global_vars.translucent_mario_texture;
2021-10-18 07:08:35 +00:00
target_src = global_vars.characters[global_vars.tool.index]
->getTextureSourceRect();
2021-05-08 22:46:10 +00:00
break;
case BlockRole::MODIFIER:
2021-06-24 18:53:58 +00:00
target_texture = global_vars.translucent_mod_texture;
2021-10-18 07:08:35 +00:00
target_src =
global_vars.mods[global_vars.tool.index]->getTextureSourceRect();
2021-05-08 22:46:10 +00:00
break;
case BlockRole::CHARACTER:
2021-08-08 19:22:00 +00:00
target_texture = global_vars.translucent_enemies_texture;
2021-10-18 07:08:35 +00:00
target_src = global_vars.characters[global_vars.tool.index]
->getTextureSourceRect();
2021-05-08 22:46:10 +00:00
break;
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
global_vars.current_tool->setHidden(true);
if (tool_id == COIN_MODIFIER_ID) {
global_vars.coin_tool->setPos(global_vars.current_tool->getPos());
global_vars.current_tool = global_vars.coin_tool;
} else {
2021-10-18 07:08:35 +00:00
global_vars.generic_tool->setPos(global_vars.current_tool->getPos());
global_vars.current_tool = global_vars.generic_tool;
2021-10-18 07:08:35 +00:00
global_vars.current_tool->setTexture(target_texture, target_src);
global_vars.current_tool->setId(tool_id);
global_vars.current_tool->getCollisions()[0]->setId(tool_id);
}
2021-10-18 07:08:35 +00:00
global_vars.current_tool->setHidden(false);
2021-05-08 22:43:53 +00:00
}
void removeMario() {
2021-10-18 07:08:35 +00:00
if (!global_vars.mario) {
2021-05-08 22:43:53 +00:00
return;
2021-08-07 10:13:23 +00:00
}
global_vars
.objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()]
.unsetCharacter();
global_vars.mario->destroy();
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
void setToolColor(const std::string &color) {
std::vector<std::shared_ptr<SDLPP::RenderObject>> *store = nullptr;
int multiplier = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.tool.type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
multiplier = TOOLS_WIDTH;
store = &global_vars.tool_boxes;
break;
case ToolType::MOD:
multiplier = MOD_WIDTH;
store = &global_vars.mod_boxes;
break;
case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH;
store = &global_vars.character_boxes;
default:
break;
}
2021-10-18 07:08:35 +00:00
if (store == nullptr) {
2021-06-04 12:49:24 +00:00
return;
2021-08-07 10:13:23 +00:00
}
2021-10-18 07:08:35 +00:00
auto index = global_vars.tool.index % (2 * multiplier);
store->at(index)->setColor(color);
}
void setToolColor() {
2021-10-18 07:08:35 +00:00
setToolColor("#FFFF8888");
}
void unsetToolColor() {
2021-10-18 07:08:35 +00:00
setToolColor("#FFFFFF00");
}
2021-10-18 07:08:35 +00:00
void updateToolSelection(int prev_index, ToolType::Value type) {
unsetToolColor();
size_t cur_page = 0;
2021-05-28 17:51:02 +00:00
size_t multiplier = 0;
2021-10-18 07:08:35 +00:00
std::vector<std::shared_ptr<MarioBlock>> *tool_vec = nullptr;
switch (type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
cur_page = global_vars.tool.cur_page_tools;
multiplier = 2 * TOOLS_WIDTH;
tool_vec = &global_vars.tools;
break;
case ToolType::MOD:
cur_page = global_vars.tool.cur_page_mods;
multiplier = 2 * MOD_WIDTH;
tool_vec = &global_vars.mods;
break;
case ToolType::CHARACTER:
cur_page = global_vars.tool.cur_page_characters;
multiplier = 2 * CHARACTER_WIDTH;
tool_vec = &global_vars.characters;
default:
break;
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
if (tool_vec == nullptr) {
2021-06-04 12:49:24 +00:00
return;
2021-08-07 10:13:23 +00:00
}
auto cur = cur_page * multiplier;
2021-05-28 17:51:02 +00:00
size_t prev = prev_index * multiplier;
2021-10-18 07:08:35 +00:00
for (size_t i = prev;
i < (tool_vec->size() < prev + multiplier ? tool_vec->size()
: prev + multiplier);
i++) {
tool_vec->at(i)->setHidden(true);
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
for (size_t i = cur;
i < (tool_vec->size() < cur + multiplier ? tool_vec->size()
: cur + multiplier);
i++) {
tool_vec->at(i)->setHidden(false);
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index / multiplier == cur_page) {
setToolColor();
2021-06-04 12:49:24 +00:00
}
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
void moveToolsLeft(ToolType::Value type) {
switch (type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
global_vars.tool.cur_page_tools--;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_tools + 1, type);
2021-05-29 17:24:47 +00:00
break;
case ToolType::MOD:
global_vars.tool.cur_page_mods--;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_mods + 1, type);
2021-05-29 17:24:47 +00:00
break;
case ToolType::CHARACTER:
global_vars.tool.cur_page_characters--;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_characters + 1, type);
2021-05-29 17:24:47 +00:00
default:
break;
2021-05-28 17:51:02 +00:00
}
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
void moveToolsRight(ToolType::Value type) {
switch (type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
global_vars.tool.cur_page_tools++;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_tools - 1, type);
2021-05-29 17:24:47 +00:00
break;
case ToolType::MOD:
global_vars.tool.cur_page_mods++;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_mods - 1, type);
2021-05-29 17:24:47 +00:00
break;
case ToolType::CHARACTER:
global_vars.tool.cur_page_characters++;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_characters - 1, type);
2021-05-29 17:24:47 +00:00
default:
break;
2021-05-28 17:51:02 +00:00
}
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
void updateToolIndex(uint64_t new_index, ToolType::Value new_type) {
2021-05-28 17:51:02 +00:00
int multiplier = 0;
int *page = nullptr;
2021-10-18 07:08:35 +00:00
switch (new_type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH;
page = &global_vars.tool.cur_page_tools;
break;
case ToolType::MOD:
multiplier = 2 * MOD_WIDTH;
page = &global_vars.tool.cur_page_mods;
break;
case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH;
page = &global_vars.tool.cur_page_characters;
default:
break;
}
unsetToolColor();
2021-06-04 12:49:24 +00:00
global_vars.tool.type = new_type;
global_vars.tool.index = new_index;
setToolColor();
updateTool();
2021-10-18 07:08:35 +00:00
if (new_index / multiplier != static_cast<uint64_t>(*page)) {
auto prev = *page;
*page = new_index / multiplier;
2021-10-18 07:08:35 +00:00
updateToolSelection(prev, global_vars.tool.type);
}
}
2021-10-18 07:08:35 +00:00
void updateToolIndex(uint64_t new_index) {
updateToolIndex(new_index, global_vars.tool.type);
2021-06-04 12:49:24 +00:00
}
void selectLowerTool() {
int multiplier = 0;
size_t max_index = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.tool.type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH;
max_index = global_vars.tools.size() - 1;
break;
case ToolType::MOD:
multiplier = 2 * MOD_WIDTH;
max_index = global_vars.mods.size() - 1;
break;
case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH;
max_index = global_vars.characters.size() - 1;
default:
break;
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index % multiplier >=
static_cast<uint64_t>(multiplier / 2) ||
global_vars.tool.index + multiplier / 2 > max_index) {
2021-05-08 22:43:53 +00:00
return;
2021-05-29 17:24:47 +00:00
}
2021-10-18 07:08:35 +00:00
updateToolIndex(global_vars.tool.index + multiplier / 2);
}
void selectUpperTool() {
int multiplier = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.tool.type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH;
break;
case ToolType::MOD:
multiplier = 2 * MOD_WIDTH;
break;
case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH;
default:
break;
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index % multiplier <
static_cast<uint64_t>(multiplier / 2)) {
return;
2021-05-29 17:24:47 +00:00
}
2021-10-18 07:08:35 +00:00
updateToolIndex(global_vars.tool.index - multiplier / 2);
2021-05-08 22:43:53 +00:00
}
void selectPrevTool() {
2021-05-28 17:51:02 +00:00
int multiplier = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.tool.type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
multiplier = TOOLS_WIDTH;
break;
case ToolType::MOD:
multiplier = MOD_WIDTH;
break;
case ToolType::CHARACTER:
multiplier = CHARACTER_WIDTH;
default:
break;
}
int subtraction = 1;
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index % multiplier == 0) {
subtraction = multiplier + 1;
2021-08-07 10:13:23 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index == 0 || global_vars.tool.index - subtraction >
static_cast<uint64_t>(-multiplier)) {
return;
2021-05-29 17:24:47 +00:00
}
2021-10-18 07:08:35 +00:00
updateToolIndex(global_vars.tool.index - subtraction);
}
void selectNextTool() {
2021-05-28 17:51:02 +00:00
size_t max_index = 0;
int multiplier = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.tool.type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
max_index = global_vars.tools.size() - 1;
multiplier = TOOLS_WIDTH;
break;
case ToolType::MOD:
max_index = global_vars.mods.size() - 1;
multiplier = MOD_WIDTH;
break;
case ToolType::CHARACTER:
max_index = global_vars.characters.size() - 1;
multiplier = CHARACTER_WIDTH;
default:
break;
2021-05-28 17:51:02 +00:00
}
int addition = 1;
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index % multiplier ==
static_cast<uint64_t>(multiplier - 1)) {
addition = multiplier + 1;
2021-08-07 10:13:23 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index == max_index) {
2021-05-08 22:43:53 +00:00
return;
2021-08-07 10:13:23 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.tool.index + addition > max_index) {
addition = 1;
2021-08-07 10:13:23 +00:00
}
2021-10-18 07:08:35 +00:00
updateToolIndex(global_vars.tool.index + addition);
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
void setFlag(uint64_t flag) {
global_vars.flags |= flag;
}
2021-10-18 07:08:35 +00:00
void unsetFlag(uint64_t flag) {
global_vars.flags &= ~flag;
}
2021-10-18 07:08:35 +00:00
bool getFlag(uint64_t flag) {
return global_vars.flags & flag;
}
2021-05-31 12:03:11 +00:00
void updateWorld() {
2021-10-18 07:08:35 +00:00
for (auto &block : global_vars.tools) {
block->setType(global_vars.current_world_type);
2021-05-31 12:03:11 +00:00
}
2021-10-18 07:08:35 +00:00
for (auto &block : global_vars.characters) {
block->setType(global_vars.current_world_type);
2021-05-31 12:03:11 +00:00
}
2021-10-18 07:08:35 +00:00
if (getBlockRole(global_vars.current_tool->getId()) !=
BlockRole::MODIFIER) {
global_vars.current_tool->setType(global_vars.current_world_type);
}
2021-05-31 12:03:11 +00:00
}
// TODO add mouse wheel control for modifiers
2021-10-18 07:08:35 +00:00
void handleKeyUp(SDL_Keycode key, SDLPP::Scene &scene) {
switch (key) {
2021-04-30 19:02:14 +00:00
case SDLK_ESCAPE:
2021-10-18 07:08:35 +00:00
setFlag(QUIT_FLAG);
2021-04-30 19:02:14 +00:00
break;
case SDLK_a:
2021-05-08 22:43:53 +00:00
selectPrevTool();
2021-04-30 19:02:14 +00:00
break;
case SDLK_d:
2021-05-08 22:43:53 +00:00
selectNextTool();
2021-04-30 19:02:14 +00:00
break;
case SDLK_w:
selectUpperTool();
break;
case SDLK_s:
selectLowerTool();
break;
2021-05-31 12:03:11 +00:00
case SDLK_1:
global_vars.current_world_type = possibleLands[0];
updateWorld();
break;
case SDLK_2:
global_vars.current_world_type = possibleLands[1];
updateWorld();
break;
case SDLK_3:
global_vars.current_world_type = possibleLands[2];
updateWorld();
break;
case SDLK_4:
global_vars.current_world_type = possibleLands[3];
updateWorld();
break;
2021-04-30 19:02:14 +00:00
case SDLK_r:
scene.getRenderer().setRenderColiders(
2021-10-18 07:08:35 +00:00
!scene.getRenderer().getRenderColiders());
2021-04-30 19:02:14 +00:00
default:
break;
}
}
2021-10-18 07:08:35 +00:00
void getMousePositionFlags(SDLPP::Scene &scene) {
auto mouse = scene.getObjects({ EDITOR_MOUSE_ID })[0];
2021-05-08 22:43:53 +00:00
// move mouse colider to mouse position
2021-10-18 07:08:35 +00:00
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
2021-05-02 12:28:17 +00:00
2021-05-08 22:43:53 +00:00
MouseVisitor visitor;
2021-10-18 07:08:35 +00:00
scene.visitCollisions(*mouse, visitor);
global_vars.mouse.cur_flags = visitor.getFlags();
// + 1 because the left map arrow is on position 0
global_vars.mouse.edit_box =
2021-10-18 07:08:35 +00:00
visitor.getEditBoxIndexes() + SDLPP::Vec2D<int>(1, 0);
global_vars.mouse.tool_box = visitor.getToolBoxIndexes();
2021-05-29 17:24:47 +00:00
global_vars.mouse.tool_type =
2021-10-18 07:08:35 +00:00
static_cast<ToolType::Value>(visitor.getToolType());
2021-05-08 22:43:53 +00:00
// if we found an edit box, move tool icon to that box
2021-10-18 07:08:35 +00:00
if (visitor.foundEditBox()) {
const auto &box = global_vars.mouse.edit_box;
2021-10-18 07:08:35 +00:00
global_vars.current_tool->setPos(box.getX() * BLOCK_SIZE,
1 - MAP_HEIGHT * BLOCK_SIZE +
box.getY() * BLOCK_SIZE);
2021-05-08 22:43:53 +00:00
}
}
2021-10-18 07:08:35 +00:00
void mouseUpAction(uint64_t flags, SDLPP::Scene &scene) {
if (MouseVisitor::moveMapLeft(flags) && global_vars.map.cur_page != 0) {
global_vars.map.cur_page--;
2021-10-18 07:08:35 +00:00
scene.moveEverything(BLOCK_SIZE, 0);
2021-05-07 08:08:41 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveMapRight(flags)) {
if (global_vars.map.cur_page == global_vars.map.max_page) {
2021-05-08 22:43:53 +00:00
// add column
2021-10-18 07:08:35 +00:00
global_vars.objects.resize(global_vars.objects.size() + 1);
global_vars.map.max_page++;
2021-05-08 22:43:53 +00:00
}
global_vars.map.cur_page += 1;
2021-10-18 07:08:35 +00:00
scene.moveEverything(-BLOCK_SIZE, 0);
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveToolsLeft(flags) &&
global_vars.tool.cur_page_tools != 0) {
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_tools--;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_tools + 1,
ToolType::BLOCK);
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveToolsRight(flags) &&
global_vars.tool.cur_page_tools != global_vars.tool.max_page_tools) {
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_tools++;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_tools - 1,
ToolType::BLOCK);
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveModsLeft(flags) &&
global_vars.tool.cur_page_mods != 0) {
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_mods--;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_mods + 1, ToolType::MOD);
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveModsRight(flags) &&
global_vars.tool.cur_page_mods != global_vars.tool.max_page_mods) {
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_mods++;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_mods - 1, ToolType::MOD);
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveCharactersLeft(flags) &&
global_vars.tool.cur_page_characters != 0) {
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_characters--;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_characters + 1,
ToolType::CHARACTER);
2021-05-28 17:51:02 +00:00
}
2021-10-18 07:08:35 +00:00
if (MouseVisitor::moveCharactersRight(flags) &&
global_vars.tool.cur_page_characters !=
global_vars.tool.max_page_characters) {
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_characters++;
2021-10-18 07:08:35 +00:00
updateToolSelection(global_vars.tool.cur_page_characters - 1,
ToolType::CHARACTER);
2021-05-07 08:08:41 +00:00
}
}
2021-10-18 07:08:35 +00:00
SDLPP::Vec2D<int> getSelectedObjectIndexes() {
// -1 because we're indexing edit boxes from 1 (due to left arrow on map)
return global_vars.mouse.edit_box +
2021-10-18 07:08:35 +00:00
SDLPP::Vec2D<int>(global_vars.map.cur_page - 1, 0);
2021-05-08 22:43:53 +00:00
}
MapObject &getSelectedObject() {
2021-05-31 14:10:19 +00:00
auto pos = getSelectedObjectIndexes();
return global_vars.objects[pos.getX()][pos.getY()];
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
void placeTool(SDLPP::Scene &scene) {
std::lock_guard<std::mutex> lock(destruction_mutex);
2021-05-08 22:43:53 +00:00
ToolVisitor visitor;
2021-10-18 07:08:35 +00:00
visitor.setSourceType(global_vars.current_world_type);
visitor.setSourceData(global_vars.current_tool->getData());
auto tool_type = getBlockRole(global_vars.current_tool->getId());
switch (tool_type) {
2021-05-08 22:46:10 +00:00
case BlockRole::TERRAIN:
2021-10-18 07:08:35 +00:00
visitor.setVisitorType(VisitorType::Terrain);
2021-05-08 22:46:10 +00:00
break;
2021-08-08 19:22:00 +00:00
case BlockRole::CHARACTER:
case BlockRole::MARIO:
2021-10-18 07:08:35 +00:00
visitor.setVisitorType(VisitorType::Character);
2021-08-08 19:22:00 +00:00
break;
2021-05-08 22:46:10 +00:00
default:
2021-10-18 07:08:35 +00:00
visitor.setVisitorType(VisitorType::Modifier);
2021-05-08 22:46:10 +00:00
break;
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
scene.visitCollisions(*global_vars.current_tool, visitor);
auto &obj = getSelectedObject();
2021-10-18 07:08:35 +00:00
if (visitor.removeBlock() && !visitor.addBlock()) {
switch (visitor.getVisitorType()) {
2021-05-08 22:46:10 +00:00
case VisitorType::Terrain:
obj.unsetTerrain();
2021-05-08 22:46:10 +00:00
break;
2021-08-08 19:22:00 +00:00
case VisitorType::Character:
obj.unsetCharacter();
break;
2021-05-08 22:46:10 +00:00
case VisitorType::Modifier:
obj.unsetModifier();
2021-05-08 22:46:10 +00:00
break;
default:
break;
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
} else if (visitor.addBlock()) {
auto renderer = scene.getRendererShared();
2021-08-07 10:13:23 +00:00
uint64_t z_index = 1;
2021-10-18 07:08:35 +00:00
std::shared_ptr<MarioBlock> new_obj = nullptr;
switch (visitor.getVisitorType()) {
2021-05-08 22:46:10 +00:00
case VisitorType::Terrain:
2021-10-18 07:08:35 +00:00
obj.setTerrain(global_vars.current_tool->getId(),
global_vars.current_world_type);
new_obj = createTerrainBlock(
obj.getTerrainId(), obj.getTerrainType(), renderer,
global_vars.mouse.edit_box.getX(),
2021-10-18 07:08:35 +00:00
global_vars.mouse.edit_box.getY(), false, true);
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
2021-05-08 22:46:10 +00:00
break;
2021-08-08 19:22:00 +00:00
case VisitorType::Character:
2021-10-18 07:08:35 +00:00
if (tool_type == BlockRole::MARIO) {
obj.setCharacter(MARIO_ID, global_vars.current_world_type);
new_obj = createMario(global_vars.current_world_type, renderer,
global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), true);
// remove mario if exists
removeMario();
global_vars.mario = new_obj;
2021-05-31 15:28:26 +00:00
global_vars.mario_pos = getSelectedObjectIndexes();
2021-10-18 07:08:35 +00:00
new_obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID);
z_index = scene.getObjects().size() - 1;
} else {
2021-10-18 07:08:35 +00:00
obj.setCharacter(global_vars.current_tool->getId(),
global_vars.current_world_type);
new_obj = createTerrainBlock(
obj.getCharacterId(), obj.getCharacterType(), renderer,
global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), false, true);
2021-08-08 19:22:00 +00:00
new_obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID);
2021-10-18 07:08:35 +00:00
dynamic_cast<MarioBlock *>(new_obj.get())->setTerrain(false);
z_index = scene.getObjects().size() - 1;
}
2021-05-08 22:46:10 +00:00
break;
2021-08-08 19:22:00 +00:00
case VisitorType::Modifier:
2021-10-18 07:08:35 +00:00
obj.setModifier(global_vars.current_tool->getId(),
global_vars.current_tool->getData());
2021-08-08 19:22:00 +00:00
new_obj = createTerrainBlock(
obj.getModifierId(), LandType::OVERWORLD, renderer,
global_vars.mouse.edit_box.getX(),
2021-10-18 07:08:35 +00:00
global_vars.mouse.edit_box.getY(), false, true);
new_obj->setTextureKeepSRC(global_vars.translucent_mod_texture);
new_obj->setData(global_vars.current_tool->getData());
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
2021-08-08 19:22:00 +00:00
// TODO createModifierBlock
2021-10-18 07:08:35 +00:00
dynamic_cast<MarioBlock *>(new_obj.get())->setTerrain(false);
2021-08-08 19:22:00 +00:00
z_index = scene.getObjects().size() - 1;
break;
2021-05-08 22:46:10 +00:00
default:
break;
2021-05-08 22:43:53 +00:00
}
2021-10-18 07:08:35 +00:00
scene.addObject(new_obj);
scene.setZIndex(new_obj, z_index);
2021-04-30 19:02:14 +00:00
}
}
2021-10-18 07:08:35 +00:00
void pollEvents(SDLPP::Scene &scene) {
2021-04-30 19:02:14 +00:00
SDL_Event event;
2021-10-18 07:08:35 +00:00
while (SDLPP::getSDLEvent(event)) {
switch (event.type) {
2021-04-30 19:02:14 +00:00
case SDL_QUIT:
2021-10-18 07:08:35 +00:00
setFlag(QUIT_FLAG);
2021-04-30 19:02:14 +00:00
break;
case SDL_KEYUP:
2021-10-18 07:08:35 +00:00
handleKeyUp(event.key.keysym.sym, scene);
2021-04-30 19:02:14 +00:00
break;
case SDL_WINDOWEVENT:
2021-10-18 07:08:35 +00:00
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
setFlag(UPDATE_FLAG);
}
break;
2021-05-08 22:43:53 +00:00
case SDL_MOUSEMOTION:
2021-10-18 07:08:35 +00:00
getMousePositionFlags(scene);
break;
case SDL_MOUSEBUTTONUP:
2021-10-18 07:08:35 +00:00
if (global_vars.mouse.cur_flags == global_vars.mouse.prev_flags) {
mouseUpAction(global_vars.mouse.cur_flags, scene);
}
2021-10-18 07:08:35 +00:00
if (global_vars.mouse.edit_box.getX() != 0) {
placeTool(scene);
2021-04-30 19:02:14 +00:00
}
2021-10-18 07:08:35 +00:00
if (global_vars.mouse.tool_box.getX() != -1) {
auto &tool_box = global_vars.mouse.tool_box;
2021-05-28 17:51:02 +00:00
int multiplier = 0;
size_t max_index = 0;
size_t cur_page = 0;
2021-10-18 07:08:35 +00:00
switch (global_vars.mouse.tool_type) {
2021-05-29 17:24:47 +00:00
case ToolType::BLOCK:
multiplier = 2 * TOOLS_WIDTH;
max_index = global_vars.tools.size();
cur_page = global_vars.tool.cur_page_tools;
break;
case ToolType::MOD:
multiplier = 2 * MOD_WIDTH;
max_index = global_vars.mods.size();
cur_page = global_vars.tool.cur_page_mods;
break;
case ToolType::CHARACTER:
multiplier = 2 * CHARACTER_WIDTH;
max_index = global_vars.characters.size();
cur_page = global_vars.tool.cur_page_characters;
2021-05-31 12:03:11 +00:00
break;
case ToolType::WORLD:
multiplier = 2 * OVERWORLD_WIDTH;
2021-05-29 17:24:47 +00:00
default:
break;
2021-05-28 17:51:02 +00:00
}
2021-05-29 17:24:47 +00:00
size_t index =
2021-10-18 07:08:35 +00:00
tool_box.getY() * (multiplier / 2) + tool_box.getX();
if (global_vars.mouse.tool_type == ToolType::WORLD) {
2021-05-31 12:03:11 +00:00
global_vars.current_world_type = possibleLands[index];
updateWorld();
2021-10-18 07:08:35 +00:00
} else if (index < max_index) {
updateToolIndex(cur_page * multiplier + index,
global_vars.mouse.tool_type);
2021-05-07 07:43:57 +00:00
}
}
break;
case SDL_MOUSEBUTTONDOWN:
2021-05-08 22:43:53 +00:00
// store current mouse flags in previous mouse flags
global_vars.mouse.prev_flags = global_vars.mouse.cur_flags;
break;
case SDL_MOUSEWHEEL:
2021-10-18 07:08:35 +00:00
if (event.wheel.y > 0) {
global_vars.current_tool->onScrollUp();
2021-10-18 07:08:35 +00:00
} else if (event.wheel.y < 0) {
global_vars.current_tool->onScrollDown();
}
break;
2021-04-30 19:02:14 +00:00
default:
break;
}
}
}
2021-10-18 07:08:35 +00:00
void doInput(std::shared_ptr<SDLPP::Scene> scene) {
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 (true) {
SDL_framerateDelay(&gFPS);
pollEvents(*scene);
2021-04-30 19:02:14 +00:00
scene->updateScene();
}
}
2021-10-18 07:08:35 +00:00
void createGrid(double start_x, double start_y, int count_x, int count_y,
std::shared_ptr<SDLPP::Scene> &scene) {
auto renderer = scene->getRendererShared();
auto width = count_x * BLOCK_SIZE;
auto height = count_y * BLOCK_SIZE;
2021-10-18 07:08:35 +00:00
for (int i = 0; i < count_x + 1; i++) {
auto line_vertical = std::make_shared<SDLPP::LineRenderer>(
2021-05-29 17:24:47 +00:00
start_x + i * BLOCK_SIZE, start_y, start_x + i * BLOCK_SIZE,
2021-10-18 07:08:35 +00:00
start_y + height, renderer, "#282828");
line_vertical->setPermanent();
2021-10-18 07:08:35 +00:00
line_vertical->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
scene->addObject(line_vertical);
}
2021-10-18 07:08:35 +00:00
for (int i = 0; i < count_y + 1; i++) {
auto line_horizontal = std::make_shared<SDLPP::LineRenderer>(
2021-05-29 17:24:47 +00:00
start_x, start_y + i * BLOCK_SIZE, start_x + width,
2021-10-18 07:08:35 +00:00
start_y + i * BLOCK_SIZE, renderer, "#282828");
line_horizontal->setPermanent();
2021-10-18 07:08:35 +00:00
line_horizontal->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
scene->addObject(line_horizontal);
}
}
2021-10-18 07:08:35 +00:00
std::pair<std::shared_ptr<SDLPP::TextRenderer>,
std::shared_ptr<SDLPP::TextRenderer>>
createArrowControls(double left_x, double right_x, double start_y,
double height, uint64_t left_id, uint64_t right_id,
std::shared_ptr<SDLPP::Scene> &scene,
std::shared_ptr<SDLPP::FontConfiguration> &font_config) {
auto renderer = scene->getRendererShared();
// white rectangles
2021-10-18 07:08:35 +00:00
auto rectangle1 = std::make_shared<SDLPP::RectangleRender>(
left_x, start_y, BLOCK_SIZE, height, renderer, "#FFFFFF88", true);
rectangle1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
rectangle1->setId(left_id);
rectangle1->setPermanent();
2021-10-18 07:08:35 +00:00
rectangle1->addCollision(SDLPP::RectColider(0, 0, 1, 1));
scene->addObject(rectangle1);
// white rectangles
2021-10-18 07:08:35 +00:00
auto rectangle2 = std::make_shared<SDLPP::RectangleRender>(
right_x, start_y, BLOCK_SIZE, height, renderer, "#FFFFFF88", true);
rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
rectangle2->setId(right_id);
rectangle2->setPermanent();
2021-10-18 07:08:35 +00:00
rectangle2->addCollision(SDLPP::RectColider(0, 0, 1, 1));
scene->addObject(rectangle2);
2021-10-18 07:08:35 +00:00
auto left = std::make_shared<SDLPP::TextRenderer>(
2021-05-29 17:24:47 +00:00
left_x, start_y + height / 2.0 - 0.5 * BLOCK_SIZE, BLOCK_SIZE,
2021-10-18 07:08:35 +00:00
BLOCK_SIZE, renderer, "<", font_config);
left->setId(0);
left->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
left->setPermanent();
2021-10-18 07:08:35 +00:00
scene->addObject(left);
auto right = std::make_shared<SDLPP::TextRenderer>(
right_x, start_y + height / 2.0 - 0.5 * BLOCK_SIZE, BLOCK_SIZE,
2021-10-18 07:08:35 +00:00
BLOCK_SIZE, renderer, ">", font_config);
right->setId(0);
right->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
right->setPermanent();
2021-10-18 07:08:35 +00:00
scene->addObject(right);
2021-05-29 17:24:47 +00:00
return { left, right };
}
2021-05-29 17:24:47 +00:00
void populateToolGrid(
int count_x, int count_y, float start_x, float start_y,
2021-10-18 07:08:35 +00:00
ToolType::Value type, const std::vector<uint64_t> &blocks,
std::vector<std::shared_ptr<MarioBlock>> &tool_store,
std::vector<std::shared_ptr<SDLPP::RenderObject>> &tool_box_store,
std::shared_ptr<SDLPP::Scene> &scene, const std::string &title,
const std::shared_ptr<SDLPP::FontConfiguration> &font_config) {
auto renderer = scene->getRendererShared();
2021-10-18 07:08:35 +00:00
for (int j = 0; j < count_y; j++) {
for (int i = 0; i < count_x; i++) {
auto tool_box = std::make_shared<ToolBox>(i, j, start_x, start_y,
renderer, false);
scene->addObject(tool_box);
}
}
int tool_index = 0;
2021-10-18 07:08:35 +00:00
for (auto &block : blocks) {
switch (type) {
2021-05-29 17:24:47 +00:00
case ToolType::CHARACTER:
2021-10-18 07:08:35 +00:00
if (block == MARIO_ID) {
tool_store.push_back(createMario(global_vars.current_world_type,
renderer, 0, 0, true));
2021-06-24 18:53:58 +00:00
break;
}
// fall through
case ToolType::MOD:
2021-10-18 07:08:35 +00:00
tool_store.push_back(createTerrainBlock(
block, global_vars.current_world_type, renderer, false, true));
break;
case ToolType::BLOCK:
2021-10-18 07:08:35 +00:00
tool_store.push_back(createTerrainBlock(
block, global_vars.current_world_type, renderer, false, true));
break;
2021-05-31 12:04:28 +00:00
default:
break;
}
2021-10-18 07:08:35 +00:00
tool_store.back()->setHidden(true);
tool_store.back()->setPermanent();
2021-08-08 19:22:00 +00:00
tool_store.back()->pauseAnimation();
auto x = tool_index % count_x;
auto y = tool_index / count_x;
2021-10-18 07:08:35 +00:00
tool_store.back()->setPos(start_x + x * BLOCK_SIZE,
start_y + y * BLOCK_SIZE);
scene->addObject(tool_store.back());
tool_index = (tool_index + 1) % (2 * count_x);
}
2021-10-18 07:08:35 +00:00
for (int j = 0; j < count_y; j++) {
for (int i = 0; i < count_x; i++) {
2021-05-29 17:24:47 +00:00
auto tool_box =
2021-10-18 07:08:35 +00:00
std::make_shared<ToolBox>(i, j, start_x, start_y, renderer);
tool_box->setType(type);
scene->addObject(tool_box);
tool_box->setColor("#00000000");
tool_box_store.push_back(tool_box);
}
}
2021-10-18 07:08:35 +00:00
auto tool_text = std::make_shared<SDLPP::TextRenderer>(
start_x - BLOCK_SIZE, start_y - BLOCK_SIZE, (count_x + 2) * BLOCK_SIZE,
BLOCK_SIZE, renderer, title, font_config, SDLPP_TEXT_CENTER);
tool_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
2021-05-31 12:03:11 +00:00
tool_text->setPermanent();
2021-10-18 07:08:35 +00:00
scene->addObject(tool_text);
2021-05-31 12:03:11 +00:00
}
void populateWorldType(
int count_x, int count_y, float start_x, float start_y,
2021-10-18 07:08:35 +00:00
std::shared_ptr<SDLPP::Scene> &scene, const std::string &title,
const std::shared_ptr<SDLPP::FontConfiguration> &font_config) {
2021-05-31 12:03:11 +00:00
auto renderer = scene->getRendererShared();
2021-10-18 07:08:35 +00:00
for (int j = 0; j < count_y; j++) {
for (int i = 0; i < count_x; i++) {
2021-05-31 12:04:28 +00:00
auto tool_box =
2021-10-18 07:08:35 +00:00
std::make_shared<ToolBox>(i, j, start_x, start_y, renderer);
tool_box->setType(ToolType::WORLD);
scene->addObject(tool_box);
2021-05-31 12:03:11 +00:00
}
}
int tool_index = 0;
2021-10-18 07:08:35 +00:00
for (auto &type : possibleLands) {
auto land = createTerrainBlock(BRICK_ID, type, renderer, false, true);
2021-05-31 12:03:11 +00:00
auto x = tool_index % count_x;
auto y = tool_index / count_x;
2021-10-18 07:08:35 +00:00
land->setId(EDITOR_WORLD_CHANGE_ID);
2021-05-31 12:03:11 +00:00
land->setPermanent();
2021-10-18 07:08:35 +00:00
land->setPos(start_x + x * BLOCK_SIZE, start_y + y * BLOCK_SIZE);
scene->addObject(land);
tool_index = (tool_index + 1) % (2 * count_x);
2021-05-31 12:03:11 +00:00
}
2021-10-18 07:08:35 +00:00
auto tool_text = std::make_shared<SDLPP::TextRenderer>(
2021-05-31 12:04:28 +00:00
start_x, start_y - BLOCK_SIZE, count_x * BLOCK_SIZE, BLOCK_SIZE,
2021-10-18 07:08:35 +00:00
renderer, title, font_config, SDLPP_TEXT_CENTER);
tool_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
2021-05-31 12:03:11 +00:00
tool_text->setPermanent();
2021-10-18 07:08:35 +00:00
scene->addObject(tool_text);
}
2021-10-18 07:08:35 +00:00
void checkArrowsEnabled(uint64_t cur_page, uint64_t max_page,
uint64_t left_flag, uint64_t right_flag,
std::shared_ptr<SDLPP::TextRenderer> &left_arrow,
std::shared_ptr<SDLPP::TextRenderer> &right_arrow,
std::shared_ptr<SDLPP::Font> &font, bool map = false) {
if (cur_page == 0 && getFlag(left_flag)) {
setFlag(UPDATE_FLAG);
unsetFlag(left_flag);
left_arrow->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
} else if (cur_page != 0 && !getFlag(left_flag)) {
setFlag(UPDATE_FLAG);
setFlag(left_flag);
left_arrow->setTextColor(font, "#000000", "#282828", 0.05);
}
2021-10-18 07:08:35 +00:00
if (cur_page == max_page && getFlag(right_flag)) {
setFlag(UPDATE_FLAG);
unsetFlag(right_flag);
if (map) {
right_arrow->setTextColor(font, "#00FF00", "#000000", 0.1);
right_arrow->changeText("+");
} else {
2021-10-18 07:08:35 +00:00
right_arrow->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
}
2021-10-18 07:08:35 +00:00
} else if (cur_page != max_page && !getFlag(right_flag)) {
setFlag(UPDATE_FLAG);
setFlag(right_flag);
right_arrow->setTextColor(font, "#000000", "#282828", 0.05);
if (map) {
right_arrow->changeText(">");
}
}
}
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);
auto font_config = std::make_shared<SDLPP::FontConfiguration>(
font, "#000000", "#282828", 0.05);
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);
auto scene = std::make_shared<SDLPP::Scene>(renderer);
auto bg = std::make_shared<SDLPP::RectangleRender>(
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true);
bg->setPermanent();
2021-10-18 07:08:35 +00:00
bg->setId(1);
scene->addObject(bg);
2021-04-30 19:02:14 +00:00
2021-05-31 12:03:11 +00:00
global_vars.current_world_type = LandType::OVERWORLD;
// TODO file name
2021-10-18 07:08:35 +00:00
loadMap(scene, global_vars.mario, "test_binary.bin", global_vars.objects,
true, MAP_WIDTH);
2021-04-30 21:12:53 +00:00
// create grids and arrow controls
// map
2021-05-29 17:24:47 +00:00
auto arrows = createArrowControls(
2021-10-18 07:08:35 +00:00
0, (MAP_WIDTH + 1) * BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE,
MAP_HEIGHT * BLOCK_SIZE, EDITOR_LEFT_MAP_ID, EDITOR_RIGHT_MAP_ID, scene,
2021-10-18 07:08:35 +00:00
font_config);
auto left_map_arrow = arrows.first;
auto right_map_arrow = arrows.second;
2021-10-18 07:08:35 +00:00
createGrid(BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH, MAP_HEIGHT,
scene);
// edit blocks on map
2021-10-18 07:08:35 +00:00
for (int i = 0; i < MAP_WIDTH; i++) {
for (int j = 0; j < MAP_HEIGHT; j++) {
scene->addObject(std::make_shared<EditBox>(
i, j, BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, MAP_WIDTH,
2021-10-18 07:08:35 +00:00
MAP_HEIGHT, renderer));
}
}
// tools
2021-10-18 07:08:35 +00:00
populateToolGrid(TOOLS_WIDTH, 2, (MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, ToolType::BLOCK,
possibleBlocks, global_vars.tools, global_vars.tool_boxes,
scene, "TERRAIN", font_config);
arrows = createArrowControls(
2021-10-18 07:08:35 +00:00
(MAP_WIDTH - TOOLS_WIDTH - 1) * BLOCK_SIZE, MAP_WIDTH * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, 2 * BLOCK_SIZE, EDITOR_LEFT_TOOL_ID,
EDITOR_RIGHT_TOOL_ID, scene, font_config);
auto left_tool_arrow = arrows.first;
auto right_tool_arrow = arrows.second;
2021-10-18 07:08:35 +00:00
createGrid((MAP_WIDTH - TOOLS_WIDTH) * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, TOOLS_WIDTH, 2, scene);
// mods
2021-10-18 07:08:35 +00:00
populateToolGrid(MOD_WIDTH, 2, 5 * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, ToolType::MOD,
possibleMods, global_vars.mods, global_vars.mod_boxes,
scene, "MODS", font_config);
arrows = createArrowControls(4 * BLOCK_SIZE, (MOD_WIDTH + 5) * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
2 * BLOCK_SIZE, EDITOR_LEFT_MOD_ID,
EDITOR_RIGHT_MOD_ID, scene, font_config);
auto left_mod_arrow = arrows.first;
auto right_mod_arrow = arrows.second;
2021-10-18 07:08:35 +00:00
createGrid(5 * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, MOD_WIDTH, 2,
scene);
// characters
2021-10-18 07:08:35 +00:00
populateToolGrid(CHARACTER_WIDTH, 2, (MOD_WIDTH + 8) * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, ToolType::CHARACTER,
possibleCharacters, global_vars.characters,
global_vars.character_boxes, scene, "CHARACTERS",
font_config);
arrows = createArrowControls((MOD_WIDTH + 7) * BLOCK_SIZE,
(MOD_WIDTH + 8 + CHARACTER_WIDTH) * BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
2 * BLOCK_SIZE, EDITOR_LEFT_CHARACTER_ID,
EDITOR_RIGHT_CHARACTER_ID, scene, font_config);
auto left_char_arrow = arrows.first;
auto right_char_arrow = arrows.second;
2021-10-18 07:08:35 +00:00
createGrid((MOD_WIDTH + 8) * BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE,
CHARACTER_WIDTH, 2, scene);
2021-05-31 12:03:11 +00:00
// world type
2021-10-18 07:08:35 +00:00
populateWorldType(OVERWORLD_WIDTH, 2, BLOCK_SIZE,
1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, scene, "WORLD",
font_config);
createGrid(BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, OVERWORLD_WIDTH,
2, scene);
2021-05-31 12:03:11 +00:00
global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH;
2021-05-29 17:24:47 +00:00
global_vars.tool.max_page_tools =
2021-10-18 07:08:35 +00:00
(possibleBlocks.size() - 1) / (2 * TOOLS_WIDTH);
2021-05-29 17:24:47 +00:00
global_vars.tool.max_page_mods =
2021-10-18 07:08:35 +00:00
(possibleMods.size() - 1) / (2 * MOD_WIDTH);
2021-05-29 17:24:47 +00:00
global_vars.tool.max_page_characters =
2021-10-18 07:08:35 +00:00
(possibleCharacters.size() - 1) / (2 * CHARACTER_WIDTH);
auto mouse =
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
mouse->setMinWidth(1);
mouse->setMinHeight(1);
mouse->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
mouse->setId(EDITOR_MOUSE_ID);
mouse->setColiderColor("#00FF00");
mouse->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
scene->addObject(mouse);
2021-05-07 08:08:41 +00:00
2021-08-08 19:22:00 +00:00
global_vars.translucent_terrain_texture = g_translucent_terrain_texture;
2021-10-18 07:08:35 +00:00
global_vars.translucent_mario_texture = std::make_shared<SDLPP::Texture>(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY);
global_vars.translucent_mario_texture->setAlpha(100);
global_vars.generic_tool =
2021-10-18 07:08:35 +00:00
createTerrainBlock(possibleBlocks[global_vars.tool.index],
global_vars.current_world_type, renderer, false);
global_vars.generic_tool->setTextureKeepSRC(
2021-10-18 07:08:35 +00:00
global_vars.translucent_terrain_texture);
global_vars.generic_tool->removeCollisions();
global_vars.generic_tool->addCollision(
2021-10-18 07:08:35 +00:00
SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
global_vars.current_tool = global_vars.generic_tool;
global_vars.coin_tool = createTerrainBlock(
2021-10-18 07:08:35 +00:00
COIN_MODIFIER_ID, LandType::OVERWORLD, renderer, 0, 0, false, true);
global_vars.coin_tool->setData(1);
global_vars.coin_tool->removeCollisions();
2021-10-18 07:08:35 +00:00
global_vars.coin_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
2021-06-24 18:53:58 +00:00
global_vars.translucent_mod_texture = g_translucent_mod_texture;
2021-08-08 19:22:00 +00:00
global_vars.translucent_enemies_texture = g_translucent_enemies_texture;
2021-10-18 07:08:35 +00:00
dynamic_cast<MarioBlock &>(*global_vars.current_tool).setTool();
scene->addObject(global_vars.current_tool);
scene->addObject(global_vars.coin_tool);
global_vars.coin_tool->setHidden(true);
scene->moveZTop(global_vars.current_tool);
scene->moveZTop(global_vars.coin_tool);
scene->moveEverything(BLOCK_SIZE, 0);
2021-04-30 19:02:14 +00:00
FPSmanager gFPS;
2021-10-18 07:08:35 +00:00
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
2021-04-30 19:02:14 +00:00
2021-10-18 07:08:35 +00:00
updateToolSelection(0, ToolType::BLOCK);
updateToolSelection(0, ToolType::MOD);
updateToolSelection(0, ToolType::CHARACTER);
setToolColor();
2021-05-07 07:43:57 +00:00
2021-04-30 19:02:14 +00:00
auto base = SDL_GetTicks();
int frames = 0;
2021-10-18 07:08:35 +00:00
std::thread inputThread(doInput, scene);
2021-04-30 19:02:14 +00:00
inputThread.detach();
2021-10-18 07:08:35 +00:00
setFlag(UPDATE_FLAG);
2021-05-28 17:51:02 +00:00
global_vars.tool.cur_page_tools = 0;
global_vars.tool.cur_page_mods = 0;
global_vars.tool.cur_page_characters = 0;
// required for initial correct rendering
2021-10-18 07:08:35 +00:00
setFlag(MAP_LEFT_ENABLED_FLAG | MAP_RIGHT_ENABLED_FLAG |
TOOL_LEFT_ENABLED_FLAG | TOOL_RIGHT_ENABLED_FLAG |
MOD_LEFT_ENABLED_FLAG | MOD_RIGHT_ENABLED_FLAG |
CHARACTER_LEFT_ENABLED_FLAG | CHARACTER_RIGHT_ENABLED_FLAG);
2021-08-08 19:22:00 +00:00
SDL_PumpEvents();
scene->updateSizeAndPosition();
scene->renderScene();
renderer->presentRenderer();
setFlag(UPDATE_FLAG);
2021-10-18 07:08:35 +00:00
while (!getFlag(QUIT_FLAG)) {
SDL_framerateDelay(&gFPS);
2021-08-08 19:22:00 +00:00
SDL_PumpEvents();
2021-10-18 07:08:35 +00:00
std::lock_guard<std::mutex> lock(destruction_mutex);
2021-04-30 19:02:14 +00:00
scene->renderScene();
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();
}
2021-10-18 07:08:35 +00:00
checkArrowsEnabled(global_vars.map.cur_page, global_vars.map.max_page,
MAP_LEFT_ENABLED_FLAG, MAP_RIGHT_ENABLED_FLAG,
left_map_arrow, right_map_arrow, font, true);
checkArrowsEnabled(global_vars.tool.cur_page_tools,
global_vars.tool.max_page_tools,
TOOL_LEFT_ENABLED_FLAG, TOOL_RIGHT_ENABLED_FLAG,
left_tool_arrow, right_tool_arrow, font);
checkArrowsEnabled(global_vars.tool.cur_page_mods,
global_vars.tool.max_page_mods,
MOD_LEFT_ENABLED_FLAG, MOD_RIGHT_ENABLED_FLAG,
left_mod_arrow, right_mod_arrow, font);
checkArrowsEnabled(global_vars.tool.cur_page_characters,
global_vars.tool.max_page_characters,
CHARACTER_LEFT_ENABLED_FLAG,
CHARACTER_RIGHT_ENABLED_FLAG, left_char_arrow,
right_char_arrow, font);
if (getFlag(UPDATE_FLAG)) {
scene->updateSizeAndPosition();
2021-10-18 07:08:35 +00:00
unsetFlag(UPDATE_FLAG);
}
2021-04-30 19:02:14 +00:00
}
2021-10-18 07:08:35 +00:00
saveMap("test_binary2.bin", global_vars.objects);
2021-04-30 19:02:14 +00:00
return 0;
}