Mario editor: formatting

This commit is contained in:
zv0n 2021-05-09 00:46:10 +02:00
parent b54ba5034c
commit 83cda5f860
8 changed files with 595 additions and 458 deletions

View File

@ -5,24 +5,32 @@
#include "editor_visitor.hpp" #include "editor_visitor.hpp"
#include <unordered_map> #include <unordered_map>
MarioBlock::MarioBlock(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Texture> texture, SDL_Rect src) : RectangleRender( x * BLOCK_SIZE, 1 - (16-y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ) {} MarioBlock::MarioBlock( int x, int y,
std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Texture > texture,
SDL_Rect src )
: RectangleRender( x * BLOCK_SIZE, 1 - ( 16 - y ) * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE, renderer, texture, src ) {}
void MarioBlock::visit( SDLPP::Visitor &visitor ) { void MarioBlock::visit( SDLPP::Visitor &visitor ) {
if(!_tool && _terrain && visitor.getVisitorType() == VisitorType::Terrain) { if ( !_tool && _terrain &&
visitor.getVisitorType() == VisitorType::Terrain ) {
destroy(); destroy();
} }
if(!_tool && !_terrain && visitor.getVisitorType() == VisitorType::Modifier) { if ( !_tool && !_terrain &&
visitor.getVisitorType() == VisitorType::Modifier ) {
destroy(); destroy();
} }
visitor.visit(*this); visitor.visit( *this );
} }
void MarioBlock::setTool(bool tool) { void MarioBlock::setTool( bool tool ) {
_tool = tool; _tool = tool;
} }
void MarioBlock::setTerrain(bool terrain) { void MarioBlock::setTerrain( bool terrain ) {
_terrain = terrain; _terrain = terrain;
} }
const std::vector< uint64_t > possibleBlocks = { FLOOR_ID, const std::vector< uint64_t > possibleBlocks = {
FLOOR_ID,
STEP_ID, STEP_ID,
HILL_TOP_ID, HILL_TOP_ID,
HILL_DOTS_LEFT_ID, HILL_DOTS_LEFT_ID,
@ -84,52 +92,73 @@ const std::vector< uint64_t > possibleBlocks = { FLOOR_ID,
MARIO_ID, MARIO_ID,
}; };
const std::unordered_map<uint64_t, const SDL_Rect*> block_mapping = { const std::unordered_map< uint64_t, const SDL_Rect * > block_mapping = {
{FLOOR_ID, &FLOOR_SRC}, {HILL_INCLINE_ID, &HILL_INCLINE_SRC}, { FLOOR_ID, &FLOOR_SRC },
{HILL_DECLINE_ID, &HILL_DECLINE_SRC}, {HILL_DOTS_RIGHT_ID, &HILL_DOTS_RIGHT_SRC}, { HILL_INCLINE_ID, &HILL_INCLINE_SRC },
{HILL_DOTS_LEFT_ID, &HILL_DOTS_LEFT_SRC}, {HILL_FILL_ID, &HILL_FILL_SRC}, { HILL_DECLINE_ID, &HILL_DECLINE_SRC },
{HILL_TOP_ID, &HILL_TOP_SRC}, {BUSH_LEFT_ID, &BUSH_LEFT_SRC}, {BUSH_MIDDLE_ID, &BUSH_MIDDLE_SRC}, { HILL_DOTS_RIGHT_ID, &HILL_DOTS_RIGHT_SRC },
{BUSH_RIGHT_ID, &BUSH_RIGHT_SRC}, {CLOUD_LEFT_BOTTOM_ID, &CLOUD_LEFT_BOTTOM_SRC}, { HILL_DOTS_LEFT_ID, &HILL_DOTS_LEFT_SRC },
{CLOUD_MIDDLE_BOTTOM_ID, &CLOUD_MIDDLE_BOTTOM_SRC}, {CLOUD_RIGHT_BOTTOM_ID, &CLOUD_RIGHT_BOTTOM_SRC}, { HILL_FILL_ID, &HILL_FILL_SRC },
{CLOUD_LEFT_TOP_ID, &CLOUD_LEFT_TOP_SRC}, {CLOUD_MIDDLE_TOP_ID, &CLOUD_MIDDLE_TOP_SRC}, { HILL_TOP_ID, &HILL_TOP_SRC },
{CLOUD_RIGHT_TOP_ID, &CLOUD_RIGHT_TOP_SRC}, {PIPE_LEFT_BOTTOM_ID, &PIPE_LEFT_BOTTOM_SRC}, { BUSH_LEFT_ID, &BUSH_LEFT_SRC },
{PIPE_LEFT_TOP_ID, &PIPE_LEFT_TOP_SRC}, {PIPE_RIGHT_BOTTOM_ID, &PIPE_RIGHT_BOTTOM_SRC}, { BUSH_MIDDLE_ID, &BUSH_MIDDLE_SRC },
{PIPE_RIGHT_TOP_ID, &PIPE_RIGHT_TOP_SRC}, {CASTLE_LEFT_ID, &CASTLE_LEFT_SRC}, { BUSH_RIGHT_ID, &BUSH_RIGHT_SRC },
{CASTLE_RIGHT_ID, &CASTLE_RIGHT_SRC}, {CASTLE_BLACK_ID, &CASTLE_BLACK_SRC}, { CLOUD_LEFT_BOTTOM_ID, &CLOUD_LEFT_BOTTOM_SRC },
{CASTLE_ENTRY_ID, &CASTLE_ENTRY_SRC}, {CASTLE_TOWER_ID, &CASTLE_TOWER_SRC}, { CLOUD_MIDDLE_BOTTOM_ID, &CLOUD_MIDDLE_BOTTOM_SRC },
{CASTLE_TOWER_FILLED_ID, &CASTLE_TOWER_FILLED_SRC}, {VINE_TOP_ID, &VINE_TOP_SRC}, { CLOUD_RIGHT_BOTTOM_ID, &CLOUD_RIGHT_BOTTOM_SRC },
{VINE_BOTTOM_ID, &VINE_BOTTOM_SRC}, {POLE_TOP_ID, &POLE_TOP_SRC}, { CLOUD_LEFT_TOP_ID, &CLOUD_LEFT_TOP_SRC },
{POLE_BOTTOM_ID, &POLE_BOTTOM_SRC}, {FLAG_ID, &FLAG_SRC}, { CLOUD_MIDDLE_TOP_ID, &CLOUD_MIDDLE_TOP_SRC },
{STEP_ID, &STEP_SRC}, {BRICK_ID, &BRICK_SRC}, {BRICK_TOP_ID, &BRICK_TOP_SRC}, { CLOUD_RIGHT_TOP_ID, &CLOUD_RIGHT_TOP_SRC },
{SIDEWAY_PIPE_END_TOP_ID, &SIDEWAY_PIPE_END_TOP_SRC}, { PIPE_LEFT_BOTTOM_ID, &PIPE_LEFT_BOTTOM_SRC },
{SIDEWAY_PIPE_END_BOTTOM_ID, &SIDEWAY_PIPE_END_BOTTOM_SRC}, { PIPE_LEFT_TOP_ID, &PIPE_LEFT_TOP_SRC },
{SIDEWAY_PIPE_MIDDLE_TOP_ID, &SIDEWAY_PIPE_MIDDLE_TOP_SRC}, { PIPE_RIGHT_BOTTOM_ID, &PIPE_RIGHT_BOTTOM_SRC },
{SIDEWAY_PIPE_MIDDLE_BOTTOM_ID, &SIDEWAY_PIPE_MIDDLE_BOTTOM_SRC}, { PIPE_RIGHT_TOP_ID, &PIPE_RIGHT_TOP_SRC },
{SIDEWAY_PIPE_CONNECTOR_TOP_ID, &SIDEWAY_PIPE_CONNECTOR_TOP_SRC}, { CASTLE_LEFT_ID, &CASTLE_LEFT_SRC },
{SIDEWAY_PIPE_CONNECTOR_BOTTOM_ID, &SIDEWAY_PIPE_CONNECTOR_BOTTOM_SRC}, { CASTLE_RIGHT_ID, &CASTLE_RIGHT_SRC },
{TREE_PLATFORM_TOP_LEFT_ID, &TREE_PLATFORM_TOP_LEFT_SRC}, { CASTLE_BLACK_ID, &CASTLE_BLACK_SRC },
{TREE_PLATFORM_TOP_MIDDLE_ID, &TREE_PLATFORM_TOP_MIDDLE_SRC}, { CASTLE_ENTRY_ID, &CASTLE_ENTRY_SRC },
{TREE_PLATFORM_TOP_RIGHT_ID, &TREE_PLATFORM_TOP_RIGHT_SRC}, { CASTLE_TOWER_ID, &CASTLE_TOWER_SRC },
{TREE_PLATFORM_BARK_ID, &TREE_PLATFORM_BARK_SRC}, { CASTLE_TOWER_FILLED_ID, &CASTLE_TOWER_FILLED_SRC },
{WATER_TOP_ID, &WATER_TOP_SRC}, {WATER_FILL_ID, &WATER_FILL_SRC}, { VINE_TOP_ID, &VINE_TOP_SRC },
{MUSHROOM_PLATFORM_TOP_LEFT_ID, &MUSHROOM_PLATFORM_TOP_LEFT_SRC}, { VINE_BOTTOM_ID, &VINE_BOTTOM_SRC },
{MUSHROOM_PLATFORM_TOP_MIDDLE_ID, &MUSHROOM_PLATFORM_TOP_MIDDLE_SRC}, { POLE_TOP_ID, &POLE_TOP_SRC },
{MUSHROOM_PLATFORM_TOP_RIGHT_ID, &MUSHROOM_PLATFORM_TOP_RIGHT_SRC}, { POLE_BOTTOM_ID, &POLE_BOTTOM_SRC },
{MUSHROOM_PLATFORM_BARK_TOP_ID, &MUSHROOM_PLATFORM_BARK_TOP_SRC}, { FLAG_ID, &FLAG_SRC },
{MUSHROOM_PLATFORM_BARK_BOTTOM_ID, &MUSHROOM_PLATFORM_BARK_BOTTOM_SRC}, { STEP_ID, &STEP_SRC },
{TREE_BARK_ID, &TREE_BARK_SRC}, {TREE_LEAVES_SMALL_ID, &TREE_LEAVES_SMALL_SRC}, { BRICK_ID, &BRICK_SRC },
{TREE_LEAVES_TOP_ID, &TREE_LEAVES_TOP_SRC}, { BRICK_TOP_ID, &BRICK_TOP_SRC },
{TREE_LEAVES_BOTTOM_ID, &TREE_LEAVES_BOTTOM_SRC}, { SIDEWAY_PIPE_END_TOP_ID, &SIDEWAY_PIPE_END_TOP_SRC },
{CANNON_TOWER_ID, &CANNON_TOWER_SRC}, { SIDEWAY_PIPE_END_BOTTOM_ID, &SIDEWAY_PIPE_END_BOTTOM_SRC },
{CANNON_PEDESTAL_ID, &CANNON_PEDESTAL_SRC}, { SIDEWAY_PIPE_MIDDLE_TOP_ID, &SIDEWAY_PIPE_MIDDLE_TOP_SRC },
{CANNON_ID, &CANNON_SRC}, {MARIO_ID, &MARIO_STANDING_SRC} { SIDEWAY_PIPE_MIDDLE_BOTTOM_ID, &SIDEWAY_PIPE_MIDDLE_BOTTOM_SRC },
{ SIDEWAY_PIPE_CONNECTOR_TOP_ID, &SIDEWAY_PIPE_CONNECTOR_TOP_SRC },
{ SIDEWAY_PIPE_CONNECTOR_BOTTOM_ID, &SIDEWAY_PIPE_CONNECTOR_BOTTOM_SRC },
{ TREE_PLATFORM_TOP_LEFT_ID, &TREE_PLATFORM_TOP_LEFT_SRC },
{ TREE_PLATFORM_TOP_MIDDLE_ID, &TREE_PLATFORM_TOP_MIDDLE_SRC },
{ TREE_PLATFORM_TOP_RIGHT_ID, &TREE_PLATFORM_TOP_RIGHT_SRC },
{ TREE_PLATFORM_BARK_ID, &TREE_PLATFORM_BARK_SRC },
{ WATER_TOP_ID, &WATER_TOP_SRC },
{ WATER_FILL_ID, &WATER_FILL_SRC },
{ MUSHROOM_PLATFORM_TOP_LEFT_ID, &MUSHROOM_PLATFORM_TOP_LEFT_SRC },
{ MUSHROOM_PLATFORM_TOP_MIDDLE_ID, &MUSHROOM_PLATFORM_TOP_MIDDLE_SRC },
{ MUSHROOM_PLATFORM_TOP_RIGHT_ID, &MUSHROOM_PLATFORM_TOP_RIGHT_SRC },
{ MUSHROOM_PLATFORM_BARK_TOP_ID, &MUSHROOM_PLATFORM_BARK_TOP_SRC },
{ MUSHROOM_PLATFORM_BARK_BOTTOM_ID, &MUSHROOM_PLATFORM_BARK_BOTTOM_SRC },
{ TREE_BARK_ID, &TREE_BARK_SRC },
{ TREE_LEAVES_SMALL_ID, &TREE_LEAVES_SMALL_SRC },
{ TREE_LEAVES_TOP_ID, &TREE_LEAVES_TOP_SRC },
{ TREE_LEAVES_BOTTOM_ID, &TREE_LEAVES_BOTTOM_SRC },
{ CANNON_TOWER_ID, &CANNON_TOWER_SRC },
{ CANNON_PEDESTAL_ID, &CANNON_PEDESTAL_SRC },
{ CANNON_ID, &CANNON_SRC },
{ MARIO_ID, &MARIO_STANDING_SRC }
}; };
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y, createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
std::shared_ptr< SDLPP::Texture > &texture, const SDL_Rect &src, std::shared_ptr< SDLPP::Texture > &texture, const SDL_Rect &src,
uint64_t id, bool collision = false ) { uint64_t id, bool collision = false ) {
auto block = std::make_shared<MarioBlock>(x, y, renderer, texture, src); auto block = std::make_shared< MarioBlock >( x, y, renderer, texture, src );
block->setId( id ); block->setId( id );
block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); block->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
block->setStatic(); block->setStatic();
@ -139,9 +168,9 @@ createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
} }
SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type ) { SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type ) {
if(block_mapping.find(id) == block_mapping.end()) if ( block_mapping.find( id ) == block_mapping.end() )
return {}; return {};
SDL_Rect ret_src = *block_mapping.at(id); SDL_Rect ret_src = *block_mapping.at( id );
switch ( type ) { switch ( type ) {
case LandType::OVERWORLD: case LandType::OVERWORLD:
ret_src.x += OVERWORLD_SHIFT.getX(); ret_src.x += OVERWORLD_SHIFT.getX();
@ -162,41 +191,55 @@ SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type ) {
} }
return ret_src; return ret_src;
} }
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y, std::shared_ptr<SDLPP::Texture> texture, bool collision ) { std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
std::shared_ptr< SDLPP::Texture > texture,
bool collision ) {
return createBlock( renderer, x, y, texture, return createBlock( renderer, x, y, texture,
getSourceRectByID( block_id, type ), block_id, getSourceRectByID( block_id, type ), block_id,
collision ); collision );
} }
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, std::shared_ptr<SDLPP::Texture> texture, bool collision ) { std::shared_ptr< SDLPP::RectangleRender >
return createTerrainBlock( block_id, type, renderer, 0, 0, texture, collision ); createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer,
std::shared_ptr< SDLPP::Texture > texture,
bool collision ) {
return createTerrainBlock( block_id, type, renderer, 0, 0, texture,
collision );
} }
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
int y, bool collision ) { bool collision ) {
return createTerrainBlock(block_id, type, renderer, x, y, g_terrain_texture, collision); return createTerrainBlock( block_id, type, renderer, x, y,
g_terrain_texture, collision );
} }
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type, createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, std::shared_ptr< SDLPP::Renderer > &renderer,
bool collision ) { bool collision ) {
return createTerrainBlock(block_id, type, renderer, g_terrain_texture, collision); return createTerrainBlock( block_id, type, renderer, g_terrain_texture,
collision );
} }
std::shared_ptr<SDLPP::RectangleRender> createMario( LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y ) { std::shared_ptr< SDLPP::RectangleRender >
//TODO add type additions createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer,
auto mario = createBlock( renderer, x, y, g_mario_texture, MARIO_STANDING_SRC, MARIO_ID, true ); int x, int y ) {
dynamic_cast<MarioBlock&>(*mario).setTerrain(false); // TODO add type additions
auto mario = createBlock( renderer, x, y, g_mario_texture,
MARIO_STANDING_SRC, MARIO_ID, true );
dynamic_cast< MarioBlock & >( *mario ).setTerrain( false );
return mario; return mario;
} }
enum BlockRole::Value getBlockRole(uint64_t id) { enum BlockRole::Value getBlockRole( uint64_t id ) {
if(id >= 0x7000) if ( id >= 0x7000 )
return BlockRole::TERRAIN; return BlockRole::TERRAIN;
if(id == MARIO_ID) if ( id == MARIO_ID )
return BlockRole::MARIO; return BlockRole::MARIO;
//TODO modifier/character // TODO modifier/character
return BlockRole::MODIFIER; return BlockRole::MODIFIER;
} }

View File

@ -6,24 +6,21 @@
class MarioBlock : public SDLPP::RectangleRender { class MarioBlock : public SDLPP::RectangleRender {
public: public:
MarioBlock(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Texture> texture, SDL_Rect src); MarioBlock( int x, int y, std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Texture > texture, SDL_Rect src );
void visit( SDLPP::Visitor &visitor ) override; void visit( SDLPP::Visitor &visitor ) override;
void setTool(bool tool = true); void setTool( bool tool = true );
void setTerrain(bool terrain = true); void setTerrain( bool terrain = true );
private: private:
bool _tool = false; bool _tool = false;
bool _terrain = true; bool _terrain = true;
}; };
extern const std::vector<uint64_t> possibleBlocks; extern const std::vector< uint64_t > possibleBlocks;
struct LandType { struct LandType {
enum Value { enum Value { OVERWORLD = 0, UNDERWORLD = 1, WATER = 2, BOWSER = 4 };
OVERWORLD = 0,
UNDERWORLD = 1,
WATER = 2,
BOWSER = 4
};
}; };
struct BlockRole { struct BlockRole {
@ -35,14 +32,30 @@ struct BlockRole {
}; };
}; };
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, bool collision = false ); std::shared_ptr< SDLPP::RectangleRender >
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y, bool collision = false ); createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, std::shared_ptr<SDLPP::Texture> texture, bool collision = false ); std::shared_ptr< SDLPP::Renderer > &renderer,
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y, std::shared_ptr<SDLPP::Texture> texture, bool collision = false ); bool collision = false );
std::shared_ptr<SDLPP::RectangleRender> createMario( LandType::Value type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y ); std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
bool collision = false );
std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer,
std::shared_ptr< SDLPP::Texture > texture,
bool collision = false );
std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, LandType::Value type,
std::shared_ptr< SDLPP::Renderer > &renderer, int x, int y,
std::shared_ptr< SDLPP::Texture > texture,
bool collision = false );
std::shared_ptr< SDLPP::RectangleRender >
createMario( LandType::Value type, std::shared_ptr< SDLPP::Renderer > &renderer,
int x, int y );
SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type ); SDL_Rect getSourceRectByID( uint64_t id, LandType::Value type );
enum BlockRole::Value getBlockRole(uint64_t id); enum BlockRole::Value getBlockRole( uint64_t id );
#endif #endif

View File

@ -33,33 +33,35 @@ struct MouseInfo {
std::shared_ptr< SDLPP::Renderer > g_renderer = nullptr; std::shared_ptr< SDLPP::Renderer > g_renderer = nullptr;
bool g_quit = false; bool g_quit = false;
bool g_update_size = false; bool g_update_size = false;
std::vector<mapColumnType> g_objects = {}; std::vector< mapColumnType > g_objects = {};
std::mutex g_destruction_mutex; std::mutex g_destruction_mutex;
// current mouse flags, previous mouse flags, selected edit box, selected tool box // current mouse flags, previous mouse flags, selected edit box, selected tool
std::tuple<uint64_t, uint64_t, SDLPP::Vec2D<int>, SDLPP::Vec2D<int>> g_mouse_info; // 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_start_index = 0;
int g_current_max_index = 0; int g_current_max_index = 0;
uint64_t g_current_block = 0; uint64_t g_current_block = 0;
std::vector<std::shared_ptr<SDLPP::RenderObject>> g_tools{}; std::vector< std::shared_ptr< SDLPP::RenderObject > > g_tools{};
std::shared_ptr<SDLPP::RenderObject> g_current_tool = nullptr; std::shared_ptr< SDLPP::RenderObject > g_current_tool = nullptr;
int g_current_tool_index = 0; int g_current_tool_index = 0;
int g_max_tool_index = 0; int g_max_tool_index = 0;
std::shared_ptr<SDLPP::Texture> g_placeholder_texture = nullptr; std::shared_ptr< SDLPP::Texture > g_placeholder_texture = nullptr;
std::shared_ptr<SDLPP::Texture> g_placeholder_mario = nullptr; std::shared_ptr< SDLPP::Texture > g_placeholder_mario = nullptr;
std::shared_ptr<SDLPP::RenderObject> g_mario = nullptr; std::shared_ptr< SDLPP::RenderObject > g_mario = nullptr;
SDLPP::Vec2D<int> g_mario_pos = {0,0}; SDLPP::Vec2D< int > g_mario_pos = { 0, 0 };
enum LandType::Value g_current_world_type = LandType::OVERWORLD; enum LandType::Value g_current_world_type = LandType::OVERWORLD;
void updateTool() { void updateTool() {
auto tool_role = getBlockRole(possibleBlocks[g_current_block]); auto tool_role = getBlockRole( possibleBlocks[g_current_block] );
std::shared_ptr<SDLPP::Texture> target_texture = nullptr; std::shared_ptr< SDLPP::Texture > target_texture = nullptr;
switch(tool_role) { switch ( tool_role ) {
case BlockRole::TERRAIN: case BlockRole::TERRAIN:
target_texture = g_placeholder_texture; target_texture = g_placeholder_texture;
break; break;
@ -71,47 +73,54 @@ void updateTool() {
case BlockRole::CHARACTER: case BlockRole::CHARACTER:
break; break;
} }
g_current_tool->setTexture(target_texture, getSourceRectByID(possibleBlocks[g_current_block], g_current_world_type)); g_current_tool->setTexture(
g_current_tool->setId(possibleBlocks[g_current_block]); target_texture, getSourceRectByID( possibleBlocks[g_current_block],
g_current_tool->getCollisions()[0]->setId(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() { void removeMario() {
if(!g_mario) if ( !g_mario )
return; return;
auto prev = g_objects[g_mario_pos.getX()][g_mario_pos.getY()]; auto prev = g_objects[g_mario_pos.getX()][g_mario_pos.getY()];
// remove character/modifiers // 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_objects[g_mario_pos.getX()][g_mario_pos.getY()] = {
std::get< 0 >( prev ), std::get< 1 >( prev ), 0, 0, 0, 0
};
g_mario->destroy(); g_mario->destroy();
} }
void updateToolSelection(int prev_index) { void updateToolSelection( int prev_index ) {
size_t prev = prev_index * 8; size_t prev = prev_index * 8;
size_t cur = g_current_tool_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++) { for ( size_t i = prev;
g_tools[i]->setHidden(true); 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++) { for ( size_t i = cur;
g_tools[i]->setHidden(false); i < ( g_tools.size() < cur + 8 ? g_tools.size() : cur + 8 ); i++ ) {
g_tools[i]->setHidden( false );
} }
} }
void moveToolsLeft() { void moveToolsLeft() {
g_current_tool_index--; g_current_tool_index--;
updateToolSelection(g_current_tool_index + 1); updateToolSelection( g_current_tool_index + 1 );
} }
void moveToolsRight() { void moveToolsRight() {
g_current_tool_index++; g_current_tool_index++;
updateToolSelection(g_current_tool_index - 1); updateToolSelection( g_current_tool_index - 1 );
} }
// TODO add red outline to currently selected tool // TODO add red outline to currently selected tool
// add WSAD navigation for the red highlight // add WSAD navigation for the red highlight
void selectPrevTool() { void selectPrevTool() {
if(g_current_block == 0) if ( g_current_block == 0 )
return; return;
if(g_current_block % 8 == 0) { if ( g_current_block % 8 == 0 ) {
moveToolsLeft(); moveToolsLeft();
} }
g_current_block--; g_current_block--;
@ -119,9 +128,9 @@ void selectPrevTool() {
} }
void selectNextTool() { void selectNextTool() {
if(g_current_block == g_tools.size() - 1) if ( g_current_block == g_tools.size() - 1 )
return; return;
if(g_current_block % 8 == 7) { if ( g_current_block % 8 == 7 ) {
moveToolsRight(); moveToolsRight();
} }
g_current_block++; g_current_block++;
@ -147,50 +156,57 @@ void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) {
} }
} }
void getMousePositionFlags(SDLPP::Scene &scene) { void getMousePositionFlags( SDLPP::Scene &scene ) {
auto mouse = scene.getObjects({EDITOR_MOUSE_ID})[0]; auto mouse = scene.getObjects( { EDITOR_MOUSE_ID } )[0];
// move mouse colider to mouse position // move mouse colider to mouse position
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER)); mouse->setPos( SDLPP::Mouse::getMousePositionDouble(
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ) );
MouseVisitor visitor; MouseVisitor visitor;
scene.visitCollisions(*mouse, visitor); scene.visitCollisions( *mouse, visitor );
std::get<MouseInfo::CUR_FLAGS>(g_mouse_info) = visitor.getFlags(); std::get< MouseInfo::CUR_FLAGS >( g_mouse_info ) = visitor.getFlags();
std::get<MouseInfo::EDIT_BOX>(g_mouse_info) = visitor.getEditBoxIndexes(); std::get< MouseInfo::EDIT_BOX >( g_mouse_info ) =
std::get<MouseInfo::TOOL_BOX>(g_mouse_info) = visitor.getToolBoxIndexes(); 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 we found an edit box, move tool icon to that box
if(visitor.foundEditBox()) { if ( visitor.foundEditBox() ) {
const auto &box = std::get<MouseInfo::EDIT_BOX>(g_mouse_info); 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); g_current_tool->setPos( BLOCK_SIZE + box.getX() * BLOCK_SIZE,
4 * BLOCK_SIZE + box.getY() * BLOCK_SIZE );
} }
} }
void mouseUpAction(uint64_t flags, SDLPP::Scene &scene) { void mouseUpAction( uint64_t flags, SDLPP::Scene &scene ) {
if(MouseVisitor::moveMapLeft(flags) && g_current_start_index != 0) { if ( MouseVisitor::moveMapLeft( flags ) && g_current_start_index != 0 ) {
g_current_start_index -= 1; g_current_start_index -= 1;
scene.moveEverything(BLOCK_SIZE, 0); scene.moveEverything( BLOCK_SIZE, 0 );
} }
if(MouseVisitor::moveMapRight(flags)) { if ( MouseVisitor::moveMapRight( flags ) ) {
if(g_current_start_index == g_current_max_index) { if ( g_current_start_index == g_current_max_index ) {
// add column // add column
// TODO 18 as constant // TODO 18 as constant
g_objects.resize(g_current_max_index + 18 + 1); g_objects.resize( g_current_max_index + 18 + 1 );
g_current_max_index++; g_current_max_index++;
} }
g_current_start_index += 1; g_current_start_index += 1;
scene.moveEverything(-BLOCK_SIZE,0); scene.moveEverything( -BLOCK_SIZE, 0 );
} }
if(MouseVisitor::moveToolsLeft(flags) && g_current_tool_index != 0) { if ( MouseVisitor::moveToolsLeft( flags ) && g_current_tool_index != 0 ) {
g_current_tool_index -= 1; g_current_tool_index -= 1;
updateToolSelection(g_current_tool_index + 1); updateToolSelection( g_current_tool_index + 1 );
} }
if(MouseVisitor::moveToolsRight(flags) && g_current_tool_index != g_max_tool_index) { if ( MouseVisitor::moveToolsRight( flags ) &&
g_current_tool_index != g_max_tool_index ) {
g_current_tool_index += 1; g_current_tool_index += 1;
updateToolSelection(g_current_tool_index - 1); updateToolSelection( g_current_tool_index - 1 );
} }
} }
SDLPP::Vec2D<int> getSelectedObjectPosition() { 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() }; 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() { mapObjectType &getSelectedObject() {
@ -198,69 +214,75 @@ mapObjectType &getSelectedObject() {
return g_objects[pos.getX()][pos.getY()]; return g_objects[pos.getX()][pos.getY()];
} }
void placeTool(SDLPP::Scene &scene) { void placeTool( SDLPP::Scene &scene ) {
std::lock_guard<std::mutex> lock(g_destruction_mutex); std::lock_guard< std::mutex > lock( g_destruction_mutex );
ToolVisitor visitor; ToolVisitor visitor;
auto tool_type = getBlockRole(g_current_tool->getId()); auto tool_type = getBlockRole( g_current_tool->getId() );
switch(tool_type) { switch ( tool_type ) {
case BlockRole::TERRAIN: case BlockRole::TERRAIN:
visitor.setVisitorType(VisitorType::Terrain); visitor.setVisitorType( VisitorType::Terrain );
break; break;
default: default:
visitor.setVisitorType(VisitorType::Modifier); visitor.setVisitorType( VisitorType::Modifier );
break; break;
} }
scene.visitCollisions(*g_current_tool, visitor); scene.visitCollisions( *g_current_tool, visitor );
if(visitor.removeBlock() && !visitor.addBlock()) { if ( visitor.removeBlock() && !visitor.addBlock() ) {
auto &obj = getSelectedObject(); auto &obj = getSelectedObject();
switch(visitor.getVisitorType()) { switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain: case VisitorType::Terrain:
std::get<MapObject::TERRAIN_TYPE>(obj) = LandType::OVERWORLD; std::get< MapObject::TERRAIN_TYPE >( obj ) = LandType::OVERWORLD;
std::get<MapObject::TERRAIN_ID>(obj) = 0; std::get< MapObject::TERRAIN_ID >( obj ) = 0;
break; break;
case VisitorType::Modifier: case VisitorType::Modifier:
std::get<MapObject::CHARACTER_TYPE>(obj) = 0; std::get< MapObject::CHARACTER_TYPE >( obj ) = 0;
std::get<MapObject::CHARACTER_ID>(obj) = 0; std::get< MapObject::CHARACTER_ID >( obj ) = 0;
std::get<MapObject::MODIFIER_TYPE>(obj) = 0; std::get< MapObject::MODIFIER_TYPE >( obj ) = 0;
std::get<MapObject::MODIFIER_DATA>(obj) = 0; std::get< MapObject::MODIFIER_DATA >( obj ) = 0;
break; break;
default: default:
break; break;
} }
} else if(visitor.addBlock()) { } else if ( visitor.addBlock() ) {
auto &obj = getSelectedObject(); auto &obj = getSelectedObject();
int z_index = 1; int z_index = 1;
std::shared_ptr<SDLPP::RenderObject> new_obj = nullptr; std::shared_ptr< SDLPP::RenderObject > new_obj = nullptr;
switch(visitor.getVisitorType()) { switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain: case VisitorType::Terrain:
std::get<MapObject::TERRAIN_TYPE>(obj) = LandType::OVERWORLD; std::get< MapObject::TERRAIN_TYPE >( obj ) = LandType::OVERWORLD;
std::get<MapObject::TERRAIN_ID>(obj) = g_current_tool->getId(); std::get< MapObject::TERRAIN_ID >( obj ) = g_current_tool->getId();
// TODO why 1 +? // 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 = createTerrainBlock(
new_obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID); 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; break;
case VisitorType::Modifier: case VisitorType::Modifier:
// TODO check if modifier or character // TODO check if modifier or character
std::get<MapObject::CHARACTER_TYPE>(obj) = LandType::OVERWORLD; std::get< MapObject::CHARACTER_TYPE >( obj ) = LandType::OVERWORLD;
// TODO character ID // TODO character ID
std::get<MapObject::CHARACTER_ID>(obj) = MARIO_ID; std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID;
std::get<MapObject::MODIFIER_TYPE>(obj) = 0; std::get< MapObject::MODIFIER_TYPE >( obj ) = 0;
std::get<MapObject::MODIFIER_DATA>(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()); 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 // remove mario if exists
removeMario(); removeMario();
g_mario = new_obj; g_mario = new_obj;
g_mario_pos = getSelectedObjectPosition(); g_mario_pos = getSelectedObjectPosition();
new_obj->getCollisions()[0]->setId(EDITOR_CHARACTER_ID); new_obj->getCollisions()[0]->setId( EDITOR_CHARACTER_ID );
z_index = scene.getObjects().size() - 1; z_index = scene.getObjects().size() - 1;
break; break;
default: default:
break; break;
} }
scene.addObject(new_obj); scene.addObject( new_obj );
scene.setZIndex(new_obj, z_index); scene.setZIndex( new_obj, z_index );
} }
} }
@ -280,19 +302,24 @@ void pollEvents( SDLPP::Scene &scene ) {
} }
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
getMousePositionFlags(scene); getMousePositionFlags( scene );
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if(std::get<MouseInfo::CUR_FLAGS>(g_mouse_info) == std::get<MouseInfo::PREV_FLAGS>(g_mouse_info)) { if ( std::get< MouseInfo::CUR_FLAGS >( g_mouse_info ) ==
mouseUpAction(std::get<MouseInfo::CUR_FLAGS>(g_mouse_info), scene); std::get< MouseInfo::PREV_FLAGS >( g_mouse_info ) ) {
mouseUpAction( std::get< MouseInfo::CUR_FLAGS >( g_mouse_info ),
scene );
} }
if(std::get<MouseInfo::EDIT_BOX>(g_mouse_info).getX() != -1) { if ( std::get< MouseInfo::EDIT_BOX >( g_mouse_info ).getX() !=
placeTool(scene); -1 ) {
placeTool( scene );
} }
if(std::get<MouseInfo::TOOL_BOX>(g_mouse_info).getX() != -1) { if ( std::get< MouseInfo::TOOL_BOX >( g_mouse_info ).getX() !=
auto &tool_box = std::get<MouseInfo::TOOL_BOX>(g_mouse_info); -1 ) {
auto &tool_box =
std::get< MouseInfo::TOOL_BOX >( g_mouse_info );
size_t index = tool_box.getY() * 4 + tool_box.getX(); size_t index = tool_box.getY() * 4 + tool_box.getX();
if(index < g_tools.size()) { if ( index < g_tools.size() ) {
g_current_block = g_current_tool_index * 8 + index; g_current_block = g_current_tool_index * 8 + index;
updateTool(); updateTool();
} }
@ -300,7 +327,8 @@ void pollEvents( SDLPP::Scene &scene ) {
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
// store current mouse flags in previous mouse flags // store current mouse flags in previous mouse flags
std::get<MouseInfo::PREV_FLAGS>(g_mouse_info) = std::get<MouseInfo::CUR_FLAGS>(g_mouse_info); std::get< MouseInfo::PREV_FLAGS >( g_mouse_info ) =
std::get< MouseInfo::CUR_FLAGS >( g_mouse_info );
break; break;
default: default:
break; break;
@ -351,159 +379,180 @@ int main() {
// grid // grid
for ( int i = 1; i < 20; i++ ) { for ( int i = 1; i < 20; i++ ) {
auto line_vertical = std::make_shared< SDLPP::LineRenderer >( auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
i * BLOCK_SIZE, 1 - 16 * BLOCK_SIZE, i * BLOCK_SIZE, 1.0, g_renderer, "#282828" ); i * BLOCK_SIZE, 1 - 16 * BLOCK_SIZE, i * BLOCK_SIZE, 1.0,
g_renderer, "#282828" );
line_vertical->setPermanent(); line_vertical->setPermanent();
line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject(line_vertical); scene->addObject( line_vertical );
if(i > 2) { if ( i > 2 ) {
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >( auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, 19 * BLOCK_SIZE, BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, 19 * BLOCK_SIZE,
( i + 1 ) * BLOCK_SIZE, g_renderer, "#282828" ); ( i + 1 ) * BLOCK_SIZE, g_renderer, "#282828" );
line_horizontal->setPermanent(); line_horizontal->setPermanent();
line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); line_horizontal->setAlignment( SDLPP::OBJ_CENTER,
scene->addObject(line_horizontal); SDLPP::OBJ_CENTER );
scene->addObject( line_horizontal );
} }
} }
// white rectangles // white rectangles
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >( auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true); 0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, g_renderer, "#FFFFFF88",
rectangle1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); true );
rectangle1->setId(EDITOR_LEFT_MAP_ID); rectangle1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle1->setId( EDITOR_LEFT_MAP_ID );
rectangle1->setPermanent(); rectangle1->setPermanent();
rectangle1->addCollision(SDLPP::RectColider(0, 0, 1, 1)); rectangle1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject(rectangle1); scene->addObject( rectangle1 );
// white rectangles // white rectangles
auto rectangle2 = std::make_shared< SDLPP::RectangleRender >( auto rectangle2 = std::make_shared< SDLPP::RectangleRender >(
19*BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true); 19 * BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE,
rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); g_renderer, "#FFFFFF88", true );
rectangle2->setId(EDITOR_RIGHT_MAP_ID); rectangle2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle2->setId( EDITOR_RIGHT_MAP_ID );
rectangle2->setPermanent(); rectangle2->setPermanent();
rectangle2->addCollision(SDLPP::RectColider(0, 0, 1, 1)); rectangle2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject(rectangle2); scene->addObject( rectangle2 );
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 ); auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
auto font_config = std::make_shared< SDLPP::FontConfiguration >( auto font_config = std::make_shared< SDLPP::FontConfiguration >(
font, "#000000", "#282828", 0.05 ); font, "#000000", "#282828", 0.05 );
auto left = std::make_shared< SDLPP::TextRenderer >( auto left = std::make_shared< SDLPP::TextRenderer >(
0, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<", font_config); 0, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<",
left->setId(0); font_config );
left->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); left->setId( 0 );
left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left->setPermanent(); left->setPermanent();
scene->addObject(left); scene->addObject( left );
auto right = std::make_shared< SDLPP::TextRenderer >( auto right = std::make_shared< SDLPP::TextRenderer >(
19*BLOCK_SIZE, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, ">", font_config); 19 * BLOCK_SIZE, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
right->setId(0); ">", font_config );
right->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); right->setId( 0 );
right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right->setPermanent(); right->setPermanent();
scene->addObject(right); scene->addObject( right );
for(int i = 0; i < 18; i++) { for ( int i = 0; i < 18; i++ ) {
for(int j = 0; j < 16; j++) { for ( int j = 0; j < 16; j++ ) {
scene->addObject(std::make_shared<EditBox>(i, j, g_renderer)); scene->addObject( std::make_shared< EditBox >( i, j, g_renderer ) );
} }
} }
auto mouse = auto mouse = std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0,
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0, g_renderer ); g_renderer );
mouse->setMinWidth(1); mouse->setMinWidth( 1 );
mouse->setMinHeight(1); mouse->setMinHeight( 1 );
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mouse->setId( EDITOR_MOUSE_ID ); mouse->setId( EDITOR_MOUSE_ID );
mouse->setColiderColor("#00FF00"); mouse->setColiderColor( "#00FF00" );
mouse->addCollision( mouse->addCollision( SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene->addObject( mouse ); scene->addObject( mouse );
g_current_max_index = g_objects.size() - 18; g_current_max_index = g_objects.size() - 18;
// tools // tools
g_max_tool_index = (possibleBlocks.size() - 1) / 8; g_max_tool_index = ( possibleBlocks.size() - 1 ) / 8;
for(int i = 0; i < 4; i++) { for ( int i = 0; i < 4; i++ ) {
auto tool_box1 = std::make_shared<ToolBox>(i, 0, g_renderer); auto tool_box1 = std::make_shared< ToolBox >( i, 0, g_renderer );
auto tool_box2 = std::make_shared<ToolBox>(i, 1, g_renderer); auto tool_box2 = std::make_shared< ToolBox >( i, 1, g_renderer );
scene->addObject(tool_box1); scene->addObject( tool_box1 );
scene->addObject(tool_box2); 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_box1->getPos().getX() <<
// std::cout << "TOOL BOX POS: " << tool_box2->getPos().getX() << ", " << tool_box2->getPos().getY() << std::endl; // ", " << 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; int tool_index = 0;
for(auto &block : possibleBlocks) { for ( auto &block : possibleBlocks ) {
if(block == MARIO_ID ) { if ( block == MARIO_ID ) {
g_tools.push_back(createMario(LandType::OVERWORLD, g_renderer, 0, 0)); g_tools.push_back(
createMario( LandType::OVERWORLD, g_renderer, 0, 0 ) );
} else { } else {
g_tools.push_back(createTerrainBlock(block, LandType::OVERWORLD, g_renderer, false)); g_tools.push_back( createTerrainBlock( block, LandType::OVERWORLD,
g_renderer, false ) );
} }
g_tools.back()->setHidden(true); g_tools.back()->setHidden( true );
g_tools.back()->setPermanent(); g_tools.back()->setPermanent();
auto x = tool_index % 4; auto x = tool_index % 4;
auto y = tool_index / 4; auto y = tool_index / 4;
// TODO add 14 and 1 as constants somewhere // TODO add 14 and 1 as constants somewhere
// TODO investigate when not permanent requires `-1` on x position // TODO investigate when not permanent requires `-1` on x position
g_tools.back()->setPos(14*BLOCK_SIZE + x*BLOCK_SIZE, BLOCK_SIZE + y*BLOCK_SIZE); g_tools.back()->setPos( 14 * BLOCK_SIZE + x * BLOCK_SIZE,
// std::cout << "TOOL POS: " << tools.back()->getPos().getX() << ", " << tools.back()->getPos().getY() << std::endl; BLOCK_SIZE + y * BLOCK_SIZE );
scene->addObject(g_tools.back()); // std::cout << "TOOL POS: " << tools.back()->getPos().getX() <<
tool_index = (tool_index + 1) % 8; // ", " << tools.back()->getPos().getY() << std::endl;
scene->addObject( g_tools.back() );
tool_index = ( tool_index + 1 ) % 8;
} }
for(int i = 0; i < 5; i++) { for ( int i = 0; i < 5; i++ ) {
auto line = std::make_shared<SDLPP::LineRenderer>( auto line = std::make_shared< SDLPP::LineRenderer >(
14*BLOCK_SIZE + i*BLOCK_SIZE, BLOCK_SIZE, 14 * BLOCK_SIZE + i*BLOCK_SIZE, 3*BLOCK_SIZE, g_renderer, "#282828"); 14 * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE,
14 * BLOCK_SIZE + i * BLOCK_SIZE, 3 * BLOCK_SIZE, g_renderer,
"#282828" );
line->setPermanent(); line->setPermanent();
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject(line); scene->addObject( line );
} }
for(int i = 0; i < 3; i++) { for ( int i = 0; i < 3; i++ ) {
auto line = std::make_shared<SDLPP::LineRenderer>( 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, g_renderer, "#282828"); 14 * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
14 * BLOCK_SIZE + 4 * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
g_renderer, "#282828" );
line->setPermanent(); line->setPermanent();
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject(line); scene->addObject( line );
} }
auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >( auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >(
13*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true); 13 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
tool_rect1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); "#FFFFFF88", true );
tool_rect1->setId(EDITOR_LEFT_TOOL_ID); tool_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect1->setId( EDITOR_LEFT_TOOL_ID );
tool_rect1->setPermanent(); tool_rect1->setPermanent();
tool_rect1->addCollision(SDLPP::RectColider(0, 0, 1, 1)); tool_rect1->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject(tool_rect1); scene->addObject( tool_rect1 );
// white rectangles // white rectangles
auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >( auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >(
18*BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer, "#FFFFFF88", true); 18 * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
tool_rect2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); "#FFFFFF88", true );
tool_rect2->setId(EDITOR_RIGHT_TOOL_ID); tool_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect2->setId( EDITOR_RIGHT_TOOL_ID );
tool_rect2->setPermanent(); tool_rect2->setPermanent();
tool_rect2->addCollision(SDLPP::RectColider(0, 0, 1, 1)); tool_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject(tool_rect2); scene->addObject( tool_rect2 );
auto left_tool = std::make_shared< SDLPP::TextRenderer >( auto left_tool = std::make_shared< SDLPP::TextRenderer >(
13*BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<", font_config); 13 * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
left_tool->setId(0); "<", font_config );
left_tool->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); left_tool->setId( 0 );
left_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left_tool->setPermanent(); left_tool->setPermanent();
scene->addObject(left_tool); scene->addObject( left_tool );
auto right_tool = std::make_shared< SDLPP::TextRenderer >( auto right_tool = std::make_shared< SDLPP::TextRenderer >(
18*BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, ">", font_config); 18 * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
right_tool->setId(0); ">", font_config );
right_tool->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); right_tool->setId( 0 );
right_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right_tool->setPermanent(); right_tool->setPermanent();
scene->addObject(right_tool); scene->addObject( right_tool );
g_placeholder_texture = std::make_shared< SDLPP::Texture >( g_placeholder_texture = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); g_renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_texture->setAlpha(100); g_placeholder_texture->setAlpha( 100 );
g_placeholder_mario = std::make_shared< SDLPP::Texture >( g_placeholder_mario = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY ); g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_mario->setAlpha(100); g_placeholder_mario->setAlpha( 100 );
g_current_tool = createTerrainBlock(possibleBlocks[g_current_block], LandType::OVERWORLD, g_renderer, g_placeholder_texture, false); g_current_tool = createTerrainBlock( possibleBlocks[g_current_block],
g_current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8)); LandType::OVERWORLD, g_renderer,
dynamic_cast<MarioBlock&>(*g_current_tool).setTool(); g_placeholder_texture, false );
scene->addObject(g_current_tool); g_current_tool->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
scene->moveZTop(g_current_tool); dynamic_cast< MarioBlock & >( *g_current_tool ).setTool();
scene->addObject( g_current_tool );
scene->moveZTop( g_current_tool );
scene->moveEverything(BLOCK_SIZE, 0); scene->moveEverything( BLOCK_SIZE, 0 );
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate( &gFPS ); SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 ); SDL_setFramerate( &gFPS, 60 );
updateToolSelection(0); updateToolSelection( 0 );
auto base = SDL_GetTicks(); auto base = SDL_GetTicks();
int frames = 0; int frames = 0;
@ -512,7 +561,7 @@ int main() {
while ( !g_quit ) { while ( !g_quit ) {
SDL_PumpEvents(); SDL_PumpEvents();
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
std::lock_guard<std::mutex> lock(g_destruction_mutex); std::lock_guard< std::mutex > lock( g_destruction_mutex );
scene->renderScene(); scene->renderScene();
g_renderer->presentRenderer(); g_renderer->presentRenderer();
frames++; frames++;
@ -521,33 +570,33 @@ int main() {
frames = 0; frames = 0;
base = SDL_GetTicks(); base = SDL_GetTicks();
} }
if(g_current_start_index == 0) { if ( g_current_start_index == 0 ) {
left->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05); left->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else { } else {
left->setTextColor(font, "#000000", "#282828", 0.05); left->setTextColor( font, "#000000", "#282828", 0.05 );
} }
if(g_current_start_index == g_current_max_index) { if ( g_current_start_index == g_current_max_index ) {
right->setTextColor(font, "#00FF00", "#000000", 0.1); right->setTextColor( font, "#00FF00", "#000000", 0.1 );
right->changeText("+"); right->changeText( "+" );
} else { } else {
right->setTextColor(font, "#000000", "#282828", 0.05); right->setTextColor( font, "#000000", "#282828", 0.05 );
right->changeText(">"); right->changeText( ">" );
} }
if(g_current_tool_index == 0) { if ( g_current_tool_index == 0 ) {
left_tool->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05); left_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else { } else {
left_tool->setTextColor(font, "#000000", "#282828", 0.05); left_tool->setTextColor( font, "#000000", "#282828", 0.05 );
} }
if(g_current_tool_index == g_max_tool_index) { if ( g_current_tool_index == g_max_tool_index ) {
right_tool->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05); right_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else { } else {
right_tool->setTextColor(font, "#000000", "#282828", 0.05); right_tool->setTextColor( font, "#000000", "#282828", 0.05 );
} }
if(g_update_size) { if ( g_update_size ) {
scene->updateSizeAndPosition(); scene->updateSizeAndPosition();
} }
} }
saveMap("test_binary2.bin", g_objects); saveMap( "test_binary2.bin", g_objects );
return 0; return 0;
} }

View File

@ -30,30 +30,30 @@ void MouseVisitor::visit( const SDLPP::RenderObject &obj ) {
break; break;
case EDITOR_EDIT_SQUARE: case EDITOR_EDIT_SQUARE:
edit_box = true; edit_box = true;
edit_box_location = dynamic_cast<const EditBox&>(obj).getIndexes(); edit_box_location = dynamic_cast< const EditBox & >( obj ).getIndexes();
break; break;
case EDITOR_TOOL_ID: case EDITOR_TOOL_ID:
tool_box = true; tool_box = true;
tool_box_location = dynamic_cast<const ToolBox&>(obj).getIndexes(); tool_box_location = dynamic_cast< const ToolBox & >( obj ).getIndexes();
break; break;
default: default:
break; break;
} }
} }
bool MouseVisitor::moveMapLeft(uint64_t flags) { bool MouseVisitor::moveMapLeft( uint64_t flags ) {
return flags & SELECTED_LEFT_MAP; return flags & SELECTED_LEFT_MAP;
} }
bool MouseVisitor::moveMapRight(uint64_t flags) { bool MouseVisitor::moveMapRight( uint64_t flags ) {
return flags & SELECTED_RIGHT_MAP; return flags & SELECTED_RIGHT_MAP;
} }
bool MouseVisitor::moveToolsLeft(uint64_t flags) { bool MouseVisitor::moveToolsLeft( uint64_t flags ) {
return flags & SELECTED_LEFT_TOOL; return flags & SELECTED_LEFT_TOOL;
} }
bool MouseVisitor::moveToolsRight(uint64_t flags) { bool MouseVisitor::moveToolsRight( uint64_t flags ) {
return flags & SELECTED_RIGHT_TOOL; return flags & SELECTED_RIGHT_TOOL;
} }
@ -62,12 +62,14 @@ void ToolVisitor::visit( const SDLPP::RenderObject &obj ) {
switch ( id ) { switch ( id ) {
case EDITOR_TERRAIN_ID: case EDITOR_TERRAIN_ID:
remove_block = true; remove_block = true;
if(obj.getId() == source_id && getVisitorType() == VisitorType::Terrain) { if ( obj.getId() == source_id &&
getVisitorType() == VisitorType::Terrain ) {
add_block = false; add_block = false;
} }
case EDITOR_CHARACTER_ID: case EDITOR_CHARACTER_ID:
remove_block = true; remove_block = true;
if(obj.getId() == source_id && getVisitorType() == VisitorType::Modifier) { if ( obj.getId() == source_id &&
getVisitorType() == VisitorType::Modifier ) {
add_block = false; add_block = false;
} }
default: default:

View File

@ -17,20 +17,22 @@ public:
MouseVisitor() {} MouseVisitor() {}
virtual void visit( const SDLPP::RenderObject &obj ) override; virtual void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t /*UNUSED*/ ) override {} virtual void setFromId( uint64_t /*UNUSED*/ ) override {}
virtual uint64_t getFromId() override {return 0;} virtual uint64_t getFromId() override {
return 0;
}
uint64_t getFlags() { uint64_t getFlags() {
return select_flags; return select_flags;
} }
bool foundEditBox() { bool foundEditBox() {
return edit_box; return edit_box;
} }
const SDLPP::Vec2D<int> &getEditBoxIndexes() { const SDLPP::Vec2D< int > &getEditBoxIndexes() {
return edit_box_location; return edit_box_location;
} }
bool foundToolBox() { bool foundToolBox() {
return tool_box; return tool_box;
} }
const SDLPP::Vec2D<int> &getToolBoxIndexes() { const SDLPP::Vec2D< int > &getToolBoxIndexes() {
return tool_box_location; return tool_box_location;
} }
virtual void setVisitorType( uint64_t type ) override { virtual void setVisitorType( uint64_t type ) override {
@ -40,23 +42,23 @@ public:
return _type; return _type;
} }
static bool moveMapLeft(uint64_t flags); static bool moveMapLeft( uint64_t flags );
static bool moveMapRight(uint64_t flags); static bool moveMapRight( uint64_t flags );
static bool moveToolsLeft(uint64_t flags); static bool moveToolsLeft( uint64_t flags );
static bool moveToolsRight(uint64_t flags); static bool moveToolsRight( uint64_t flags );
private: private:
uint64_t select_flags = 0; uint64_t select_flags = 0;
bool edit_box = false; bool edit_box = false;
bool tool_box = false; bool tool_box = false;
SDLPP::Vec2D<int> edit_box_location = {-1, -1}; SDLPP::Vec2D< int > edit_box_location = { -1, -1 };
SDLPP::Vec2D<int> tool_box_location = {-1, -1}; SDLPP::Vec2D< int > tool_box_location = { -1, -1 };
uint64_t _type; uint64_t _type;
}; };
class ToolVisitor : public SDLPP::Visitor { class ToolVisitor : public SDLPP::Visitor {
public: public:
ToolVisitor() {}; ToolVisitor(){};
virtual void visit( const SDLPP::RenderObject &obj ) override; virtual void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t id ) override { virtual void setFromId( uint64_t id ) override {
source_id = id; source_id = id;

View File

@ -7,126 +7,149 @@
#include "objectids.hpp" #include "objectids.hpp"
// TODO move to one function // TODO move to one function
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::RectangleRender> mario, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer) { void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
std::shared_ptr< SDLPP::RectangleRender > mario,
const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer ) {
std::ifstream map_file; std::ifstream map_file;
map_file.open(file, std::ios::in | std::ios::binary); map_file.open( file, std::ios::in | std::ios::binary );
uint16_t cols; uint16_t cols;
map_file.read((char*)&cols, sizeof(uint16_t)/sizeof(char)); map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
for(uint16_t i = 0; i < cols; i++) { for ( uint16_t i = 0; i < cols; i++ ) {
for(int j = 0; j < 16; j++) { for ( int j = 0; j < 16; j++ ) {
uint16_t input_number; uint16_t input_number;
uint8_t additional_data = 0; uint8_t additional_data = 0;
uint8_t character_type = 0, character = 0, modifier_type = 0, modifier_data = 0; uint8_t character_type = 0, character = 0, modifier_type = 0,
map_file.read((char *)&input_number, sizeof(uint16_t)/sizeof(char)); modifier_data = 0;
uint8_t type = (input_number & 0xF000)>>12; map_file.read( ( char * )&input_number,
uint16_t id = (input_number & 0x0FFF) | BLOCK_PREFIX; sizeof( uint16_t ) / sizeof( char ) );
if(type & 0x8) { uint8_t type = ( input_number & 0xF000 ) >> 12;
map_file.read((char *)&additional_data, sizeof(uint8_t)/sizeof(char)); uint16_t id = ( input_number & 0x0FFF ) | BLOCK_PREFIX;
if ( type & 0x8 ) {
map_file.read( ( char * )&additional_data,
sizeof( uint8_t ) / sizeof( char ) );
type &= ~0x8; type &= ~0x8;
if(additional_data & 0x80) { if ( additional_data & 0x80 ) {
//modifier // modifier
additional_data &= ~0x80; additional_data &= ~0x80;
modifier_type = (additional_data & 0xF0)>>4; modifier_type = ( additional_data & 0xF0 ) >> 4;
modifier_data = additional_data & 0x0F; modifier_data = additional_data & 0x0F;
} else { } else {
// character // character
character_type = (additional_data & 0xF0)>>4; character_type = ( additional_data & 0xF0 ) >> 4;
character = additional_data & 0x0F; character = additional_data & 0x0F;
} }
} }
bool collision = false; bool collision = false;
if(id == FLOOR_ID) { if ( id == FLOOR_ID ) {
collision = true; collision = true;
} }
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock
auto obj = createTerrainBlock(id, static_cast<LandType::Value>(type), renderer, i, j, collision); auto obj =
if(obj != nullptr) createTerrainBlock( id, static_cast< LandType::Value >( type ),
scene->addObject(obj); renderer, i, j, collision );
if(character) { if ( obj != nullptr )
if(character == MARIO_ID) { scene->addObject( obj );
mario->setPos(i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE); if ( character ) {
if ( character == MARIO_ID ) {
mario->setPos( i * BLOCK_SIZE,
1 - ( 16 - j ) * BLOCK_SIZE );
} }
} }
} }
} }
scene->moveZTop(mario); scene->moveZTop( mario );
} }
// editor loader // editor loader
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<mapColumnType> &objects) { void loadMap( std::shared_ptr< SDLPP::Scene > &scene, const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer,
std::vector< mapColumnType > &objects ) {
std::ifstream map_file; std::ifstream map_file;
map_file.open(file, std::ios::in | std::ios::binary); map_file.open( file, std::ios::in | std::ios::binary );
uint16_t cols; uint16_t cols;
map_file.read((char*)&cols, sizeof(uint16_t)/sizeof(char)); map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
objects.resize(cols); objects.resize( cols );
for(uint16_t i = 0; i < cols; i++) { for ( uint16_t i = 0; i < cols; i++ ) {
auto &col = objects[i]; auto &col = objects[i];
for(int j = 0; j < 16; j++) { for ( int j = 0; j < 16; j++ ) {
uint16_t input_number; uint16_t input_number;
uint8_t additional_data = 0; uint8_t additional_data = 0;
uint8_t character_type = 0, character = 0, modifier_type = 0, modifier_data = 0; uint8_t character_type = 0, character = 0, modifier_type = 0,
map_file.read((char *)&input_number, sizeof(uint16_t)/sizeof(char)); modifier_data = 0;
uint8_t type = (input_number & 0xF000)>>12; map_file.read( ( char * )&input_number,
uint16_t id = (input_number & 0x0FFF) | BLOCK_PREFIX; sizeof( uint16_t ) / sizeof( char ) );
if(type & 0x8) { uint8_t type = ( input_number & 0xF000 ) >> 12;
map_file.read((char *)&additional_data, sizeof(uint8_t)/sizeof(char)); uint16_t id = ( input_number & 0x0FFF ) | BLOCK_PREFIX;
if ( type & 0x8 ) {
map_file.read( ( char * )&additional_data,
sizeof( uint8_t ) / sizeof( char ) );
type &= ~0x8; type &= ~0x8;
if(additional_data & 0x80) { if ( additional_data & 0x80 ) {
//modifier // modifier
additional_data &= ~0x80; additional_data &= ~0x80;
modifier_type = (additional_data & 0xF0)>>4; modifier_type = ( additional_data & 0xF0 ) >> 4;
modifier_data = additional_data & 0x0F; modifier_data = additional_data & 0x0F;
} else { } else {
// character // character
character_type = (additional_data & 0xF0)>>4; character_type = ( additional_data & 0xF0 ) >> 4;
character = additional_data & 0x0F; character = additional_data & 0x0F;
} }
} }
col[j] = {type, id, character_type, character, modifier_type, modifier_data}; col[j] = { type, id, character_type, character,
modifier_type, modifier_data };
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock
auto obj = createTerrainBlock(id, static_cast<LandType::Value>(type), renderer, i, j, true); auto obj =
obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID); createTerrainBlock( id, static_cast< LandType::Value >( type ),
scene->addObject(obj); renderer, i, j, true );
if(character) { obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
if(character == MARIO_ID) { scene->addObject( obj );
scene->addObject(createMario(static_cast<LandType::Value>(character_type), renderer, i, j)); if ( character ) {
if ( character == MARIO_ID ) {
scene->addObject( createMario(
static_cast< LandType::Value >( character_type ),
renderer, i, j ) );
} }
} }
} }
} }
if(objects.size() < 18) { if ( objects.size() < 18 ) {
objects.resize(18); objects.resize( 18 );
} }
} }
// tuple - world object type, object, world character type, character, modifier type, modifier data // tuple - world object type, object, world character type, character, modifier
void saveMap(const std::string &file, std::vector<mapColumnType> &objects) { // type, modifier data
void saveMap( const std::string &file, std::vector< mapColumnType > &objects ) {
std::ofstream output_file; std::ofstream output_file;
output_file.open(file, std::ios::out | std::ios::binary); output_file.open( file, std::ios::out | std::ios::binary );
uint16_t cols = objects.size(); uint16_t cols = objects.size();
output_file.write((char*)&cols, sizeof(uint16_t)/sizeof(char)); output_file.write( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
for(auto &col : objects) { for ( auto &col : objects ) {
for(int i = 0; i < 16; i++) { for ( int i = 0; i < 16; i++ ) {
auto &obj = col[i]; auto &obj = col[i];
uint16_t wide_type = std::get<0>(obj); uint16_t wide_type = std::get< 0 >( obj );
wide_type = wide_type<<12; wide_type = wide_type << 12;
uint16_t write_num = (0x0FFF & std::get<1>(obj)) | wide_type; uint16_t write_num = ( 0x0FFF & std::get< 1 >( obj ) ) | wide_type;
// 3 becuase character type can be 0 (overworld), 4 because modifier data can be 0 (breakable) // 3 becuase character type can be 0 (overworld), 4 because modifier
if(std::get<3>(obj) || std::get<4>(obj)) { // data can be 0 (breakable)
if ( std::get< 3 >( obj ) || std::get< 4 >( obj ) ) {
write_num |= 0x8000; write_num |= 0x8000;
} }
output_file.write((char*)&write_num, sizeof(uint16_t)/sizeof(char)); output_file.write( ( char * )&write_num,
sizeof( uint16_t ) / sizeof( char ) );
uint8_t additional_data = 0; uint8_t additional_data = 0;
if(std::get<3>(obj)) { if ( std::get< 3 >( obj ) ) {
additional_data |= std::get<2>(obj)<<4; additional_data |= std::get< 2 >( obj ) << 4;
additional_data |= std::get<3>(obj); additional_data |= std::get< 3 >( obj );
} else if(std::get<4>(obj)) { } else if ( std::get< 4 >( obj ) ) {
additional_data |= std::get<4>(obj)<<4; additional_data |= std::get< 4 >( obj ) << 4;
additional_data |= 0x80; additional_data |= 0x80;
additional_data |= std::get<5>(obj); additional_data |= std::get< 5 >( obj );
} }
if(additional_data) { if ( additional_data ) {
output_file.write((char*)&additional_data, sizeof(uint8_t)/sizeof(char)); output_file.write( ( char * )&additional_data,
sizeof( uint8_t ) / sizeof( char ) );
} }
} }
} }

View File

@ -15,11 +15,17 @@ struct MapObject {
}; };
}; };
typedef std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t> mapObjectType; typedef std::tuple< uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t >
typedef std::array<mapObjectType,16> mapColumnType; mapObjectType;
typedef std::array< mapObjectType, 16 > mapColumnType;
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::RectangleRender> mario, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer); void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<mapColumnType> &objects); std::shared_ptr< SDLPP::RectangleRender > mario,
void saveMap(const std::string &file, std::vector<mapColumnType> &objects); const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer );
void loadMap( std::shared_ptr< SDLPP::Scene > &scene, const std::string &file,
std::shared_ptr< SDLPP::Renderer > &renderer,
std::vector< mapColumnType > &objects );
void saveMap( const std::string &file, std::vector< mapColumnType > &objects );
#endif #endif

View File

@ -64,7 +64,6 @@
// modifiers // modifiers
// character IDs // character IDs
#define MARIO_ID 0x0F #define MARIO_ID 0x0F