Mario editor: more refactoring

This commit is contained in:
zv0n 2021-05-09 20:45:52 +02:00
parent ede8bbbe8b
commit d80ae9a4e2
5 changed files with 33 additions and 31 deletions

View File

@ -3,7 +3,7 @@
#include "blocks.hpp"
#include "sprites.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) {
EditBox::EditBox(int x, int y, int map_width, int map_height, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(BLOCK_SIZE + x*BLOCK_SIZE, (map_width - map_height + 2)*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;
_y = y;
setId(EDITOR_EDIT_SQUARE);

View File

@ -5,7 +5,7 @@
class EditBox : public SDLPP::RectangleRender {
public:
EditBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer);
EditBox(int x, int y, int map_width, int map_height, std::shared_ptr<SDLPP::Renderer> renderer);
virtual SDLPP::Vec2D<int> getIndexes() const;
virtual void visit( SDLPP::Visitor &visitor ) override;
private:

View File

@ -21,6 +21,9 @@
#include "editor_visitor.hpp"
#include "tool_box.hpp"
#define MAP_WIDTH 50
#define MAP_HEIGHT 16
struct MouseInfo {
enum Index {
CUR_FLAGS = 0,
@ -186,7 +189,7 @@ void getMousePositionFlags( SDLPP::Scene &scene ) {
if ( visitor.foundEditBox() ) {
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 );
(MAP_WIDTH - MAP_HEIGHT + 2) * BLOCK_SIZE + box.getY() * BLOCK_SIZE );
}
}
@ -198,8 +201,7 @@ void mouseUpAction( uint64_t flags, SDLPP::Scene &scene ) {
if ( MouseVisitor::moveMapRight( flags ) ) {
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == std::get<MapInfo::MAX_PAGE>(g_map_info) ) {
// add column
// TODO 18 as constant
g_objects.resize( std::get<MapInfo::MAX_PAGE>(g_map_info) + 18 + 1 );
g_objects.resize( g_objects.size() + 1 );
std::get<MapInfo::MAX_PAGE>(g_map_info)++;
}
std::get<MapInfo::CUR_PAGE>(g_map_info) += 1;
@ -369,7 +371,7 @@ int main() {
SDLPP::init();
SDLPP::Window w( "Mario editor!" );
w.setResizable( true );
BLOCK_SIZE = 1.0 / 20;
BLOCK_SIZE = 1.0 / (MAP_WIDTH + 2);
g_renderer = std::make_shared< SDLPP::Renderer >( w );
g_renderer->setBlendMode( SDL_BLENDMODE_BLEND );
@ -390,16 +392,16 @@ int main() {
loadMap( scene, "test_binary.bin", g_renderer, g_objects );
// grid
for ( int i = 1; i < 20; i++ ) {
for ( int i = 1; i < (MAP_WIDTH + 2); i++ ) {
auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
i * BLOCK_SIZE, 1 - 16 * BLOCK_SIZE, i * BLOCK_SIZE, 1.0,
i * BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, i * BLOCK_SIZE, 1.0,
g_renderer, "#282828" );
line_vertical->setPermanent();
line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line_vertical );
if ( i > 2 ) {
if ( i > (MAP_WIDTH - MAP_HEIGHT) ) {
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, 19 * BLOCK_SIZE,
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, (MAP_WIDTH+1) * BLOCK_SIZE,
( i + 1 ) * BLOCK_SIZE, g_renderer, "#282828" );
line_horizontal->setPermanent();
line_horizontal->setAlignment( SDLPP::OBJ_CENTER,
@ -409,7 +411,7 @@ int main() {
}
// white rectangles
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, g_renderer, "#FFFFFF88",
0, (MAP_WIDTH + 2 - MAP_HEIGHT) * BLOCK_SIZE, BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE, g_renderer, "#FFFFFF88",
true );
rectangle1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle1->setId( EDITOR_LEFT_MAP_ID );
@ -418,7 +420,7 @@ int main() {
scene->addObject( rectangle1 );
// white rectangles
auto rectangle2 = std::make_shared< SDLPP::RectangleRender >(
19 * BLOCK_SIZE, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE,
(MAP_WIDTH+1) * BLOCK_SIZE, (MAP_WIDTH + 2 - MAP_HEIGHT) * BLOCK_SIZE, BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE,
g_renderer, "#FFFFFF88", true );
rectangle2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle2->setId( EDITOR_RIGHT_MAP_ID );
@ -430,23 +432,23 @@ int main() {
auto font_config = std::make_shared< SDLPP::FontConfiguration >(
font, "#000000", "#282828", 0.05 );
auto left = std::make_shared< SDLPP::TextRenderer >(
0, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<",
0, (MAP_WIDTH - MAP_HEIGHT/2.0 + 1.5) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<",
font_config );
left->setId( 0 );
left->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left->setPermanent();
scene->addObject( left );
auto right = std::make_shared< SDLPP::TextRenderer >(
19 * BLOCK_SIZE, 11.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
(MAP_WIDTH+1) * BLOCK_SIZE, (MAP_WIDTH - MAP_HEIGHT/2.0 + 1.5) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
">", font_config );
right->setId( 0 );
right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right->setPermanent();
scene->addObject( right );
for ( int i = 0; i < 18; i++ ) {
for ( int j = 0; j < 16; j++ ) {
scene->addObject( std::make_shared< EditBox >( i, j, g_renderer ) );
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, g_renderer ) );
}
}
@ -459,13 +461,13 @@ int main() {
mouse->setColiderColor( "#00FF00" );
mouse->addCollision( SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene->addObject( mouse );
std::get<MapInfo::MAX_PAGE>(g_map_info) = g_objects.size() - 18;
std::get<MapInfo::MAX_PAGE>(g_map_info) = g_objects.size() - MAP_WIDTH;
// tools
std::get<ToolInfo::MAX_PAGE>(g_tool_info) = ( possibleBlocks.size() - 1 ) / 8;
for ( int i = 0; i < 4; i++ ) {
auto tool_box1 = std::make_shared< ToolBox >( i, 0, g_renderer );
auto tool_box2 = std::make_shared< ToolBox >( i, 1, g_renderer );
auto tool_box1 = std::make_shared< ToolBox >( i, 0, MAP_WIDTH, MAP_HEIGHT, g_renderer );
auto tool_box2 = std::make_shared< ToolBox >( i, 1, MAP_WIDTH, MAP_HEIGHT, g_renderer );
scene->addObject( tool_box1 );
scene->addObject( tool_box2 );
// std::cout << "TOOL BOX POS: " << tool_box1->getPos().getX() <<
@ -488,7 +490,7 @@ int main() {
auto y = tool_index / 4;
// TODO add 14 and 1 as constants somewhere
// TODO investigate when not permanent requires `-1` on x position
g_tools.back()->setPos( 14 * BLOCK_SIZE + x * BLOCK_SIZE,
g_tools.back()->setPos( (MAP_WIDTH - 4) * BLOCK_SIZE + x * BLOCK_SIZE,
BLOCK_SIZE + y * BLOCK_SIZE );
// std::cout << "TOOL POS: " << tools.back()->getPos().getX() <<
// ", " << tools.back()->getPos().getY() << std::endl;
@ -497,8 +499,8 @@ int main() {
}
for ( int i = 0; i < 5; i++ ) {
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,
(MAP_WIDTH - 4) * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE,
(MAP_WIDTH - 4) * BLOCK_SIZE + i * BLOCK_SIZE, 3 * BLOCK_SIZE, g_renderer,
"#282828" );
line->setPermanent();
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
@ -506,8 +508,8 @@ int main() {
}
for ( int i = 0; i < 3; i++ ) {
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,
(MAP_WIDTH - 4) * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
(MAP_WIDTH - 4) * BLOCK_SIZE + 4 * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
g_renderer, "#282828" );
line->setPermanent();
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
@ -515,7 +517,7 @@ int main() {
}
auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >(
13 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
(MAP_WIDTH-5) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
"#FFFFFF88", true );
tool_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect1->setId( EDITOR_LEFT_TOOL_ID );
@ -524,7 +526,7 @@ int main() {
scene->addObject( tool_rect1 );
// white rectangles
auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >(
18 * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
MAP_WIDTH * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
"#FFFFFF88", true );
tool_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect2->setId( EDITOR_RIGHT_TOOL_ID );
@ -532,14 +534,14 @@ int main() {
tool_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject( tool_rect2 );
auto left_tool = std::make_shared< SDLPP::TextRenderer >(
13 * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
(MAP_WIDTH-5) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
"<", font_config );
left_tool->setId( 0 );
left_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left_tool->setPermanent();
scene->addObject( left_tool );
auto right_tool = std::make_shared< SDLPP::TextRenderer >(
18 * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
MAP_WIDTH * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
">", font_config );
right_tool->setId( 0 );
right_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );

View File

@ -3,7 +3,7 @@
#include "blocks.hpp"
#include "sprites.hpp"
ToolBox::ToolBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender(14*BLOCK_SIZE + x*BLOCK_SIZE, 1*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
ToolBox::ToolBox(int x, int y, int map_width, int map_height, std::shared_ptr<SDLPP::Renderer> renderer) : SDLPP::RectangleRender((map_width - 4)*BLOCK_SIZE + x*BLOCK_SIZE, 1*BLOCK_SIZE + y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, renderer) {
_x = x;
_y = y;
setId(EDITOR_TOOL_ID);

View File

@ -5,7 +5,7 @@
class ToolBox : public SDLPP::RectangleRender {
public:
ToolBox(int x, int y, std::shared_ptr<SDLPP::Renderer> renderer);
ToolBox(int x, int y, int map_width, int map_height, std::shared_ptr<SDLPP::Renderer> renderer);
virtual SDLPP::Vec2D<int> getIndexes() const;
virtual void visit( SDLPP::Visitor &visitor ) override;
private: