Basic editor functionality arrived!

Terrible code, must be changed!!!
This commit is contained in:
zvon 2021-05-01 21:55:43 +02:00
parent 183bd53b66
commit 61e45e89a5
9 changed files with 265 additions and 128 deletions

View File

@ -70,19 +70,30 @@ 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 ) {
return createBlock( renderer, x, y, texture,
getSourceRectByID( block_id, type ), block_id,
collision );
}
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 ) {
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, BlockType type, createTerrainBlock( uint64_t block_id, BlockType type,
std::shared_ptr< SDLPP::Renderer > &renderer, double x, std::shared_ptr< SDLPP::Renderer > &renderer, double x,
double y, bool collision ) { double y, bool collision ) {
return createBlock( renderer, x, y, g_terrain_texture, return createTerrainBlock(block_id, type, renderer, x, y, g_terrain_texture, collision);
getSourceRectByID( block_id, type ), block_id,
collision );
} }
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, std::shared_ptr< SDLPP::Renderer > &renderer,
bool collision ) { bool collision ) {
return createTerrainBlock( block_id, type, renderer, 0, 0, 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 ) {
//TODO add type additions
return createBlock( renderer, x, y, g_mario_texture, MARIO_STANDING_SRC, MARIO_ID, true );
} }

View File

@ -15,5 +15,8 @@ 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, 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, 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> createMario( BlockType type, std::shared_ptr<SDLPP::Renderer> &renderer, double x, double y );
#endif #endif

View File

@ -14,10 +14,43 @@
#include "objectids.hpp" #include "objectids.hpp"
#include "blocks.hpp" #include "blocks.hpp"
#include "maploader.hpp" #include "maploader.hpp"
#include "../sdlpp/sdlpp_mouse.hpp"
#define EDIT_SQUARE 0xF001
#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::pair<uint8_t, uint16_t>,16>> objects = {}; 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 selected_left = false;
bool selected_right = false;
int current_start_index = 0;
int current_max_index = 0;
std::pair<int, int> current_box = {0, 0};
std::pair<int, int> backup_box = {0, 0};
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 ) {
@ -70,8 +103,64 @@ void pollEvents( SDLPP::Scene &scene ) {
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
scene.updateSizeAndPosition(); update_size = true;
} }
break;
case SDL_MOUSEMOTION: {
auto mouse = scene.getObjects({MOUSE_ID})[0];
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
auto collisions = scene.getCollisions(*mouse);
selected_left = false;
selected_right = false;
current_box = {-1, -1};
if(collisions.size() >= 2) {
for(auto &collision : collisions) {
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;
case SDL_MOUSEBUTTONUP:
if(selected_left && current_start_index != 0) {
current_start_index -= 1;
scene.moveEverything(BLOCK_SIZE, 0);
}
if(selected_right && current_start_index != current_max_index) {
current_start_index += 1;
scene.moveEverything(-BLOCK_SIZE,0);
}
if(current_box == backup_box && current_box.first != -1) {
auto mouse = scene.getObjects({MOUSE_ID})[0];
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
auto collisions = scene.getCollisions(*mouse);
std::cout << "BOUT TO" << std::endl;
bool deleted = false;
for(auto &collision : collisions) {
if(collision.second->getId() == FLOOR_ID) {
std::cout << "DELETING" << std::endl;
collision.second->destroy();
objects[current_start_index + current_box.first][current_box.second] = {OVERWORLD, 0, 0, 0, 0, 0};
deleted = true;
break;
}
}
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;
case SDL_MOUSEBUTTONDOWN:
backup_box = current_box;
break;
default: default:
break; break;
} }
@ -106,11 +195,13 @@ int main() {
// prepare global vars // prepare global vars
g_terrain_texture = std::make_shared< SDLPP::Texture >( g_terrain_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY ); renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_mario_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
auto scene = std::make_shared< SDLPP::Scene >( renderer ); auto scene = std::make_shared< SDLPP::Scene >( renderer );
auto bg = std::make_shared< SDLPP::RectangleRender >( auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true ); 0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
bg->setStatic(); bg->setPermanent();
bg->setId( 1 ); bg->setId( 1 );
scene->addObject( bg ); scene->addObject( bg );
@ -120,14 +211,14 @@ int main() {
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, renderer, "#282828" ); i * BLOCK_SIZE, 1 - 16 * BLOCK_SIZE, i * BLOCK_SIZE, 1.0, renderer, "#282828" );
line_vertical->setStatic(); 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, renderer, "#282828" ); ( i + 1 ) * BLOCK_SIZE, renderer, "#282828" );
line_horizontal->setStatic(); line_horizontal->setPermanent();
line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER ); line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene->addObject(line_horizontal); scene->addObject(line_horizontal);
} }
@ -136,18 +227,60 @@ 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(1); rectangle1->setId(LEFT_ID);
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, 20*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(1); rectangle2->setId(RIGHT_ID);
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_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, renderer, "<", font_config);
left->setId(LEFT_ID);
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, renderer, ">", font_config);
right->setId(RIGHT_ID);
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, renderer));
}
}
auto mouse =
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0.01, 0.01, renderer );
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mouse->setId( MOUSE_ID );
mouse->setColiderColor("#00FF00");
mouse->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene->addObject( mouse );
current_max_index = objects.size() - 18;
auto placeholder_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
placeholderGround = createTerrainBlock(FLOOR_ID, OVERWORLD, renderer, placeholder_texture, true);
placeholderGround->setTextureAlpha(100);
placeholderGround->setId(0);
scene->addObject(placeholderGround);
scene->moveZTop(placeholderGround);
scene->moveEverything(BLOCK_SIZE, 0);
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate( &gFPS ); SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 ); SDL_setFramerate( &gFPS, 60 );
@ -167,8 +300,21 @@ int main() {
frames = 0; frames = 0;
base = SDL_GetTicks(); base = SDL_GetTicks();
} }
if(current_start_index == 0) {
left->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
} else {
left->setTextColor(font, "#000000", "#282828", 0.05);
}
if(current_start_index == current_max_index) {
right->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
} else {
right->setTextColor(font, "#000000", "#282828", 0.05);
}
if(update_size) {
scene->updateSizeAndPosition();
}
} }
// saveMap("test_binary.bin", objects); saveMap("test_binary2.bin", objects);
return 0; return 0;
} }

View File

@ -2,3 +2,4 @@
#include "../sdlpp/sdlpp_texture.hpp" #include "../sdlpp/sdlpp_texture.hpp"
std::shared_ptr< SDLPP::Texture > g_terrain_texture{}; std::shared_ptr< SDLPP::Texture > g_terrain_texture{};
std::shared_ptr< SDLPP::Texture > g_mario_texture{};

View File

@ -4,5 +4,6 @@
#include "../sdlpp/sdlpp.hpp" #include "../sdlpp/sdlpp.hpp"
extern std::shared_ptr< SDLPP::Texture > g_terrain_texture; extern std::shared_ptr< SDLPP::Texture > g_terrain_texture;
extern std::shared_ptr< SDLPP::Texture > g_mario_texture;
#endif #endif

View File

@ -212,9 +212,9 @@ int main() {
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) ); SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) );
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0, 0, 0.1, 0.9, MARIO_LEFT_SIDE_DETECT ) ); SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
mario->addCollision( mario->addCollision(
SDLPP::RectColider( 0.9, 0, 0.1, 0.9, MARIO_RIGHT_SIDE_DETECT ) ); SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
mario->setStatic( false ); mario->setStatic( false );
scene->addObject( mario ); scene->addObject( mario );
@ -240,7 +240,7 @@ int main() {
leftStop->setColiderColor( "#FF00FF" ); leftStop->setColiderColor( "#FF00FF" );
scene->addObject( leftStop ); scene->addObject( leftStop );
loadMap( scene, mario, "testmap.txt", renderer ); loadMap( scene, mario, "test_binary2.bin", renderer );
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate( &gFPS ); SDL_initFramerate( &gFPS );

View File

@ -5,119 +5,53 @@
#include "blocks.hpp" #include "blocks.hpp"
#include "objectids.hpp" #include "objectids.hpp"
uint64_t decodeObject( char obj ) { // TODO move to one function
switch(obj) {
case 'F':
return FLOOR_ID;
case 'I':
return HILL_INCLINE_ID;
case 'R':
return HILL_DOTS_RIGHT_ID;
case 'G':
return HILL_FILL_ID;
case 'L':
return HILL_DOTS_LEFT_ID;
case 'D':
return HILL_DECLINE_ID;
case 'T':
return HILL_TOP_ID;
case 'q':
return BUSH_LEFT_ID;
case 'w':
return BUSH_MIDDLE_ID;
case 'r':
return BUSH_RIGHT_ID;
case 'a':
return CLOUD_LEFT_BOTTOM_ID;
case 's':
return CLOUD_MIDDLE_BOTTOM_ID;
case 'd':
return CLOUD_RIGHT_BOTTOM_ID;
case 'z':
return CLOUD_LEFT_TOP_ID;
case 'x':
return CLOUD_MIDDLE_TOP_ID;
case 'c':
return CLOUD_RIGHT_TOP_ID;
}
return 0;
}
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::fstream mapFile; std::ifstream map_file;
mapFile.open(file, std::ios::in); map_file.open(file, std::ios::in | std::ios::binary);
std::string buffer; uint16_t cols;
std::getline(mapFile, buffer); map_file.read((char*)&cols, sizeof(uint16_t)/sizeof(char));
auto cols = std::stoi(buffer); for(uint16_t i = 0; i < cols; i++) {
std::getline(mapFile, buffer); for(int j = 0; j < 16; j++) {
auto rows = std::stoi(buffer); uint16_t input_number;
std::getline(mapFile, buffer); uint8_t additional_data = 0;
auto mario_x = std::stoi(buffer); uint8_t character_type = 0, character = 0, modifier_type = 0, modifier_data = 0;
std::getline(mapFile, buffer); map_file.read((char *)&input_number, sizeof(uint16_t)/sizeof(char));
auto mario_y = std::stoi(buffer); uint8_t type = (input_number & 0xF000)>>12;
auto cur_y = 1 - rows * BLOCK_SIZE; uint16_t id = (input_number & 0x0FFF) | BLOCK_PREFIX;
for(int i = 0; i < rows; i++) { if(type & 0x8) {
std::getline(mapFile, buffer); map_file.read((char *)&additional_data, sizeof(uint8_t)/sizeof(char));
auto cur_x = -BLOCK_SIZE; type &= ~0x8;
for(int j = 0; j < cols; j++) { if(additional_data & 0x80) {
cur_x += BLOCK_SIZE; //modifier
if(buffer[j] == ' ') additional_data &= ~0x80;
continue; modifier_type = (additional_data & 0xF0)>>4;
auto id = decodeObject(buffer[j]); modifier_data = additional_data & 0x0F;
std::shared_ptr<SDLPP::RectangleRender> obj = nullptr; } else {
if(id == FLOOR_ID) // character
obj = createTerrainBlock(id, OVERWORLD, renderer, cur_x, cur_y, true); character_type = (additional_data & 0xF0)>>4;
else character = additional_data & 0x0F;
obj = createTerrainBlock(id, OVERWORLD, renderer, cur_x, cur_y); }
}
bool collision = false;
if(id == FLOOR_ID) {
collision = true;
}
// TODO add modifiers to createTerrainBlock
auto obj = createTerrainBlock(id, static_cast<BlockType>(type), renderer, i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE, collision);
if(obj != nullptr) if(obj != nullptr)
scene->addObject(obj); scene->addObject(obj);
if(character) {
if(character == MARIO_ID) {
mario->setPos(i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE);
}
}
} }
cur_y += BLOCK_SIZE;
} }
mario->setPos(mario_x * BLOCK_SIZE, 1 - (rows - mario_y) * BLOCK_SIZE);
scene->moveZTop(mario); scene->moveZTop(mario);
} }
/*void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<std::array<std::pair<uint8_t, uint16_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::fstream mapFile;
mapFile.open(file, std::ios::in);
std::string buffer;
std::getline(mapFile, buffer);
auto cols = std::stoi(buffer);
std::getline(mapFile, buffer);
auto rows = std::stoi(buffer);
std::getline(mapFile, buffer);
auto mario_x = std::stoi(buffer);
std::getline(mapFile, buffer);
auto mario_y = std::stoi(buffer);
auto cur_y = 1 - rows * BLOCK_SIZE;
objects.resize(cols);
for(int i = 0; i < rows; i++) {
std::getline(mapFile, buffer);
auto cur_x = -BLOCK_SIZE;
for(int j = 0; j < cols; j++) {
cur_x += BLOCK_SIZE;
if(buffer[j] == ' ') {
objects[j][i] = {OVERWORLD, 0};
continue;
}
auto id = decodeObject(buffer[j]);
objects[j][i] = {OVERWORLD, id};
std::shared_ptr<SDLPP::RectangleRender> obj = nullptr;
if(id == FLOOR_ID)
obj = createTerrainBlock(id, OVERWORLD, renderer, cur_x, cur_y, true);
else
obj = createTerrainBlock(id, OVERWORLD, renderer, cur_x, cur_y);
if(obj != nullptr)
scene->addObject(obj);
}
cur_y += BLOCK_SIZE;
}
objects[mario_x][mario_y] = {OVERWORLD, MARIO_ID};
}*/
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<std::array<std::pair<uint8_t, uint16_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);
uint16_t cols; uint16_t cols;
@ -127,22 +61,45 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std:
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 character_type = 0, character = 0, modifier_type = 0, modifier_data = 0;
map_file.read((char *)&input_number, sizeof(uint16_t)/sizeof(char)); map_file.read((char *)&input_number, sizeof(uint16_t)/sizeof(char));
uint8_t type = (input_number & 0xF000)>>12; uint8_t type = (input_number & 0xF000)>>12;
uint16_t id = (input_number & 0x0FFF) | BLOCK_PREFIX; uint16_t id = (input_number & 0x0FFF) | BLOCK_PREFIX;
col[i] = {type, id}; if(type & 0x8) {
map_file.read((char *)&additional_data, sizeof(uint8_t)/sizeof(char));
type &= ~0x8;
if(additional_data & 0x80) {
//modifier
additional_data &= ~0x80;
modifier_type = (additional_data & 0xF0)>>4;
modifier_data = additional_data & 0x0F;
} else {
// character
character_type = (additional_data & 0xF0)>>4;
character = additional_data & 0x0F;
}
}
col[j] = {type, id, character_type, character, modifier_type, modifier_data};
bool collision = false; bool collision = false;
if(id == FLOOR_ID) { if(id == FLOOR_ID) {
collision = true; collision = true;
} }
// 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 * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE, collision);
if(obj != nullptr) if(obj != nullptr)
scene->addObject(obj); scene->addObject(obj);
if(character) {
if(character == MARIO_ID) {
scene->addObject(createMario(static_cast<BlockType>(character_type), renderer, i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE));
}
}
} }
} }
} }
void saveMap(const std::string &file, std::vector<std::array<std::pair<uint8_t, uint16_t>,16>> &objects) { // tuple - world object type, object, world character type, character, modifier type, modifier data
void saveMap(const std::string &file, std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> &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();
@ -150,10 +107,26 @@ void saveMap(const std::string &file, std::vector<std::array<std::pair<uint8_t,
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 = obj.first; uint16_t wide_type = std::get<0>(obj);
wide_type = wide_type<<12; wide_type = wide_type<<12;
uint16_t write_num = (0x0FFF & obj.second) | 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)
if(std::get<3>(obj) || std::get<4>(obj)) {
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;
if(std::get<3>(obj)) {
additional_data |= std::get<2>(obj)<<4;
additional_data |= std::get<3>(obj);
} else if(std::get<4>(obj)) {
additional_data |= std::get<4>(obj)<<4;
additional_data |= 0x80;
additional_data |= std::get<5>(obj);
}
if(additional_data) {
output_file.write((char*)&additional_data, sizeof(uint8_t)/sizeof(char));
}
} }
} }
output_file.close(); output_file.close();

View File

@ -5,7 +5,7 @@
#include "../sdlpp/sdlpp_rectrenderer.hpp" #include "../sdlpp/sdlpp_rectrenderer.hpp"
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);
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<std::array<std::pair<uint8_t, uint16_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);
void saveMap(const std::string &file, std::vector<std::array<std::pair<uint8_t, uint16_t>,16>> &objects); void saveMap(const std::string &file, std::vector<std::array<std::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> &objects);
#endif #endif

View File

@ -18,7 +18,9 @@
#define CLOUD_LEFT_TOP_ID 0x700E #define CLOUD_LEFT_TOP_ID 0x700E
#define CLOUD_MIDDLE_TOP_ID 0x700F #define CLOUD_MIDDLE_TOP_ID 0x700F
#define CLOUD_RIGHT_TOP_ID 0x7010 #define CLOUD_RIGHT_TOP_ID 0x7010
#define MARIO_ID 0x7FFF
// character IDs
#define MARIO_ID 0x0F
#define DEATH_ID 0x1001 #define DEATH_ID 0x1001
#define STOP_MOVEMENT 0x2000 #define STOP_MOVEMENT 0x2000