2021-04-25 20:42:55 +00:00
|
|
|
#include "maploader.hpp"
|
|
|
|
#include "../sdlpp/sdlpp_rectrenderer.hpp"
|
|
|
|
#include <fstream>
|
|
|
|
#include "sprites.hpp"
|
|
|
|
#include "blocks.hpp"
|
|
|
|
#include "objectids.hpp"
|
|
|
|
|
2021-04-30 21:12:53 +00:00
|
|
|
uint64_t decodeObject( char obj ) {
|
2021-04-25 20:42:55 +00:00
|
|
|
switch(obj) {
|
|
|
|
case 'F':
|
2021-04-30 21:12:53 +00:00
|
|
|
return FLOOR_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'I':
|
2021-04-30 21:12:53 +00:00
|
|
|
return HILL_INCLINE_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'R':
|
2021-04-30 21:12:53 +00:00
|
|
|
return HILL_DOTS_RIGHT_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'G':
|
2021-04-30 21:12:53 +00:00
|
|
|
return HILL_FILL_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'L':
|
2021-04-30 21:12:53 +00:00
|
|
|
return HILL_DOTS_LEFT_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'D':
|
2021-04-30 21:12:53 +00:00
|
|
|
return HILL_DECLINE_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'T':
|
2021-04-30 21:12:53 +00:00
|
|
|
return HILL_TOP_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'q':
|
2021-04-30 21:12:53 +00:00
|
|
|
return BUSH_LEFT_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'w':
|
2021-04-30 21:12:53 +00:00
|
|
|
return BUSH_MIDDLE_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'r':
|
2021-04-30 21:12:53 +00:00
|
|
|
return BUSH_RIGHT_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'a':
|
2021-04-30 21:12:53 +00:00
|
|
|
return CLOUD_LEFT_BOTTOM_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 's':
|
2021-04-30 21:12:53 +00:00
|
|
|
return CLOUD_MIDDLE_BOTTOM_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'd':
|
2021-04-30 21:12:53 +00:00
|
|
|
return CLOUD_RIGHT_BOTTOM_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'z':
|
2021-04-30 21:12:53 +00:00
|
|
|
return CLOUD_LEFT_TOP_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'x':
|
2021-04-30 21:12:53 +00:00
|
|
|
return CLOUD_MIDDLE_TOP_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
case 'c':
|
2021-04-30 21:12:53 +00:00
|
|
|
return CLOUD_RIGHT_TOP_ID;
|
2021-04-25 20:42:55 +00:00
|
|
|
}
|
2021-04-30 21:12:53 +00:00
|
|
|
return 0;
|
2021-04-25 20:42:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 07:10:58 +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-04-25 20:42:55 +00:00
|
|
|
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;
|
2021-04-29 10:33:31 +00:00
|
|
|
for(int i = 0; i < rows; i++) {
|
2021-04-25 20:42:55 +00:00
|
|
|
std::getline(mapFile, buffer);
|
|
|
|
auto cur_x = -BLOCK_SIZE;
|
2021-04-29 10:33:31 +00:00
|
|
|
for(int j = 0; j < cols; j++) {
|
2021-04-25 20:42:55 +00:00
|
|
|
cur_x += BLOCK_SIZE;
|
|
|
|
if(buffer[j] == ' ')
|
|
|
|
continue;
|
2021-04-30 21:12:53 +00:00
|
|
|
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);
|
2021-04-25 20:42:55 +00:00
|
|
|
if(obj != nullptr)
|
|
|
|
scene->addObject(obj);
|
|
|
|
}
|
|
|
|
cur_y += BLOCK_SIZE;
|
|
|
|
}
|
2021-04-30 21:12:53 +00:00
|
|
|
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) {
|
|
|
|
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<BlockType>(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<std::array<std::pair<uint8_t, uint16_t>,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));
|
|
|
|
}
|
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
|
|
|
}
|