Reorganized classes & namespaces. Made sprite a component.

This commit is contained in:
Tiberiu Chibici 2016-12-02 22:20:04 +02:00
parent f255905c73
commit 33de4a8d1f
51 changed files with 648 additions and 580 deletions

View File

@ -3,7 +3,8 @@
<GameObject name="Level 1 Sword"> <GameObject name="Level 1 Sword">
<Transform /> <Transform />
<SpriteRenderer sprite="sprites/items/Lvl1Sword.sprite" /> <Sprite src="sprites/items/Lvl1Sword.sprite" />
<SpriteRenderer />
<Item name="Level 1 Sword" <Item name="Level 1 Sword"
description="Better than nothing." description="Better than nothing."
level="1" /> level="1" />
@ -15,7 +16,8 @@
<GameObject name="Level 2 Sword"> <GameObject name="Level 2 Sword">
<Transform /> <Transform />
<SpriteRenderer sprite="sprites/items/Lvl2Sword.sprite" /> <Sprite src="sprites/items/Lvl2Sword.sprite" />
<SpriteRenderer />
<Item name="Level 2 Sword" <Item name="Level 2 Sword"
description="Better than basic sword." description="Better than basic sword."
level="2" /> level="2" />

View File

@ -17,7 +17,8 @@
<!-- Player object --> <!-- Player object -->
<GameObject name="Player"> <GameObject name="Player">
<Transform x="120" y="100" /> <Transform x="120" y="100" />
<SpriteRenderer sprite="sprites/Player.sprite" /> <Sprite src="sprites/Player.sprite" />
<SpriteRenderer />
<PlayerController /> <PlayerController />
</GameObject> </GameObject>

View File

@ -19,23 +19,23 @@
</State> </State>
<State name="Walking right"> <State name="Walking right">
<Frame tileSet="tilesets/PlayerTiles.png" cell="0" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="0" w="1" h="2" duration="0.2" />
<Frame tileSet="tilesets/PlayerTiles.png" cell="1" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="1" w="1" h="2" duration="0.2" />
</State> </State>
<State name="Walking up"> <State name="Walking up">
<Frame tileSet="tilesets/PlayerTiles.png" cell="2" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="2" w="1" h="2" duration="0.2" />
<Frame tileSet="tilesets/PlayerTiles.png" cell="3" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="3" w="1" h="2" duration="0.2" />
</State> </State>
<State name="Walking left"> <State name="Walking left">
<Frame tileSet="tilesets/PlayerTiles.png" cell="4" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="4" w="1" h="2" duration="0.2" />
<Frame tileSet="tilesets/PlayerTiles.png" cell="5" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="5" w="1" h="2" duration="0.2" />
</State> </State>
<State name="Walking down"> <State name="Walking down">
<Frame tileSet="tilesets/PlayerTiles.png" cell="6" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="6" w="1" h="2" duration="0.2" />
<Frame tileSet="tilesets/PlayerTiles.png" cell="7" w="1" h="2" duration="7" /> <Frame tileSet="tilesets/PlayerTiles.png" cell="7" w="1" h="2" duration="0.2" />
</State> </State>
</Sprite> </Sprite>

View File

@ -5,7 +5,6 @@
<Frame tileSet="sprites/items/sword.png" cell="0" w="1" h="1" duration="1" /> <Frame tileSet="sprites/items/sword.png" cell="0" w="1" h="1" duration="1" />
</State> </State>
<State name="Attack"> <State name="Attack">
<Frame tileSet="sprites/items/sword.png" cell="0" w="1" h="1" duration="10" /> <Frame tileSet="sprites/items/sword.png" cell="1" w="1" h="1" duration="1" />
<Frame tileSet="sprites/items/sword.png" cell="1" w="1" h="1" duration="10" />
</State> </State>
</Sprite> </Sprite>

View File

@ -8,10 +8,10 @@
#ifndef MODEL_GAMESTATE_H_ #ifndef MODEL_GAMESTATE_H_
#define MODEL_GAMESTATE_H_ #define MODEL_GAMESTATE_H_
#include <base/GameObject.h> #include <model/GameObject.h>
#include <base/RenderContext.h>
#include <model/Scene.h> #include <model/Scene.h>
#include <model/Configuration.h> #include <model/Configuration.h>
#include <graphics/RenderContext.h>
#include <resources/ResourceManager.h> #include <resources/ResourceManager.h>
#include <memory> #include <memory>
@ -30,11 +30,11 @@ namespace farmlands {
model::Configuration* config; model::Configuration* config;
// Render context // Render context
base::RenderContext renderContext; graphics::RenderContext renderContext;
// Scene // Scene
model::Scene* scene; model::Scene* scene;
std::vector<base::GameObject*> itemPrefabs; std::vector<model::GameObject*> itemPrefabs;
// Misc // Misc
float elapsedTime; float elapsedTime;

View File

@ -1,52 +0,0 @@
/*
* RenderContext.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef GRAPHICS_RENDERCONTEXT_H_
#define GRAPHICS_RENDERCONTEXT_H_
#include <base/Viewport.h>
#include <SDL2/SDL.h>
namespace farmlands {
namespace base {
class GameObject;
struct Transform;
struct Camera;
class RenderContext
{
public:
float xToWorld(float x);
float yToWorld(float y);
float xToScreen(float x);
float yToScreen(float y);
bool visible(SDL_Rect& rect);
inline GameObject* cameraObj() { return m_cameraObj; }
inline Camera* camera() { return m_camera; }
inline Transform* cameraTransform() { return m_cameraTransform; }
void setCamera(GameObject* camera);
/**
* Screen properties
*/
Viewport viewport;
float uiScale = 1.0f;
private:
Transform* m_cameraTransform;
Camera* m_camera;
GameObject* m_cameraObj;
};
}
}
#endif /* GRAPHICS_RENDERCONTEXT_H_ */

View File

@ -5,13 +5,13 @@
* Author: tibi * Author: tibi
*/ */
#include <model/Background.h> #include <components/Background.h>
#include <utils/Assert.h> #include <utils/Assert.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace model { namespace components {
Background::Background(size_t layerCount, size_t rowCount, size_t columnCount) Background::Background(size_t layerCount, size_t rowCount, size_t columnCount)
@ -29,7 +29,7 @@ Background::~Background()
delete[] m_textures; delete[] m_textures;
} }
base::Component* Background::clone() model::Component* Background::clone()
{ {
Background* clone = new Background(m_layers, m_rows, m_columns); Background* clone = new Background(m_layers, m_rows, m_columns);
memcpy(clone->m_cells, m_cells, sizeof(Cell) * m_layers * m_rows * m_columns); memcpy(clone->m_cells, m_cells, sizeof(Cell) * m_layers * m_rows * m_columns);

View File

@ -8,21 +8,21 @@
#ifndef MODEL_BACKGROUND_H_ #ifndef MODEL_BACKGROUND_H_
#define MODEL_BACKGROUND_H_ #define MODEL_BACKGROUND_H_
#include <base/Component.h> #include <model/Component.h>
#include <resources/ResourceManager.h> #include <resources/ResourceManager.h>
namespace farmlands { namespace farmlands {
namespace model { namespace components {
typedef int16_t Cell; typedef int16_t Cell;
class Background: public base::Component class Background: public model::Component
{ {
public: public:
Background(size_t layerCount, size_t rowCount, size_t columnCount); Background(size_t layerCount, size_t rowCount, size_t columnCount);
virtual ~Background(); virtual ~Background();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
inline size_t layerCount() const { return m_layers; } inline size_t layerCount() const { return m_layers; }

View File

@ -6,8 +6,8 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <base/Camera.h> #include <components/basic/Camera.h>
#include <controller/DebugController.h> #include <components/DebugController.h>
#include <input/Input.h> #include <input/Input.h>
#include <iostream> #include <iostream>
@ -15,7 +15,7 @@
using namespace farmlands::input; using namespace farmlands::input;
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
static const float ScaleVelocity = 0.5f; static const float ScaleVelocity = 0.5f;
static const float ScaleShiftVelocity = 2.0f; static const float ScaleShiftVelocity = 2.0f;
@ -29,7 +29,7 @@ DebugController::~DebugController()
{ {
} }
base::Component* DebugController::clone() model::Component* DebugController::clone()
{ {
return new DebugController(); return new DebugController();
} }

View File

@ -8,26 +8,26 @@
#ifndef CONTROLLER_DEBUGCONTROLLER_H_ #ifndef CONTROLLER_DEBUGCONTROLLER_H_
#define CONTROLLER_DEBUGCONTROLLER_H_ #define CONTROLLER_DEBUGCONTROLLER_H_
#include <base/Component.h> #include <model/Component.h>
#include <base/Camera.h> #include <components/basic/Camera.h>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
class DebugController: public base::Component class DebugController: public model::Component
{ {
public: public:
DebugController(); DebugController();
virtual ~DebugController(); virtual ~DebugController();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onInitialize() override; virtual void onInitialize() override;
virtual void onUpdateLogic() override; virtual void onUpdateLogic() override;
private: private:
base::Camera* m_camera; basic::Camera* m_camera;
}; };
} /* namespace controller */ } /* namespace controller */

View File

@ -6,13 +6,13 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <controller/GuiController.h> #include <components/GuiController.h>
#include <gui/widgets/TextArea.h> #include <gui/widgets/TextArea.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
GuiController::GuiController() GuiController::GuiController()
: m_canvas() : m_canvas()
@ -23,7 +23,7 @@ GuiController::~GuiController()
{ {
} }
base::Component* GuiController::clone() model::Component* GuiController::clone()
{ {
return new GuiController(); return new GuiController();
} }
@ -70,7 +70,6 @@ bool GuiController::onEvent(SDL_Event& event)
void GuiController::onRender() void GuiController::onRender()
{ {
// Compute render context
// Render // Render
m_canvas.render(m_context); m_canvas.render(m_context);
} }

View File

@ -8,22 +8,22 @@
#ifndef CONTROLLER_GUICONTROLLER_H_ #ifndef CONTROLLER_GUICONTROLLER_H_
#define CONTROLLER_GUICONTROLLER_H_ #define CONTROLLER_GUICONTROLLER_H_
#include <base/Component.h> #include <model/Component.h>
#include <base/RenderContext.h> #include <graphics/RenderContext.h>
#include <gui/layout/Canvas.h> #include <gui/layout/Canvas.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
class GuiController : public base::Component class GuiController : public model::Component
{ {
public: public:
GuiController(); GuiController();
virtual ~GuiController(); virtual ~GuiController();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
/** /**
@ -35,7 +35,7 @@ namespace controller {
private: private:
gui::layout::Canvas m_canvas; gui::layout::Canvas m_canvas;
base::RenderContext* m_context; graphics::RenderContext* m_context;
}; };
} /* namespace controller */ } /* namespace controller */

View File

@ -6,13 +6,16 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <base/Component.h> #include <components/basic/Camera.h>
#include <base/Camera.h> #include <model/Component.h>
#include <iostream> #include <iostream>
using namespace farmlands::model;
namespace farmlands { namespace farmlands {
namespace base { namespace components {
namespace basic {
Camera::Camera() Camera::Camera()
: scale(1), : scale(1),
@ -53,4 +56,5 @@ void Camera::dump(unsigned level)
} }
} }
}

View File

@ -8,18 +8,19 @@
#ifndef MODEL_CAMERA_H_ #ifndef MODEL_CAMERA_H_
#define MODEL_CAMERA_H_ #define MODEL_CAMERA_H_
#include <base/Component.h> #include <model/Component.h>
namespace farmlands { namespace farmlands {
namespace base { namespace components {
namespace basic {
class Camera : public base::Component class Camera : public model::Component
{ {
public: public:
Camera(); Camera();
virtual ~Camera(); virtual ~Camera();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onCreate(); virtual void onCreate();
@ -30,5 +31,6 @@ namespace base {
} }
} }
}
#endif /* MODEL_CAMERA_H_ */ #endif /* MODEL_CAMERA_H_ */

View File

@ -5,22 +5,26 @@
* Author: tibi * Author: tibi
*/ */
#include <base/Sprite.h> #include <GameState.h>
#include <components/basic/Sprite.h>
#include <utils/Assert.h> #include <utils/Assert.h>
#include <algorithm> #include <algorithm>
#include <iostream>
namespace farmlands { namespace farmlands {
namespace base { namespace components {
namespace basic {
Sprite::Sprite() Sprite::Sprite()
: anchorX(0), anchorY(0), : name(),
name(), anchorX(0), anchorY(0),
animationVelocity(1.0f),
m_states(), m_states(),
m_stateNames(),
m_currentState(0), m_currentState(0),
m_currentFrame(0), m_currentFrame(0),
m_currentFrameTimeLeft(0), m_currentFrameTimeLeft(0)
m_animationVelocity(1)
{ {
} }
@ -28,11 +32,45 @@ Sprite::~Sprite()
{ {
} }
model::Component* Sprite::clone()
{
Sprite* clone = new Sprite();
// Copy public fields
clone->name = name;
clone->anchorX = anchorX;
clone->anchorY = anchorY;
clone->animationVelocity = animationVelocity;
// Copy private memberes
std::copy(m_states.begin(), m_states.end(), std::back_inserter(clone->m_states));
std::copy(m_stateNames.begin(), m_stateNames.end(), std::inserter(clone->m_stateNames, clone->m_stateNames.end()));
clone->m_currentState = m_currentState;
clone->m_currentFrame = m_currentFrame;
clone->m_currentFrameTimeLeft = m_currentFrameTimeLeft;
return clone;
}
void Sprite::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Sprite ";
std::cout << "name="<<name<<"\n";
}
void Sprite::onPreRender()
{
advanceTime(GameState::current().elapsedTime);
}
void Sprite::addState(const SpriteState& state) void Sprite::addState(const SpriteState& state)
{ {
Assert(state.frames.size() > 0, "State must have at least one frame!"); Assert(state.frames.size() > 0, "State must have at least one frame!");
#ifdef BUILD_DEBUG #ifdef BUILD_DEBUG
uint32_t totalDuration = 0; float totalDuration = 0;
for (auto frame : state.frames) for (auto frame : state.frames)
totalDuration += frame.duration; totalDuration += frame.duration;
@ -63,11 +101,23 @@ void Sprite::setState(const std::string& name)
setState(m_stateNames.at(name)); setState(m_stateNames.at(name));
} }
SpriteState& Sprite::currentState()
{
Assert(m_states.size() > 0, "Sprite must have at least one state!");
return m_states.at(m_currentState);
}
Frame& Sprite::currentFrame()
{
Assert(currentState().frames.size() > 0, "State must have at least one frame!");
return currentState().frames.at(m_currentFrame);
}
void Sprite::advanceTime(float fractions) void Sprite::advanceTime(float fractions)
{ {
Assert(m_states.size() > 0, "Sprite must have at least one state!"); Assert(m_states.size() > 0, "Sprite must have at least one state!");
m_currentFrameTimeLeft -= fractions * m_animationVelocity; m_currentFrameTimeLeft -= fractions * animationVelocity;
while (m_currentFrameTimeLeft <= 0) while (m_currentFrameTimeLeft <= 0)
{ {
@ -79,22 +129,6 @@ void Sprite::advanceTime(float fractions)
} }
} }
SpriteState& Sprite::currentState()
{
Assert(m_states.size() > 0, "Sprite must have at least one state!");
return m_states.at(m_currentState);
} }
void Sprite::setAnimationVelocity(float velocity)
{
m_animationVelocity = velocity;
} }
Frame& Sprite::currentFrame()
{
Assert(currentState().frames.size() > 0, "State must have at least one frame!");
return currentState().frames.at(m_currentFrame);
}
} /* namespace model */
} /* namespace farmlands */ } /* namespace farmlands */

View File

@ -8,13 +8,15 @@
#ifndef MODEL_SPRITE_H_ #ifndef MODEL_SPRITE_H_
#define MODEL_SPRITE_H_ #define MODEL_SPRITE_H_
#include <model/Component.h>
#include <utils/Exceptions.h> #include <utils/Exceptions.h>
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
namespace farmlands { namespace farmlands {
namespace base { namespace components {
namespace basic {
/** /**
* Defines an animation frame * Defines an animation frame
@ -24,11 +26,11 @@ namespace base {
uint32_t tileSetId; uint32_t tileSetId;
uint32_t tileSetCell; uint32_t tileSetCell;
uint32_t width, height; uint32_t width, height;
uint32_t duration; float duration;
}; };
/** /**
* Defines a sprite state (aka an animation). * Defines a sprite state (i.e. an animation).
*/ */
struct SpriteState struct SpriteState
{ {
@ -39,12 +41,17 @@ namespace base {
/** /**
* Defines a sprite * Defines a sprite
*/ */
class Sprite class Sprite : public model::Component
{ {
public: public:
Sprite(); Sprite();
virtual ~Sprite(); virtual ~Sprite();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onPreRender() override;
/** /**
* Adds a state to the sprite. * Adds a state to the sprite.
*/ */
@ -60,35 +67,28 @@ namespace base {
*/ */
void setState(const std::string& name); void setState(const std::string& name);
/**
* Advances the current frame
*/
void advanceTime(float fractions);
/**
* Sets the animation velocity. Using this property, animation can be slowed or accelerated.
*/
void setAnimationVelocity(float velocity);
// Getters // Getters
SpriteState& currentState(); SpriteState& currentState();
Frame& currentFrame(); Frame& currentFrame();
// Public fields // Public fields
float anchorX, anchorY;
std::string name; std::string name;
float anchorX, anchorY;
float animationVelocity;
private: private:
void advanceTime(float fractions);
std::vector<SpriteState> m_states; std::vector<SpriteState> m_states;
std::unordered_map<std::string, size_t> m_stateNames; std::unordered_map<std::string, size_t> m_stateNames;
size_t m_currentState; size_t m_currentState;
size_t m_currentFrame; size_t m_currentFrame;
float m_currentFrameTimeLeft; float m_currentFrameTimeLeft;
float m_animationVelocity;
}; };
} /* namespace model */ }
} /* namespace components */
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* MODEL_SPRITE_H_ */ #endif /* MODEL_SPRITE_H_ */

View File

@ -5,13 +5,14 @@
* Author: tibi * Author: tibi
*/ */
#include <base/GameObject.h> #include <model/GameObject.h>
#include <base/Transform.h> #include <components/basic/Transform.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace base { namespace components {
namespace basic {
Transform::Transform() Transform::Transform()
: x(0), y(0), : x(0), y(0),
@ -24,7 +25,7 @@ Transform::~Transform()
{ {
} }
Component* Transform::clone() model::Component* Transform::clone()
{ {
Transform* clone = new Transform(); Transform* clone = new Transform();
clone->x = x; clone->x = x;
@ -34,7 +35,7 @@ Component* Transform::clone()
return clone; return clone;
} }
void farmlands::base::Transform::onCreate() void Transform::onCreate()
{ {
if (gameObject->parent() != nullptr) if (gameObject->parent() != nullptr)
m_parent = gameObject->parent()->component<Transform>(); m_parent = gameObject->parent()->component<Transform>();
@ -72,7 +73,8 @@ void Transform::dump(unsigned level)
std::cout << "h="<<h<<"\n"; std::cout << "h="<<h<<"\n";
} }
} /* namespace base */ }
}
} /* namespace farmlands */ } /* namespace farmlands */

View File

@ -8,18 +8,19 @@
#ifndef BASE_TRANSFORM_H_ #ifndef BASE_TRANSFORM_H_
#define BASE_TRANSFORM_H_ #define BASE_TRANSFORM_H_
#include <base/Component.h> #include <model/Component.h>
namespace farmlands { namespace farmlands {
namespace base { namespace components {
namespace basic {
class Transform: public Component class Transform: public model::Component
{ {
public: public:
Transform(); Transform();
virtual ~Transform(); virtual ~Transform();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onCreate(); virtual void onCreate();
@ -40,7 +41,8 @@ namespace base {
Transform* m_parent; Transform* m_parent;
}; };
} /* namespace base */ }
}
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* BASE_TRANSFORM_H_ */ #endif /* BASE_TRANSFORM_H_ */

View File

@ -5,12 +5,12 @@
* Author: tibi * Author: tibi
*/ */
#include <controller/items/Giftable.h> #include <components/items/Giftable.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
namespace items { namespace items {
Giftable::Giftable() Giftable::Giftable()
@ -21,7 +21,7 @@ Giftable::~Giftable()
{ {
} }
base::Component* Giftable::clone() model::Component* Giftable::clone()
{ {
return new Giftable(); return new Giftable();
} }
@ -46,5 +46,5 @@ void Giftable::dump(unsigned level)
} }
} /* namespace items */ } /* namespace items */
} /* namespace controller */ } /* namespace components */
} /* namespace farmlands */ } /* namespace farmlands */

View File

@ -8,20 +8,20 @@
#ifndef CONTROLLER_ITEMS_GIFTABLE_H_ #ifndef CONTROLLER_ITEMS_GIFTABLE_H_
#define CONTROLLER_ITEMS_GIFTABLE_H_ #define CONTROLLER_ITEMS_GIFTABLE_H_
#include <base/Component.h> #include <model/Component.h>
#include <model/Direction.h> #include <model/Direction.h>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
namespace items { namespace items {
class Giftable: public base::Component class Giftable: public model::Component
{ {
public: public:
Giftable(); Giftable();
virtual ~Giftable(); virtual ~Giftable();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
bool canGift(float x, float y, model::Direction d); bool canGift(float x, float y, model::Direction d);
@ -29,7 +29,7 @@ namespace items {
}; };
} /* namespace items */ } /* namespace items */
} /* namespace controller */ } /* namespace components */
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_GIFTABLE_H_ */ #endif /* CONTROLLER_ITEMS_GIFTABLE_H_ */

View File

@ -8,19 +8,20 @@
#ifndef MODEL_ITEM_H_ #ifndef MODEL_ITEM_H_
#define MODEL_ITEM_H_ #define MODEL_ITEM_H_
#include <base/Component.h> #include <model/Component.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace model { namespace components {
namespace items {
class Item: public base::Component class Item: public model::Component
{ {
public: public:
Item(); Item();
virtual ~Item(); virtual ~Item();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
std::string name; std::string name;
@ -41,7 +42,7 @@ namespace model {
{ {
} }
inline base::Component* Item::clone() inline model::Component* Item::clone()
{ {
Item* clone = new Item(); Item* clone = new Item();
clone->name = name; clone->name = name;
@ -59,7 +60,8 @@ namespace model {
std::cout << " .Component: Item\n"; std::cout << " .Component: Item\n";
} }
} /* namespace model */ }
}
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* MODEL_ITEM_H_ */ #endif /* MODEL_ITEM_H_ */

View File

@ -6,12 +6,12 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <controller/items/Weapon.h> #include <components/items/Weapon.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
namespace items { namespace items {
Weapon::Weapon() Weapon::Weapon()
@ -19,7 +19,7 @@ Weapon::Weapon()
critProbability(0), critProbability(0),
critDamage(0), critDamage(0),
attackDuration(1.0f), attackDuration(1.0f),
m_spriteRenderer(nullptr), m_sprite(nullptr),
m_attackTimeLeft(0) m_attackTimeLeft(0)
{ {
} }
@ -28,7 +28,7 @@ Weapon::~Weapon()
{ {
} }
base::Component* Weapon::clone() model::Component* Weapon::clone()
{ {
Weapon* clone = new Weapon(); Weapon* clone = new Weapon();
clone->damage = damage; clone->damage = damage;
@ -41,12 +41,12 @@ base::Component* Weapon::clone()
void Weapon::onInitialize() void Weapon::onInitialize()
{ {
m_spriteRenderer = gameObject->component<graphics::SpriteRenderer>(); m_sprite = gameObject->component<basic::Sprite>();
} }
void Weapon::onPreRender() void Weapon::onPreRender()
{ {
m_spriteRenderer->sprite->setState(m_attackTimeLeft > 0); m_sprite->setState(m_attackTimeLeft > 0);
} }
void Weapon::onUpdateLogic() void Weapon::onUpdateLogic()
@ -78,6 +78,6 @@ void Weapon::dump(unsigned level)
} }
} }
} /* namespace model */ } /* namespace components */
} /* namespace farmlands */ } /* namespace farmlands */

View File

@ -8,21 +8,22 @@
#ifndef CONTROLLER_ITEMS_WEAPON_H_ #ifndef CONTROLLER_ITEMS_WEAPON_H_
#define CONTROLLER_ITEMS_WEAPON_H_ #define CONTROLLER_ITEMS_WEAPON_H_
#include <base/Component.h> #include <components/basic/Sprite.h>
#include <graphics/SpriteRenderer.h> #include <graphics/SpriteRenderer.h>
#include <model/Component.h>
#include <model/Direction.h> #include <model/Direction.h>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
namespace items { namespace items {
class Weapon: public base::Component class Weapon: public model::Component
{ {
public: public:
Weapon(); Weapon();
virtual ~Weapon(); virtual ~Weapon();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onInitialize() override; virtual void onInitialize() override;
@ -42,12 +43,12 @@ namespace items {
float attackDuration; // In seconds float attackDuration; // In seconds
private: private:
graphics::SpriteRenderer* m_spriteRenderer; basic::Sprite* m_sprite;
float m_attackTimeLeft; float m_attackTimeLeft;
}; };
} }
} /* namespace model */ } /* namespace components */
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_WEAPON_H_ */ #endif /* CONTROLLER_ITEMS_WEAPON_H_ */

View File

@ -6,24 +6,24 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <controller/items/Giftable.h> #include <components/items/Giftable.h>
#include <controller/items/Weapon.h> #include <components/items/Item.h>
#include <controller/player/PlayerController.h> #include <components/items/Weapon.h>
#include <components/player/PlayerController.h>
#include <graphics/SpriteRenderer.h> #include <graphics/SpriteRenderer.h>
#include <input/Input.h> #include <input/Input.h>
#include <model/Item.h>
#include <utils/Assert.h> #include <utils/Assert.h>
#include <iostream> #include <iostream>
using namespace farmlands::base; using namespace farmlands::components::basic;
using namespace farmlands::controller::items; using namespace farmlands::components::items;
using namespace farmlands::graphics; using namespace farmlands::graphics;
using namespace farmlands::input; using namespace farmlands::input;
using namespace farmlands::model; using namespace farmlands::model;
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
namespace player { namespace player {
static const float PlayerWalkVelocity = 2.0f; // The default velocity of the player when walking (units/sec). static const float PlayerWalkVelocity = 2.0f; // The default velocity of the player when walking (units/sec).
@ -53,14 +53,14 @@ PlayerController::~PlayerController()
{ {
} }
base::Component* PlayerController::clone() model::Component* PlayerController::clone()
{ {
return new PlayerController(); return new PlayerController();
} }
void PlayerController::onInitialize() void PlayerController::onInitialize()
{ {
m_transform = gameObject->component<base::Transform>(); m_transform = gameObject->component<components::basic::Transform>();
} }
bool PlayerController::onEvent(SDL_Event& event) bool PlayerController::onEvent(SDL_Event& event)
@ -171,7 +171,7 @@ void PlayerController::onUpdateLogic()
void PlayerController::onPreRender() void PlayerController::onPreRender()
{ {
// Get sprite // Get sprite
SpriteRenderer* spriteRenderer = gameObject->component<SpriteRenderer>(); Sprite* sprite = gameObject->component<Sprite>();
// Compute current state // Compute current state
std::string stateName = (m_walking) ? "Walking " : "Idle "; std::string stateName = (m_walking) ? "Walking " : "Idle ";
@ -185,14 +185,14 @@ void PlayerController::onPreRender()
else else
stateName += "down"; stateName += "down";
spriteRenderer->sprite->setState(stateName); sprite->setState(stateName);
// Set animation velocity // Set animation velocity
float animVelocity = (m_running) ? 1.0f : 0.7f; float animVelocity = (m_running) ? 1.0f : 0.7f;
// if (m_attackTimeLeft) // if (m_attackTimeLeft)
// animVelocity = 0.1f; // animVelocity = 0.1f;
spriteRenderer->sprite->setAnimationVelocity(animVelocity); sprite->animationVelocity = animVelocity;
// Set weapon // Set weapon
if (m_currentItem) if (m_currentItem)
@ -203,7 +203,7 @@ void PlayerController::onPreRender()
} }
// Set camera // Set camera
base::Transform* cam = GameState::current().renderContext.cameraTransform(); components::basic::Transform* cam = GameState::current().renderContext.cameraTransform();
cam->x = m_transform->x; cam->x = m_transform->x;
cam->y = m_transform->y - 1; cam->y = m_transform->y - 1;
} }

View File

@ -8,23 +8,23 @@
#ifndef CONTROLLER_PLAYERCONTROLLER_H_ #ifndef CONTROLLER_PLAYERCONTROLLER_H_
#define CONTROLLER_PLAYERCONTROLLER_H_ #define CONTROLLER_PLAYERCONTROLLER_H_
#include <base/Component.h> #include <components/basic/Transform.h>
#include <base/Transform.h> #include <model/Component.h>
#include <model/Direction.h> #include <model/Direction.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
namespace farmlands { namespace farmlands {
namespace controller { namespace components {
namespace player { namespace player {
class PlayerController : public base::Component class PlayerController : public model::Component
{ {
public: public:
PlayerController(); PlayerController();
virtual ~PlayerController(); virtual ~PlayerController();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onInitialize() override; virtual void onInitialize() override;
@ -41,15 +41,15 @@ namespace player {
bool canMove(float x, float y); bool canMove(float x, float y);
void attack(); void attack();
base::Transform* m_transform; basic::Transform* m_transform;
model::Direction m_facingDirection; model::Direction m_facingDirection;
bool m_walking, m_running; bool m_walking, m_running;
base::GameObject* m_currentItem; model::GameObject* m_currentItem;
}; };
} }
} /* namespace controller */ } /* namespace components */
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* CONTROLLER_PLAYERCONTROLLER_H_ */ #endif /* CONTROLLER_PLAYERCONTROLLER_H_ */

View File

@ -5,13 +5,9 @@
* Author: tibi * Author: tibi
*/ */
#include <base/GameObject.h> #include <model/GameObject.h>
#include <base/Camera.h>
#include <controller/DebugController.h>
#include <controller/FarmlandsGame.h> #include <controller/FarmlandsGame.h>
#include <graphics/backend/SdlRenderer.h> #include <graphics/backend/SdlRenderer.h>
#include <graphics/BackgroundRenderer.h>
#include <graphics/SpriteRenderer.h>
#include <resources/Resources.h> #include <resources/Resources.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@ -20,7 +16,6 @@
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
using namespace farmlands::base;
using namespace farmlands::graphics; using namespace farmlands::graphics;
using namespace farmlands::graphics::backend; using namespace farmlands::graphics::backend;
using namespace farmlands::resources; using namespace farmlands::resources;

View File

@ -6,7 +6,7 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <base/GameObject.h> #include <model/GameObject.h>
#include <graphics/backend/SdlRenderer.h> #include <graphics/backend/SdlRenderer.h>
#include <graphics/BackgroundRenderer.h> #include <graphics/BackgroundRenderer.h>
#include <resources/ResourceManager.h> #include <resources/ResourceManager.h>
@ -16,6 +16,7 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
using namespace farmlands::components;
using namespace farmlands::graphics::backend; using namespace farmlands::graphics::backend;
using namespace farmlands::resources; using namespace farmlands::resources;
@ -32,7 +33,7 @@ BackgroundRenderer::~BackgroundRenderer()
{ {
} }
base::Component* BackgroundRenderer::clone() model::Component* BackgroundRenderer::clone()
{ {
return new BackgroundRenderer(); return new BackgroundRenderer();
} }
@ -42,7 +43,7 @@ void BackgroundRenderer::onInitialize()
Assert(gameObject != nullptr, "Component not properly initialized!"); Assert(gameObject != nullptr, "Component not properly initialized!");
m_context = &GameState::current().renderContext; m_context = &GameState::current().renderContext;
m_back = gameObject->component<model::Background>(); m_back = gameObject->component<Background>();
} }
void BackgroundRenderer::onRender() void BackgroundRenderer::onRender()

View File

@ -8,22 +8,22 @@
#ifndef GRAPHICS_BACKGROUNDRENDERER_H_ #ifndef GRAPHICS_BACKGROUNDRENDERER_H_
#define GRAPHICS_BACKGROUNDRENDERER_H_ #define GRAPHICS_BACKGROUNDRENDERER_H_
#include <base/Component.h> #include <model/Component.h>
#include <base/Camera.h> #include <components/Background.h>
#include <base/Transform.h> #include <components/basic/Camera.h>
#include <base/RenderContext.h> #include <components/basic/Transform.h>
#include <model/Background.h> #include <graphics/RenderContext.h>
namespace farmlands { namespace farmlands {
namespace graphics { namespace graphics {
class BackgroundRenderer: public base::Component class BackgroundRenderer: public model::Component
{ {
public: public:
BackgroundRenderer(); BackgroundRenderer();
virtual ~BackgroundRenderer(); virtual ~BackgroundRenderer();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onInitialize() override; virtual void onInitialize() override;
@ -33,8 +33,8 @@ private:
void getCell(SDL_Texture* texture, uint32_t cell, int* outX, int* outY); void getCell(SDL_Texture* texture, uint32_t cell, int* outX, int* outY);
// Private fields // Private fields
base::RenderContext* m_context; graphics::RenderContext* m_context;
model::Background* m_back; components::Background* m_back;
}; };
} /* namespace graphics */ } /* namespace graphics */

View File

@ -4,13 +4,17 @@
* Created on: Nov 30, 2016 * Created on: Nov 30, 2016
* Author: tibi * Author: tibi
*/ */
#include <base/Camera.h>
#include <base/GameObject.h> #include <components/basic/Camera.h>
#include <base/RenderContext.h> #include <components/basic/Transform.h>
#include <base/Transform.h> #include <graphics/RenderContext.h>
#include <model/GameObject.h>
using namespace farmlands::components::basic;
using namespace farmlands::model;
namespace farmlands { namespace farmlands {
namespace base { namespace graphics {
float RenderContext::xToWorld(float x) float RenderContext::xToWorld(float x)
{ {
@ -36,7 +40,7 @@ float RenderContext::yToScreen(float y)
return (y - m_cameraTransform->y) * cellH + viewport.height / 2; return (y - m_cameraTransform->y) * cellH + viewport.height / 2;
} }
bool farmlands::base::RenderContext::visible(SDL_Rect& rect) bool RenderContext::visible(SDL_Rect& rect)
{ {
SDL_Rect screen = { 0, 0, viewport.width, viewport.height }; SDL_Rect screen = { 0, 0, viewport.width, viewport.height };
return SDL_HasIntersection(&screen, &rect); return SDL_HasIntersection(&screen, &rect);

View File

@ -0,0 +1,54 @@
/*
* RenderContext.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef GRAPHICS_RENDERCONTEXT_H_
#define GRAPHICS_RENDERCONTEXT_H_
#include <model/Viewport.h>
#include <SDL2/SDL.h>
namespace farmlands {
// Forward declarations
namespace model { class GameObject; }
namespace components { namespace basic { struct Transform; } }
namespace components { namespace basic { struct Camera; } }
namespace graphics {
class RenderContext
{
public:
float xToWorld(float x);
float yToWorld(float y);
float xToScreen(float x);
float yToScreen(float y);
bool visible(SDL_Rect& rect);
inline model::GameObject* cameraObj() { return m_cameraObj; }
inline components::basic::Camera* camera() { return m_camera; }
inline components::basic::Transform* cameraTransform() { return m_cameraTransform; }
void setCamera(model::GameObject* camera);
/**
* Screen properties
*/
model::Viewport viewport;
float uiScale = 1.0f;
private:
model::GameObject* m_cameraObj;
components::basic::Transform* m_cameraTransform;
components::basic::Camera* m_camera;
};
}
}
#endif /* GRAPHICS_RENDERCONTEXT_H_ */

View File

@ -6,14 +6,15 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <base/GameObject.h> #include <model/GameObject.h>
#include <base/Camera.h> #include <components/basic/Camera.h>
#include <graphics/backend/SdlRenderer.h> #include <graphics/backend/SdlRenderer.h>
#include <graphics/SpriteRenderer.h> #include <graphics/SpriteRenderer.h>
#include <utils/Assert.h> #include <utils/Assert.h>
#include <iostream> #include <iostream>
using namespace farmlands::components::basic;
using namespace farmlands::graphics::backend; using namespace farmlands::graphics::backend;
using namespace farmlands::resources; using namespace farmlands::resources;
@ -21,31 +22,26 @@ namespace farmlands {
namespace graphics { namespace graphics {
SpriteRenderer::SpriteRenderer() SpriteRenderer::SpriteRenderer()
: sprite(nullptr), : m_transform(nullptr),
m_transform(nullptr), m_context(nullptr),
m_context(nullptr) m_sprite(nullptr)
{ {
} }
SpriteRenderer::~SpriteRenderer() SpriteRenderer::~SpriteRenderer()
{ {
delete sprite;
} }
base::Component* SpriteRenderer::clone() model::Component* SpriteRenderer::clone()
{ {
SpriteRenderer* clone = new SpriteRenderer(); return new SpriteRenderer();
clone->sprite = new base::Sprite(*sprite);
return clone;
} }
void SpriteRenderer::onInitialize() void SpriteRenderer::onInitialize()
{ {
Assert(gameObject != nullptr, "Component not properly initialized!");
m_transform = gameObject->component<base::Transform>();
m_context = &GameState::current().renderContext; m_context = &GameState::current().renderContext;
m_transform = gameObject->component<components::basic::Transform>();
m_sprite = gameObject->component<Sprite>();
} }
void SpriteRenderer::onRender() void SpriteRenderer::onRender()
@ -53,27 +49,27 @@ void SpriteRenderer::onRender()
float posX = m_context->xToScreen(m_transform->globalX()); float posX = m_context->xToScreen(m_transform->globalX());
float posY = m_context->yToScreen(m_transform->globalY()); float posY = m_context->yToScreen(m_transform->globalY());
float w = sprite->currentFrame().width * m_context->viewport.pixelsPerUnitX; float w = m_sprite->currentFrame().width * m_context->viewport.pixelsPerUnitX;
float h = sprite->currentFrame().height * m_context->viewport.pixelsPerUnitY; float h = m_sprite->currentFrame().height * m_context->viewport.pixelsPerUnitY;
// Compute destination rectangle // Compute destination rectangle
float scale = m_context->camera()->scale; float scale = m_context->camera()->scale;
SDL_Rect dest; SDL_Rect dest;
dest.x = posX - sprite->anchorX * w * scale; dest.x = posX - m_sprite->anchorX * w * scale;
dest.y = posY - sprite->anchorY * h * scale; dest.y = posY - m_sprite->anchorY * h * scale;
dest.w = w * scale; dest.w = w * scale;
dest.h = h * scale; dest.h = h * scale;
if (m_context->visible(dest)) if (m_context->visible(dest))
{ {
// Obtain texture // Obtain texture
int texId = sprite->currentFrame().tileSetId; int texId = m_sprite->currentFrame().tileSetId;
SDL_Texture* texture = resources::ResourceManager::instance().texture(texId); SDL_Texture* texture = resources::ResourceManager::instance().texture(texId);
// Compute source rectangle // Compute source rectangle
SDL_Rect src; SDL_Rect src;
getCell(texture, sprite->currentFrame().tileSetCell, &src.x, &src.y); getCell(texture, m_sprite->currentFrame().tileSetCell, &src.x, &src.y);
src.w = w; src.w = w;
src.h = h; src.h = h;
@ -82,14 +78,6 @@ void SpriteRenderer::onRender()
} }
} }
void SpriteRenderer::onPostRender()
{
float animFps = GameState::current().config->animationFps;
float deltaTime = GameState::current().elapsedTime;
sprite->advanceTime(animFps * deltaTime);
}
void SpriteRenderer::getCell(SDL_Texture* texture, uint32_t cell, int* outX, int* outY) void SpriteRenderer::getCell(SDL_Texture* texture, uint32_t cell, int* outX, int* outY)
{ {
int texWidth, texHeight; int texWidth, texHeight;
@ -108,8 +96,7 @@ void SpriteRenderer::dump(unsigned level)
for (unsigned i = 0; i < level; i++) for (unsigned i = 0; i < level; i++)
std::cout<<" "; std::cout<<" ";
std::cout << " .Component: SpriteRenderer sprite="; std::cout << " .Component: SpriteRenderer\n";
std::cout << sprite->name <<"\n";
} }
} /* namespace graphics */ } /* namespace graphics */

View File

@ -8,38 +8,35 @@
#ifndef GRAPHICS_SPRITERENDERER_H_ #ifndef GRAPHICS_SPRITERENDERER_H_
#define GRAPHICS_SPRITERENDERER_H_ #define GRAPHICS_SPRITERENDERER_H_
#include <base/Component.h> #include <model/Component.h>
#include <base/RenderContext.h> #include <graphics/RenderContext.h>
#include <base/Sprite.h> #include <components/basic/Sprite.h>
#include <base/Transform.h> #include <components/basic/Transform.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
namespace farmlands { namespace farmlands {
namespace graphics { namespace graphics {
class SpriteRenderer: public base::Component class SpriteRenderer: public model::Component
{ {
public: public:
SpriteRenderer(); SpriteRenderer();
virtual ~SpriteRenderer(); virtual ~SpriteRenderer();
virtual base::Component* clone() override; virtual model::Component* clone() override;
virtual void dump(unsigned level) override; virtual void dump(unsigned level) override;
virtual void onInitialize() override; virtual void onInitialize() override;
virtual void onRender() override; virtual void onRender() override;
virtual void onPostRender() override;
// Public fields
base::Sprite* sprite;
private: private:
void getCell(SDL_Texture* texture, uint32_t cell, int* outX, int* outY); void getCell(SDL_Texture* texture, uint32_t cell, int* outX, int* outY);
// Private fields // Private fields
base::Transform* m_transform; graphics::RenderContext* m_context;
base::RenderContext* m_context; components::basic::Transform* m_transform;
components::basic::Sprite* m_sprite;
}; };
} /* namespace graphics */ } /* namespace graphics */

View File

@ -44,7 +44,7 @@ SdlRenderer::~SdlRenderer()
SDL_Quit(); SDL_Quit();
} }
bool SdlRenderer::initialize(base::RenderContext* renderContext) bool SdlRenderer::initialize(graphics::RenderContext* renderContext)
{ {
if (SDL_Init(SDL_INIT_VIDEO) != 0) { if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "Failed to initialize SDL!\n"; std::cerr << "Failed to initialize SDL!\n";

View File

@ -8,7 +8,7 @@
#ifndef GRAPHICS_SDLRENDERER_H_ #ifndef GRAPHICS_SDLRENDERER_H_
#define GRAPHICS_SDLRENDERER_H_ #define GRAPHICS_SDLRENDERER_H_
#include <base/RenderContext.h> #include <graphics/RenderContext.h>
#include <string> #include <string>
@ -31,7 +31,7 @@ namespace backend {
* *
* @returns true if successful, false otherwise. * @returns true if successful, false otherwise.
*/ */
bool initialize(base::RenderContext* renderContext); bool initialize(graphics::RenderContext* renderContext);
/** /**
* Performs steps for beginning to render. * Performs steps for beginning to render.

View File

@ -19,7 +19,7 @@ Canvas::~Canvas()
{ {
} }
void Canvas::render(base::RenderContext* context) void Canvas::render(graphics::RenderContext* context)
{ {
for (auto child : m_children) for (auto child : m_children)
child->render(context); child->render(context);

View File

@ -20,7 +20,7 @@ namespace layout {
Canvas(); Canvas();
virtual ~Canvas(); virtual ~Canvas();
virtual void render(base::RenderContext* context) override; virtual void render(graphics::RenderContext* context) override;
}; };
} }

View File

@ -27,7 +27,7 @@ UIElement::~UIElement()
{ {
} }
void UIElement::render(base::RenderContext* context) void UIElement::render(graphics::RenderContext* context)
{ {
if (m_backColor.a > 0) if (m_backColor.a > 0)
{ {

View File

@ -10,7 +10,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <base/RenderContext.h> #include <graphics/RenderContext.h>
namespace farmlands { namespace farmlands {
namespace gui { namespace gui {
@ -22,7 +22,7 @@ namespace primitives {
UIElement(); UIElement();
virtual ~UIElement(); virtual ~UIElement();
virtual void render(base::RenderContext* context); virtual void render(graphics::RenderContext* context);
virtual bool handleEvent(SDL_Event& event); virtual bool handleEvent(SDL_Event& event);
// Getters & setters // Getters & setters

View File

@ -41,7 +41,7 @@ TextArea::~TextArea()
{ {
} }
void TextArea::render(base::RenderContext* context) void TextArea::render(graphics::RenderContext* context)
{ {
UIElement::render(context); UIElement::render(context);
@ -258,7 +258,7 @@ void TextArea::wrapText(TTF_Font* font)
m_wrappedText.erase(m_wrappedText.size() - 1); m_wrappedText.erase(m_wrappedText.size() - 1);
} }
void TextArea::renderLines(base::RenderContext* context, TTF_Font* font) void TextArea::renderLines(graphics::RenderContext* context, TTF_Font* font)
{ {
// Clean up old textures // Clean up old textures
for (SDL_Texture* tex : m_renderedLines) for (SDL_Texture* tex : m_renderedLines)

View File

@ -49,7 +49,7 @@ namespace widgets {
TextArea(); TextArea();
virtual ~TextArea(); virtual ~TextArea();
virtual void render(base::RenderContext* context) override; virtual void render(graphics::RenderContext* context) override;
// Getters and setters // Getters and setters
inline std::string text() const { return m_text; } inline std::string text() const { return m_text; }
@ -71,7 +71,7 @@ namespace widgets {
private: private:
void wrapText(TTF_Font* font); void wrapText(TTF_Font* font);
void renderLines(base::RenderContext* context, TTF_Font* font); void renderLines(graphics::RenderContext* context, TTF_Font* font);
std::string m_text; std::string m_text;
std::string m_wrappedText; std::string m_wrappedText;

View File

@ -5,10 +5,10 @@
* Author: tibi * Author: tibi
*/ */
#include <base/Component.h> #include <model/Component.h>
namespace farmlands { namespace farmlands {
namespace base { namespace model {
Component::Component() Component::Component()
: gameObject(nullptr) : gameObject(nullptr)
@ -61,7 +61,7 @@ void Component::onDisable()
} }
} }
/* namespace base */ /* namespace model */
} /* namespace farmlands */ } /* namespace farmlands */

View File

@ -8,13 +8,13 @@
#ifndef COMPONENT_H_ #ifndef COMPONENT_H_
#define COMPONENT_H_ #define COMPONENT_H_
#include <base/ICloneable.h> #include <model/ICloneable.h>
#include <base/INonAssignable.h> #include <model/INonAssignable.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
namespace farmlands { namespace farmlands {
namespace base { namespace model {
class GameObject; class GameObject;
@ -44,7 +44,7 @@ namespace base {
}; };
} }
/* namespace base */ /* namespace model */
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* COMPONENT_H_ */ #endif /* COMPONENT_H_ */

View File

@ -6,13 +6,13 @@
*/ */
#include <GameState.h> #include <GameState.h>
#include <base/GameObject.h> #include <model/GameObject.h>
#include <base/Component.h> #include <model/Component.h>
#include <iostream> #include <iostream>
namespace farmlands { namespace farmlands {
namespace base { namespace model {
GameObject::GameObject() GameObject::GameObject()
: name("unnamed"), : name("unnamed"),
@ -87,20 +87,30 @@ void GameObject::addChild(GameObject* obj)
obj->m_parent = this; obj->m_parent = this;
} }
GameObject* GameObject::removeChild(size_t index) GameObject* GameObject::removeChild(GameObject::iterator it)
{ {
GameObject* child = m_children.at(index); m_children.erase(it);
child->m_parent = nullptr; return *it;
m_children.erase(m_children.begin() + index);
return child;
} }
void GameObject::destroyChild(size_t index) GameObject* GameObject::removeChild(GameObject* obj)
{ {
delete m_children.at(index); for (auto it = m_children.begin(); it != m_children.end(); it++)
m_children.erase(m_children.begin() + index); {
if (*it == obj)
{
m_children.erase(it);
return *it;
}
}
return nullptr;
}
void GameObject::destroyChild(GameObject::iterator it)
{
delete *it;
m_children.erase(it);
} }
void GameObject::destroyChild(GameObject* obj) void GameObject::destroyChild(GameObject* obj)
@ -116,14 +126,19 @@ void GameObject::destroyChild(GameObject* obj)
} }
} }
size_t GameObject::childrenCount() const size_t GameObject::childrenSize() const
{ {
return m_children.size(); return m_children.size();
} }
GameObject* GameObject::child(size_t index) GameObject::iterator GameObject::childrenBegin()
{ {
return m_children.at(index); return m_children.begin();
}
GameObject::iterator GameObject::childrenEnd()
{
return m_children.end();
} }
GameObject* GameObject::parent() GameObject* GameObject::parent()
@ -286,11 +301,5 @@ void GameObject::dumpTree(unsigned level)
child->dumpTree(level + 1); child->dumpTree(level + 1);
} }
} /* namespace base */ } /* namespace model */
} /* namespace farmlands */ } /* namespace farmlands */

View File

@ -8,8 +8,8 @@
#ifndef GAMEOBJECT_H_ #ifndef GAMEOBJECT_H_
#define GAMEOBJECT_H_ #define GAMEOBJECT_H_
#include <base/ICloneable.h> #include <model/ICloneable.h>
#include <base/INonAssignable.h> #include <model/INonAssignable.h>
#include <utils/Assert.h> #include <utils/Assert.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@ -19,7 +19,7 @@
#include <unordered_map> #include <unordered_map>
namespace farmlands { namespace farmlands {
namespace base { namespace model {
class Component; class Component;
class RenderContext; class RenderContext;
@ -27,10 +27,11 @@ namespace base {
class GameObject : public INonAssignable, public ICloneable<GameObject> class GameObject : public INonAssignable, public ICloneable<GameObject>
{ {
public: public:
typedef std::vector<GameObject*>::iterator iterator;
// Constructors // Constructors
GameObject(); GameObject();
virtual ~GameObject(); virtual ~GameObject();
virtual GameObject* clone() override; virtual GameObject* clone() override;
static GameObject* instantiate(GameObject* gameObject, std::string name, GameObject* parent); static GameObject* instantiate(GameObject* gameObject, std::string name, GameObject* parent);
@ -43,11 +44,15 @@ namespace base {
// Tree methods // Tree methods
void addChild(GameObject* obj); void addChild(GameObject* obj);
GameObject* removeChild(size_t index); GameObject* removeChild(iterator it);
void destroyChild(size_t index); GameObject* removeChild(GameObject* obj);
void destroyChild(iterator it);
void destroyChild(GameObject* obj); void destroyChild(GameObject* obj);
GameObject* child(size_t index);
size_t childrenCount() const; size_t childrenSize() const;
iterator childrenBegin();
iterator childrenEnd();
GameObject* parent(); GameObject* parent();
// Game object methods // Game object methods
@ -132,7 +137,7 @@ namespace base {
return m_components.erase(typeIndex); return m_components.erase(typeIndex);
} }
} /* namespace base */ } /* namespace model */
} /* namespace farmlands */ } /* namespace farmlands */
#endif /* GAMEOBJECT_H_ */ #endif /* GAMEOBJECT_H_ */

View File

@ -9,7 +9,7 @@
#define BASE_ICLONEABLE_H_ #define BASE_ICLONEABLE_H_
namespace farmlands { namespace farmlands {
namespace base { namespace model {
template <typename T> template <typename T>
class ICloneable class ICloneable

View File

@ -9,7 +9,7 @@
#define BASE_INONASSIGNABLE_H_ #define BASE_INONASSIGNABLE_H_
namespace farmlands { namespace farmlands {
namespace base { namespace model {
class INonAssignable class INonAssignable
{ {

View File

@ -8,7 +8,7 @@
#ifndef MODEL_SCENE_H_ #ifndef MODEL_SCENE_H_
#define MODEL_SCENE_H_ #define MODEL_SCENE_H_
#include <base/GameObject.h> #include <model/GameObject.h>
#include <vector> #include <vector>
@ -18,7 +18,7 @@ namespace model {
struct Scene struct Scene
{ {
uint32_t cellWidth = 1, cellHeight = 1; uint32_t cellWidth = 1, cellHeight = 1;
base::GameObject root; model::GameObject root;
}; };
} /* namespace model */ } /* namespace model */

View File

@ -9,7 +9,7 @@
#define MODEL_VIEWPORT_H_ #define MODEL_VIEWPORT_H_
namespace farmlands { namespace farmlands {
namespace base { namespace model {
struct Viewport struct Viewport
{ {

View File

@ -8,7 +8,7 @@
#ifndef STORAGE_RESOURCEMANAGER_H_ #ifndef STORAGE_RESOURCEMANAGER_H_
#define STORAGE_RESOURCEMANAGER_H_ #define STORAGE_RESOURCEMANAGER_H_
#include <base/Sprite.h> #include <components/basic/Sprite.h>
#include <vector> #include <vector>
#include <map> #include <map>

View File

@ -8,170 +8,20 @@
#include <storage/Parsers.h> #include <storage/Parsers.h>
#include <resources/Resources.h> #include <resources/Resources.h>
using namespace farmlands::components;
using namespace farmlands::components::basic;
using namespace farmlands::components::items;
using namespace farmlands::components::player;
using namespace farmlands::graphics;
using namespace farmlands::model;
using namespace farmlands::resources; using namespace farmlands::resources;
namespace farmlands { namespace farmlands {
namespace storage { namespace storage {
/****** Base namespace ******/ /****** Components ******/
template <> void parseBackgroundCells(resources::ResourceId cellsResource, Background* back, size_t layer)
base::Camera* parse<base::Camera> (boost::property_tree::ptree& root)
{
if (root.front().first == "Camera")
root = root.front().second;
base::Camera* camera = new base::Camera();
camera->scale = root.get<float>("<xmlattr>.scale", 1.0f);
camera->mainCamera = root.get<bool>("<xmlattr>.mainCamera", false);
return camera;
}
template <>
base::Frame* parse<base::Frame> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Frame")
root = root.front().second;
base::Frame* frame = new base::Frame();
// Obtine tile set id
std::string tileSetPath = root.get<std::string>("<xmlattr>.tileSet");
frame->tileSetId = resources::ResourceManager::instance().getId(tileSetPath);
// Set properties
frame->tileSetCell = root.get<int>("<xmlattr>.cell");
frame->width = root.get<uint32_t>("<xmlattr>.w", 1u);
frame->height = root.get<uint32_t>("<xmlattr>.h", 1u);
frame->duration = root.get<uint32_t>("<xmlattr>.duration");
return frame;
}
template <>
base::GameObject* parse<base::GameObject> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "GameObject")
root = root.front().second;
base::GameObject* gameObj = new base::GameObject();
gameObj->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
// Base objects
if (child.first == "Transform")
gameObj->addComponent(parse<base::Transform>(child.second));
else if (child.first == "Camera")
gameObj->addComponent(parse<base::Camera>(child.second));
// Model
else if (child.first == "Background")
{
std::string path = child.second.get<std::string>("<xmlattr>.src");
resources::ResourceId id = resources::ResourceManager::instance().getId(path);
gameObj->addComponent(parse<model::Background>(id));
}
else if (child.first == "Item")
gameObj->addComponent(parse<model::Item>(child.second));
// Graphics
else if (child.first == "BackgroundRenderer")
gameObj->addComponent(parse<graphics::BackgroundRenderer>(child.second));
else if (child.first == "SpriteRenderer")
gameObj->addComponent(parse<graphics::SpriteRenderer>(child.second));
// Controllers
else if (child.first == "DebugController")
gameObj->addComponent(parse<controller::DebugController>(child.second));
else if (child.first == "Giftable")
gameObj->addComponent(parse<controller::items::Giftable>(child.second));
else if (child.first == "Weapon")
gameObj->addComponent(parse<controller::items::Weapon>(child.second));
else if (child.first == "PlayerController")
gameObj->addComponent(parse<controller::player::PlayerController>(child.second));
// !!! Add additional types here !!!
}
return gameObj;
}
template <>
base::Sprite* parse<base::Sprite> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Sprite")
root = root.front().second;
base::Sprite* sprite = new base::Sprite();
sprite->anchorX = root.get<float>("<xmlattr>.anchorX");
sprite->anchorY = root.get<float>("<xmlattr>.anchorY");
for (auto child : root)
{
if (child.first == "State")
{
base::SpriteState* state = parse<base::SpriteState>(child.second);
sprite->addState(*state);
delete state;
}
}
return sprite;
}
template <>
base::SpriteState* parse<base::SpriteState> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "SpriteState")
root = root.front().second;
base::SpriteState* spriteState = new base::SpriteState();
spriteState->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
if (child.first == "Frame")
{
base::Frame* frame = parse<base::Frame>(child.second);
spriteState->frames.push_back(*frame);
delete frame;
}
}
return spriteState;
}
template <>
base::Transform* parse<base::Transform> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Transform")
root = root.front().second;
base::Transform* transform = new base::Transform();
transform->x = root.get<float>("<xmlattr>.x", 0.0f);
transform->y = root.get<float>("<xmlattr>.y", 0.0f);
transform->w = root.get<float>("<xmlattr>.w", 0.0f);
transform->h = root.get<float>("<xmlattr>.h", 0.0f);
return transform;
}
/****** Model namespace ******/
void parseBackgroundCells(resources::ResourceId cellsResource, model::Background* back, size_t layer)
{ {
Assert(RInfo[cellsResource].type == ResourceType::BackgroundLayer, "Resource must be a level layer."); Assert(RInfo[cellsResource].type == ResourceType::BackgroundLayer, "Resource must be a level layer.");
@ -195,7 +45,7 @@ void parseBackgroundCells(resources::ResourceId cellsResource, model::Background
char* nextNum = strtok(buffer, ",;"); char* nextNum = strtok(buffer, ",;");
for (size_t col = 0; col < back->columnCount() && nextNum != NULL; col++) for (size_t col = 0; col < back->columnCount() && nextNum != NULL; col++)
{ {
model::Cell cell = (model::Cell)strtol(nextNum, NULL, 10); Cell cell = (Cell) strtol(nextNum, NULL, 10);
back->setCell(layer, row, col, cell); back->setCell(layer, row, col, cell);
nextNum = strtok(NULL, ",;"); nextNum = strtok(NULL, ",;");
@ -206,19 +56,27 @@ void parseBackgroundCells(resources::ResourceId cellsResource, model::Background
} }
template <> template <>
model::Background* parse<model::Background> (boost::property_tree::ptree& root) Background* parse<Background> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Background") if (root.front().first == "Background")
root = root.front().second; root = root.front().second;
// This object can be declared in another file
std::string src = root.get<std::string>("<xmlattr>.src", "");
if (!src.empty())
{
ResourceId id = ResourceManager::instance().getId(src);
return parse<Background>(id);
}
// Read sizes // Read sizes
uint32_t layers = root.count("Layer"); uint32_t layers = root.count("Layer");
uint32_t rows = root.get<uint32_t>("<xmlattr>.rows"); uint32_t rows = root.get<uint32_t>("<xmlattr>.rows");
uint32_t cols = root.get<uint32_t>("<xmlattr>.columns"); uint32_t cols = root.get<uint32_t>("<xmlattr>.columns");
// Create background object // Create background object
model::Background* back = new model::Background(layers, rows, cols); Background* back = new Background(layers, rows, cols);
// Read layers // Read layers
size_t layerNum = 0; size_t layerNum = 0;
@ -245,91 +103,144 @@ model::Background* parse<model::Background> (boost::property_tree::ptree& root)
} }
template <> template <>
model::Configuration* parse<model::Configuration> (boost::property_tree::ptree& root) Camera* parse<Camera> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) if (root.front().first == "Camera")
if (root.front().first == "Configuration")
root = root.front().second; root = root.front().second;
model::Configuration* config = new model::Configuration(); Camera* camera = new Camera();
config->animationFps = root.get<float>("animationFps"); camera->scale = root.get<float>("<xmlattr>.scale", 1.0f);
camera->mainCamera = root.get<bool>("<xmlattr>.mainCamera", false);
return config; return camera;
} }
template <> template <>
model::Item* parse<model::Item> (boost::property_tree::ptree& root) Frame* parse<Frame> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Item") if (root.front().first == "Frame")
root = root.front().second; root = root.front().second;
// Parse Frame* frame = new Frame();
model::Item* item = new model::Item();
item->name = root.get<std::string>("<xmlattr>.name"); // Obtine tile set id
item->description = root.get<std::string>("<xmlattr>.description"); std::string tileSetPath = root.get<std::string>("<xmlattr>.tileSet");
item->level = root.get<uint8_t>("<xmlattr>.level"); frame->tileSetId = resources::ResourceManager::instance().getId(tileSetPath);
return item;
// Set properties
frame->tileSetCell = root.get<int>("<xmlattr>.cell");
frame->width = root.get<uint32_t>("<xmlattr>.w", 1u);
frame->height = root.get<uint32_t>("<xmlattr>.h", 1u);
frame->duration = root.get<float>("<xmlattr>.duration");
return frame;
} }
template <> template <>
model::Scene* parse<model::Scene> (boost::property_tree::ptree& root) Sprite* parse<Sprite> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Scene") if (root.front().first == "Sprite")
root = root.front().second; root = root.front().second;
model::Scene* scene = new model::Scene(); // This object can be declared in another file
scene->cellWidth = root.get<uint32_t>("<xmlattr>.cellWidth"); std::string src = root.get<std::string>("<xmlattr>.src", "");
scene->cellHeight = root.get<uint32_t>("<xmlattr>.cellHeight"); if (!src.empty())
{
ResourceId id = ResourceManager::instance().getId(src);
return parse<Sprite>(id);
}
// Parse sprite
Sprite* sprite = new Sprite();
sprite->anchorX = root.get<float>("<xmlattr>.anchorX");
sprite->anchorY = root.get<float>("<xmlattr>.anchorY");
sprite->animationVelocity = root.get<float>("<xmlattr>.animationVelocity", 1.0f);
for (auto child : root) for (auto child : root)
{ {
if (child.first == "GameObject") if (child.first == "State")
{ {
base::GameObject* obj = parse<base::GameObject>(child.second); SpriteState* state = parse<SpriteState>(child.second);
scene->root.addChild(obj); sprite->addState(*state);
delete state;
} }
} }
return scene; return sprite;
} }
/****** Controller namespace ******/
template <> template <>
controller::DebugController* parse<controller::DebugController> (boost::property_tree::ptree& root) SpriteState* parse<SpriteState> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "SpriteState")
root = root.front().second;
SpriteState* spriteState = new SpriteState();
spriteState->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
if (child.first == "Frame")
{
Frame* frame = parse<Frame>(child.second);
spriteState->frames.push_back(*frame);
delete frame;
}
}
return spriteState;
}
template <>
Transform* parse<components::basic::Transform> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Transform")
root = root.front().second;
Transform* transform = new Transform();
transform->x = root.get<float>("<xmlattr>.x", 0.0f);
transform->y = root.get<float>("<xmlattr>.y", 0.0f);
transform->w = root.get<float>("<xmlattr>.w", 0.0f);
transform->h = root.get<float>("<xmlattr>.h", 0.0f);
return transform;
}
template <>
DebugController* parse<DebugController> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "DebugController") if (root.front().first == "DebugController")
root = root.front().second; root = root.front().second;
controller::DebugController* controller = new controller::DebugController(); DebugController* controller = new DebugController();
return controller; return controller;
} }
template <> template <>
controller::items::Giftable* parse<controller::items::Giftable> (boost::property_tree::ptree& root) Giftable* parse<items::Giftable> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Giftable") if (root.front().first == "Giftable")
root = root.front().second; root = root.front().second;
// Parse // Parse
return new controller::items::Giftable(); return new Giftable();
} }
template <> template <>
controller::items::Weapon* parse<controller::items::Weapon> (boost::property_tree::ptree& root) Weapon* parse<items::Weapon> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Weapon") if (root.front().first == "Weapon")
root = root.front().second; root = root.front().second;
// Parse // Parse
controller::items::Weapon* weapon = new controller::items::Weapon(); Weapon* weapon = new Weapon();
weapon->damage = root.get<float>("<xmlattr>.damage"); weapon->damage = root.get<float>("<xmlattr>.damage");
weapon->critProbability = root.get<float>("<xmlattr>.critProbability"); weapon->critProbability = root.get<float>("<xmlattr>.critProbability");
weapon->critDamage = root.get<float>("<xmlattr>.critDamage"); weapon->critDamage = root.get<float>("<xmlattr>.critDamage");
@ -338,18 +249,18 @@ controller::items::Weapon* parse<controller::items::Weapon> (boost::property_tre
} }
template <> template <>
controller::player::PlayerController* parse<controller::player::PlayerController> (boost::property_tree::ptree& root) PlayerController* parse<PlayerController> (boost::property_tree::ptree& root)
{ {
// Ensure we are on the scene node (property tree seems to add root of its own) // Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "PlayerController") if (root.front().first == "PlayerController")
root = root.front().second; root = root.front().second;
controller::player::PlayerController* controller = new controller::player::PlayerController(); PlayerController* controller = new PlayerController();
return controller; return controller;
} }
/****** Graphics namespace ******/ /****** Graphics ******/
template <> template <>
graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::property_tree::ptree& root) graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::property_tree::ptree& root)
@ -370,15 +281,127 @@ graphics::SpriteRenderer* parse<graphics::SpriteRenderer> (boost::property_tree:
root = root.front().second; root = root.front().second;
graphics::SpriteRenderer* renderer = new graphics::SpriteRenderer(); graphics::SpriteRenderer* renderer = new graphics::SpriteRenderer();
// Load sprite
std::string spritePath = root.get<std::string>("<xmlattr>.sprite");
resources::ResourceId spriteId = resources::ResourceManager::instance().getId(spritePath);
renderer->sprite = parse<base::Sprite>(spriteId);
return renderer; return renderer;
} }
/****** Model ******/
template <>
GameObject* parse<GameObject> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "GameObject")
root = root.front().second;
GameObject* gameObj = new GameObject();
gameObj->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
// Components::basic
if (child.first == "Camera")
gameObj->addComponent(parse<Camera>(child.second));
else if (child.first == "Sprite")
gameObj->addComponent(parse<Sprite>(child.second));
else if (child.first == "Transform")
gameObj->addComponent(parse<Transform>(child.second));
// Components::items
else if (child.first == "Giftable")
gameObj->addComponent(parse<Giftable>(child.second));
else if (child.first == "Item")
gameObj->addComponent(parse<Item>(child.second));
else if (child.first == "Weapon")
gameObj->addComponent(parse<Weapon>(child.second));
// Components::player
else if (child.first == "PlayerController")
gameObj->addComponent(parse<PlayerController>(child.second));
// Components
else if (child.first == "Background")
gameObj->addComponent(parse<Background>(child.second));
else if (child.first == "DebugController")
gameObj->addComponent(parse<DebugController>(child.second));
// Graphics
else if (child.first == "BackgroundRenderer")
gameObj->addComponent(parse<BackgroundRenderer>(child.second));
else if (child.first == "SpriteRenderer")
gameObj->addComponent(parse<SpriteRenderer>(child.second));
// !!! Add additional types here !!!
}
return gameObj;
}
template <>
Configuration* parse<Configuration> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Configuration")
root = root.front().second;
Configuration* config = new Configuration();
config->animationFps = root.get<float>("animationFps");
return config;
}
template <>
Item* parse<Item> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Item")
root = root.front().second;
// Parse
Item* item = new Item();
item->name = root.get<std::string>("<xmlattr>.name");
item->description = root.get<std::string>("<xmlattr>.description");
item->level = root.get<uint8_t>("<xmlattr>.level");
return item;
}
template <>
Scene* parse<Scene> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Scene")
root = root.front().second;
Scene* scene = new Scene();
scene->cellWidth = root.get<uint32_t>("<xmlattr>.cellWidth");
scene->cellHeight = root.get<uint32_t>("<xmlattr>.cellHeight");
for (auto child : root)
{
if (child.first == "GameObject")
{
GameObject* obj = parse<GameObject>(child.second);
scene->root.addChild(obj);
}
}
return scene;
}
/****** Controller namespace ******/
} }
} }

View File

@ -8,80 +8,65 @@
#ifndef STORAGE_PARSERS_PARSERS_H_ #ifndef STORAGE_PARSERS_PARSERS_H_
#define STORAGE_PARSERS_PARSERS_H_ #define STORAGE_PARSERS_PARSERS_H_
#include <base/Camera.h> #include <components/Background.h>
#include <base/GameObject.h> #include <components/basic/Camera.h>
#include <base/Sprite.h> #include <components/basic/Sprite.h>
#include <base/Transform.h> #include <components/basic/Transform.h>
#include <controller/DebugController.h> #include <components/DebugController.h>
#include <controller/items/Giftable.h> #include <components/items/Giftable.h>
#include <controller/items/Weapon.h> #include <components/items/Item.h>
#include <controller/player/PlayerController.h> #include <components/items/Weapon.h>
#include <components/player/PlayerController.h>
#include <graphics/BackgroundRenderer.h> #include <graphics/BackgroundRenderer.h>
#include <graphics/SpriteRenderer.h> #include <graphics/SpriteRenderer.h>
#include <model/GameObject.h>
#include <model/Configuration.h> #include <model/Configuration.h>
#include <model/Item.h>
#include <model/Scene.h> #include <model/Scene.h>
#include <storage/Parse.h> #include <storage/Parse.h>
namespace farmlands { namespace farmlands {
namespace storage { namespace storage {
/****** Base namespace ******/ /****** Components ******/
template <> template <>
base::Camera* parse<base::Camera> (boost::property_tree::ptree& root); components::Background* parse<components::Background> (boost::property_tree::ptree& root);
template <> template <>
base::Frame* parse<base::Frame> (boost::property_tree::ptree& root); components::basic::Camera* parse<components::basic::Camera> (boost::property_tree::ptree& root);
template <> template <>
base::GameObject* parse<base::GameObject> (boost::property_tree::ptree& root); components::basic::Frame* parse<components::basic::Frame> (boost::property_tree::ptree& root);
template <> template <>
base::Sprite* parse<base::Sprite> (boost::property_tree::ptree& root); components::basic::Sprite* parse<components::basic::Sprite> (boost::property_tree::ptree& root);
template <> template <>
base::SpriteState* parse<base::SpriteState> (boost::property_tree::ptree& root); components::basic::SpriteState* parse<components::basic::SpriteState> (boost::property_tree::ptree& root);
template <> template <>
base::Transform* parse<base::Transform> (boost::property_tree::ptree& root); components::basic::Transform* parse<components::basic::Transform> (boost::property_tree::ptree& root);
/****** Model namespace ******/
template <> template <>
model::Background* parse<model::Background> (boost::property_tree::ptree& root); components::DebugController* parse<components::DebugController> (boost::property_tree::ptree& root);
template <> template <>
model::Configuration* parse<model::Configuration> (boost::property_tree::ptree& root); components::items::Giftable* parse<components::items::Giftable> (boost::property_tree::ptree& root);
template <> template <>
model::Item* parse<model::Item> (boost::property_tree::ptree& root); components::items::Item* parse<components::items::Item> (boost::property_tree::ptree& root);
template <> template <>
model::Scene* parse<model::Scene> (boost::property_tree::ptree& root); components::items::Weapon* parse<components::items::Weapon> (boost::property_tree::ptree& root);
/****** Controller namespace ******/
template <> template <>
controller::DebugController* parse<controller::DebugController> (boost::property_tree::ptree& root); components::player::PlayerController* parse<components::player::PlayerController> (boost::property_tree::ptree& root);
template <>
controller::items::Giftable* parse<controller::items::Giftable> (boost::property_tree::ptree& root);
template <>
controller::items::Weapon* parse<controller::items::Weapon> (boost::property_tree::ptree& root);
template <>
controller::DebugController* parse<controller::DebugController> (boost::property_tree::ptree& root);
template <> /****** Graphics ******/
controller::player::PlayerController* parse<controller::player::PlayerController> (boost::property_tree::ptree& root);
/****** Graphics namespace ******/
template <> template <>
graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::property_tree::ptree& root); graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::property_tree::ptree& root);
@ -90,6 +75,18 @@ namespace storage {
graphics::SpriteRenderer* parse<graphics::SpriteRenderer> (boost::property_tree::ptree& root); graphics::SpriteRenderer* parse<graphics::SpriteRenderer> (boost::property_tree::ptree& root);
/****** Model ******/
template <>
model::GameObject* parse<model::GameObject> (boost::property_tree::ptree& root);
template <>
model::Configuration* parse<model::Configuration> (boost::property_tree::ptree& root);
template <>
model::Scene* parse<model::Scene> (boost::property_tree::ptree& root);
} }
} }