Basic editor functionality arrived!
Terrible code, must be changed!!!
This commit is contained in:
+82
-109
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user