Mario: editor - move all global vars into a single struct

This commit is contained in:
zv0n 2021-05-22 22:13:22 +02:00
parent d80ae9a4e2
commit d04cbea02d

View File

@ -21,142 +21,154 @@
#include "editor_visitor.hpp"
#include "tool_box.hpp"
#define MAP_WIDTH 50
#define MAP_WIDTH 18
#define MAP_HEIGHT 16
#define QUIT_FLAG 0x00000001
#define UPDATE_FLAG 0x00000002
#define MAP_LEFT_ENABLED_FLAG 0x00000004
#define MAP_RIGHT_ENABLED_FLAG 0x00000008
#define TOOL_LEFT_ENABLED_FLAG 0x00000010
#define TOOL_RIGHT_ENABLED_FLAG 0x00000020
struct MouseInfo {
enum Index {
CUR_FLAGS = 0,
PREV_FLAGS = 1,
EDIT_BOX = 2,
TOOL_BOX = 3,
};
uint64_t cur_flags;
uint64_t prev_flags;
SDLPP::Vec2D< int > edit_box;
SDLPP::Vec2D< int > tool_box;
};
struct MapInfo {
enum Value {
CUR_PAGE = 0,
MAX_PAGE = 1,
};
int cur_page;
int max_page;
};
struct ToolInfo {
enum Value {
INDEX = 0,
CUR_PAGE = 1,
MAX_PAGE = 2,
};
uint64_t index;
int cur_page;
int max_page;
};
std::shared_ptr< SDLPP::Renderer > g_renderer = nullptr;
bool g_quit = false;
bool g_update_size = false;
std::vector< mapColumnType > g_objects = {};
std::vector< std::shared_ptr< SDLPP::RenderObject > > g_tools{};
std::shared_ptr< SDLPP::RenderObject > g_current_tool = nullptr;
struct GlobalVars {
MouseInfo mouse;
MapInfo map;
ToolInfo tool;
uint64_t flags;
std::vector< mapColumnType > objects;
std::vector< std::shared_ptr< SDLPP::RenderObject > > tools;
std::shared_ptr< SDLPP::RenderObject > current_tool;
std::shared_ptr< SDLPP::Texture > translucent_terrain_texture;
std::shared_ptr< SDLPP::Texture > translucent_mario_texture;
std::shared_ptr< SDLPP::RenderObject > mario;
SDLPP::Vec2D< int > mario_pos;
};
std::mutex g_destruction_mutex;
// current mouse flags, previous mouse flags, selected edit box, selected tool
// box
std::tuple< uint64_t, uint64_t, SDLPP::Vec2D< int >, SDLPP::Vec2D< int > >
g_mouse_info;
// current page, max page
std::tuple< int, int > g_map_info;
// tool index (in possibleBlocks), current pange, max page
std::tuple<uint64_t, int, int> g_tool_info;
std::shared_ptr< SDLPP::Texture > g_placeholder_texture = nullptr;
std::shared_ptr< SDLPP::Texture > g_placeholder_mario = nullptr;
std::shared_ptr< SDLPP::RenderObject > g_mario = nullptr;
SDLPP::Vec2D< int > g_mario_pos = { 0, 0 };
GlobalVars global_vars;
std::mutex destruction_mutex;
enum LandType::Value g_current_world_type = LandType::OVERWORLD;
void updateTool() {
auto tool_role = getBlockRole( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)] );
auto tool_index = global_vars.tool.index;
auto tool_role = getBlockRole( possibleBlocks[tool_index] );
std::shared_ptr< SDLPP::Texture > target_texture = nullptr;
switch ( tool_role ) {
case BlockRole::TERRAIN:
target_texture = g_placeholder_texture;
target_texture = global_vars.translucent_terrain_texture;
break;
case BlockRole::MARIO:
target_texture = g_placeholder_mario;
target_texture = global_vars.translucent_mario_texture;
break;
case BlockRole::MODIFIER:
break;
case BlockRole::CHARACTER:
break;
}
g_current_tool->setTexture(
target_texture, getSourceRectByID( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)],
g_current_world_type ) );
g_current_tool->setId( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)] );
g_current_tool->getCollisions()[0]->setId(
possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)] );
global_vars.current_tool->setTexture(
target_texture,
getSourceRectByID( possibleBlocks[tool_index], g_current_world_type ) );
global_vars.current_tool->setId( possibleBlocks[tool_index] );
global_vars.current_tool->getCollisions()[0]->setId(
possibleBlocks[tool_index] );
}
void removeMario() {
if ( !g_mario )
if ( !global_vars.mario )
return;
auto prev = g_objects[g_mario_pos.getX()][g_mario_pos.getY()];
auto prev = global_vars.objects[global_vars.mario_pos.getX()]
[global_vars.mario_pos.getY()];
// remove character/modifiers
g_objects[g_mario_pos.getX()][g_mario_pos.getY()] = {
global_vars
.objects[global_vars.mario_pos.getX()][global_vars.mario_pos.getY()] = {
std::get< 0 >( prev ), std::get< 1 >( prev ), 0, 0, 0, 0
};
g_mario->destroy();
global_vars.mario->destroy();
}
void updateToolSelection( int prev_index ) {
size_t prev = prev_index * 8;
size_t cur = std::get<ToolInfo::CUR_PAGE>(g_tool_info) * 8;
size_t cur = global_vars.tool.cur_page * 8;
for ( size_t i = prev;
i < ( g_tools.size() < prev + 8 ? g_tools.size() : prev + 8 ); i++ ) {
g_tools[i]->setHidden( true );
i < ( global_vars.tools.size() < prev + 8 ? global_vars.tools.size()
: prev + 8 );
i++ ) {
global_vars.tools[i]->setHidden( true );
}
for ( size_t i = cur;
i < ( g_tools.size() < cur + 8 ? g_tools.size() : cur + 8 ); i++ ) {
g_tools[i]->setHidden( false );
i < ( global_vars.tools.size() < cur + 8 ? global_vars.tools.size()
: cur + 8 );
i++ ) {
global_vars.tools[i]->setHidden( false );
}
}
void moveToolsLeft() {
std::get<ToolInfo::CUR_PAGE>(g_tool_info)--;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) + 1 );
global_vars.tool.cur_page--;
updateToolSelection( global_vars.tool.cur_page + 1 );
}
void moveToolsRight() {
std::get<ToolInfo::CUR_PAGE>(g_tool_info)++;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) - 1 );
global_vars.tool.cur_page++;
updateToolSelection( global_vars.tool.cur_page - 1 );
}
// TODO add red outline to currently selected tool
// add WSAD navigation for the red highlight
// add mouse wheel navigation
void selectPrevTool() {
if ( std::get<ToolInfo::INDEX>(g_tool_info) == 0 )
if ( global_vars.tool.index == 0 )
return;
if ( std::get<ToolInfo::INDEX>(g_tool_info) % 8 == 0 ) {
if ( global_vars.tool.index % 8 == 0 ) {
moveToolsLeft();
}
std::get<ToolInfo::INDEX>(g_tool_info)--;
global_vars.tool.index--;
updateTool();
}
void selectNextTool() {
if ( std::get<ToolInfo::INDEX>(g_tool_info) == g_tools.size() - 1 )
if ( global_vars.tool.index == global_vars.tools.size() - 1 )
return;
if ( std::get<ToolInfo::INDEX>(g_tool_info) % 8 == 7 ) {
if ( global_vars.tool.index % 8 == 7 ) {
moveToolsRight();
}
std::get<ToolInfo::INDEX>(g_tool_info)++;
global_vars.tool.index++;
updateTool();
}
void setFlag( uint64_t flag ) {
global_vars.flags |= flag;
}
void unsetFlag( uint64_t flag ) {
global_vars.flags &= ~flag;
}
bool getFlag( uint64_t flag ) {
return global_vars.flags & flag;
}
void handleKeyUp( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
case SDLK_ESCAPE:
g_quit = true;
setFlag( QUIT_FLAG );
break;
case SDLK_a:
selectPrevTool();
@ -180,60 +192,60 @@ void getMousePositionFlags( SDLPP::Scene &scene ) {
MouseVisitor visitor;
scene.visitCollisions( *mouse, visitor );
std::get< MouseInfo::CUR_FLAGS >( g_mouse_info ) = visitor.getFlags();
std::get< MouseInfo::EDIT_BOX >( g_mouse_info ) =
visitor.getEditBoxIndexes();
std::get< MouseInfo::TOOL_BOX >( g_mouse_info ) =
visitor.getToolBoxIndexes();
global_vars.mouse.cur_flags = visitor.getFlags();
global_vars.mouse.edit_box = visitor.getEditBoxIndexes();
global_vars.mouse.tool_box = visitor.getToolBoxIndexes();
// if we found an edit box, move tool icon to that box
if ( visitor.foundEditBox() ) {
const auto &box = std::get< MouseInfo::EDIT_BOX >( g_mouse_info );
g_current_tool->setPos( BLOCK_SIZE + box.getX() * BLOCK_SIZE,
(MAP_WIDTH - MAP_HEIGHT + 2) * BLOCK_SIZE + box.getY() * BLOCK_SIZE );
const auto &box = global_vars.mouse.edit_box;
global_vars.current_tool->setPos( BLOCK_SIZE + box.getX() * BLOCK_SIZE,
( MAP_WIDTH - MAP_HEIGHT + 2 ) *
BLOCK_SIZE +
box.getY() * BLOCK_SIZE );
}
}
void mouseUpAction( uint64_t flags, SDLPP::Scene &scene ) {
if ( MouseVisitor::moveMapLeft( flags ) && std::get<MapInfo::CUR_PAGE>(g_map_info) != 0 ) {
std::get<MapInfo::CUR_PAGE>(g_map_info)--;
if ( MouseVisitor::moveMapLeft( flags ) && global_vars.map.cur_page != 0 ) {
global_vars.map.cur_page--;
scene.moveEverything( BLOCK_SIZE, 0 );
}
if ( MouseVisitor::moveMapRight( flags ) ) {
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == std::get<MapInfo::MAX_PAGE>(g_map_info) ) {
if ( global_vars.map.cur_page == global_vars.map.max_page ) {
// add column
g_objects.resize( g_objects.size() + 1 );
std::get<MapInfo::MAX_PAGE>(g_map_info)++;
global_vars.objects.resize( global_vars.objects.size() + 1 );
global_vars.map.max_page++;
}
std::get<MapInfo::CUR_PAGE>(g_map_info) += 1;
global_vars.map.cur_page += 1;
scene.moveEverything( -BLOCK_SIZE, 0 );
}
if ( MouseVisitor::moveToolsLeft( flags ) && std::get<ToolInfo::CUR_PAGE>(g_tool_info) != 0 ) {
std::get<ToolInfo::CUR_PAGE>(g_tool_info) -= 1;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) + 1 );
if ( MouseVisitor::moveToolsLeft( flags ) &&
global_vars.tool.cur_page != 0 ) {
global_vars.tool.cur_page--;
updateToolSelection( global_vars.tool.cur_page + 1 );
}
if ( MouseVisitor::moveToolsRight( flags ) &&
std::get<ToolInfo::CUR_PAGE>(g_tool_info) != std::get<ToolInfo::MAX_PAGE>(g_tool_info) ) {
std::get<ToolInfo::CUR_PAGE>(g_tool_info) += 1;
updateToolSelection( std::get<ToolInfo::CUR_PAGE>(g_tool_info) - 1 );
global_vars.tool.cur_page != global_vars.tool.max_page ) {
global_vars.tool.cur_page++;
updateToolSelection( global_vars.tool.cur_page - 1 );
}
}
SDLPP::Vec2D< int > getSelectedObjectPosition() {
return { std::get<MapInfo::CUR_PAGE>(g_map_info) +
std::get< MouseInfo::EDIT_BOX >( g_mouse_info ).getX(),
std::get< MouseInfo::EDIT_BOX >( g_mouse_info ).getY() };
return global_vars.mouse.edit_box +
SDLPP::Vec2D< int >( global_vars.map.cur_page, 0 );
}
mapObjectType &getSelectedObject() {
auto pos = getSelectedObjectPosition();
return g_objects[pos.getX()][pos.getY()];
return global_vars.objects[pos.getX()][pos.getY()];
}
void placeTool( SDLPP::Scene &scene ) {
std::lock_guard< std::mutex > lock( g_destruction_mutex );
std::lock_guard< std::mutex > lock( destruction_mutex );
ToolVisitor visitor;
auto tool_type = getBlockRole( g_current_tool->getId() );
auto tool_type = getBlockRole( global_vars.current_tool->getId() );
switch ( tool_type ) {
case BlockRole::TERRAIN:
visitor.setVisitorType( VisitorType::Terrain );
@ -243,7 +255,7 @@ void placeTool( SDLPP::Scene &scene ) {
break;
}
scene.visitCollisions( *g_current_tool, visitor );
scene.visitCollisions( *global_vars.current_tool, visitor );
if ( visitor.removeBlock() && !visitor.addBlock() ) {
auto &obj = getSelectedObject();
switch ( visitor.getVisitorType() ) {
@ -262,17 +274,19 @@ void placeTool( SDLPP::Scene &scene ) {
}
} else if ( visitor.addBlock() ) {
auto &obj = getSelectedObject();
auto renderer = scene.getRendererShared();
int z_index = 1;
std::shared_ptr< SDLPP::RenderObject > new_obj = nullptr;
switch ( visitor.getVisitorType() ) {
case VisitorType::Terrain:
std::get< MapObject::TERRAIN_TYPE >( obj ) = LandType::OVERWORLD;
std::get< MapObject::TERRAIN_ID >( obj ) = g_current_tool->getId();
std::get< MapObject::TERRAIN_ID >( obj ) =
global_vars.current_tool->getId();
// 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 );
global_vars.current_tool->getId(), LandType::OVERWORLD,
renderer, 1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY(), true );
new_obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
break;
case VisitorType::Modifier:
@ -282,14 +296,13 @@ void placeTool( SDLPP::Scene &scene ) {
std::get< MapObject::CHARACTER_ID >( obj ) = MARIO_ID;
std::get< MapObject::MODIFIER_TYPE >( obj ) = 0;
std::get< MapObject::MODIFIER_DATA >( obj ) = 0;
new_obj = createMario(
LandType::OVERWORLD, 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, renderer,
1 + global_vars.mouse.edit_box.getX(),
global_vars.mouse.edit_box.getY() );
// remove mario if exists
removeMario();
g_mario = new_obj;
g_mario_pos = getSelectedObjectPosition();
global_vars.mario = new_obj;
global_vars.mario_pos = getSelectedObjectPosition();
new_obj->getCollisions()[0]->setId( EDITOR_CHARACTER_ID );
z_index = scene.getObjects().size() - 1;
break;
@ -306,44 +319,39 @@ void pollEvents( SDLPP::Scene &scene ) {
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
g_quit = true;
setFlag( QUIT_FLAG );
break;
case SDL_KEYUP:
handleKeyUp( event.key.keysym.sym, scene );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
g_update_size = true;
setFlag( UPDATE_FLAG );
}
break;
case SDL_MOUSEMOTION:
getMousePositionFlags( scene );
break;
case SDL_MOUSEBUTTONUP:
if ( std::get< MouseInfo::CUR_FLAGS >( g_mouse_info ) ==
std::get< MouseInfo::PREV_FLAGS >( g_mouse_info ) ) {
mouseUpAction( std::get< MouseInfo::CUR_FLAGS >( g_mouse_info ),
scene );
if ( global_vars.mouse.cur_flags == global_vars.mouse.prev_flags ) {
mouseUpAction( global_vars.mouse.cur_flags, scene );
}
if ( std::get< MouseInfo::EDIT_BOX >( g_mouse_info ).getX() !=
-1 ) {
if ( global_vars.mouse.edit_box.getX() != -1 ) {
placeTool( scene );
}
if ( std::get< MouseInfo::TOOL_BOX >( g_mouse_info ).getX() !=
-1 ) {
auto &tool_box =
std::get< MouseInfo::TOOL_BOX >( g_mouse_info );
if ( global_vars.mouse.tool_box.getX() != -1 ) {
auto &tool_box = global_vars.mouse.tool_box;
size_t index = tool_box.getY() * 4 + tool_box.getX();
if ( index < g_tools.size() ) {
std::get<ToolInfo::INDEX>(g_tool_info) = std::get<ToolInfo::CUR_PAGE>(g_tool_info) * 8 + index;
if ( index < global_vars.tools.size() ) {
global_vars.tool.index =
global_vars.tool.cur_page * 8 + index;
updateTool();
}
}
break;
case SDL_MOUSEBUTTONDOWN:
// store current mouse flags in previous mouse flags
std::get< MouseInfo::PREV_FLAGS >( g_mouse_info ) =
std::get< MouseInfo::CUR_FLAGS >( g_mouse_info );
global_vars.mouse.prev_flags = global_vars.mouse.cur_flags;
break;
default:
break;
@ -371,38 +379,39 @@ int main() {
SDLPP::init();
SDLPP::Window w( "Mario editor!" );
w.setResizable( true );
BLOCK_SIZE = 1.0 / (MAP_WIDTH + 2);
BLOCK_SIZE = 1.0 / ( MAP_WIDTH + 2 );
g_renderer = std::make_shared< SDLPP::Renderer >( w );
g_renderer->setBlendMode( SDL_BLENDMODE_BLEND );
auto renderer = std::make_shared< SDLPP::Renderer >( w );
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
// prepare global vars
g_terrain_texture = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_mario_texture = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
auto scene = std::make_shared< SDLPP::Scene >( g_renderer );
auto scene = std::make_shared< SDLPP::Scene >( renderer );
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, g_renderer, MARIO_OVERWORLD_COLORKEY, true );
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
bg->setPermanent();
bg->setId( 1 );
scene->addObject( bg );
loadMap( scene, "test_binary.bin", g_renderer, g_objects );
loadMap( scene, "test_binary.bin", renderer, global_vars.objects );
// grid
for ( int i = 1; i < (MAP_WIDTH + 2); i++ ) {
for ( int i = 1; i < ( MAP_WIDTH + 2 ); i++ ) {
auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
i * BLOCK_SIZE, 1 - MAP_HEIGHT * BLOCK_SIZE, i * BLOCK_SIZE, 1.0,
g_renderer, "#282828" );
renderer, "#282828" );
line_vertical->setPermanent();
line_vertical->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line_vertical );
if ( i > (MAP_WIDTH - MAP_HEIGHT) ) {
if ( i > ( MAP_WIDTH - MAP_HEIGHT ) ) {
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, (MAP_WIDTH+1) * BLOCK_SIZE,
( i + 1 ) * BLOCK_SIZE, g_renderer, "#282828" );
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE,
( MAP_WIDTH + 1 ) * BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE,
renderer, "#282828" );
line_horizontal->setPermanent();
line_horizontal->setAlignment( SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER );
@ -411,8 +420,8 @@ int main() {
}
// white rectangles
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
0, (MAP_WIDTH + 2 - MAP_HEIGHT) * BLOCK_SIZE, BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE, g_renderer, "#FFFFFF88",
true );
0, ( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, BLOCK_SIZE,
MAP_HEIGHT * BLOCK_SIZE, renderer, "#FFFFFF88", true );
rectangle1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle1->setId( EDITOR_LEFT_MAP_ID );
rectangle1->setPermanent();
@ -420,8 +429,9 @@ int main() {
scene->addObject( rectangle1 );
// white rectangles
auto rectangle2 = std::make_shared< SDLPP::RectangleRender >(
(MAP_WIDTH+1) * BLOCK_SIZE, (MAP_WIDTH + 2 - MAP_HEIGHT) * BLOCK_SIZE, BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE,
g_renderer, "#FFFFFF88", true );
( MAP_WIDTH + 1 ) * BLOCK_SIZE,
( MAP_WIDTH + 2 - MAP_HEIGHT ) * BLOCK_SIZE, BLOCK_SIZE,
MAP_HEIGHT * BLOCK_SIZE, renderer, "#FFFFFF88", true );
rectangle2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
rectangle2->setId( EDITOR_RIGHT_MAP_ID );
rectangle2->setPermanent();
@ -432,15 +442,16 @@ int main() {
auto font_config = std::make_shared< SDLPP::FontConfiguration >(
font, "#000000", "#282828", 0.05 );
auto left = std::make_shared< SDLPP::TextRenderer >(
0, (MAP_WIDTH - MAP_HEIGHT/2.0 + 1.5) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer, "<",
font_config );
0, ( MAP_WIDTH - MAP_HEIGHT / 2.0 + 1.5 ) * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, 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 >(
(MAP_WIDTH+1) * BLOCK_SIZE, (MAP_WIDTH - MAP_HEIGHT/2.0 + 1.5) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
">", font_config );
( MAP_WIDTH + 1 ) * BLOCK_SIZE,
( MAP_WIDTH - MAP_HEIGHT / 2.0 + 1.5 ) * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, renderer, ">", font_config );
right->setId( 0 );
right->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right->setPermanent();
@ -448,12 +459,13 @@ int main() {
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 ) );
scene->addObject( std::make_shared< EditBox >(
i, j, MAP_WIDTH, MAP_HEIGHT, renderer ) );
}
}
auto mouse = std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0, 0,
g_renderer );
renderer );
mouse->setMinWidth( 1 );
mouse->setMinHeight( 1 );
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
@ -461,13 +473,15 @@ 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() - MAP_WIDTH;
global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH;
// tools
std::get<ToolInfo::MAX_PAGE>(g_tool_info) = ( possibleBlocks.size() - 1 ) / 8;
global_vars.tool.max_page = ( possibleBlocks.size() - 1 ) / 8;
for ( int i = 0; i < 4; i++ ) {
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 );
auto tool_box1 = std::make_shared< ToolBox >( i, 0, MAP_WIDTH,
MAP_HEIGHT, renderer );
auto tool_box2 = std::make_shared< ToolBox >( i, 1, MAP_WIDTH,
MAP_HEIGHT, renderer );
scene->addObject( tool_box1 );
scene->addObject( tool_box2 );
// std::cout << "TOOL BOX POS: " << tool_box1->getPos().getX() <<
@ -478,47 +492,48 @@ int main() {
int tool_index = 0;
for ( auto &block : possibleBlocks ) {
if ( block == MARIO_ID ) {
g_tools.push_back(
createMario( LandType::OVERWORLD, g_renderer, 0, 0 ) );
global_vars.tools.push_back(
createMario( LandType::OVERWORLD, renderer, 0, 0 ) );
} else {
g_tools.push_back( createTerrainBlock( block, LandType::OVERWORLD,
g_renderer, false ) );
global_vars.tools.push_back( createTerrainBlock(
block, LandType::OVERWORLD, renderer, false ) );
}
g_tools.back()->setHidden( true );
g_tools.back()->setPermanent();
global_vars.tools.back()->setHidden( true );
global_vars.tools.back()->setPermanent();
auto x = tool_index % 4;
auto y = tool_index / 4;
// TODO add 14 and 1 as constants somewhere
// TODO investigate when not permanent requires `-1` on x position
g_tools.back()->setPos( (MAP_WIDTH - 4) * BLOCK_SIZE + x * BLOCK_SIZE,
global_vars.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;
scene->addObject( g_tools.back() );
scene->addObject( global_vars.tools.back() );
tool_index = ( tool_index + 1 ) % 8;
}
for ( int i = 0; i < 5; i++ ) {
auto line = std::make_shared< SDLPP::LineRenderer >(
(MAP_WIDTH - 4) * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE,
(MAP_WIDTH - 4) * BLOCK_SIZE + i * BLOCK_SIZE, 3 * BLOCK_SIZE, g_renderer,
"#282828" );
( MAP_WIDTH - 4 ) * BLOCK_SIZE + i * BLOCK_SIZE, BLOCK_SIZE,
( MAP_WIDTH - 4 ) * BLOCK_SIZE + i * BLOCK_SIZE, 3 * BLOCK_SIZE,
renderer, "#282828" );
line->setPermanent();
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line );
}
for ( int i = 0; i < 3; i++ ) {
auto line = std::make_shared< SDLPP::LineRenderer >(
(MAP_WIDTH - 4) * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
(MAP_WIDTH - 4) * BLOCK_SIZE + 4 * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
g_renderer, "#282828" );
( MAP_WIDTH - 4 ) * BLOCK_SIZE, BLOCK_SIZE + i * BLOCK_SIZE,
( MAP_WIDTH - 4 ) * BLOCK_SIZE + 4 * BLOCK_SIZE,
BLOCK_SIZE + i * BLOCK_SIZE, renderer, "#282828" );
line->setPermanent();
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject( line );
}
auto tool_rect1 = std::make_shared< SDLPP::RectangleRender >(
(MAP_WIDTH-5) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
"#FFFFFF88", true );
( MAP_WIDTH - 5 ) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE,
renderer, "#FFFFFF88", true );
tool_rect1->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect1->setId( EDITOR_LEFT_TOOL_ID );
tool_rect1->setPermanent();
@ -526,41 +541,43 @@ int main() {
scene->addObject( tool_rect1 );
// white rectangles
auto tool_rect2 = std::make_shared< SDLPP::RectangleRender >(
MAP_WIDTH * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE, g_renderer,
"#FFFFFF88", true );
MAP_WIDTH * BLOCK_SIZE, 1 * BLOCK_SIZE, BLOCK_SIZE, 2 * BLOCK_SIZE,
renderer, "#FFFFFF88", true );
tool_rect2->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tool_rect2->setId( EDITOR_RIGHT_TOOL_ID );
tool_rect2->setPermanent();
tool_rect2->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
scene->addObject( tool_rect2 );
auto left_tool = std::make_shared< SDLPP::TextRenderer >(
(MAP_WIDTH-5) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
"<", font_config );
( MAP_WIDTH - 5 ) * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE, 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 >(
MAP_WIDTH * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, g_renderer,
">", font_config );
MAP_WIDTH * BLOCK_SIZE, 1.5 * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE,
renderer, ">", font_config );
right_tool->setId( 0 );
right_tool->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right_tool->setPermanent();
scene->addObject( right_tool );
g_placeholder_texture = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_texture->setAlpha( 100 );
g_placeholder_mario = std::make_shared< SDLPP::Texture >(
g_renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_placeholder_mario->setAlpha( 100 );
g_current_tool = createTerrainBlock( possibleBlocks[std::get<ToolInfo::INDEX>(g_tool_info)],
LandType::OVERWORLD, g_renderer,
g_placeholder_texture, false );
g_current_tool->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
dynamic_cast< MarioBlock & >( *g_current_tool ).setTool();
scene->addObject( g_current_tool );
scene->moveZTop( g_current_tool );
global_vars.translucent_terrain_texture =
std::make_shared< SDLPP::Texture >( renderer, "sprites/terrain.png",
MARIO_OVERWORLD_COLORKEY );
global_vars.translucent_terrain_texture->setAlpha( 100 );
global_vars.translucent_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
global_vars.translucent_mario_texture->setAlpha( 100 );
global_vars.current_tool = createTerrainBlock(
possibleBlocks[global_vars.tool.index], LandType::OVERWORLD, renderer,
global_vars.translucent_terrain_texture, false );
global_vars.current_tool->addCollision(
SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
dynamic_cast< MarioBlock & >( *global_vars.current_tool ).setTool();
scene->addObject( global_vars.current_tool );
scene->moveZTop( global_vars.current_tool );
scene->moveEverything( BLOCK_SIZE, 0 );
FPSmanager gFPS;
@ -573,45 +590,76 @@ int main() {
int frames = 0;
std::thread inputThread( doInput, scene );
inputThread.detach();
while ( !g_quit ) {
setFlag( UPDATE_FLAG );
// required for initial correct rendering
setFlag( MAP_LEFT_ENABLED_FLAG );
setFlag( MAP_RIGHT_ENABLED_FLAG );
setFlag( TOOL_LEFT_ENABLED_FLAG );
setFlag( TOOL_RIGHT_ENABLED_FLAG );
while ( !getFlag( QUIT_FLAG ) ) {
SDL_PumpEvents();
SDL_framerateDelay( &gFPS );
std::lock_guard< std::mutex > lock( g_destruction_mutex );
std::lock_guard< std::mutex > lock( destruction_mutex );
scene->renderScene();
g_renderer->presentRenderer();
renderer->presentRenderer();
frames++;
if ( SDL_GetTicks() - base >= 1000 ) {
std::cout << "FPS: " << frames << std::endl;
frames = 0;
base = SDL_GetTicks();
}
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == 0 ) {
if ( global_vars.map.cur_page == 0 &&
getFlag( MAP_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( MAP_LEFT_ENABLED_FLAG );
left->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else {
} else if ( global_vars.map.cur_page != 0 &&
!getFlag( MAP_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( MAP_LEFT_ENABLED_FLAG );
left->setTextColor( font, "#000000", "#282828", 0.05 );
}
if ( std::get<MapInfo::CUR_PAGE>(g_map_info) == std::get<MapInfo::MAX_PAGE>(g_map_info) ) {
if ( global_vars.map.cur_page == global_vars.map.max_page &&
getFlag( MAP_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( MAP_RIGHT_ENABLED_FLAG );
right->setTextColor( font, "#00FF00", "#000000", 0.1 );
right->changeText( "+" );
} else {
} else if ( global_vars.map.cur_page != global_vars.map.max_page &&
!getFlag( MAP_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( MAP_RIGHT_ENABLED_FLAG );
right->setTextColor( font, "#000000", "#282828", 0.05 );
right->changeText( ">" );
}
if ( std::get<ToolInfo::CUR_PAGE>(g_tool_info) == 0 ) {
if ( global_vars.tool.cur_page == 0 &&
getFlag( TOOL_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( TOOL_LEFT_ENABLED_FLAG );
left_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else {
} else if ( global_vars.tool.cur_page != 0 &&
!getFlag( TOOL_LEFT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( TOOL_LEFT_ENABLED_FLAG );
left_tool->setTextColor( font, "#000000", "#282828", 0.05 );
}
if ( std::get<ToolInfo::CUR_PAGE>(g_tool_info) == std::get<ToolInfo::MAX_PAGE>(g_tool_info) ) {
if ( global_vars.tool.cur_page == global_vars.tool.max_page &&
getFlag( TOOL_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
unsetFlag( TOOL_RIGHT_ENABLED_FLAG );
right_tool->setTextColor( font, "#CCCCCC", "#CCCCCC", 0.05 );
} else {
} else if ( global_vars.tool.cur_page != global_vars.tool.max_page &&
!getFlag( TOOL_RIGHT_ENABLED_FLAG ) ) {
setFlag( UPDATE_FLAG );
setFlag( TOOL_RIGHT_ENABLED_FLAG );
right_tool->setTextColor( font, "#000000", "#282828", 0.05 );
}
if ( g_update_size ) {
if ( getFlag( UPDATE_FLAG ) ) {
scene->updateSizeAndPosition();
unsetFlag( UPDATE_FLAG );
}
}
saveMap( "test_binary2.bin", g_objects );
saveMap( "test_binary2.bin", global_vars.objects );
return 0;
}