Mario editor: use visitors

This commit is contained in:
zvon 2021-05-02 14:14:11 +02:00
parent 34324d3054
commit 96857a99af
11 changed files with 268 additions and 100 deletions

View File

@ -18,7 +18,7 @@ endif
COMMON_OBJECTS = blocks.${OBJEXT} global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT} COMMON_OBJECTS = blocks.${OBJEXT} global_vars.${OBJEXT} sprites.${OBJEXT} maploader.${OBJEXT}
MARIO_OBJECTS = mario.${OBJEXT} mario_visitor.${OBJEXT} ${COMMON_OBJECTS} MARIO_OBJECTS = mario.${OBJEXT} mario_visitor.${OBJEXT} ${COMMON_OBJECTS}
EDITOR_OBJECTS = editor.${OBJEXT} ${COMMON_OBJECTS} EDITOR_OBJECTS = editor.${OBJEXT} edit_box.${OBJEXT} editor_visitor.${OBJEXT} ${COMMON_OBJECTS}
ifeq ($(UNAME_S),Linux) ifeq ($(UNAME_S),Linux)
MARIO_OBJECTS += libsdlpp.so MARIO_OBJECTS += libsdlpp.so
@ -61,6 +61,10 @@ mario_visitor.${OBJEXT}: mario_visitor.cpp ../sdlpp/sdlpp.hpp mario_visitor.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
editor.${OBJEXT}: editor.cpp ../sdlpp/sdlpp.hpp sprites.hpp editor.${OBJEXT}: editor.cpp ../sdlpp/sdlpp.hpp sprites.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $< $(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
edit_box.${OBJEXT}: edit_box.cpp ../sdlpp/sdlpp.hpp sprites.hpp edit_box.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
editor_visitor.${OBJEXT}: editor_visitor.cpp ../sdlpp/sdlpp.hpp sprites.hpp editor_visitor.hpp
$(CXX) $(CXXFLAGS) -c ${OUTPUTFLAG}$@ $<
libsdlpp.so: ../sdlpp libsdlpp.so: ../sdlpp
$(MAKE) clean -C ../sdlpp $(MAKE) clean -C ../sdlpp
$(MAKE) -C ../sdlpp $(MAKE) -C ../sdlpp

View File

@ -4,6 +4,17 @@
#include "sprites.hpp" #include "sprites.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 ) {}
void MarioBlock::visit( SDLPP::Visitor &visitor ) {
if(!_tool && visitor.getVisitorType() == TOOL_VISITOR_TYPE) {
destroy();
}
visitor.visit(*this);
}
void MarioBlock::setTool(bool tool) {
_tool = tool;
}
const std::vector< uint64_t > possibleBlocks = { FLOOR_ID, const std::vector< uint64_t > possibleBlocks = { FLOOR_ID,
HILL_INCLINE_ID, HILL_INCLINE_ID,
HILL_DECLINE_ID, HILL_DECLINE_ID,
@ -33,11 +44,10 @@ const std::unordered_map<uint64_t, const SDL_Rect*> block_mapping = {
}; };
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createBlock( std::shared_ptr< SDLPP::Renderer > &renderer, double x, double 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< SDLPP::RectangleRender >( auto block = std::make_shared<MarioBlock>(x, y, renderer, texture, src);
x, y, BLOCK_SIZE, BLOCK_SIZE, 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();
@ -70,7 +80,7 @@ SDL_Rect getSourceRectByID( uint64_t id, BlockType type ) {
} }
return ret_src; return ret_src;
} }
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y, std::shared_ptr<SDLPP::Texture> texture, bool collision ) { std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType 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 );
@ -81,8 +91,8 @@ std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, B
std::shared_ptr< SDLPP::RectangleRender > std::shared_ptr< SDLPP::RectangleRender >
createTerrainBlock( uint64_t block_id, BlockType type, createTerrainBlock( uint64_t block_id, BlockType type,
std::shared_ptr< SDLPP::Renderer > &renderer, double x, std::shared_ptr< SDLPP::Renderer > &renderer, int x,
double y, bool collision ) { int y, 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);
} }
@ -93,7 +103,7 @@ createTerrainBlock( uint64_t block_id, BlockType type,
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( BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y ) { std::shared_ptr<SDLPP::RectangleRender> createMario( BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y ) {
//TODO add type additions //TODO add type additions
return createBlock( renderer, x, y, g_mario_texture, MARIO_STANDING_SRC, MARIO_ID, true ); return createBlock( renderer, x, y, g_mario_texture, MARIO_STANDING_SRC, MARIO_ID, true );
} }

View File

@ -4,6 +4,15 @@
#include "../sdlpp/sdlpp_rectrenderer.hpp" #include "../sdlpp/sdlpp_rectrenderer.hpp"
#include <memory> #include <memory>
class MarioBlock : public SDLPP::RectangleRender {
public:
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 setTool(bool tool = true);
private:
bool _tool = false;
};
extern const std::vector<uint64_t> possibleBlocks; extern const std::vector<uint64_t> possibleBlocks;
enum BlockType { enum BlockType {
@ -14,9 +23,9 @@ enum BlockType {
}; };
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, bool collision = false ); std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, bool collision = false );
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y, bool collision = false ); std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y, bool collision = false );
std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType 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, BlockType 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, BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y, std::shared_ptr<SDLPP::Texture> texture, bool collision = false ); std::shared_ptr<SDLPP::RectangleRender> createTerrainBlock( uint64_t block_id, BlockType 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( BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y ); std::shared_ptr<SDLPP::RectangleRender> createMario( BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, int x, int y );
#endif #endif

19
mario/edit_box.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "edit_box.hpp"
EditBox::EditBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(BLOCK_SIZE + x*BLOCK_SIZE, 4*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;
_y = y;
setId(EDITOR_EDIT_SQUARE);
setColiderColor("#FF00AA");
setPermanent();
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
addCollision(SDLPP::RectColider(0,0,1,1));
}
SDLPP::Vec2D<int> EditBox::getIndexes() const {
return {_x, _y};
}
void EditBox::visit( SDLPP::Visitor &visitor ) {
visitor.visit( *this );
}

14
mario/edit_box.hpp Normal file
View File

@ -0,0 +1,14 @@
#include "../sdlpp/sdlpp.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "sprites.hpp"
class EditBox : public SDLPP::RectangleRender {
public:
EditBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer);
SDLPP::Vec2D<int> getIndexes() const;
void visit( SDLPP::Visitor &visitor ) override;
private:
int _x;
int _y;
};

View File

@ -10,47 +10,27 @@
#endif // UNIX #endif // UNIX
#include <thread> #include <thread>
#include <mutex>
#include "global_vars.hpp" #include "global_vars.hpp"
#include "objectids.hpp" #include "objectids.hpp"
#include "blocks.hpp" #include "blocks.hpp"
#include "maploader.hpp" #include "maploader.hpp"
#include "../sdlpp/sdlpp_mouse.hpp" #include "../sdlpp/sdlpp_mouse.hpp"
#include "edit_box.hpp"
#define EDIT_SQUARE 0xF001 #include "editor_visitor.hpp"
#define MOUSE_ID 0xF002
#define LEFT_ID 0xF003
#define RIGHT_ID 0xF004
std::shared_ptr< SDLPP::Renderer > renderer = nullptr; std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
bool quit = false; bool quit = false;
std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> objects = {};
bool update_size = false; bool update_size = false;
bool selected_left = false; std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> objects = {};
bool selected_right = false; uint64_t current_selected_flags = 0;
uint64_t previous_selected_flags = 0;
std::mutex destruction_mutex;
int current_start_index = 0; int current_start_index = 0;
int current_max_index = 0; int current_max_index = 0;
std::pair<int, int> current_box = {0, 0}; SDLPP::Vec2D<int> current_box = {0, 0};
std::pair<int, int> backup_box = {0, 0}; std::shared_ptr<SDLPP::RenderObject> current_tool = nullptr;
std::shared_ptr<SDLPP::RenderObject> placeholderGround = nullptr;
class EditBox : public SDLPP::RectangleRender {
public:
EditBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(BLOCK_SIZE + x*BLOCK_SIZE, 4*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;
_y = y;
setId(EDIT_SQUARE);
setColiderColor("#FF00AA");
setPermanent();
setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
addCollision(SDLPP::RectColider(0,0,1,1));
}
std::pair<int, int> getIndexes() {
return {_x, _y};
}
private:
int _x;
int _y;
};
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) { switch ( key ) {
@ -107,59 +87,48 @@ void pollEvents( SDLPP::Scene &scene ) {
} }
break; break;
case SDL_MOUSEMOTION: { case SDL_MOUSEMOTION: {
auto mouse = scene.getObjects({MOUSE_ID})[0]; auto mouse = scene.getObjects({EDITOR_MOUSE_ID})[0];
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));
auto collisions = scene.getCollisions(*mouse); MouseVisitor visitor;
selected_left = false; scene.visitCollisions(*mouse, visitor);
selected_right = false; current_selected_flags = visitor.getFlags();
current_box = {-1, -1}; current_box = visitor.getEditBoxIndexes();
if(collisions.size() >= 2) { if(visitor.foundEditBox()) {
for(auto &collision : collisions) { current_tool->setPos(BLOCK_SIZE + current_box.getX() * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.getY() * BLOCK_SIZE);
if(collision.second->getId() == EDIT_SQUARE) {
current_box = dynamic_cast<EditBox&>(*collision.second).getIndexes();
placeholderGround->setPos(BLOCK_SIZE + current_box.first * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.second * BLOCK_SIZE);
} else if (collision.second->getId() == LEFT_ID) {
selected_left = true;
} else if (collision.second->getId() == RIGHT_ID) {
selected_right = true;
}
}
} }
} }
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if(selected_left && current_start_index != 0) { if(previous_selected_flags == current_selected_flags && MouseVisitor::moveMapLeft(current_selected_flags) && current_start_index != 0) {
current_start_index -= 1; current_start_index -= 1;
scene.moveEverything(BLOCK_SIZE, 0); scene.moveEverything(BLOCK_SIZE, 0);
} }
if(selected_right && current_start_index != current_max_index) { if(previous_selected_flags == current_selected_flags && MouseVisitor::moveMapRight(current_selected_flags) && current_start_index != current_max_index) {
current_start_index += 1; current_start_index += 1;
scene.moveEverything(-BLOCK_SIZE,0); scene.moveEverything(-BLOCK_SIZE,0);
} }
if(current_box == backup_box && current_box.first != -1) { if(current_box.getX() != -1) {
auto mouse = scene.getObjects({MOUSE_ID})[0]; std::lock_guard<std::mutex> lock(destruction_mutex);
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
auto collisions = scene.getCollisions(*mouse); ToolVisitor visitor;
std::cout << "BOUT TO" << std::endl; visitor.setVisitorType(TOOL_VISITOR_TYPE);
bool deleted = false; scene.visitCollisions(*current_tool, visitor);
for(auto &collision : collisions) { if(visitor.removeBlock()) {
if(collision.second->getId() == FLOOR_ID) { // TODO check if modifier
std::cout << "DELETING" << std::endl; objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, 0, 0, 0, 0, 0};
collision.second->destroy();
objects[current_start_index + current_box.first][current_box.second] = {OVERWORLD, 0, 0, 0, 0, 0};
deleted = true;
break;
} }
if(visitor.addBlock()) {
// TODO check if modifier
objects[current_start_index + current_box.getX()][current_box.getY()] = {OVERWORLD, current_tool->getId(), 0, 0, 0, 0};
auto obj = createTerrainBlock(current_tool->getId(), OVERWORLD, renderer, 1 + current_box.getX(), current_box.getY(), true);
obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
scene.addObject(obj);
scene.setZIndex(obj, 1);
} }
if(deleted)
break;
std::cout << "ADDING" << std::endl;
objects[current_start_index + current_box.first][current_box.second] = {OVERWORLD, FLOOR_ID, 0, 0, 0, 0};
scene.addObject(createTerrainBlock(FLOOR_ID, OVERWORLD, renderer, BLOCK_SIZE + current_box.first * BLOCK_SIZE, 4*BLOCK_SIZE + current_box.second * BLOCK_SIZE, true));
} }
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
backup_box = current_box; previous_selected_flags = current_selected_flags;
break; break;
default: default:
break; break;
@ -227,7 +196,7 @@ int main() {
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >( auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, renderer, "#FFFFFF88", true); 0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
rectangle1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); rectangle1->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
rectangle1->setId(LEFT_ID); 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);
@ -235,7 +204,7 @@ int main() {
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, renderer, "#FFFFFF88", true); 19*BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); rectangle2->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
rectangle2->setId(RIGHT_ID); 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);
@ -245,13 +214,13 @@ int main() {
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, renderer, "<", font_config); 0, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, "<", font_config);
left->setId(LEFT_ID); left->setId(0);
left->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); 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, renderer, ">", font_config); 19*BLOCK_SIZE, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer, ">", font_config);
right->setId(RIGHT_ID); right->setId(0);
right->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); right->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
right->setPermanent(); right->setPermanent();
scene->addObject(right); scene->addObject(right);
@ -263,9 +232,11 @@ int main() {
} }
auto mouse = auto mouse =
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0.01, 0.01, renderer ); std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0, renderer );
mouse->setMinWidth(1);
mouse->setMinHeight(1);
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mouse->setId( 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 } ) );
@ -274,11 +245,12 @@ int main() {
auto placeholder_texture = std::make_shared< SDLPP::Texture >( auto placeholder_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
placeholderGround = createTerrainBlock(FLOOR_ID, OVERWORLD, renderer, placeholder_texture, true); current_tool = createTerrainBlock(FLOOR_ID, OVERWORLD, renderer, placeholder_texture, false);
placeholderGround->setTextureAlpha(100); current_tool->addCollision(SDLPP::RectColider(0.1, 0.1, 0.8, 0.8));
placeholderGround->setId(0); current_tool->setTextureAlpha(100);
scene->addObject(placeholderGround); dynamic_cast<MarioBlock&>(*current_tool).setTool();
scene->moveZTop(placeholderGround); scene->addObject(current_tool);
scene->moveZTop(current_tool);
scene->moveEverything(BLOCK_SIZE, 0); scene->moveEverything(BLOCK_SIZE, 0);
FPSmanager gFPS; FPSmanager gFPS;
@ -292,6 +264,7 @@ int main() {
while ( !quit ) { while ( !quit ) {
SDL_PumpEvents(); SDL_PumpEvents();
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
std::lock_guard<std::mutex> lock(destruction_mutex);
scene->renderScene(); scene->renderScene();
renderer->presentRenderer(); renderer->presentRenderer();
frames++; frames++;

56
mario/editor_visitor.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "editor_visitor.hpp"
#include "../sdlpp/sdlpp_renderobject.hpp"
#include "objectids.hpp"
#include "edit_box.hpp"
#define SELECTED_LEFT_MAP 0x00000001
#define SELECTED_RIGHT_MAP 0x00000002
#define SELECTED_LEFT_SELECT 0x00000004
#define SELECTED_RIGHT_SELECT 0x00000004
#define SELECTED_REMOVE_BLOCK 0x00000008
#define SELECTED_REMOVE_MODIFIER 0x00000010
void MouseVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getId();
switch ( id ) {
case EDITOR_LEFT_MAP_ID:
select_flags |= SELECTED_LEFT_MAP;
break;
case EDITOR_RIGHT_MAP_ID:
select_flags |= SELECTED_RIGHT_MAP;
break;
case EDITOR_EDIT_SQUARE:
edit_box = true;
edit_box_location = dynamic_cast<const EditBox&>(obj).getIndexes();
break;
default:
break;
}
}
bool MouseVisitor::moveMapLeft(uint64_t flags) {
return flags & SELECTED_LEFT_MAP;
}
bool MouseVisitor::moveMapRight(uint64_t flags) {
return flags & SELECTED_RIGHT_MAP;
}
void ToolVisitor::visit( const SDLPP::RenderObject &obj ) {
auto id = obj.getCollisions()[0]->getId();
switch ( id ) {
case EDITOR_TERRAIN_ID:
remove_block = true;
if(obj.getId() == source_id) {
add_block = false;
}
default:
break;
}
}
bool ToolVisitor::addBlock() {
return add_block;
}
bool ToolVisitor::removeBlock() {
return remove_block;
}

66
mario/editor_visitor.hpp Normal file
View File

@ -0,0 +1,66 @@
#ifndef EDITOR_VISITOR_H
#define EDITOR_VISITOR_H
#include "../sdlpp/sdlpp_visitor.hpp"
#include "../sdlpp/sdlpp_geometry.hpp"
#include <memory>
class MouseVisitor : public SDLPP::Visitor {
public:
MouseVisitor() {}
virtual void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t /*UNUSED*/ ) override {}
virtual uint64_t getFromId() override {return 0;}
uint64_t getFlags() {
return select_flags;
}
bool foundEditBox() {
return edit_box;
}
SDLPP::Vec2D<int> getEditBoxIndexes() {
return edit_box_location;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
virtual uint64_t getVisitorType() override {
return _type;
}
static bool moveMapLeft(uint64_t flags);
static bool moveMapRight(uint64_t flags);
private:
uint64_t select_flags = 0;
bool edit_box = false;
SDLPP::Vec2D<int> edit_box_location = {-1, -1};
uint64_t _type;
};
class ToolVisitor : public SDLPP::Visitor {
public:
ToolVisitor() {};
virtual void visit( const SDLPP::RenderObject &obj ) override;
virtual void setFromId( uint64_t id ) override {
source_id = id;
}
virtual uint64_t getFromId() override {
return source_id;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
virtual uint64_t getVisitorType() override {
return _type;
}
bool addBlock();
bool removeBlock();
private:
bool remove_block = false;
bool add_block = true;
uint64_t source_id = 0;
uint64_t _type = 0;
};
#endif

View File

@ -38,7 +38,7 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::Rectan
collision = true; collision = true;
} }
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock
auto obj = createTerrainBlock(id, static_cast<BlockType>(type), renderer, i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE, collision); auto obj = createTerrainBlock(id, static_cast<BlockType>(type), renderer, i, j, collision);
if(obj != nullptr) if(obj != nullptr)
scene->addObject(obj); scene->addObject(obj);
if(character) { if(character) {
@ -51,6 +51,7 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::Rectan
scene->moveZTop(mario); scene->moveZTop(mario);
} }
// editor loader
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> &objects) { void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> &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);
@ -81,17 +82,13 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std:
} }
} }
col[j] = {type, id, character_type, character, modifier_type, modifier_data}; col[j] = {type, id, character_type, character, modifier_type, modifier_data};
bool collision = false;
if(id == FLOOR_ID) {
collision = true;
}
// TODO add modifiers to createTerrainBlock // TODO add modifiers to createTerrainBlock
auto obj = createTerrainBlock(id, static_cast<BlockType>(type), renderer, i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE, collision); auto obj = createTerrainBlock(id, static_cast<BlockType>(type), renderer, i, j, true);
if(obj != nullptr) obj->getCollisions()[0]->setId(EDITOR_TERRAIN_ID);
scene->addObject(obj); scene->addObject(obj);
if(character) { if(character) {
if(character == MARIO_ID) { if(character == MARIO_ID) {
scene->addObject(createMario(static_cast<BlockType>(character_type), renderer, i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE)); scene->addObject(createMario(static_cast<BlockType>(character_type), renderer, i, j));
} }
} }
} }

View File

@ -20,9 +20,18 @@ public:
double newXPos() { double newXPos() {
return newX; return newX;
} }
virtual void fromId( uint64_t id ) override { virtual void setFromId( uint64_t id ) override {
from = id; from = id;
} }
virtual uint64_t getFromId() override {
return from;
}
virtual void setVisitorType( uint64_t type ) override {
_type = type;
}
virtual uint64_t getVisitorType() override {
return _type;
}
bool canGoLeft() { bool canGoLeft() {
return !left; return !left;
} }
@ -38,6 +47,7 @@ private:
uint64_t from = -1; uint64_t from = -1;
bool left = false; bool left = false;
bool right = false; bool right = false;
uint64_t _type = 0;
}; };
#endif #endif

View File

@ -29,4 +29,14 @@
#define MARIO_LEFT_SIDE_DETECT 0x2002 #define MARIO_LEFT_SIDE_DETECT 0x2002
#define MARIO_RIGHT_SIDE_DETECT 0x2003 #define MARIO_RIGHT_SIDE_DETECT 0x2003
#define EDITOR_EDIT_SQUARE 0xF001
#define EDITOR_MOUSE_ID 0xF002
#define EDITOR_LEFT_MAP_ID 0xF003
#define EDITOR_RIGHT_MAP_ID 0xF004
#define EDITOR_TERRAIN_ID 0xF005
#define TOOL_VISITOR_TYPE 0xE001
#define MOUSE_VISITOR_TYPE 0xE002
#define MARIO_VISITOR_TYPE 0xE003
#endif #endif