2021-04-25 20:42:55 +00:00
|
|
|
#include "maploader.hpp"
|
|
|
|
#include "../sdlpp/sdlpp_rectrenderer.hpp"
|
2021-05-07 06:35:01 +00:00
|
|
|
#include <array>
|
2021-04-25 20:42:55 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include "sprites.hpp"
|
|
|
|
#include "blocks.hpp"
|
|
|
|
#include "objectids.hpp"
|
2021-05-25 22:46:19 +00:00
|
|
|
#include "global_vars.hpp"
|
2021-04-25 20:42:55 +00:00
|
|
|
|
2021-05-01 19:55:43 +00:00
|
|
|
// TODO move to one function
|
2021-05-08 22:46:10 +00:00
|
|
|
void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
|
|
|
|
std::shared_ptr< SDLPP::RectangleRender > mario,
|
|
|
|
const std::string &file,
|
|
|
|
std::shared_ptr< SDLPP::Renderer > &renderer ) {
|
2021-05-01 19:55:43 +00:00
|
|
|
std::ifstream map_file;
|
2021-05-08 22:46:10 +00:00
|
|
|
map_file.open( file, std::ios::in | std::ios::binary );
|
2021-05-01 19:55:43 +00:00
|
|
|
uint16_t cols;
|
2021-05-08 22:46:10 +00:00
|
|
|
map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
|
|
|
|
for ( uint16_t i = 0; i < cols; i++ ) {
|
|
|
|
for ( int j = 0; j < 16; j++ ) {
|
2021-05-01 19:55:43 +00:00
|
|
|
uint16_t input_number;
|
|
|
|
uint8_t additional_data = 0;
|
2021-05-08 22:46:10 +00:00
|
|
|
uint8_t character_type = 0, character = 0, modifier_type = 0,
|
|
|
|
modifier_data = 0;
|
|
|
|
map_file.read( ( char * )&input_number,
|
|
|
|
sizeof( uint16_t ) / sizeof( char ) );
|
|
|
|
uint8_t type = ( input_number & 0xF000 ) >> 12;
|
|
|
|
uint16_t id = ( input_number & 0x0FFF ) | BLOCK_PREFIX;
|
|
|
|
if ( type & 0x8 ) {
|
|
|
|
map_file.read( ( char * )&additional_data,
|
|
|
|
sizeof( uint8_t ) / sizeof( char ) );
|
2021-05-01 19:55:43 +00:00
|
|
|
type &= ~0x8;
|
2021-05-08 22:46:10 +00:00
|
|
|
if ( additional_data & 0x80 ) {
|
|
|
|
// modifier
|
2021-05-01 19:55:43 +00:00
|
|
|
additional_data &= ~0x80;
|
2021-05-08 22:46:10 +00:00
|
|
|
modifier_type = ( additional_data & 0xF0 ) >> 4;
|
2021-05-01 19:55:43 +00:00
|
|
|
modifier_data = additional_data & 0x0F;
|
|
|
|
} else {
|
|
|
|
// character
|
2021-05-08 22:46:10 +00:00
|
|
|
character_type = ( additional_data & 0xF0 ) >> 4;
|
2021-05-01 19:55:43 +00:00
|
|
|
character = additional_data & 0x0F;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool collision = false;
|
2021-05-23 22:07:40 +00:00
|
|
|
bool destructible = false;
|
2021-05-23 21:57:29 +00:00
|
|
|
if ( id == FLOOR_ID || id == BRICK_ID || id == BRICK_TOP_ID ) {
|
2021-05-01 19:55:43 +00:00
|
|
|
collision = true;
|
|
|
|
}
|
2021-05-26 12:18:39 +00:00
|
|
|
// TODO definitely make this somehow more streamlined, probably
|
|
|
|
// flags
|
|
|
|
if ( modifier_type == DESTRUCTIBLE_ID ) {
|
2021-05-25 22:46:19 +00:00
|
|
|
destructible = true;
|
|
|
|
}
|
2021-05-01 19:55:43 +00:00
|
|
|
// TODO add modifiers to createTerrainBlock
|
2021-05-08 22:46:10 +00:00
|
|
|
auto obj =
|
|
|
|
createTerrainBlock( id, static_cast< LandType::Value >( type ),
|
2021-05-23 22:07:40 +00:00
|
|
|
renderer, i, j, collision, destructible );
|
2021-05-08 22:46:10 +00:00
|
|
|
if ( obj != nullptr )
|
|
|
|
scene->addObject( obj );
|
|
|
|
if ( character ) {
|
|
|
|
if ( character == MARIO_ID ) {
|
|
|
|
mario->setPos( i * BLOCK_SIZE,
|
|
|
|
1 - ( 16 - j ) * BLOCK_SIZE );
|
2021-05-01 19:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-25 20:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 22:46:10 +00:00
|
|
|
scene->moveZTop( mario );
|
2021-04-30 21:12:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 12:14:11 +00:00
|
|
|
// editor loader
|
2021-05-26 12:18:39 +00:00
|
|
|
void loadMap( std::shared_ptr< SDLPP::Scene > &scene,
|
|
|
|
std::shared_ptr< SDLPP::RenderObject > &mario,
|
|
|
|
const std::string &file,
|
2021-05-08 22:46:10 +00:00
|
|
|
std::shared_ptr< SDLPP::Renderer > &renderer,
|
|
|
|
std::vector< mapColumnType > &objects ) {
|
2021-04-30 21:12:53 +00:00
|
|
|
std::ifstream map_file;
|
2021-05-08 22:46:10 +00:00
|
|
|
map_file.open( file, std::ios::in | std::ios::binary );
|
2021-04-30 21:12:53 +00:00
|
|
|
uint16_t cols;
|
2021-05-08 22:46:10 +00:00
|
|
|
map_file.read( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
|
|
|
|
objects.resize( cols );
|
|
|
|
for ( uint16_t i = 0; i < cols; i++ ) {
|
2021-04-30 21:12:53 +00:00
|
|
|
auto &col = objects[i];
|
2021-05-08 22:46:10 +00:00
|
|
|
for ( int j = 0; j < 16; j++ ) {
|
2021-04-30 21:12:53 +00:00
|
|
|
uint16_t input_number;
|
2021-05-01 19:55:43 +00:00
|
|
|
uint8_t additional_data = 0;
|
2021-05-08 22:46:10 +00:00
|
|
|
uint8_t character_type = 0, character = 0, modifier_type = 0,
|
|
|
|
modifier_data = 0;
|
|
|
|
map_file.read( ( char * )&input_number,
|
|
|
|
sizeof( uint16_t ) / sizeof( char ) );
|
|
|
|
uint8_t type = ( input_number & 0xF000 ) >> 12;
|
|
|
|
uint16_t id = ( input_number & 0x0FFF ) | BLOCK_PREFIX;
|
|
|
|
if ( type & 0x8 ) {
|
|
|
|
map_file.read( ( char * )&additional_data,
|
|
|
|
sizeof( uint8_t ) / sizeof( char ) );
|
2021-05-01 19:55:43 +00:00
|
|
|
type &= ~0x8;
|
2021-05-08 22:46:10 +00:00
|
|
|
if ( additional_data & 0x80 ) {
|
|
|
|
// modifier
|
2021-05-01 19:55:43 +00:00
|
|
|
additional_data &= ~0x80;
|
2021-05-08 22:46:10 +00:00
|
|
|
modifier_type = ( additional_data & 0xF0 ) >> 4;
|
2021-05-01 19:55:43 +00:00
|
|
|
modifier_data = additional_data & 0x0F;
|
|
|
|
} else {
|
|
|
|
// character
|
2021-05-08 22:46:10 +00:00
|
|
|
character_type = ( additional_data & 0xF0 ) >> 4;
|
2021-05-01 19:55:43 +00:00
|
|
|
character = additional_data & 0x0F;
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 12:18:39 +00:00
|
|
|
col[j] = { type, id, character_type, character,
|
2021-05-08 22:46:10 +00:00
|
|
|
modifier_type, modifier_data };
|
2021-05-01 19:55:43 +00:00
|
|
|
// TODO add modifiers to createTerrainBlock
|
2021-05-08 22:46:10 +00:00
|
|
|
auto obj =
|
|
|
|
createTerrainBlock( id, static_cast< LandType::Value >( type ),
|
|
|
|
renderer, i, j, true );
|
|
|
|
obj->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
|
|
|
|
scene->addObject( obj );
|
|
|
|
if ( character ) {
|
|
|
|
if ( character == MARIO_ID ) {
|
|
|
|
scene->addObject( createMario(
|
|
|
|
static_cast< LandType::Value >( character_type ),
|
|
|
|
renderer, i, j ) );
|
2021-05-26 12:18:39 +00:00
|
|
|
mario = scene->getObject(scene->getObjects().size() - 1);
|
2021-05-01 19:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-25 22:46:19 +00:00
|
|
|
if ( modifier_type ) {
|
2021-05-26 12:18:39 +00:00
|
|
|
auto mod = createTerrainBlock(
|
|
|
|
modifier_type, LandType::OVERWORLD, renderer, i, j,
|
|
|
|
g_translucent_terrain_texture, true );
|
2021-05-25 22:46:19 +00:00
|
|
|
mod->getCollisions()[0]->setId( EDITOR_TERRAIN_ID );
|
2021-05-26 12:18:39 +00:00
|
|
|
dynamic_cast< MarioBlock * >( mod.get() )->setTerrain( false );
|
|
|
|
scene->addObject( mod );
|
2021-05-25 22:46:19 +00:00
|
|
|
}
|
2021-04-30 21:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 22:46:10 +00:00
|
|
|
if ( objects.size() < 18 ) {
|
|
|
|
objects.resize( 18 );
|
2021-05-07 06:35:01 +00:00
|
|
|
}
|
2021-04-30 21:12:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 22:46:10 +00:00
|
|
|
// tuple - world object type, object, world character type, character, modifier
|
|
|
|
// type, modifier data
|
|
|
|
void saveMap( const std::string &file, std::vector< mapColumnType > &objects ) {
|
2021-04-30 21:12:53 +00:00
|
|
|
std::ofstream output_file;
|
2021-05-08 22:46:10 +00:00
|
|
|
output_file.open( file, std::ios::out | std::ios::binary );
|
2021-04-30 21:12:53 +00:00
|
|
|
uint16_t cols = objects.size();
|
2021-05-08 22:46:10 +00:00
|
|
|
output_file.write( ( char * )&cols, sizeof( uint16_t ) / sizeof( char ) );
|
|
|
|
for ( auto &col : objects ) {
|
|
|
|
for ( int i = 0; i < 16; i++ ) {
|
2021-04-30 21:12:53 +00:00
|
|
|
auto &obj = col[i];
|
2021-05-25 22:46:19 +00:00
|
|
|
uint16_t wide_type = std::get< MapObject::TERRAIN_TYPE >( obj );
|
2021-05-08 22:46:10 +00:00
|
|
|
wide_type = wide_type << 12;
|
2021-05-26 12:18:39 +00:00
|
|
|
uint16_t write_num =
|
|
|
|
( 0x0FFF & std::get< MapObject::TERRAIN_ID >( obj ) ) |
|
|
|
|
wide_type;
|
2021-05-25 22:46:19 +00:00
|
|
|
// character type can be 0 (overworld), modifier data can be 0
|
2021-05-26 12:18:39 +00:00
|
|
|
if ( std::get< MapObject::CHARACTER_ID >( obj ) ||
|
|
|
|
std::get< MapObject::MODIFIER_TYPE >( obj ) ) {
|
2021-05-01 19:55:43 +00:00
|
|
|
write_num |= 0x8000;
|
|
|
|
}
|
2021-05-08 22:46:10 +00:00
|
|
|
output_file.write( ( char * )&write_num,
|
|
|
|
sizeof( uint16_t ) / sizeof( char ) );
|
2021-05-01 19:55:43 +00:00
|
|
|
uint8_t additional_data = 0;
|
2021-05-25 22:46:19 +00:00
|
|
|
if ( std::get< MapObject::CHARACTER_ID >( obj ) ) {
|
2021-05-26 12:18:39 +00:00
|
|
|
additional_data |= std::get< MapObject::CHARACTER_TYPE >( obj )
|
|
|
|
<< 4;
|
2021-05-25 22:46:19 +00:00
|
|
|
additional_data |= std::get< MapObject::CHARACTER_ID >( obj );
|
|
|
|
} else if ( std::get< MapObject::MODIFIER_TYPE >( obj ) ) {
|
|
|
|
std::cout << "SAVING MODIFIERS!" << std::endl;
|
2021-05-26 12:18:39 +00:00
|
|
|
additional_data |= std::get< MapObject::MODIFIER_TYPE >( obj )
|
|
|
|
<< 4;
|
2021-05-01 19:55:43 +00:00
|
|
|
additional_data |= 0x80;
|
2021-05-25 22:46:19 +00:00
|
|
|
additional_data |= std::get< MapObject::MODIFIER_DATA >( obj );
|
2021-05-01 19:55:43 +00:00
|
|
|
}
|
2021-05-08 22:46:10 +00:00
|
|
|
if ( additional_data ) {
|
|
|
|
output_file.write( ( char * )&additional_data,
|
|
|
|
sizeof( uint8_t ) / sizeof( char ) );
|
2021-05-01 19:55:43 +00:00
|
|
|
}
|
2021-04-30 21:12:53 +00:00
|
|
|
}
|
2021-04-30 07:10:58 +00:00
|
|
|
}
|
2021-04-30 21:12:53 +00:00
|
|
|
output_file.close();
|
2021-04-25 20:42:55 +00:00
|
|
|
}
|