Skip to content

Instantly share code, notes, and snippets.

@OperationDarkside
Last active October 11, 2017 20:27
Show Gist options
  • Save OperationDarkside/103a58c9dbc19f97cad70d9a7ea1e831 to your computer and use it in GitHub Desktop.
Save OperationDarkside/103a58c9dbc19f97cad70d9a7ea1e831 to your computer and use it in GitHub Desktop.
Block Loader - Could even contain the texture data
#ifndef TEMP_BLOCK_H
#define TEMP_BLOCK_H
//#include "BlockId.h"
#include <SFML/Graphics.hpp>
#include <vector>
#include <functional>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include "../../Util/NonCopyable.h"
#include "../../Util/Singleton.h"
enum class BlockMeshType {
Cube = 0,
X = 1,
};
// Stores block data
class Block {
public:
unsigned id; // Identifies a block
std::string name;
sf::Vector2i texTopCoord;
sf::Vector2i texSideCoord;
sf::Vector2i texBottomCoord;
BlockMeshType meshType;
bool isOpaque;
bool isCollidable;
};
// Handles all blocks
class BlockDB : public Singleton {
public:
static BlockDB& get();
void Load(std::string filename);
Block& operator[](std::string name);
Block& operator[](unsigned id);
private:
// dynamic storage for moddability
std::vector<Block> blocks;
// hashmap for faster named access
std::unordered_map<std::string, Block*> name_block_rel;
// hashmap for faster id access
std::unordered_map<unsigned, Block*> id_block_rel;
};
class CsvLoader {
public:
//template<typename T>
static void LoadFromFile(std::string filename, const size_t value_count, std::function<void(std::vector<std::string>)> func);
};
#endif // !TEMP_BLOCKDATA_H
// ---------------------------------------------------------------------
#include "TempBlockLoader.h"
void BlockDB::Load(std::string filename) {
CsvLoader::LoadFromFile(filename, 11, [&](std::vector<std::string> values) {
unsigned meshtype = 0, texture1 = 0, texture2 = 0;
Block block;
// ID
block.id = std::stoul(values[0]);
// Name
block.name = values[1];
// Top Texture
texture1 = std::stoul(values[2]);
texture2 = std::stoul(values[3]);
block.texTopCoord = {texture1, texture2};
// Side Texture
texture1 = std::stoul(values[4]);
texture2 = std::stoul(values[5]);
block.texSideCoord = {texture1, texture2};
// Bottom Texture
texture1 = std::stoul(values[6]);
texture2 = std::stoul(values[7]);
block.texBottomCoord = {texture1, texture2};
// Mesh
meshtype = std::stoul(values[8]);
block.meshType = static_cast<BlockMeshType>(meshtype);
// Opaqueness
block.isOpaque = std::stoul(values[9]);
// Collidable
block.isCollidable = std::stoul(values[10]);
blocks.push_back(block);
id_block_rel.insert({block.id, blocks.back()});
name_block_rel.insert({block.name, blocks.back()});
});
}
Block& BlockDB::operator[](std::string name) {
return *name_block_rel[name];
}
Block& BlockDB::operator[](unsigned id) {
return *id_block_rel[id];
}
BlockDB & BlockDB::get() {
static BlockDB db;
return db;
}
void CsvLoader::LoadFromFile(std::string filename, const size_t value_count, std::function<void(std::vector<std::string>)> func) {
std::ifstream inFile(filename);
// check open-ness
if(!inFile.is_open()) {
throw std::runtime_error("Unable to open file: " + filename + "!");
}
// read every line and get values
std::string line;
while(std::getline(inFile, line)) {
std::string val;
std::stringstream ss(line);
std::vector<std::string> values;
// take the line appart
while(std::getline(ss, val, ';')) {
values.push_back(val);
}
if(values.size() != value_count) {
throw std::runtime_error("Number of values doesn't match!");
}
func(values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment