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

View File

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

View File

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

View File

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

View File

@ -8,10 +8,10 @@
#ifndef MODEL_GAMESTATE_H_
#define MODEL_GAMESTATE_H_
#include <base/GameObject.h>
#include <base/RenderContext.h>
#include <model/GameObject.h>
#include <model/Scene.h>
#include <model/Configuration.h>
#include <graphics/RenderContext.h>
#include <resources/ResourceManager.h>
#include <memory>
@ -30,11 +30,11 @@ namespace farmlands {
model::Configuration* config;
// Render context
base::RenderContext renderContext;
graphics::RenderContext renderContext;
// Scene
model::Scene* scene;
std::vector<base::GameObject*> itemPrefabs;
std::vector<model::GameObject*> itemPrefabs;
// Misc
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
*/
#include <model/Background.h>
#include <components/Background.h>
#include <utils/Assert.h>
#include <iostream>
namespace farmlands {
namespace model {
namespace components {
Background::Background(size_t layerCount, size_t rowCount, size_t columnCount)
@ -29,7 +29,7 @@ Background::~Background()
delete[] m_textures;
}
base::Component* Background::clone()
model::Component* Background::clone()
{
Background* clone = new Background(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_
#define MODEL_BACKGROUND_H_
#include <base/Component.h>
#include <model/Component.h>
#include <resources/ResourceManager.h>
namespace farmlands {
namespace model {
namespace components {
typedef int16_t Cell;
class Background: public base::Component
class Background: public model::Component
{
public:
Background(size_t layerCount, size_t rowCount, size_t columnCount);
virtual ~Background();
virtual base::Component* clone() override;
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
inline size_t layerCount() const { return m_layers; }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,22 +5,26 @@
* Author: tibi
*/
#include <base/Sprite.h>
#include <GameState.h>
#include <components/basic/Sprite.h>
#include <utils/Assert.h>
#include <algorithm>
#include <iostream>
namespace farmlands {
namespace base {
namespace components {
namespace basic {
Sprite::Sprite()
: anchorX(0), anchorY(0),
name(),
: name(),
anchorX(0), anchorY(0),
animationVelocity(1.0f),
m_states(),
m_stateNames(),
m_currentState(0),
m_currentFrame(0),
m_currentFrameTimeLeft(0),
m_animationVelocity(1)
m_currentFrameTimeLeft(0)
{
}
@ -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)
{
Assert(state.frames.size() > 0, "State must have at least one frame!");
#ifdef BUILD_DEBUG
uint32_t totalDuration = 0;
float totalDuration = 0;
for (auto frame : state.frames)
totalDuration += frame.duration;
@ -63,11 +101,23 @@ void Sprite::setState(const std::string& 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)
{
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)
{
@ -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 */

View File

@ -8,13 +8,15 @@
#ifndef MODEL_SPRITE_H_
#define MODEL_SPRITE_H_
#include <model/Component.h>
#include <utils/Exceptions.h>
#include <vector>
#include <unordered_map>
namespace farmlands {
namespace base {
namespace components {
namespace basic {
/**
* Defines an animation frame
@ -24,11 +26,11 @@ namespace base {
uint32_t tileSetId;
uint32_t tileSetCell;
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
{
@ -39,12 +41,17 @@ namespace base {
/**
* Defines a sprite
*/
class Sprite
class Sprite : public model::Component
{
public:
Sprite();
virtual ~Sprite();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onPreRender() override;
/**
* Adds a state to the sprite.
*/
@ -60,35 +67,28 @@ namespace base {
*/
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
SpriteState& currentState();
Frame& currentFrame();
// Public fields
float anchorX, anchorY;
std::string name;
float anchorX, anchorY;
float animationVelocity;
private:
void advanceTime(float fractions);
std::vector<SpriteState> m_states;
std::unordered_map<std::string, size_t> m_stateNames;
size_t m_currentState;
size_t m_currentFrame;
float m_currentFrameTimeLeft;
float m_animationVelocity;
};
} /* namespace model */
}
} /* namespace components */
} /* namespace farmlands */
#endif /* MODEL_SPRITE_H_ */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,13 +4,17 @@
* Created on: Nov 30, 2016
* Author: tibi
*/
#include <base/Camera.h>
#include <base/GameObject.h>
#include <base/RenderContext.h>
#include <base/Transform.h>
#include <components/basic/Camera.h>
#include <components/basic/Transform.h>
#include <graphics/RenderContext.h>
#include <model/GameObject.h>
using namespace farmlands::components::basic;
using namespace farmlands::model;
namespace farmlands {
namespace base {
namespace graphics {
float RenderContext::xToWorld(float x)
{
@ -36,7 +40,7 @@ float RenderContext::yToScreen(float y)
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 };
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 <base/GameObject.h>
#include <base/Camera.h>
#include <model/GameObject.h>
#include <components/basic/Camera.h>
#include <graphics/backend/SdlRenderer.h>
#include <graphics/SpriteRenderer.h>
#include <utils/Assert.h>
#include <iostream>
using namespace farmlands::components::basic;
using namespace farmlands::graphics::backend;
using namespace farmlands::resources;
@ -21,31 +22,26 @@ namespace farmlands {
namespace graphics {
SpriteRenderer::SpriteRenderer()
: sprite(nullptr),
m_transform(nullptr),
m_context(nullptr)
: m_transform(nullptr),
m_context(nullptr),
m_sprite(nullptr)
{
}
SpriteRenderer::~SpriteRenderer()
{
delete sprite;
}
base::Component* SpriteRenderer::clone()
model::Component* SpriteRenderer::clone()
{
SpriteRenderer* clone = new SpriteRenderer();
clone->sprite = new base::Sprite(*sprite);
return clone;
return new SpriteRenderer();
}
void SpriteRenderer::onInitialize()
{
Assert(gameObject != nullptr, "Component not properly initialized!");
m_transform = gameObject->component<base::Transform>();
m_context = &GameState::current().renderContext;
m_transform = gameObject->component<components::basic::Transform>();
m_sprite = gameObject->component<Sprite>();
}
void SpriteRenderer::onRender()
@ -53,27 +49,27 @@ void SpriteRenderer::onRender()
float posX = m_context->xToScreen(m_transform->globalX());
float posY = m_context->yToScreen(m_transform->globalY());
float w = sprite->currentFrame().width * m_context->viewport.pixelsPerUnitX;
float h = sprite->currentFrame().height * m_context->viewport.pixelsPerUnitY;
float w = m_sprite->currentFrame().width * m_context->viewport.pixelsPerUnitX;
float h = m_sprite->currentFrame().height * m_context->viewport.pixelsPerUnitY;
// Compute destination rectangle
float scale = m_context->camera()->scale;
SDL_Rect dest;
dest.x = posX - sprite->anchorX * w * scale;
dest.y = posY - sprite->anchorY * h * scale;
dest.x = posX - m_sprite->anchorX * w * scale;
dest.y = posY - m_sprite->anchorY * h * scale;
dest.w = w * scale;
dest.h = h * scale;
if (m_context->visible(dest))
{
// Obtain texture
int texId = sprite->currentFrame().tileSetId;
int texId = m_sprite->currentFrame().tileSetId;
SDL_Texture* texture = resources::ResourceManager::instance().texture(texId);
// Compute source rectangle
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.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)
{
int texWidth, texHeight;
@ -108,8 +96,7 @@ void SpriteRenderer::dump(unsigned level)
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: SpriteRenderer sprite=";
std::cout << sprite->name <<"\n";
std::cout << " .Component: SpriteRenderer\n";
}
} /* namespace graphics */

View File

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

View File

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

View File

@ -8,7 +8,7 @@
#ifndef GRAPHICS_SDLRENDERER_H_
#define GRAPHICS_SDLRENDERER_H_
#include <base/RenderContext.h>
#include <graphics/RenderContext.h>
#include <string>
@ -31,7 +31,7 @@ namespace backend {
*
* @returns true if successful, false otherwise.
*/
bool initialize(base::RenderContext* renderContext);
bool initialize(graphics::RenderContext* renderContext);
/**
* 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)
child->render(context);

View File

@ -20,7 +20,7 @@ namespace layout {
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)
{

View File

@ -10,7 +10,7 @@
#include <SDL2/SDL.h>
#include <base/RenderContext.h>
#include <graphics/RenderContext.h>
namespace farmlands {
namespace gui {
@ -22,7 +22,7 @@ namespace primitives {
UIElement();
virtual ~UIElement();
virtual void render(base::RenderContext* context);
virtual void render(graphics::RenderContext* context);
virtual bool handleEvent(SDL_Event& event);
// 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);
@ -258,7 +258,7 @@ void TextArea::wrapText(TTF_Font* font)
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
for (SDL_Texture* tex : m_renderedLines)

View File

@ -49,7 +49,7 @@ namespace widgets {
TextArea();
virtual ~TextArea();
virtual void render(base::RenderContext* context) override;
virtual void render(graphics::RenderContext* context) override;
// Getters and setters
inline std::string text() const { return m_text; }
@ -71,7 +71,7 @@ namespace widgets {
private:
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_wrappedText;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,170 +8,20 @@
#include <storage/Parsers.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;
namespace farmlands {
namespace storage {
/****** Base namespace ******/
/****** Components ******/
template <>
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)
void parseBackgroundCells(resources::ResourceId cellsResource, Background* back, size_t 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, ",;");
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);
nextNum = strtok(NULL, ",;");
@ -206,19 +56,27 @@ void parseBackgroundCells(resources::ResourceId cellsResource, model::Background
}
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)
if (root.front().first == "Background")
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
uint32_t layers = root.count("Layer");
uint32_t rows = root.get<uint32_t>("<xmlattr>.rows");
uint32_t cols = root.get<uint32_t>("<xmlattr>.columns");
// Create background object
model::Background* back = new model::Background(layers, rows, cols);
Background* back = new Background(layers, rows, cols);
// Read layers
size_t layerNum = 0;
@ -245,91 +103,144 @@ model::Background* parse<model::Background> (boost::property_tree::ptree& root)
}
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 == "Configuration")
if (root.front().first == "Camera")
root = root.front().second;
model::Configuration* config = new model::Configuration();
config->animationFps = root.get<float>("animationFps");
return config;
Camera* camera = new Camera();
camera->scale = root.get<float>("<xmlattr>.scale", 1.0f);
camera->mainCamera = root.get<bool>("<xmlattr>.mainCamera", false);
return camera;
}
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)
if (root.front().first == "Item")
if (root.front().first == "Frame")
root = root.front().second;
// Parse
model::Item* item = new model::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;
Frame* frame = new 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<float>("<xmlattr>.duration");
return frame;
}
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)
if (root.front().first == "Scene")
if (root.front().first == "Sprite")
root = root.front().second;
model::Scene* scene = new model::Scene();
scene->cellWidth = root.get<uint32_t>("<xmlattr>.cellWidth");
scene->cellHeight = root.get<uint32_t>("<xmlattr>.cellHeight");
// 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<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)
{
if (child.first == "GameObject")
if (child.first == "State")
{
base::GameObject* obj = parse<base::GameObject>(child.second);
scene->root.addChild(obj);
SpriteState* state = parse<SpriteState>(child.second);
sprite->addState(*state);
delete state;
}
}
return scene;
return sprite;
}
template <>
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;
/****** Controller namespace ******/
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 <>
controller::DebugController* parse<controller::DebugController> (boost::property_tree::ptree& root)
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)
if (root.front().first == "DebugController")
root = root.front().second;
controller::DebugController* controller = new controller::DebugController();
DebugController* controller = new DebugController();
return controller;
}
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)
if (root.front().first == "Giftable")
root = root.front().second;
// Parse
return new controller::items::Giftable();
return new Giftable();
}
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)
if (root.front().first == "Weapon")
root = root.front().second;
// Parse
controller::items::Weapon* weapon = new controller::items::Weapon();
Weapon* weapon = new Weapon();
weapon->damage = root.get<float>("<xmlattr>.damage");
weapon->critProbability = root.get<float>("<xmlattr>.critProbability");
weapon->critDamage = root.get<float>("<xmlattr>.critDamage");
@ -338,18 +249,18 @@ controller::items::Weapon* parse<controller::items::Weapon> (boost::property_tre
}
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)
if (root.front().first == "PlayerController")
root = root.front().second;
controller::player::PlayerController* controller = new controller::player::PlayerController();
PlayerController* controller = new PlayerController();
return controller;
}
/****** Graphics namespace ******/
/****** Graphics ******/
template <>
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;
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;
}
/****** 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_
#define STORAGE_PARSERS_PARSERS_H_
#include <base/Camera.h>
#include <base/GameObject.h>
#include <base/Sprite.h>
#include <base/Transform.h>
#include <controller/DebugController.h>
#include <controller/items/Giftable.h>
#include <controller/items/Weapon.h>
#include <controller/player/PlayerController.h>
#include <components/Background.h>
#include <components/basic/Camera.h>
#include <components/basic/Sprite.h>
#include <components/basic/Transform.h>
#include <components/DebugController.h>
#include <components/items/Giftable.h>
#include <components/items/Item.h>
#include <components/items/Weapon.h>
#include <components/player/PlayerController.h>
#include <graphics/BackgroundRenderer.h>
#include <graphics/SpriteRenderer.h>
#include <model/GameObject.h>
#include <model/Configuration.h>
#include <model/Item.h>
#include <model/Scene.h>
#include <storage/Parse.h>
namespace farmlands {
namespace storage {
/****** Base namespace ******/
/****** Components ******/
template <>
base::Camera* parse<base::Camera> (boost::property_tree::ptree& root);
components::Background* parse<components::Background> (boost::property_tree::ptree& root);
template <>
base::Frame* parse<base::Frame> (boost::property_tree::ptree& root);
components::basic::Camera* parse<components::basic::Camera> (boost::property_tree::ptree& root);
template <>
base::GameObject* parse<base::GameObject> (boost::property_tree::ptree& root);
components::basic::Frame* parse<components::basic::Frame> (boost::property_tree::ptree& root);
template <>
base::Sprite* parse<base::Sprite> (boost::property_tree::ptree& root);
components::basic::Sprite* parse<components::basic::Sprite> (boost::property_tree::ptree& root);
template <>
base::SpriteState* parse<base::SpriteState> (boost::property_tree::ptree& root);
components::basic::SpriteState* parse<components::basic::SpriteState> (boost::property_tree::ptree& root);
template <>
base::Transform* parse<base::Transform> (boost::property_tree::ptree& root);
/****** Model namespace ******/
components::basic::Transform* parse<components::basic::Transform> (boost::property_tree::ptree& root);
template <>
model::Background* parse<model::Background> (boost::property_tree::ptree& root);
components::DebugController* parse<components::DebugController> (boost::property_tree::ptree& root);
template <>
model::Configuration* parse<model::Configuration> (boost::property_tree::ptree& root);
components::items::Giftable* parse<components::items::Giftable> (boost::property_tree::ptree& root);
template <>
model::Item* parse<model::Item> (boost::property_tree::ptree& root);
components::items::Item* parse<components::items::Item> (boost::property_tree::ptree& root);
template <>
model::Scene* parse<model::Scene> (boost::property_tree::ptree& root);
/****** Controller namespace ******/
components::items::Weapon* parse<components::items::Weapon> (boost::property_tree::ptree& root);
template <>
controller::DebugController* parse<controller::DebugController> (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);
components::player::PlayerController* parse<components::player::PlayerController> (boost::property_tree::ptree& root);
template <>
controller::player::PlayerController* parse<controller::player::PlayerController> (boost::property_tree::ptree& root);
/****** Graphics namespace ******/
/****** Graphics ******/
template <>
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);
/****** 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);
}
}