2021-04-30 21:02:14 +02: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 08:35:01 +02:00
|
|
|
#include <array>
|
2021-04-30 21:02:14 +02:00
|
|
|
#include <thread>
|
2021-05-02 14:14:11 +02:00
|
|
|
#include <mutex>
|
2021-04-30 21:02:14 +02:00
|
|
|
#include "global_vars.hpp"
|
|
|
|
|
#include "objectids.hpp"
|
|
|
|
|
#include "blocks.hpp"
|
|
|
|
|
#include "maploader.hpp"
|
2021-05-01 21:55:43 +02:00
|
|
|
#include "../sdlpp/sdlpp_mouse.hpp"
|
2021-05-02 14:14:11 +02:00
|
|
|
#include "edit_box.hpp"
|
|
|
|
|
#include "editor_visitor.hpp"
|
2021-05-07 09:43:57 +02:00
|
|
|
#include "tool_box.hpp"
|
2021-04-30 21:02:14 +02:00
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
struct MouseInfo {
|
|
|
|
|
enum Index {
|
|
|
|
|
CUR_FLAGS = 0,
|
|
|
|
|
PREV_FLAGS = 1,
|
|
|
|
|
EDIT_BOX = 2,
|
|
|
|
|
TOOL_BOX = 3,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::shared_ptr< SDLPP::Renderer > g_renderer = nullptr;
|
|
|
|
|
bool g_quit = false;
|
|
|
|
|
bool g_update_size = false;
|
|
|
|
|
std::vector<mapColumnType> g_objects = {};
|
|
|
|
|
std::mutex g_destruction_mutex;
|
|
|
|
|
|
|
|
|
|
// current mouse flags, previous mouse flags, selected edit box, selected tool box
|
|
|
|
|
std::tuple<uint64_t, uint64_t, SDLPP::Vec2D<int>, SDLPP::Vec2D<int>> g_mouse_info;
|
|
|
|
|
|
|
|
|
|
int g_current_start_index = 0;
|
|
|
|
|
int g_current_max_index = 0;
|
|
|
|
|
uint64_t g_current_block = 0;
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<SDLPP::RenderObject>> g_tools{};
|
|
|
|
|
std::shared_ptr<SDLPP::RenderObject> g_current_tool = nullptr;
|
|
|
|
|
int g_current_tool_index = 0;
|
|
|
|
|
int g_max_tool_index = 0;
|
2021-04-30 21:02:14 +02:00
|
|
|
|
2021-05-02 14:28:17 +02:00
|
|
|
std::shared_ptr<SDLPP::Texture> g_placeholder_texture = nullptr;
|
2021-05-07 23:17:05 +02:00
|
|
|
std::shared_ptr<SDLPP::Texture> g_placeholder_mario = nullptr;
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<SDLPP::RenderObject> g_mario = nullptr;
|
|
|
|
|
SDLPP::Vec2D<int> g_mario_pos = {0,0};
|
2021-05-02 14:28:17 +02:00
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
enum LandType::Value g_current_world_type = LandType::OVERWORLD;
|
|
|
|
|
|
|
|
|
|
void updateTool() {
|
|
|
|
|
auto tool_role = getBlockRole(possibleBlocks[g_current_block]);
|
|
|
|
|
std::shared_ptr<SDLPP::Texture> target_texture = nullptr;
|
|
|
|
|
switch(tool_role) {
|
|
|
|
|
case BlockRole::TERRAIN:
|
|
|
|
|
target_texture = g_placeholder_texture;
|
|
|
|
|
break;
|
|
|
|
|
case BlockRole::MARIO:
|
|
|
|
|
target_texture = g_placeholder_mario;
|
|
|
|
|
break;
|
|
|
|
|
case BlockRole::MODIFIER:
|
|
|
|
|
break;
|
|
|
|
|
case BlockRole::CHARACTER:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
g_current_tool->setTexture(target_texture, getSourceRectByID(possibleBlocks[g_current_block], g_current_world_type));
|
|
|
|
|
g_current_tool->setId(possibleBlocks[g_current_block]);
|
|
|
|
|
g_current_tool->getCollisions()[0]->setId(possibleBlocks[g_current_block]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void removeMario() {
|
|
|
|
|
if(!g_mario)
|
|
|
|
|
return;
|
|
|
|
|
auto prev = g_objects[g_mario_pos.getX()][g_mario_pos.getY()];
|
|
|
|
|
// remove character/modifiers
|
|
|
|
|
g_objects[g_mario_pos.getX()][g_mario_pos.getY()] = {std::get<0>(prev), std::get<1>(prev), 0, 0, 0, 0};
|
|
|
|
|
g_mario->destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateToolSelection(int prev_index) {
|
|
|
|
|
size_t prev = prev_index * 8;
|
|
|
|
|
size_t cur = g_current_tool_index * 8;
|
|
|
|
|
for(size_t i = prev; i < (g_tools.size() < prev + 8 ? g_tools.size() : prev + 8); i++) {
|
|
|
|
|
g_tools[i]->setHidden(true);
|
|
|
|
|
}
|
|
|
|
|
for(size_t i = cur; i < (g_tools.size() < cur + 8 ? g_tools.size() : cur + 8); i++) {
|
|
|
|
|
g_tools[i]->setHidden(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void moveToolsLeft() {
|
|
|
|
|
g_current_tool_index--;
|
|
|
|
|
updateToolSelection(g_current_tool_index + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void moveToolsRight() {
|
|
|
|
|
g_current_tool_index++;
|
|
|
|
|
updateToolSelection(g_current_tool_index - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO add red outline to currently selected tool
|
|
|
|
|
// add WSAD navigation for the red highlight
|
|
|
|
|
void selectPrevTool() {
|
|
|
|
|
if(g_current_block == 0)
|
|
|
|
|
return;
|
|
|
|
|
if(g_current_block % 8 == 0) {
|
|
|
|
|
moveToolsLeft();
|
|
|
|
|
}
|
|
|
|
|
g_current_block--;
|
|
|
|
|
updateTool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void selectNextTool() {
|
|
|
|
|
if(g_current_block == g_tools.size() - 1)
|
|
|
|
|
return;
|
|
|
|
|
if(g_current_block % 8 == 7) {
|
|
|
|
|
moveToolsRight();
|
|
|
|
|
}
|
|
|
|
|
g_current_block++;
|
|
|
|
|
updateTool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) {
|
2021-04-30 21:02:14 +02:00
|
|
|
switch ( key ) {
|
|
|
|
|
case SDLK_ESCAPE:
|
2021-05-09 00:43:53 +02:00
|
|
|
g_quit = true;
|
2021-04-30 21:02:14 +02:00
|
|
|
break;
|
|
|
|
|
case SDLK_a:
|
2021-05-09 00:43:53 +02:00
|
|
|
selectPrevTool();
|
2021-04-30 21:02:14 +02:00
|
|
|
break;
|
|
|
|
|
case SDLK_d:
|
2021-05-09 00:43:53 +02:00
|
|
|
selectNextTool();
|
2021-04-30 21:02:14 +02:00
|
|
|
break;
|
|
|
|
|
case SDLK_r:
|
|
|
|
|
scene.getRenderer().setRenderColiders(
|
|
|
|
|
!scene.getRenderer().getRenderColiders() );
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
void getMousePositionFlags(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));
|
2021-05-02 14:28:17 +02:00
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
MouseVisitor visitor;
|
|
|
|
|
scene.visitCollisions(*mouse, visitor);
|
|
|
|
|
std::get<MouseInfo::CUR_FLAGS>(g_mouse_info) = visitor.getFlags();
|
|
|
|
|
std::get<MouseInfo::EDIT_BOX>(g_mouse_info) = visitor.getEditBoxIndexes();
|
|
|
|
|
std::get<MouseInfo::TOOL_BOX>(g_mouse_info) = visitor.getToolBoxIndexes();
|
|
|
|
|
// if we found an edit box, move tool icon to that box
|
|
|
|
|
if(visitor.foundEditBox()) {
|
|
|
|
|
const auto &box = std::get<MouseInfo::EDIT_BOX>(g_mouse_info);
|
|
|
|
|
g_current_tool->setPos(BLOCK_SIZE + box.getX() * BLOCK_SIZE, 4*BLOCK_SIZE + box.getY() * BLOCK_SIZE);
|
|
|
|
|
}
|
2021-05-07 23:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
void mouseUpAction(uint64_t flags, SDLPP::Scene &scene) {
|
|
|
|
|
if(MouseVisitor::moveMapLeft(flags) && g_current_start_index != 0) {
|
|
|
|
|
g_current_start_index -= 1;
|
|
|
|
|
scene.moveEverything(BLOCK_SIZE, 0);
|
2021-05-07 10:08:41 +02:00
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(MouseVisitor::moveMapRight(flags)) {
|
|
|
|
|
if(g_current_start_index == g_current_max_index) {
|
|
|
|
|
// add column
|
|
|
|
|
// TODO 18 as constant
|
|
|
|
|
g_objects.resize(g_current_max_index + 18 + 1);
|
|
|
|
|
g_current_max_index++;
|
|
|
|
|
}
|
|
|
|
|
g_current_start_index += 1;
|
|
|
|
|
scene.moveEverything(-BLOCK_SIZE,0);
|
|
|
|
|
}
|
|
|
|
|
if(MouseVisitor::moveToolsLeft(flags) && g_current_tool_index != 0) {
|
|
|
|
|
g_current_tool_index -= 1;
|
|
|
|
|
updateToolSelection(g_current_tool_index + 1);
|
|
|
|
|
}
|
|
|
|
|
if(MouseVisitor::moveToolsRight(flags) && g_current_tool_index != g_max_tool_index) {
|
|
|
|
|
g_current_tool_index += 1;
|
|
|
|
|
updateToolSelection(g_current_tool_index - 1);
|
2021-05-07 10:08:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
SDLPP::Vec2D<int> getSelectedObjectPosition() {
|
|
|
|
|
return { g_current_start_index + std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getX(), std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getY() };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapObjectType &getSelectedObject() {
|
|
|
|
|
auto pos = getSelectedObjectPosition();
|
|
|
|
|
return g_objects[pos.getX()][pos.getY()];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void placeTool(SDLPP::Scene &scene) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(g_destruction_mutex);
|
|
|
|
|
|
|
|
|
|
ToolVisitor visitor;
|
|
|
|
|
auto tool_type = getBlockRole(g_current_tool->getId());
|
|
|
|
|
switch(tool_type) {
|
|
|
|
|
case BlockRole::TERRAIN:
|
|
|
|
|
visitor.setVisitorType(VisitorType::Terrain);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
visitor.setVisitorType(VisitorType::Modifier);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scene.visitCollisions(*g_current_tool, visitor);
|
|
|
|
|
if(visitor.removeBlock() && !visitor.addBlock()) {
|
|
|
|
|
auto &obj = getSelectedObject();
|
|
|
|
|
switch(visitor.getVisitorType()) {
|
|
|
|
|
case VisitorType::Terrain:
|
|
|
|
|
std::get<MapObject::TERRAIN_TYPE>(obj) = LandType::OVERWORLD;
|
|
|
|
|
std::get<MapObject::TERRAIN_ID>(obj) = 0;
|
|
|
|
|
break;
|
|
|
|
|
case VisitorType::Modifier:
|
|
|
|
|
std::get<MapObject::CHARACTER_TYPE>(obj) = 0;
|
|
|
|
|
std::get<MapObject::CHARACTER_ID>(obj) = 0;
|
|
|
|
|
std::get<MapObject::MODIFIER_TYPE>(obj) = 0;
|
|
|
|
|
std::get<MapObject::MODIFIER_DATA>(obj) = 0;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if(visitor.addBlock()) {
|
|
|
|
|
auto &obj = getSelectedObject();
|
|
|
|
|
int z_index = 1;
|
|
|
|
|
std::shared_ptr<SDLPP::RenderObject> new_obj = nullptr;
|
|
|
|
|
switch(visitor.getVisitorType()) {
|
|
|
|
|
case VisitorType::Terrain:
|
|
|
|
|
std::get<MapObject::TERRAIN_TYPE>(obj) = LandType::OVERWORLD;
|
|
|
|
|
std::get<MapObject::TERRAIN_ID>(obj) = g_current_tool->getId();
|
|
|
|
|
// TODO why 1 +?
|
|
|
|
|
new_obj = createTerrainBlock(g_current_tool->getId(), LandType::OVERWORLD, g_renderer, 1 + std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getX(), std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getY(), true);
|
|
|
|
|
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
|
|
|
|
|
break;
|
|
|
|
|
case VisitorType::Modifier:
|
|
|
|
|
// TODO check if modifier or character
|
|
|
|
|
std::get<MapObject::CHARACTER_TYPE>(obj) = LandType::OVERWORLD;
|
|
|
|
|
// TODO character ID
|
|
|
|
|
std::get<MapObject::CHARACTER_ID>(obj) = MARIO_ID;
|
|
|
|
|
std::get<MapObject::MODIFIER_TYPE>(obj) = 0;
|
|
|
|
|
std::get<MapObject::MODIFIER_DATA>(obj) = 0;
|
|
|
|
|
new_obj = createMario(LandType::OVERWORLD, g_renderer, 1 + std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getX(), std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getY());
|
|
|
|
|
// remove mario if exists
|
|
|
|
|
removeMario();
|
|
|
|
|
g_mario = new_obj;
|
|
|
|
|
g_mario_pos = getSelectedObjectPosition();
|
|
|
|
|
new_obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID);
|
|
|
|
|
z_index = scene.getObjects().size() - 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
scene.addObject(new_obj);
|
|
|
|
|
scene.setZIndex(new_obj, z_index);
|
2021-04-30 21:02:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pollEvents( SDLPP::Scene &scene ) {
|
|
|
|
|
SDL_Event event;
|
|
|
|
|
while ( SDLPP::getSDLEvent( event ) ) {
|
|
|
|
|
switch ( event.type ) {
|
|
|
|
|
case SDL_QUIT:
|
2021-05-09 00:43:53 +02:00
|
|
|
g_quit = true;
|
2021-04-30 21:02:14 +02:00
|
|
|
break;
|
|
|
|
|
case SDL_KEYUP:
|
2021-05-09 00:43:53 +02:00
|
|
|
handleKeyUp( event.key.keysym.sym, scene );
|
2021-04-30 21:02:14 +02:00
|
|
|
break;
|
|
|
|
|
case SDL_WINDOWEVENT:
|
|
|
|
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
2021-05-09 00:43:53 +02:00
|
|
|
g_update_size = true;
|
2021-05-01 21:55:43 +02:00
|
|
|
}
|
|
|
|
|
break;
|
2021-05-09 00:43:53 +02:00
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
|
getMousePositionFlags(scene);
|
2021-05-01 21:55:43 +02:00
|
|
|
break;
|
|
|
|
|
case SDL_MOUSEBUTTONUP:
|
2021-05-09 00:43:53 +02:00
|
|
|
if(std::get<MouseInfo::CUR_FLAGS>(g_mouse_info) == std::get<MouseInfo::PREV_FLAGS>(g_mouse_info)) {
|
|
|
|
|
mouseUpAction(std::get<MouseInfo::CUR_FLAGS>(g_mouse_info), scene);
|
2021-05-01 21:55:43 +02:00
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getX() != -1) {
|
|
|
|
|
placeTool(scene);
|
2021-04-30 21:02:14 +02:00
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(std::get<MouseInfo::TOOL_BOX>(g_mouse_info).getX() != -1) {
|
|
|
|
|
auto &tool_box = std::get<MouseInfo::TOOL_BOX>(g_mouse_info);
|
|
|
|
|
size_t index = tool_box.getY() * 4 + tool_box.getX();
|
|
|
|
|
if(index < g_tools.size()) {
|
|
|
|
|
g_current_block = g_current_tool_index * 8 + index;
|
2021-05-07 09:43:57 +02:00
|
|
|
updateTool();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-01 21:55:43 +02:00
|
|
|
break;
|
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2021-05-09 00:43:53 +02:00
|
|
|
// store current mouse flags in previous mouse flags
|
|
|
|
|
std::get<MouseInfo::PREV_FLAGS>(g_mouse_info) = std::get<MouseInfo::CUR_FLAGS>(g_mouse_info);
|
2021-05-01 21:55:43 +02:00
|
|
|
break;
|
2021-04-30 21:02:14 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
|
|
|
|
FPSmanager gFPS;
|
|
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
|
|
|
|
while ( true ) {
|
|
|
|
|
SDL_framerateDelay( &gFPS );
|
|
|
|
|
pollEvents( *scene );
|
|
|
|
|
scene->updateScene();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|
|
|
|
PWSTR szCmdLine, int nCmdShow ) {
|
|
|
|
|
#else
|
|
|
|
|
int main() {
|
|
|
|
|
#endif
|
|
|
|
|
SDLPP::init();
|
2021-05-07 10:08:41 +02:00
|
|
|
SDLPP::Window w( "Mario editor!" );
|
2021-04-30 21:02:14 +02:00
|
|
|
w.setResizable( true );
|
|
|
|
|
BLOCK_SIZE = 1.0 / 20;
|
|
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
g_renderer = std::make_shared< SDLPP::Renderer >( w );
|
|
|
|
|
g_renderer->setBlendMode( SDL_BLENDMODE_BLEND );
|
2021-04-30 21:02:14 +02:00
|
|
|
|
|
|
|
|
// prepare global vars
|
|
|
|
|
g_terrain_texture = std::make_shared< SDLPP::Texture >(
|
2021-05-09 00:43:53 +02:00
|
|
|
g_renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
|
2021-05-01 21:55:43 +02:00
|
|
|
g_mario_texture = std::make_shared< SDLPP::Texture >(
|
2021-05-09 00:43:53 +02:00
|
|
|
g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
|
2021-04-30 21:02:14 +02:00
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
auto scene = std::make_shared< SDLPP::Scene >( g_renderer );
|
2021-04-30 21:02:14 +02:00
|
|
|
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
2021-05-09 00:43:53 +02:00
|
|
|
0, 0, 10, 10, g_renderer, MARIO_OVERWORLD_COLORKEY, true );
|
2021-05-01 21:55:43 +02:00
|
|
|
bg->setPermanent();
|
2021-04-30 21:02:14 +02:00
|
|
|
bg->setId( 1 );
|
|
|
|
|
scene->addObject( bg );
|
|
|
|
|
|
2021-05-09 00:43:53 +02:00
|
|
|
loadMap( scene, "test_binary.bin", g_renderer, g_objects );
|
2021-04-30 23:12:53 +02:00
|
|
|
|
|
|
|
|
// grid
|
|
|
|
|
for ( int i = 1; i < 20; i++ ) {
|
|
|
|
|
auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
|
2021-05-09 00:43:53 +02:00
|
|
|
i * BLOCK_SIZE, 1 - 16 * BLOCK_SIZE, i * BLOCK_SIZE, 1.0, g_renderer, "#282828" );
|
2021-05-01 21:55:43 +02:00
|
|
|
line_vertical->setPermanent();
|
2021-04-30 23:12:53 +02:00
|
|
|
line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
|
|
|
scene->addObject(line_vertical);
|
|
|
|
|
if(i > 2) {
|
|
|
|
|
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
|
|
|
|
|
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, 19 * BLOCK_SIZE,
|
2021-05-09 00:43:53 +02:00
|
|
|
( i + 1 ) * BLOCK_SIZE, g_renderer, "#282828" );
|
2021-05-01 21:55:43 +02:00
|
|
|
line_horizontal->setPermanent();
|
2021-04-30 23:12:53 +02:00
|
|
|
line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
|
|
|
scene->addObject(line_horizontal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// white rectangles
|
|
|
|
|
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
|
2021-05-09 00:43:53 +02:00
|
|
|
0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true);
|
2021-04-30 23:12:53 +02:00
|
|
|
rectangle1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
2021-05-02 14:14:11 +02:00
|
|
|
rectangle1->setId(EDITOR_LEFT_MAP_ID);
|
2021-05-01 21:55:43 +02:00
|
|
|
rectangle1->setPermanent();
|
2021-04-30 23:12:53 +02:00
|
|
|
rectangle1->addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
|
|
|
|
scene->addObject(rectangle1);
|
|
|
|
|
// white rectangles
|
|
|
|
|
auto rectangle2 = std::make_shared< SDLPP::RectangleRender >(
|
2021-05-09 00:43:53 +02:00
|
|
|
19*BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true);
|
2021-04-30 23:12:53 +02:00
|
|
|
rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
2021-05-02 14:14:11 +02:00
|
|
|
rectangle2->setId(EDITOR_RIGHT_MAP_ID);
|
2021-05-01 21:55:43 +02:00
|
|
|
rectangle2->setPermanent();
|
2021-04-30 23:12:53 +02:00
|
|
|
rectangle2->addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
|
|
|
|
scene->addObject(rectangle2);
|
|
|
|
|
|
2021-05-01 21:55:43 +02:00
|
|
|
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
|
|
|
|
|
auto font_config = std::make_shared< SDLPP::FontConfiguration >(
|
|
|
|
|
font, "#000000", "#282828", 0.05 );
|
|
|
|
|
auto left = std::make_shared< SDLPP::TextRenderer >(
|
2021-05-09 00:43:53 +02:00
|
|
|
0, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<", font_config);
|
2021-05-02 14:14:11 +02:00
|
|
|
left->setId(0);
|
2021-05-01 21:55:43 +02:00
|
|
|
left->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
left->setPermanent();
|
|
|
|
|
scene->addObject(left);
|
|
|
|
|
auto right = std::make_shared< SDLPP::TextRenderer >(
|
2021-05-09 00:43:53 +02:00
|
|
|
19*BLOCK_SIZE, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, ">", font_config);
|
2021-05-02 14:14:11 +02:00
|
|
|
right->setId(0);
|
2021-05-01 21:55:43 +02:00
|
|
|
right->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
right->setPermanent();
|
|
|
|
|
scene->addObject(right);
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 18; i++) {
|
|
|
|
|
for(int j = 0; j < 16; j++) {
|
2021-05-09 00:43:53 +02:00
|
|
|
scene->addObject(std::make_shared<EditBox>(i, j, g_renderer));
|
2021-05-01 21:55:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto mouse =
|
2021-05-09 00:43:53 +02:00
|
|
|
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0, g_renderer );
|
2021-05-02 14:14:11 +02:00
|
|
|
mouse->setMinWidth(1);
|
|
|
|
|
mouse->setMinHeight(1);
|
2021-05-01 21:55:43 +02:00
|
|
|
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
2021-05-02 14:14:11 +02:00
|
|
|
mouse->setId( EDITOR_MOUSE_ID );
|
2021-05-01 21:55:43 +02:00
|
|
|
mouse->setColiderColor("#00FF00");
|
|
|
|
|
mouse->addCollision(
|
|
|
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
|
|
|
|
|
scene->addObject( mouse );
|
2021-05-09 00:43:53 +02:00
|
|
|
g_current_max_index = g_objects.size() - 18;
|
2021-04-30 21:02:14 +02:00
|
|
|
|
2021-05-07 09:43:57 +02:00
|
|
|
// tools
|
2021-05-09 00:43:53 +02:00
|
|
|
g_max_tool_index = (possibleBlocks.size() - 1) / 8;
|
2021-05-07 09:43:57 +02:00
|
|
|
for(int i = 0; i < 4; i++) {
|
2021-05-09 00:43:53 +02:00
|
|
|
auto tool_box1 = std::make_shared<ToolBox>(i, 0, g_renderer);
|
|
|
|
|
auto tool_box2 = std::make_shared<ToolBox>(i, 1, g_renderer);
|
2021-05-07 09:43:57 +02:00
|
|
|
scene->addObject(tool_box1);
|
|
|
|
|
scene->addObject(tool_box2);
|
|
|
|
|
// std::cout << "TOOL BOX POS: " << tool_box1->getPos().getX() << ", " << tool_box1->getPos().getY() << std::endl;
|
|
|
|
|
// std::cout << "TOOL BOX POS: " << tool_box2->getPos().getX() << ", " << tool_box2->getPos().getY() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
int tool_index = 0;
|
|
|
|
|
for(auto &block : possibleBlocks) {
|
2021-05-07 23:17:05 +02:00
|
|
|
if(block == MARIO_ID ) {
|
2021-05-09 00:43:53 +02:00
|
|
|
g_tools.push_back(createMario(LandType::OVERWORLD, g_renderer, 0, 0));
|
2021-05-07 23:17:05 +02:00
|
|
|
} else {
|
2021-05-09 00:43:53 +02:00
|
|
|
g_tools.push_back(createTerrainBlock(block, LandType::OVERWORLD, g_renderer, false));
|
2021-05-07 23:17:05 +02:00
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
g_tools.back()->setHidden(true);
|
|
|
|
|
g_tools.back()->setPermanent();
|
2021-05-07 09:43:57 +02:00
|
|
|
auto x = tool_index % 4;
|
|
|
|
|
auto y = tool_index / 4;
|
|
|
|
|
// TODO add 14 and 1 as constants somewhere
|
2021-05-07 23:17:05 +02:00
|
|
|
// TODO investigate when not permanent requires `-1` on x position
|
2021-05-09 00:43:53 +02:00
|
|
|
g_tools.back()->setPos(14*BLOCK_SIZE + x*BLOCK_SIZE, BLOCK_SIZE + y*BLOCK_SIZE);
|
2021-05-07 09:43:57 +02:00
|
|
|
// std::cout << "TOOL POS: " << tools.back()->getPos().getX() << ", " << tools.back()->getPos().getY() << std::endl;
|
2021-05-09 00:43:53 +02:00
|
|
|
scene->addObject(g_tools.back());
|
2021-05-07 09:43:57 +02:00
|
|
|
tool_index = (tool_index + 1) % 8;
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < 5; i++) {
|
|
|
|
|
auto line = std::make_shared<SDLPP::LineRenderer>(
|
2021-05-09 00:43:53 +02:00
|
|
|
14*BLOCK_SIZE + i*BLOCK_SIZE, BLOCK_SIZE, 14 * BLOCK_SIZE + i*BLOCK_SIZE, 3*BLOCK_SIZE, g_renderer, "#282828");
|
2021-05-07 09:43:57 +02:00
|
|
|
line->setPermanent();
|
|
|
|
|
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
scene->addObject(line);
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
|
auto line = std::make_shared<SDLPP::LineRenderer>(
|
2021-05-09 00:43:53 +02:00
|
|
|
14*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, 14 * BLOCK_SIZE + 4*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, g_renderer, "#282828");
|
2021-05-07 09:43:57 +02:00
|
|
|
line->setPermanent();
|
|
|
|
|
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
scene->addObject(line);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-07 10:08:41 +02:00
|
|
|
auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >(
|
2021-05-09 00:43:53 +02:00
|
|
|
13*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true);
|
2021-05-07 10:08:41 +02:00
|
|
|
tool_rect1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
tool_rect1->setId(EDITOR_LEFT_TOOL_ID);
|
|
|
|
|
tool_rect1->setPermanent();
|
|
|
|
|
tool_rect1->addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
|
|
|
|
scene->addObject(tool_rect1);
|
|
|
|
|
// white rectangles
|
|
|
|
|
auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >(
|
2021-05-09 00:43:53 +02:00
|
|
|
18*BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true);
|
2021-05-07 10:08:41 +02:00
|
|
|
tool_rect2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
tool_rect2->setId(EDITOR_RIGHT_TOOL_ID);
|
|
|
|
|
tool_rect2->setPermanent();
|
|
|
|
|
tool_rect2->addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
|
|
|
|
scene->addObject(tool_rect2);
|
|
|
|
|
auto left_tool = std::make_shared< SDLPP::TextRenderer >(
|
2021-05-09 00:43:53 +02:00
|
|
|
13*BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<", font_config);
|
2021-05-07 10:08:41 +02:00
|
|
|
left_tool->setId(0);
|
|
|
|
|
left_tool->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
left_tool->setPermanent();
|
|
|
|
|
scene->addObject(left_tool);
|
|
|
|
|
auto right_tool = std::make_shared< SDLPP::TextRenderer >(
|
2021-05-09 00:43:53 +02:00
|
|
|
18*BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, ">", font_config);
|
2021-05-07 10:08:41 +02:00
|
|
|
right_tool->setId(0);
|
|
|
|
|
right_tool->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
|
|
|
|
right_tool->setPermanent();
|
|
|
|
|
scene->addObject(right_tool);
|
|
|
|
|
|
2021-05-02 14:28:17 +02:00
|
|
|
g_placeholder_texture = std::make_shared< SDLPP::Texture >(
|
2021-05-09 00:43:53 +02:00
|
|
|
g_renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
|
2021-05-07 23:17:05 +02:00
|
|
|
g_placeholder_texture->setAlpha(100);
|
|
|
|
|
g_placeholder_mario = std::make_shared< SDLPP::Texture >(
|
2021-05-09 00:43:53 +02:00
|
|
|
g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
|
2021-05-07 23:17:05 +02:00
|
|
|
g_placeholder_mario->setAlpha(100);
|
2021-05-09 00:43:53 +02:00
|
|
|
g_current_tool = createTerrainBlock(possibleBlocks[g_current_block], LandType::OVERWORLD, g_renderer, g_placeholder_texture, false);
|
|
|
|
|
g_current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
|
|
|
|
|
dynamic_cast<MarioBlock&>(*g_current_tool).setTool();
|
|
|
|
|
scene->addObject(g_current_tool);
|
|
|
|
|
scene->moveZTop(g_current_tool);
|
2021-05-01 21:55:43 +02:00
|
|
|
|
|
|
|
|
scene->moveEverything(BLOCK_SIZE, 0);
|
2021-04-30 21:02:14 +02:00
|
|
|
FPSmanager gFPS;
|
|
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
|
SDL_setFramerate( &gFPS, 60 );
|
|
|
|
|
|
2021-05-07 10:08:41 +02:00
|
|
|
updateToolSelection(0);
|
2021-05-07 09:43:57 +02:00
|
|
|
|
2021-04-30 21:02:14 +02:00
|
|
|
auto base = SDL_GetTicks();
|
|
|
|
|
int frames = 0;
|
|
|
|
|
std::thread inputThread( doInput, scene );
|
|
|
|
|
inputThread.detach();
|
2021-05-09 00:43:53 +02:00
|
|
|
while ( !g_quit ) {
|
2021-04-30 21:02:14 +02:00
|
|
|
SDL_PumpEvents();
|
|
|
|
|
SDL_framerateDelay( &gFPS );
|
2021-05-09 00:43:53 +02:00
|
|
|
std::lock_guard<std::mutex> lock(g_destruction_mutex);
|
2021-04-30 21:02:14 +02:00
|
|
|
scene->renderScene();
|
2021-05-09 00:43:53 +02:00
|
|
|
g_renderer->presentRenderer();
|
2021-04-30 21:02:14 +02:00
|
|
|
frames++;
|
|
|
|
|
if ( SDL_GetTicks() - base >= 1000 ) {
|
|
|
|
|
std::cout << "FPS: " << frames << std::endl;
|
|
|
|
|
frames = 0;
|
|
|
|
|
base = SDL_GetTicks();
|
|
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(g_current_start_index == 0) {
|
2021-05-01 21:55:43 +02:00
|
|
|
left->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
|
|
|
|
|
} else {
|
|
|
|
|
left->setTextColor(font, "#000000", "#282828", 0.05);
|
|
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(g_current_start_index == g_current_max_index) {
|
2021-05-07 08:49:46 +02:00
|
|
|
right->setTextColor(font, "#00FF00", "#000000", 0.1);
|
|
|
|
|
right->changeText("+");
|
2021-05-01 21:55:43 +02:00
|
|
|
} else {
|
|
|
|
|
right->setTextColor(font, "#000000", "#282828", 0.05);
|
2021-05-07 08:49:46 +02:00
|
|
|
right->changeText(">");
|
2021-05-01 21:55:43 +02:00
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(g_current_tool_index == 0) {
|
2021-05-07 10:08:41 +02:00
|
|
|
left_tool->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
|
|
|
|
|
} else {
|
|
|
|
|
left_tool->setTextColor(font, "#000000", "#282828", 0.05);
|
|
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(g_current_tool_index == g_max_tool_index) {
|
2021-05-07 10:08:41 +02:00
|
|
|
right_tool->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
|
|
|
|
|
} else {
|
|
|
|
|
right_tool->setTextColor(font, "#000000", "#282828", 0.05);
|
|
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
if(g_update_size) {
|
2021-05-01 21:55:43 +02:00
|
|
|
scene->updateSizeAndPosition();
|
|
|
|
|
}
|
2021-04-30 21:02:14 +02:00
|
|
|
}
|
2021-05-09 00:43:53 +02:00
|
|
|
saveMap("test_binary2.bin", g_objects);
|
2021-04-30 21:02:14 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|