Basic editor functionality arrived!
Terrible code, must be changed!!!
This commit is contained in:
parent
183bd53b66
commit
61e45e89a5
@ -70,19 +70,30 @@ SDL_Rect getSourceRectByID( uint64_t id, BlockType type ) {
|
||||
}
|
||||
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 >
|
||||
createTerrainBlock( uint64_t block_id, BlockType type,
|
||||
std::shared_ptr< SDLPP::Renderer > &renderer, double x,
|
||||
double y, bool collision ) {
|
||||
return createBlock( renderer, x, y, g_terrain_texture,
|
||||
getSourceRectByID( block_id, type ), block_id,
|
||||
collision );
|
||||
return createTerrainBlock(block_id, type, renderer, x, y, g_terrain_texture, collision);
|
||||
}
|
||||
|
||||
std::shared_ptr< SDLPP::RectangleRender >
|
||||
createTerrainBlock( uint64_t block_id, BlockType type,
|
||||
std::shared_ptr< SDLPP::Renderer > &renderer,
|
||||
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 );
|
||||
}
|
||||
|
@ -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, 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
|
||||
|
164
mario/editor.cpp
164
mario/editor.cpp
@ -14,10 +14,43 @@
|
||||
#include "objectids.hpp"
|
||||
#include "blocks.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;
|
||||
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 ) {
|
||||
switch ( key ) {
|
||||
@ -70,8 +103,64 @@ void pollEvents( SDLPP::Scene &scene ) {
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
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:
|
||||
break;
|
||||
}
|
||||
@ -106,11 +195,13 @@ int main() {
|
||||
// prepare global vars
|
||||
g_terrain_texture = std::make_shared< SDLPP::Texture >(
|
||||
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 bg = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
|
||||
bg->setStatic();
|
||||
bg->setPermanent();
|
||||
bg->setId( 1 );
|
||||
scene->addObject( bg );
|
||||
|
||||
@ -120,14 +211,14 @@ int main() {
|
||||
for ( int i = 1; i < 20; i++ ) {
|
||||
auto line_vertical = std::make_shared< SDLPP::LineRenderer >(
|
||||
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 );
|
||||
scene->addObject(line_vertical);
|
||||
if(i > 2) {
|
||||
auto line_horizontal = std::make_shared< SDLPP::LineRenderer >(
|
||||
BLOCK_SIZE, ( i + 1 ) * BLOCK_SIZE, 19 * BLOCK_SIZE,
|
||||
( i + 1 ) * BLOCK_SIZE, renderer, "#282828" );
|
||||
line_horizontal->setStatic();
|
||||
line_horizontal->setPermanent();
|
||||
line_horizontal->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
||||
scene->addObject(line_horizontal);
|
||||
}
|
||||
@ -136,18 +227,60 @@ int main() {
|
||||
auto rectangle1 = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 4 * BLOCK_SIZE, BLOCK_SIZE, 16 * BLOCK_SIZE, renderer, "#FFFFFF88", true);
|
||||
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));
|
||||
scene->addObject(rectangle1);
|
||||
// white rectangles
|
||||
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->setId(1);
|
||||
rectangle2->setId(RIGHT_ID);
|
||||
rectangle2->setPermanent();
|
||||
rectangle2->addCollision(SDLPP::RectColider(0, 0, 1, 1));
|
||||
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;
|
||||
SDL_initFramerate( &gFPS );
|
||||
SDL_setFramerate( &gFPS, 60 );
|
||||
@ -167,8 +300,21 @@ int main() {
|
||||
frames = 0;
|
||||
base = SDL_GetTicks();
|
||||
}
|
||||
if(current_start_index == 0) {
|
||||
left->setTextColor(font, "#CCCCCC", "#CCCCCC", 0.05);
|
||||
} else {
|
||||
left->setTextColor(font, "#000000", "#282828", 0.05);
|
||||
}
|
||||
// saveMap("test_binary.bin", objects);
|
||||
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_binary2.bin", objects);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,3 +2,4 @@
|
||||
#include "../sdlpp/sdlpp_texture.hpp"
|
||||
|
||||
std::shared_ptr< SDLPP::Texture > g_terrain_texture{};
|
||||
std::shared_ptr< SDLPP::Texture > g_mario_texture{};
|
||||
|
@ -4,5 +4,6 @@
|
||||
#include "../sdlpp/sdlpp.hpp"
|
||||
|
||||
extern std::shared_ptr< SDLPP::Texture > g_terrain_texture;
|
||||
extern std::shared_ptr< SDLPP::Texture > g_mario_texture;
|
||||
|
||||
#endif
|
||||
|
@ -212,9 +212,9 @@ int main() {
|
||||
mario->addCollision(
|
||||
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) );
|
||||
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(
|
||||
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 );
|
||||
scene->addObject( mario );
|
||||
|
||||
@ -240,7 +240,7 @@ int main() {
|
||||
leftStop->setColiderColor( "#FF00FF" );
|
||||
scene->addObject( leftStop );
|
||||
|
||||
loadMap( scene, mario, "testmap.txt", renderer );
|
||||
loadMap( scene, mario, "test_binary2.bin", renderer );
|
||||
|
||||
FPSmanager gFPS;
|
||||
SDL_initFramerate( &gFPS );
|
||||
|
@ -5,119 +5,53 @@
|
||||
#include "blocks.hpp"
|
||||
#include "objectids.hpp"
|
||||
|
||||
uint64_t decodeObject( char obj ) {
|
||||
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;
|
||||
}
|
||||
|
||||
// TODO move to one function
|
||||
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;
|
||||
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;
|
||||
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] == ' ')
|
||||
continue;
|
||||
auto id = decodeObject(buffer[j]);
|
||||
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);
|
||||
std::ifstream map_file;
|
||||
map_file.open(file, std::ios::in | std::ios::binary);
|
||||
uint16_t cols;
|
||||
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++) {
|
||||
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));
|
||||
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));
|
||||
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;
|
||||
}
|
||||
}
|
||||
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)
|
||||
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);
|
||||
}
|
||||
|
||||
/*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::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) {
|
||||
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;
|
||||
map_file.open(file, std::ios::in | std::ios::binary);
|
||||
uint16_t cols;
|
||||
@ -127,22 +61,45 @@ void loadMap(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file, std:
|
||||
auto &col = objects[i];
|
||||
for(int j = 0; j < 16; j++) {
|
||||
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));
|
||||
uint8_t type = (input_number & 0xF000)>>12;
|
||||
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;
|
||||
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)
|
||||
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;
|
||||
output_file.open(file, std::ios::out | std::ios::binary);
|
||||
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(int i = 0; i < 16; i++) {
|
||||
auto &obj = col[i];
|
||||
uint16_t wide_type = obj.first;
|
||||
uint16_t wide_type = std::get<0>(obj);
|
||||
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));
|
||||
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();
|
||||
|
@ -5,7 +5,7 @@
|
||||
#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, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer, std::vector<std::array<std::pair<uint8_t, uint16_t>,16>> &objects);
|
||||
void saveMap(const std::string &file, 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::tuple<uint8_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t>,16>> &objects);
|
||||
|
||||
#endif
|
||||
|
@ -18,7 +18,9 @@
|
||||
#define CLOUD_LEFT_TOP_ID 0x700E
|
||||
#define CLOUD_MIDDLE_TOP_ID 0x700F
|
||||
#define CLOUD_RIGHT_TOP_ID 0x7010
|
||||
#define MARIO_ID 0x7FFF
|
||||
|
||||
// character IDs
|
||||
#define MARIO_ID 0x0F
|
||||
|
||||
#define DEATH_ID 0x1001
|
||||
#define STOP_MOVEMENT 0x2000
|
||||
|
Loading…
Reference in New Issue
Block a user