game/mario/editor.cpp

308 lines
11 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
#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-04-30 19:02:14 +00:00
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
bool quit = false;
bool update_size = false;
2021-05-02 12:14:11 +00:00
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;
2021-05-02 12:28:17 +00:00
uint64_t current_block = 0;
2021-05-02 12:14:11 +00:00
SDLPP::Vec2D<int> current_box = {0, 0};
std::shared_ptr<SDLPP::RenderObject> current_tool = nullptr;
2021-04-30 19:02:14 +00:00
2021-05-02 12:28:17 +00:00
std::shared_ptr<SDLPP::Texture> g_placeholder_texture = nullptr;
2021-04-30 19:02:14 +00:00
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;
}
}
2021-05-02 12:28:17 +00:00
void updateTool() {
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]);
}
2021-04-30 19:02:14 +00:00
void handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
2021-05-02 12:28:17 +00:00
current_block = (current_block + possibleBlocks.size() - 1) % possibleBlocks.size();
updateTool();
2021-04-30 19:02:14 +00:00
break;
case SDLK_d:
2021-05-02 12:28:17 +00:00
current_block = (current_block + 1) % possibleBlocks.size();
updateTool();
2021-04-30 19:02:14 +00:00
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: {
2021-05-02 12:14:11 +00:00
auto mouse = scene.getObjects({EDITOR_MOUSE_ID})[0];
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
2021-05-02 12:14:11 +00:00
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:
2021-05-02 12:14:11 +00:00
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);
}
2021-05-02 12:14:11 +00:00
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);
}
2021-05-02 12:14:11 +00:00
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() && !visitor.addBlock()) {
2021-05-02 12:14:11 +00:00
// TODO check if modifier
auto prev = objects[current_start_index + current_box.getX()][current_box.getY()];
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, 0, std::get<2>(prev), std::get<3>(prev), 0, 0};
} else if(visitor.addBlock()) {
2021-05-02 12:14:11 +00:00
// TODO check if modifier
auto prev = objects[current_start_index + current_box.getX()][current_box.getY()];
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, current_tool->getId(), std::get<2>(prev), std::get<3>(prev), 0, 0};
2021-05-02 12:14:11 +00:00
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);
}
2021-04-30 19:02:14 +00:00
}
break;
case SDL_MOUSEBUTTONDOWN:
2021-05-02 12:14:11 +00:00
previous_selected_flags = current_selected_flags;
break;
2021-04-30 19:02:14 +00: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();
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 );
2021-04-30 19:02:14 +00:00
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-04-30 19:02:14 +00:00
bg->setId( 1 );
scene->addObject( bg );
2021-04-30 21:12:53 +00:00
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();
2021-04-30 21:12:53 +00: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,
( i + 1 ) * BLOCK_SIZE, renderer, "#282828" );
line_horizontal->setPermanent();
2021-04-30 21:12:53 +00:00
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);
2021-05-02 12:14:11 +00:00
rectangle1->setId(EDITOR_LEFT_MAP_ID);
rectangle1->setPermanent();
2021-04-30 21:12:53 +00:00
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);
2021-04-30 21:12:53 +00:00
rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
2021-05-02 12:14:11 +00:00
rectangle2->setId(EDITOR_RIGHT_MAP_ID);
rectangle2->setPermanent();
2021-04-30 21:12:53 +00:00
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);
2021-05-02 12:14:11 +00:00
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);
2021-05-02 12:14:11 +00:00
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 =
2021-05-02 12:14:11 +00:00
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 );
2021-05-02 12:14:11 +00:00
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;
2021-04-30 19:02:14 +00:00
2021-05-02 12:28:17 +00:00
g_placeholder_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
2021-05-02 12:28:17 +00:00
current_tool = createTerrainBlock(possibleBlocks[current_block], OVERWORLD, renderer, g_placeholder_texture, false);
2021-05-02 12:14:11 +00:00
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);
2021-04-30 19:02:14 +00:00
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 );
2021-05-02 12:14:11 +00:00
std::lock_guard<std::mutex> lock(destruction_mutex);
2021-04-30 19:02:14 +00:00
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();
}
2021-04-30 19:02:14 +00:00
}
saveMap("test_binary2.bin", objects);
2021-04-30 19:02:14 +00:00
return 0;
}