#include "maploader.hpp" #include "../sdlpp/sdlpp_rectrenderer.hpp" #include #include "sprites.hpp" #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; } void loadMap(std::shared_ptr &scene, std::shared_ptr mario, const std::string &file, std::shared_ptr &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 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; } mario->setPos(mario_x * BLOCK_SIZE, 1 - (rows - mario_y) * BLOCK_SIZE); scene->moveZTop(mario); } /*void loadMap(std::shared_ptr &scene, const std::string &file, std::shared_ptr &renderer, std::vector,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 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 &scene, const std::string &file, std::shared_ptr &renderer, std::vector,16>> &objects) { 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)); objects.resize(cols); for(uint16_t i = 0; i < cols; i++) { auto &col = objects[i]; for(int j = 0; j < 16; j++) { uint16_t input_number; 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}; bool collision = false; if(id == FLOOR_ID) { collision = true; } auto obj = createTerrainBlock(id, static_cast(type), renderer, i * BLOCK_SIZE, 1 - (16-j) * BLOCK_SIZE, collision); if(obj != nullptr) scene->addObject(obj); } } } void saveMap(const std::string &file, std::vector,16>> &objects) { std::ofstream output_file; output_file.open(file, std::ios::out | std::ios::binary); uint16_t cols = objects.size(); output_file.write((char*)&cols, sizeof(uint16_t)/sizeof(char)); for(auto &col : objects) { for(int i = 0; i < 16; i++) { auto &obj = col[i]; uint16_t wide_type = obj.first; wide_type = wide_type<<12; uint16_t write_num = (0x0FFF & obj.second) | wide_type; output_file.write((char*)&write_num, sizeof(uint16_t)/sizeof(char)); } } output_file.close(); }