game/mario/editor.cpp
2021-05-02 14:14:11 +02:00

294 lines
10 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 <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"
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;
SDLPP::Vec2D<int> current_box = {0, 0};
std::shared_ptr<SDLPP::RenderObject> current_tool = nullptr;
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 handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
break;
case SDLK_d:
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);
}
}
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) && current_start_index != current_max_index) {
current_start_index += 1;
scene.moveEverything(-BLOCK_SIZE,0);
}
if(current_box.getX() != -1) {
std::lock_guard<std::mutex> lock(destruction_mutex);
ToolVisitor visitor;
visitor.setVisitorType(TOOL_VISITOR_TYPE);
scene.visitCollisions(*current_tool, visitor);
if(visitor.removeBlock()) {
// TODO check if modifier
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, 0, 0, 0, 0, 0};
}
if(visitor.addBlock()) {
// TODO check if modifier
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, current_tool->getId(), 0, 0, 0, 0};
auto obj = createTerrainBlock(current_tool->getId(), OVERWORLD, renderer, 1 + current_box.getX(), current_box.getY(), true);
obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
scene.addObject(obj);
scene.setZIndex(obj, 1);
}
}
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 clone!" );
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;
auto placeholder_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
current_tool = createTerrainBlock(FLOOR_ID, OVERWORLD, renderer, placeholder_texture, false);
current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
current_tool->setTextureAlpha(100);
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 );
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, "#CCCCCC", "#CCCCCC", 0.05);
} else {
right->setTextColor(font, "#000000", "#282828", 0.05);
}
if(update_size) {
scene->updateSizeAndPosition();
}
}
saveMap("test_binary2.bin", objects);
return 0;
}