Skip to content

Instantly share code, notes, and snippets.

@ryonagana ryonagana/level.cpp
Created Sep 17, 2019

Embed
What would you like to do?
Level
#include "include/engine/level/CB_Level.h"
#include "include/engine/CB_Debug.h"
#include <fstream>
CB_Level::CB_Level()
{
map_tiles = new CB_Tile[ MAX_GRID_SIZE_W * MAX_GRID_SIZE_H ];
InitFreshMap();
}
CB_Tile *CB_Level::getTile(Uint32 x, Uint32 y)
{
if(map_tiles == nullptr) {
DEBUG_LOG("CB_Level::getTile - Map Tiles is NULL");
return nullptr;
}
return &map_tiles[ y * MAX_GRID_SIZE_W + x];
}
void CB_Level::setTile(Uint32 x, Uint32 y, Uint32 id, Uint32 flags)
{
if(map_tiles == nullptr){
DEBUG_LOG("CB_Level::setTile - Map Tiles is NULL");
return;
}
map_tiles[y * MAX_GRID_SIZE_W + x].id = id;
map_tiles[y * MAX_GRID_SIZE_W + x].flags = flags;
}
void CB_Level::InitFreshMap()
{
for(Uint32 i = 0; i < MAX_GRID_SIZE_W * MAX_GRID_SIZE_H; i++){
map_tiles[i].id = TileType::TileNone;
map_tiles[i].flags = TileFlags::TileNoCollision;
}
background_id = BG_BLACK;
width = 20;
height = 20;
}
CB_Level::CB_MapLoadResult CB_Level::LoadMap(std::string filename)
{
std::ifstream in(filename, std::ifstream::binary);
if(in.bad()){
DEBUG_LOG("Error Trying to load Map! %s", filename.c_str());
return MAP_ERROR;
}
in.read(this->magic, sizeof(char) * strlen(MAP_HEADER));
if(!CB_Level::checkHeader(this->magic)){
DEBUG_LOG("CB_Level::LoadMap: WRONG MAP FORMAT! - %s", filename.c_str());
return MAP_ERROR;
}
version = static_cast<int>(in.get());
width = static_cast<Uint32>(in.get());
height = static_cast<Uint32>(in.get());
background_id = static_cast<unsigned char>(in.get());
for(int i = 0; i < 6;i++){
keys[i].x = static_cast<Uint32>(in.get());
keys[i].y = static_cast<Uint32>(in.get());
}
CB_Level::getTilesFromFile(in, map_tiles, width, height);
in.close();
return MAP_SAVED;
}
CB_Level::CB_MapLoadResult CB_Level::SaveMap(std::string filename)
{
std::ofstream out(filename, std::ofstream::binary);
char magic[6] = {0};
strncpy(magic, MAP_HEADER, 6);
if(out.bad()){
return CB_MapLoadResult::MAP_ERROR;
}
out.write(magic, sizeof(char) * strlen(MAP_HEADER));
out.put(MAP_VERSION_ID);
out.put(static_cast<char>(width));
out.put(static_cast<char>(height));
out.put(static_cast<char>(background_id));
for(int i = 0; i < 6;i++){
out.put(static_cast<char>(keys[i].x));
out.put(static_cast<char>(keys[i].y));
}
CB_Level::writeTilesFromFile(out, map_tiles, width, height);
out.close();
return MAP_SAVED;
}
bool CB_Level::checkHeader(const char magic[6])
{
if(magic[0] == 'C' && magic[1] == 'B' && magic[2] == 'M' && magic[3] == 'A' && magic[4] == 'P'){
return true;
}
return false;
}
void CB_Level::writeTilesFromFile(std::ofstream &fs, CB_Tile *tilemap, int width, int height)
{
for(Uint32 i = 0; i < width * height; i++){
fs.put(static_cast<char>(tilemap[i].id));
fs.put(static_cast<char>(tilemap[i].flags));
}
}
void CB_Level::getTilesFromFile(std::ifstream &fs, CB_Tile *tilemap, int width, int height)
{
for(Uint32 i = 0; i < width * height; i++){
tilemap[i].id = static_cast<Uint32>(fs.get());
tilemap[i].flags = static_cast<Uint32>(fs.get());
}
}
void CB_Level::Draw()
{
for (Uint32 y = 0; y < height; y++) {
for(Uint32 x = 0; x < width; x++){
//map_tiles[(y + width) + x];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.