game/mario/editor.cpp

686 lines
26 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"
2021-04-30 19:02:14 +00:00
#define MAP_WIDTH 18
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-08 22:43:53 +00:00
struct MouseInfo {
uint64_t cur_flags;
uint64_t prev_flags;
SDLPP::Vec2D< int > edit_box;
SDLPP::Vec2D< int > tool_box;
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 {
uint64_t index;
int cur_page;
int max_page;
2021-05-09 18:21:53 +00:00
};
struct GlobalVars {
MouseInfo mouse;
MapInfo map;
ToolInfo tool;
uint64_t flags;
std::vector< mapColumnType > objects;
std::vector< std::shared_ptr< SDLPP::RenderObject > > tools;
std::shared_ptr< SDLPP::RenderObject > current_tool;
std::shared_ptr< SDLPP::Texture > translucent_terrain_texture;
std::shared_ptr< SDLPP::Texture > translucent_mario_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
enum LandType::Value g_current_world_type = LandType::OVERWORLD;
void updateTool() {
auto tool_index = global_vars.tool.index;
auto tool_role = getBlockRole( possibleBlocks[tool_index] );
2021-05-08 22:46:10 +00:00
std::shared_ptr< SDLPP::Texture > target_texture = nullptr;
switch ( tool_role ) {
case BlockRole::TERRAIN:
target_texture = global_vars.translucent_terrain_texture;
2021-05-08 22:46:10 +00:00
break;
case BlockRole::MARIO:
target_texture = global_vars.translucent_mario_texture;
2021-05-08 22:46:10 +00:00
break;
case BlockRole::MODIFIER:
target_texture = global_vars.translucent_terrain_texture;
2021-05-08 22:46:10 +00:00
break;
case BlockRole::CHARACTER:
break;
2021-05-08 22:43:53 +00:00
}
global_vars.current_tool->setTexture(
target_texture,
getSourceRectByID( possibleBlocks[tool_index], g_current_world_type ) );
global_vars.current_tool->setId( possibleBlocks[tool_index] );
global_vars.current_tool->getCollisions()[0]->setId(
possibleBlocks[tool_index] );
2021-05-08 22:43:53 +00:00
}
void removeMario() {
if ( !global_vars.mario )
2021-05-08 22:43:53 +00:00
return;
auto prev = global_vars.objects[global_vars.mario_pos.getX()]
[global_vars.mario_pos.getY()];
2021-05-08 22:43:53 +00:00
// remove character/modifiers
global_vars
.objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()] = {
2021-05-08 22:46:10 +00:00
std::get< 0 >( prev ), std::get< 1 >( prev ), 0, 0, 0, 0
};
global_vars.mario->destroy();
2021-05-08 22:43:53 +00:00
}
2021-05-08 22:46:10 +00:00
void updateToolSelection( int prev_index ) {
2021-05-08 22:43:53 +00:00
size_t prev = prev_index * 8;
size_t cur = global_vars.tool.cur_page * 8;
2021-05-08 22:46:10 +00:00
for ( size_t i = prev;
i < ( global_vars.tools.size() < prev + 8 ? global_vars.tools.size()
: prev + 8 );
i++ ) {
global_vars.tools[i]->setHidden( true );
2021-05-08 22:43:53 +00:00
}
2021-05-08 22:46:10 +00:00
for ( size_t i = cur;
i < ( global_vars.tools.size() < cur + 8 ? global_vars.tools.size()
: cur + 8 );
i++ ) {
global_vars.tools[i]->setHidden( false );
2021-05-08 22:43:53 +00:00
}
}
void moveToolsLeft() {
global_vars.tool.cur_page--;
updateToolSelection( global_vars.tool.cur_page + 1 );
2021-05-08 22:43:53 +00:00
}
void moveToolsRight() {
global_vars.tool.cur_page++;
updateToolSelection( global_vars.tool.cur_page - 1 );
2021-05-08 22:43:53 +00:00
}
// TODO add red outline to currently selected tool
// add WSAD navigation for the red highlight
// add mouse wheel navigation
2021-05-08 22:43:53 +00:00
void selectPrevTool() {
if ( global_vars.tool.index == 0 )
2021-05-08 22:43:53 +00:00
return;
if ( global_vars.tool.index % 8 == 0 ) {
2021-05-08 22:43:53 +00:00
moveToolsLeft();
}
global_vars.tool.index--;
2021-05-08 22:43:53 +00:00
updateTool();
}
void selectNextTool() {
if ( global_vars.tool.index == global_vars.tools.size() - 1 )
2021-05-08 22:43:53 +00:00
return;
if ( global_vars.tool.index % 8 == 7 ) {
2021-05-08 22:43:53 +00:00
moveToolsRight();
}
global_vars.tool.index++;
2021-05-08 22:43:53 +00:00
updateTool();
}
void setFlag( uint64_t flag ) {
global_vars.flags |= flag;
}
void unsetFlag( uint64_t flag ) {
global_vars.flags &= ~flag;
}
bool getFlag( uint64_t flag ) {
return global_vars.flags & flag;
}
2021-05-08 22:43:53 +00:00
void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) {
2021-04-30 19:02:14 +00:00
switch ( key ) {
case SDLK_ESCAPE:
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_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
default:
break;
}
}
2021-05-08 22:46:10 +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-05-08 22:46:10 +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-05-08 22:46:10 +00:00
scene.visitCollisions( *mouse, visitor );
global_vars.mouse.cur_flags = visitor.getFlags();
global_vars.mouse.edit_box = visitor.getEditBoxIndexes();
global_vars.mouse.tool_box = visitor.getToolBoxIndexes();
2021-05-08 22:43:53 +00:00
// if we found an edit box, move tool icon to that box
2021-05-08 22:46:10 +00:00
if ( visitor.foundEditBox() ) {
const auto &box = global_vars.mouse.edit_box;
global_vars.current_tool->setPos( BLOCK_SIZE + box.getX() * BLOCK_SIZE,
( MAP_WIDTH - MAP_HEIGHT + 2 ) *
BLOCK_SIZE +
box.getY() * BLOCK_SIZE );
2021-05-08 22:43:53 +00:00
}
}
2021-05-08 22:46:10 +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-05-08 22:46:10 +00:00
scene.moveEverything( BLOCK_SIZE, 0 );
2021-05-07 08:08:41 +00:00
}
2021-05-08 22:46:10 +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
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-05-08 22:46:10 +00:00
scene.moveEverything( -BLOCK_SIZE, 0 );
2021-05-08 22:43:53 +00:00
}
if ( MouseVisitor::moveToolsLeft( flags ) &&
global_vars.tool.cur_page != 0 ) {
global_vars.tool.cur_page--;
updateToolSelection( global_vars.tool.cur_page + 1 );
2021-05-08 22:43:53 +00:00
}
2021-05-08 22:46:10 +00:00
if ( MouseVisitor::moveToolsRight( flags ) &&
global_vars.tool.cur_page != global_vars.tool.max_page ) {
global_vars.tool.cur_page++;
updateToolSelection( global_vars.tool.cur_page - 1 );
2021-05-07 08:08:41 +00:00
}
}
2021-05-08 22:46:10 +00:00
SDLPP::Vec2D< int > getSelectedObjectPosition() {
return global_vars.mouse.edit_box +
SDLPP::Vec2D< int >( global_vars.map.cur_page, 0 );
2021-05-08 22:43:53 +00:00
}
mapObjectType &getSelectedObject() {
auto pos = getSelectedObjectPosition();
return global_vars.objects[pos.getX()][pos.getY()];
2021-05-08 22:43:53 +00:00
}
2021-05-08 22:46:10 +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;
auto tool_type = getBlockRole( global_vars.current_tool->getId() );
2021-05-08 22:46:10 +00:00
switch ( tool_type ) {
case BlockRole::TERRAIN:
visitor.setVisitorType( VisitorType::Terrain );
break;
default:
visitor.setVisitorType( VisitorType::Modifier );
break;
2021-05-08 22:43:53 +00:00
}
scene.visitCollisions( *global_vars.current_tool, visitor );
2021-05-08 22:46:10 +00:00
if ( visitor.removeBlock() && !visitor.addBlock() ) {
2021-05-08 22:43:53 +00:00
auto &obj = getSelectedObject();
2021-05-08 22:46:10 +00:00
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;
2021-05-08 22:43:53 +00:00
}
2021-05-08 22:46:10 +00:00
} else if ( visitor.addBlock() ) {
2021-05-08 22:43:53 +00:00
auto &obj = getSelectedObject();
auto renderer = scene.getRendererShared();
2021-05-08 22:43:53 +00:00
int z_index = 1;
2021-05-08 22:46:10 +00:00
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 ) =
global_vars.current_tool->getId();
2021-05-08 22:46:10 +00:00
// TODO why 1 +?
new_obj = createTerrainBlock(
global_vars.current_tool->getId(), LandType::OVERWORLD,
renderer, 1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), true );
2021-05-08 22:46:10 +00:00
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
break;
case VisitorType::Modifier:
if(tool_type == BlockRole::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, renderer,
1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY() );
// remove mario if exists
removeMario();
global_vars.mario = new_obj;
global_vars.mario_pos = getSelectedObjectPosition();
new_obj->getCollisions()[0]->setId( EDITOR_CHARACTER_ID );
z_index = scene.getObjects().size() - 1;
} else {
// TODO data
std::cout << "Want to set: " << (int)std::get< MapObject::MODIFIER_TYPE >( obj ) << std::endl;
std::get< MapObject::MODIFIER_TYPE >( obj ) = global_vars.current_tool->getId();
std::get< MapObject::MODIFIER_DATA >( obj ) = 0;
std::cout << "SET MODIFIER_TYPE: " << (int)std::get< MapObject::MODIFIER_TYPE >( obj ) << std::endl;
new_obj = createTerrainBlock(
global_vars.current_tool->getId(), LandType::OVERWORLD,
renderer, 1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), global_vars.translucent_terrain_texture, true );
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
// TODO createModifierBlock
dynamic_cast<MarioBlock*>(new_obj.get())->setTerrain(false);
z_index = scene.getObjects().size() - 1;
}
2021-05-08 22:46:10 +00:00
break;
default:
break;
2021-05-08 22:43:53 +00:00
}
2021-05-08 22:46:10 +00:00
scene.addObject( new_obj );
scene.setZIndex( new_obj, z_index );
2021-04-30 19:02:14 +00:00
}
}
void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
setFlag( QUIT_FLAG );
2021-04-30 19:02:14 +00:00
break;
case SDL_KEYUP:
2021-05-08 22:43:53 +00:00
handleKeyUp( event.key.keysym.sym, scene );
2021-04-30 19:02:14 +00:00
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
setFlag( UPDATE_FLAG );
}
break;
2021-05-08 22:43:53 +00:00
case SDL_MOUSEMOTION:
2021-05-08 22:46:10 +00:00
getMousePositionFlags( scene );
break;
case SDL_MOUSEBUTTONUP:
if ( global_vars.mouse.cur_flags == global_vars.mouse.prev_flags ) {
mouseUpAction( global_vars.mouse.cur_flags, scene );
}
if ( global_vars.mouse.edit_box.getX() != -1 ) {
2021-05-08 22:46:10 +00:00
placeTool( scene );
2021-04-30 19:02:14 +00:00
}
if ( global_vars.mouse.tool_box.getX() != -1 ) {
auto &tool_box = global_vars.mouse.tool_box;
2021-05-08 22:43:53 +00:00
size_t index = tool_box.getY() * 4 + tool_box.getX();
if ( index < global_vars.tools.size() ) {
global_vars.tool.index =
global_vars.tool.cur_page * 8 + index;
2021-05-07 07:43:57 +00:00
updateTool();
}
}
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;
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();
2021-05-07 08:08:41 +00:00
SDLPP::Window w( "Mario editor!" );
2021-04-30 19:02:14 +00:00
w.setResizable( true );
BLOCK_SIZE = 1.0 / ( MAP_WIDTH + 2 );
2021-04-30 19:02:14 +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
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_translucent_terrain_texture =
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",
MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture->setAlpha( 100 );
2021-04-30 19:02:14 +00:00
auto scene = std::make_shared< SDLPP::Scene >( renderer );
2021-04-30 19:02:14 +00:00
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 );
loadMap( scene, "test_binary.bin", renderer, global_vars.objects );
2021-04-30 21:12:53 +00:00
// grid
for ( int i = 1; i < ( MAP_WIDTH + 2 ); i++ ) {
2021-04-30 21:12:53 +00:00
auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
2021-05-09 18:45:52 +00:00
i * BLOCK_SIZE, 1 - MAP_HEIGHT * 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 );
2021-05-08 22:46:10 +00:00
scene->addObject( line_vertical );
if ( i > ( MAP_WIDTH - MAP_HEIGHT ) ) {
2021-04-30 21:12:53 +00:00
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE,
( MAP_WIDTH + 1 ) * BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE,
renderer, "#282828" );
line_horizontal->setPermanent();
2021-05-08 22:46:10 +00:00
line_horizontal->setAlignment( SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER );
scene->addObject( line_horizontal );
2021-04-30 21:12:53 +00:00
}
}
// white rectangles
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
0, ( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, BLOCK_SIZE,
MAP_HEIGHT * BLOCK_SIZE, renderer, "#FFFFFF88", true );
2021-05-08 22:46:10 +00:00
rectangle1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle1->setId( EDITOR_LEFT_MAP_ID );
rectangle1->setPermanent();
2021-05-08 22:46:10 +00:00
rectangle1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject( rectangle1 );
2021-04-30 21:12:53 +00:00
// white rectangles
auto rectangle2 = std::make_shared< SDLPP::RectangleRender >(
( MAP_WIDTH + 1 ) * BLOCK_SIZE,
( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, BLOCK_SIZE,
MAP_HEIGHT * BLOCK_SIZE, renderer, "#FFFFFF88", true );
2021-05-08 22:46:10 +00:00
rectangle2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle2->setId( EDITOR_RIGHT_MAP_ID );
rectangle2->setPermanent();
2021-05-08 22:46:10 +00:00
rectangle2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject( rectangle2 );
2021-04-30 21:12:53 +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 );
auto left = std::make_shared< SDLPP::TextRenderer >(
0, ( MAP_WIDTH - MAP_HEIGHT / 2.0 + 1.5 ) * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, renderer, "<", font_config );
2021-05-08 22:46:10 +00:00
left->setId( 0 );
left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left->setPermanent();
2021-05-08 22:46:10 +00:00
scene->addObject( left );
auto right = std::make_shared< SDLPP::TextRenderer >(
( MAP_WIDTH + 1 ) * BLOCK_SIZE,
( MAP_WIDTH - MAP_HEIGHT / 2.0 + 1.5 ) * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, renderer, ">", font_config );
2021-05-08 22:46:10 +00:00
right->setId( 0 );
right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right->setPermanent();
2021-05-08 22:46:10 +00:00
scene->addObject( right );
2021-05-09 18:45:52 +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, MAP_WIDTH, MAP_HEIGHT, renderer ) );
}
}
2021-05-08 22:46:10 +00:00
auto mouse = std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0,
renderer );
2021-05-08 22:46:10 +00:00
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 );
2021-05-08 22:46:10 +00:00
mouse->setColiderColor( "#00FF00" );
mouse->addCollision( SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene->addObject( mouse );
global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH;
2021-04-30 19:02:14 +00:00
2021-05-07 07:43:57 +00:00
// tools
global_vars.tool.max_page = ( possibleBlocks.size() - 1 ) / 8;
2021-05-08 22:46:10 +00:00
for ( int i = 0; i < 4; i++ ) {
auto tool_box1 = std::make_shared< ToolBox >( i, 0, MAP_WIDTH,
MAP_HEIGHT, renderer );
auto tool_box2 = std::make_shared< ToolBox >( i, 1, MAP_WIDTH,
MAP_HEIGHT, renderer );
2021-05-08 22:46:10 +00: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;
2021-05-07 07:43:57 +00:00
}
int tool_index = 0;
2021-05-08 22:46:10 +00:00
for ( auto &block : possibleBlocks ) {
if ( block == MARIO_ID ) {
global_vars.tools.push_back(
createMario( LandType::OVERWORLD, renderer, 0, 0 ) );
} else {
global_vars.tools.push_back( createTerrainBlock(
block, LandType::OVERWORLD, renderer, false ) );
}
global_vars.tools.back()->setHidden( true );
global_vars.tools.back()->setPermanent();
2021-05-07 07:43:57 +00:00
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
global_vars.tools.back()->setPos( ( MAP_WIDTH - 4 ) * BLOCK_SIZE +
x * BLOCK_SIZE,
BLOCK_SIZE + y * BLOCK_SIZE );
2021-05-08 22:46:10 +00:00
// std::cout << "TOOL POS: " << tools.back()->getPos().getX() <<
// ", " << tools.back()->getPos().getY() << std::endl;
scene->addObject( global_vars.tools.back() );
2021-05-08 22:46:10 +00:00
tool_index = ( tool_index + 1 ) % 8;
2021-05-07 07:43:57 +00:00
}
2021-05-08 22:46:10 +00:00
for ( int i = 0; i < 5; i++ ) {
auto line = std::make_shared< SDLPP::LineRenderer >(
( MAP_WIDTH - 4 ) * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE,
( MAP_WIDTH - 4 ) * BLOCK_SIZE + i * BLOCK_SIZE, 3 * BLOCK_SIZE,
renderer, "#282828" );
2021-05-07 07:43:57 +00:00
line->setPermanent();
2021-05-08 22:46:10 +00:00
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line );
2021-05-07 07:43:57 +00:00
}
2021-05-08 22:46:10 +00:00
for ( int i = 0; i < 3; i++ ) {
auto line = std::make_shared< SDLPP::LineRenderer >(
( MAP_WIDTH - 4 ) * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
( MAP_WIDTH - 4 ) * BLOCK_SIZE + 4 * BLOCK_SIZE,
BLOCK_SIZE + i * BLOCK_SIZE, renderer, "#282828" );
2021-05-07 07:43:57 +00:00
line->setPermanent();
2021-05-08 22:46:10 +00:00
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line );
2021-05-07 07:43:57 +00:00
}
2021-05-07 08:08:41 +00:00
auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >(
( MAP_WIDTH - 5 ) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE,
renderer, "#FFFFFF88", true );
2021-05-08 22:46:10 +00:00
tool_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect1->setId( EDITOR_LEFT_TOOL_ID );
2021-05-07 08:08:41 +00:00
tool_rect1->setPermanent();
2021-05-08 22:46:10 +00:00
tool_rect1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject( tool_rect1 );
2021-05-07 08:08:41 +00:00
// white rectangles
auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >(
MAP_WIDTH * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE,
renderer, "#FFFFFF88", true );
2021-05-08 22:46:10 +00:00
tool_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect2->setId( EDITOR_RIGHT_TOOL_ID );
2021-05-07 08:08:41 +00:00
tool_rect2->setPermanent();
2021-05-08 22:46:10 +00:00
tool_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject( tool_rect2 );
2021-05-07 08:08:41 +00:00
auto left_tool = std::make_shared< SDLPP::TextRenderer >(
( MAP_WIDTH - 5 ) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, renderer, "<", font_config );
2021-05-08 22:46:10 +00:00
left_tool->setId( 0 );
left_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2021-05-07 08:08:41 +00:00
left_tool->setPermanent();
2021-05-08 22:46:10 +00:00
scene->addObject( left_tool );
2021-05-07 08:08:41 +00:00
auto right_tool = std::make_shared< SDLPP::TextRenderer >(
MAP_WIDTH * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE,
renderer, ">", font_config );
2021-05-08 22:46:10 +00:00
right_tool->setId( 0 );
right_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2021-05-07 08:08:41 +00:00
right_tool->setPermanent();
2021-05-08 22:46:10 +00:00
scene->addObject( right_tool );
2021-05-07 08:08:41 +00:00
global_vars.translucent_terrain_texture =
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",
MARIO_OVERWORLD_COLORKEY );
global_vars.translucent_terrain_texture->setAlpha( 100 );
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.current_tool = createTerrainBlock(
possibleBlocks[global_vars.tool.index], LandType::OVERWORLD, renderer,
global_vars.translucent_terrain_texture, false );
global_vars.current_tool->addCollision(
SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
dynamic_cast< MarioBlock & >( *global_vars.current_tool ).setTool();
scene->addObject( global_vars.current_tool );
scene->moveZTop( global_vars.current_tool );
2021-05-08 22:46:10 +00:00
scene->moveEverything( BLOCK_SIZE, 0 );
2021-04-30 19:02:14 +00:00
FPSmanager gFPS;
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
2021-05-08 22:46:10 +00:00
updateToolSelection( 0 );
2021-05-07 07:43:57 +00:00
2021-04-30 19:02:14 +00:00
auto base = SDL_GetTicks();
int frames = 0;
std::thread inputThread( doInput, scene );
inputThread.detach();
setFlag( UPDATE_FLAG );
// required for initial correct rendering
setFlag( MAP_LEFT_ENABLED_FLAG );
setFlag( MAP_RIGHT_ENABLED_FLAG );
setFlag( TOOL_LEFT_ENABLED_FLAG );
setFlag( TOOL_RIGHT_ENABLED_FLAG );
while ( !getFlag( QUIT_FLAG ) ) {
2021-04-30 19:02:14 +00:00
SDL_PumpEvents();
SDL_framerateDelay( &gFPS );
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++;
if ( SDL_GetTicks() - base >= 1000 ) {
std::cout << "FPS: " << frames << std::endl;
frames = 0;
base = SDL_GetTicks();
}
if ( global_vars.map.cur_page == 0 &&
getFlag( MAP_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( MAP_LEFT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
left->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else if ( global_vars.map.cur_page != 0 &&
!getFlag( MAP_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( MAP_LEFT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
left->setTextColor( font, "#000000", "#282828", 0.05 );
}
if ( global_vars.map.cur_page == global_vars.map.max_page &&
getFlag( MAP_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( MAP_RIGHT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
right->setTextColor( font, "#00FF00", "#000000", 0.1 );
right->changeText( "+" );
} else if ( global_vars.map.cur_page != global_vars.map.max_page &&
!getFlag( MAP_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( MAP_RIGHT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
right->setTextColor( font, "#000000", "#282828", 0.05 );
right->changeText( ">" );
}
if ( global_vars.tool.cur_page == 0 &&
getFlag( TOOL_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( TOOL_LEFT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
left_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else if ( global_vars.tool.cur_page != 0 &&
!getFlag( TOOL_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( TOOL_LEFT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
left_tool->setTextColor( font, "#000000", "#282828", 0.05 );
2021-05-07 08:08:41 +00:00
}
if ( global_vars.tool.cur_page == global_vars.tool.max_page &&
getFlag( TOOL_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( TOOL_RIGHT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
right_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else if ( global_vars.tool.cur_page != global_vars.tool.max_page &&
!getFlag( TOOL_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( TOOL_RIGHT_ENABLED_FLAG );
2021-05-08 22:46:10 +00:00
right_tool->setTextColor( font, "#000000", "#282828", 0.05 );
2021-05-07 08:08:41 +00:00
}
if ( getFlag( UPDATE_FLAG ) ) {
scene->updateSizeAndPosition();
unsetFlag( UPDATE_FLAG );
}
2021-04-30 19:02:14 +00:00
}
saveMap( "test_binary2.bin", global_vars.objects );
2021-04-30 19:02:14 +00:00
return 0;
}