Skip to content

Commit

Permalink
Preliminary entity loading done #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruskiy69 committed Jul 5, 2013
1 parent 281e5bf commit 6431bfe
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 9 deletions.
116 changes: 114 additions & 2 deletions include/Zenderer/Objects/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "Zenderer/CoreGraphics/Drawable.hpp"
#include "Zenderer/Graphics/Material.hpp"
#include "Zenderer/Graphics/Quad.hpp"
#include "Zenderer/Utilities/INIParser.hpp"

namespace zen
{
Expand All @@ -44,17 +45,128 @@ namespace obj
/// A base class for all "in-game" objects.
class ZEN_API CEntity
{
protected:
// Internal error types.
enum class ErrorType : uint16_t
{
BAD_PAIR,
BAD_POSITION,
NO_TEXTURE
};

public:
CEntity();
virtual ~CEntity();

bool LoadFromFile(const string_t& filename);
/// @todo Move semantics on `util::split()`
bool LoadFromFile(const string_t& filename)
{
ZEN_ASSERT(!filename.empty());

util::CINIParser Parser;
std::ifstream file(filename);
std::string line;
uint32_t line_no = 0;

if(!file) return false;

gfx::CQuad* pPrim = nullptr;

while(std::getline(file, line))
{
++line_no;
if(line.empty() || line[0] == '/') continue;

if(line.find("position") != std::string::npos)
{
std::vector<string_t> pair = util::split(line, '=');
if(pair.size() != 2) return this->FileError(filename, line, line_no);

pair = util::split(line, ',');
if(pair.size() < 2)
return this->FileError(filename, line, line_no, ErrorType::BAD_POSITION);

this->Move(std::stod(pairs[0]), std::stod(pairs[1]);

// Depth is optional
if(pair.size() == 3) m_Position.z = std::stod(pairs[2]);
}

else if(line.find("<prim>") != std::string::npos)
{
if(pPrim != nullptr) mp_allPrims.push_back(pPrim);

pPrim = new gfx::CQuad(0, 0);
std::streampos start = file.tellg();

// Find end of primitive block.
while(std::getline(file, line) &&
line.find("</prim>") == std::string::npos);

std::streampos end = file.tellg();

Parser.LoadFromStream(file, start, end, filename.c_str());

// We have loaded key=value pairs for a primitive instance.
if(!Parser.Exists("texture"))
return this->FileError(filename, line, line_no, ErrorType::NO_TEXTURE);

gfx::CMaterial* pMat = new gfx::CMaterial(m_Assets);
pMat->LoadFromStream(file, start, end);

if(Parser.Exists("width") && Parser.Exists("height"))
pPrim->Resize(Parser.GetValuei("width"), Parser.GetValuei("height"));
else
pPrim->Resize(pMat->GetTexture()->GetWidth(),
pMat->GetTexture()->GetHeight());

if(Parser.Exists("invert")) pPrim->SetInvertible(Parser.GetValueb("invert"));
if(Parser.Exists("repeat")) pPrim->SetRepeatable(Parser.GetValueb("repeat"));
}
}

m_filename = filename;
return true;
}

bool LoadFromTexture(const string_t& filename);
bool AddPrimitive(const CQuad& Prim);
bool Create();
bool Draw(bool is_bound = false);

protected:
protected:
bool FileError(const string_t& filename,
const string_t& line, const uint32_t line_no,
const ErrorType& Err = ErrorType::BAD_PAIR)
{
m_Log << m_Log.SetMode(LogMode::ZEN_ERROR) << m_Log.SetSystem("Entity")
<< "Error while parsing '" << filename << "' on line " << line_no
<< ": " << line << "(";

switch(Err)
{
case ErrorType::BAD_PAIR:
m_Log << "bad key=value pair";
break;

case ErrorType::BAD_POSITION:
m_Log << "position must at least contain x,y coordinates";
break;

case ErrorType::NO_TEXTURE:
m_Log << "no texture specified for primitive";
break;

default:
m_Log << "parsing error";
break;
}

m_Log << ")." << CLog::endl;
return false;
}

string_t m_filename;
std::vector<CQuad*> mp_allPrims;
};

Expand Down
13 changes: 6 additions & 7 deletions include/Zenderer/Zenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ namespace zen
* <mesh>
* width=64
* height=64
* repeat=1
* invert=1
* repeat=true
* invert=true
* texture=textures/grass.png
* shader=shaders/default.vs,shaders/GaussianBlurH.fs
* params=radius:0.005
Expand Down Expand Up @@ -427,13 +427,12 @@ namespace zen
* loaded, as opposed to the material file.
*
* If the shader files are left off, the default will be used (like for primitives).
* If the size is specified (`width`/`height`), *both* must be specified. Otherwise,
* the default values are used for both of them.
*
* The `primcount` option will allow for a tiny speed up in loading, but will only
* make a real difference if the entity contains dozens of primitives.
*
* For the boolean parameters (like `invert` and `repeat`), use `0` and `1` as
* opposed to `true` and `false`.
*
* @see zen::gfx::CQuad
*
* @subsubsection ZEntEx Example File
Expand Down Expand Up @@ -465,8 +464,8 @@ namespace zen
* width=64
* height=64
*
* repeat=1
* invert=1
* repeat=true
* invert=true
* </prim>
*
* // Nothing special about this one
Expand Down

0 comments on commit 6431bfe

Please sign in to comment.