game/mario/editor.cpp

470 lines
19 KiB
C++

#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
#include <array>
#include <thread>
#include <mutex>
#include "global_vars.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "maploader.hpp"
#include "../sdlpp/sdlpp_mouse.hpp"
#include "edit_box.hpp"
#include "editor_visitor.hpp"
#include "tool_box.hpp"
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
bool quit = false;
bool update_size = false;
std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> objects = {};
uint64_t current_selected_flags = 0;
uint64_t previous_selected_flags = 0;
std::mutex destruction_mutex;
int current_start_index = 0;
int current_max_index = 0;
uint64_t current_block = 0;
SDLPP::Vec2D<int> current_box = {0, 0};
SDLPP::Vec2D<int> current_tool_box = {0, 0};
std::vector<std::shared_ptr<SDLPP::RenderObject>> tools{};
std::shared_ptr<SDLPP::RenderObject> current_tool = nullptr;
int current_tool_index = 0;
int max_tool_index = 0;
std::shared_ptr<SDLPP::Texture> g_placeholder_texture = nullptr;
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};
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_a:
break;
case SDLK_d:
break;
case SDLK_SPACE:
case SDLK_w:
break;
case SDLK_s:
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
default:
break;
}
}
void updateTool() {
if(possibleBlocks[current_block] == MARIO_ID) {
current_tool->setTexture(g_placeholder_mario, getSourceRectByID(possibleBlocks[current_block], OVERWORLD));
} else {
current_tool->setTexture(g_placeholder_texture, getSourceRectByID(possibleBlocks[current_block], OVERWORLD));
}
current_tool->setId(possibleBlocks[current_block]);
current_tool->getCollisions()[0]->setId(possibleBlocks[current_block]);
}
void removeMario() {
auto prev = objects[g_mario_pos.getX()][g_mario_pos.getY()];
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) {
auto prev = prev_index * 8;
auto cur = current_tool_index * 8;
for(int i = prev; i < (tools.size() < prev + 8 ? tools.size() : prev + 8); i++) {
tools[i]->setHidden(true);
}
for(int i = cur; i < (tools.size() < cur + 8 ? tools.size() : cur + 8); i++) {
tools[i]->setHidden(false);
}
}
void handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
// current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size();
// updateTool();
break;
case SDLK_d:
// current_block = (current_block + 1) % possibleBlocks.size();
// updateTool();
break;
case SDLK_w:
case SDLK_s:
default:
break;
}
}
void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDown( event.key.keysym.sym, scene );
break;
case SDL_KEYUP:
handleKeyUp( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
update_size = true;
}
break;
case SDL_MOUSEMOTION: {
auto mouse = scene.getObjects({EDITOR_MOUSE_ID})[0];
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
MouseVisitor visitor;
scene.visitCollisions(*mouse, visitor);
current_selected_flags = visitor.getFlags();
current_box = visitor.getEditBoxIndexes();
if(visitor.foundEditBox()) {
current_tool->setPos(BLOCK_SIZE + current_box.getX() * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.getY() * BLOCK_SIZE);
}
current_tool_box = visitor.getToolBoxIndexes();
}
break;
case SDL_MOUSEBUTTONUP:
if(previous_selected_flags == current_selected_flags && MouseVisitor::moveMapLeft(current_selected_flags) && current_start_index != 0) {
current_start_index -= 1;
scene.moveEverything(BLOCK_SIZE, 0);
}
if(previous_selected_flags == current_selected_flags && MouseVisitor::moveMapRight(current_selected_flags)) {
if(current_start_index == current_max_index) {
objects.resize(current_max_index + 18 + 1);
current_max_index++;
}
current_start_index += 1;
scene.moveEverything(-BLOCK_SIZE,0);
}
if(previous_selected_flags == current_selected_flags && MouseVisitor::moveToolsLeft(current_selected_flags) && current_tool_index != 0) {
current_tool_index -= 1;
updateToolSelection(current_tool_index + 1);
}
if(previous_selected_flags == current_selected_flags && MouseVisitor::moveToolsRight(current_selected_flags) && current_tool_index != max_tool_index) {
current_tool_index += 1;
updateToolSelection(current_tool_index - 1);
}
if(current_box.getX() != -1) {
std::lock_guard<std::mutex> lock(destruction_mutex);
ToolVisitor visitor;
if(current_tool->getId() < 0x7000) {
visitor.setVisitorType(MODIFIER_VISITOR_TYPE);
} else {
visitor.setVisitorType(TOOL_VISITOR_TYPE);
}
// TODO
scene.visitCollisions(*current_tool, visitor);
if(visitor.removeBlock() && !visitor.addBlock()) {
auto prev = objects[current_start_index + current_box.getX()][current_box.getY()];
if(visitor.getVisitorType() == TOOL_VISITOR_TYPE) {
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, 0, std::get<2>(prev), std::get<3>(prev), std::get<4>(prev), std::get<5>(prev)};
} else {
objects[current_start_index + current_box.getX()][current_box.getY()] = {std::get<0>(prev), std::get<1>(prev), 0, 0, 0, 0};
}
} else if(visitor.addBlock()) {
auto prev = objects[current_start_index + current_box.getX()][current_box.getY()];
int z_index = 1;
std::shared_ptr<SDLPP::RenderObject> obj = nullptr;
if(visitor.getVisitorType() == TOOL_VISITOR_TYPE) {
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, current_tool->getId(), std::get<2>(prev), std::get<3>(prev), std::get<4>(prev), std::get<5>(prev)};
obj = createTerrainBlock(current_tool->getId(), OVERWORLD, renderer, 1 + current_box.getX(), current_box.getY(), true);
obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
} else {
// TODO check if modifier or character
objects[current_start_index + current_box.getX()][current_box.getY()] = {std::get<0>(prev), std::get<1>(prev), OVERWORLD, MARIO_ID, 0, 0};
obj = createMario(OVERWORLD, renderer, 1 + current_box.getX(), current_box.getY());
if(g_mario) {
removeMario();
}
g_mario = obj;
g_mario_pos = {current_start_index + current_box.getX(), current_box.getY()};
obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID);
z_index = scene.getObjects().size() - 1;
}
scene.addObject(obj);
scene.setZIndex(obj, z_index);
}
}
if(current_tool_box.getX() != -1) {
auto index = current_tool_box.getY() * 4 + current_tool_box.getX();
if(index < tools.size()) {
current_block = current_tool_index * 8 + index;
updateTool();
}
}
break;
case SDL_MOUSEBUTTONDOWN:
previous_selected_flags = current_selected_flags;
break;
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();
SDLPP::Window w( "Mario editor!" );
w.setResizable( true );
BLOCK_SIZE = 1.0 / 20;
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_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
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();
bg->setId( 1 );
scene->addObject( bg );
loadMap( scene, "test_binary.bin", renderer, objects );
// grid
for ( int i = 1; i < 20; i++ ) {
auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
i * BLOCK_SIZE, 1 - 16 * BLOCK_SIZE, i * BLOCK_SIZE, 1.0, renderer, "#282828" );
line_vertical->setPermanent();
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,
( i + 1 ) * BLOCK_SIZE, renderer, "#282828" );
line_horizontal->setPermanent();
line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject(line_horizontal);
}
}
// white rectangles
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
rectangle1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
rectangle1->setId(EDITOR_LEFT_MAP_ID);
rectangle1->setPermanent();
rectangle1->addCollision(SDLPP::RectColider(0, 0, 1, 1));
scene->addObject(rectangle1);
// white rectangles
auto rectangle2 = std::make_shared< SDLPP::RectangleRender >(
19*BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
rectangle2->setId(EDITOR_RIGHT_MAP_ID);
rectangle2->setPermanent();
rectangle2->addCollision(SDLPP::RectColider(0, 0, 1, 1));
scene->addObject(rectangle2);
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 >(
0, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, "<", font_config);
left->setId(0);
left->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
left->setPermanent();
scene->addObject(left);
auto right = std::make_shared< SDLPP::TextRenderer >(
19*BLOCK_SIZE, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, ">", font_config);
right->setId(0);
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++) {
scene->addObject(std::make_shared<EditBox>(i, j, renderer));
}
}
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 );
current_max_index = objects.size() - 18;
// tools
max_tool_index = (possibleBlocks.size() - 1) / 8;
for(int i = 0; i < 4; i++) {
auto tool_box1 = std::make_shared<ToolBox>(i, 0, renderer);
auto tool_box2 = std::make_shared<ToolBox>(i, 1, renderer);
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) {
if(block == MARIO_ID ) {
tools.push_back(createMario(OVERWORLD, renderer, 0, 0));
} else {
tools.push_back(createTerrainBlock(block, OVERWORLD, renderer, false));
}
tools.back()->setHidden(true);
tools.back()->setPermanent();
auto x = tool_index % 4;
auto y = tool_index / 4;
// TODO add 14 and 1 as constants somewhere
// TODO investigate when not permanent requires `-1` on x position
tools.back()->setPos(14*BLOCK_SIZE + x*BLOCK_SIZE, BLOCK_SIZE + y*BLOCK_SIZE);
// std::cout << "TOOL POS: " << tools.back()->getPos().getX() << ", " << tools.back()->getPos().getY() << std::endl;
scene->addObject(tools.back());
tool_index = (tool_index + 1) % 8;
}
for(int i = 0; i < 5; i++) {
auto line = std::make_shared<SDLPP::LineRenderer>(
14*BLOCK_SIZE + i*BLOCK_SIZE, BLOCK_SIZE, 14 * BLOCK_SIZE + i*BLOCK_SIZE, 3*BLOCK_SIZE, renderer, "#282828");
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>(
14*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, 14 * BLOCK_SIZE + 4*BLOCK_SIZE, BLOCK_SIZE + i*BLOCK_SIZE, renderer, "#282828");
line->setPermanent();
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
scene->addObject(line);
}
auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >(
13*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
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 >(
18*BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
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 >(
13*BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, "<", font_config);
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 >(
18*BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, ">", font_config);
right_tool->setId(0);
right_tool->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
right_tool->setPermanent();
scene->addObject(right_tool);
g_placeholder_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_texture->setAlpha(100);
g_placeholder_mario = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_mario->setAlpha(100);
current_tool = createTerrainBlock(possibleBlocks[current_block], OVERWORLD, renderer, g_placeholder_texture, false);
current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
dynamic_cast<MarioBlock&>(*current_tool).setTool();
scene->addObject(current_tool);
scene->moveZTop(current_tool);
scene->moveEverything(BLOCK_SIZE, 0);
FPSmanager gFPS;
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
updateToolSelection(0);
auto base = SDL_GetTicks();
int frames = 0;
std::thread inputThread( doInput, scene );
inputThread.detach();
while ( !quit ) {
SDL_PumpEvents();
SDL_framerateDelay( &gFPS );
std::lock_guard<std::mutex> lock(destruction_mutex);
scene->renderScene();
renderer->presentRenderer();
frames++;
if ( SDL_GetTicks() - base >= 1000 ) {
std::cout << "FPS: " << frames << std::endl;
frames = 0;
base = SDL_GetTicks();
}
if(current_start_index == 0) {
left->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
} else {
left->setTextColor(font, "#000000", "#282828", 0.05);
}
if(current_start_index == current_max_index) {
right->setTextColor(font, "#00FF00", "#000000", 0.1);
right->changeText("+");
} else {
right->setTextColor(font, "#000000", "#282828", 0.05);
right->changeText(">");
}
if(current_tool_index == 0) {
left_tool->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
} else {
left_tool->setTextColor(font, "#000000", "#282828", 0.05);
}
if(current_tool_index == max_tool_index) {
right_tool->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
} else {
right_tool->setTextColor(font, "#000000", "#282828", 0.05);
}
if(update_size) {
scene->updateSizeAndPosition();
}
}
saveMap("test_binary2.bin", objects);
return 0;
}