aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Animator.cpp13
-rw-r--r--src/crepe/api/Animator.h6
-rw-r--r--src/crepe/api/CMakeLists.txt1
-rw-r--r--src/crepe/api/Camera.cpp10
-rw-r--r--src/crepe/api/Camera.h25
-rw-r--r--src/crepe/api/Config.h23
-rw-r--r--src/crepe/api/GameObject.cpp6
-rw-r--r--src/crepe/api/GameObject.h9
-rw-r--r--src/crepe/api/LoopManager.cpp2
-rw-r--r--src/crepe/api/LoopTimer.cpp2
-rw-r--r--src/crepe/api/LoopTimer.h4
-rw-r--r--src/crepe/api/Scene.cpp5
-rw-r--r--src/crepe/api/Scene.h23
-rw-r--r--src/crepe/api/SceneManager.h4
-rw-r--r--src/crepe/api/SceneManager.hpp12
-rw-r--r--src/crepe/api/Sprite.cpp19
-rw-r--r--src/crepe/api/Sprite.h64
-rw-r--r--src/crepe/api/Texture.cpp24
-rw-r--r--src/crepe/api/Texture.h17
19 files changed, 159 insertions, 110 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index 464b0fd..45f67f6 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -14,11 +14,14 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a
col(col) {
dbg_trace();
- animator_rect = spritesheet.sprite_rect;
- animator_rect.h /= col;
- animator_rect.w /= row;
- animator_rect.x = 0;
- animator_rect.y = col_animator * animator_rect.h;
+ this->spritesheet.mask.h /= col;
+ this->spritesheet.mask.w /= row;
+ this->spritesheet.mask.x = 0;
+ this->spritesheet.mask.y = col_animator * this->spritesheet.mask.h;
this->active = false;
+
+ // need to do this for to get the aspect ratio for a single clipping in the spritesheet
+ this->spritesheet.aspect_ratio
+ = static_cast<double>(this->spritesheet.mask.w) / this->spritesheet.mask.h;
}
Animator::~Animator() { dbg_trace(); }
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h
index 53f4b91..6c506aa 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -40,10 +40,6 @@ public:
Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate);
~Animator(); // dbg_trace
- Animator(const Animator &) = delete;
- Animator(Animator &&) = delete;
- Animator & operator=(const Animator &) = delete;
- Animator & operator=(Animator &&) = delete;
private:
//! A reference to the Sprite sheet containing the animation frames.
@@ -61,8 +57,6 @@ private:
//! The current row being animated.
int curr_row = 0;
- Rect animator_rect;
-
//TODO: Is this necessary?
//int fps;
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 057f88c..d8215b9 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -12,7 +12,6 @@ target_sources(crepe PUBLIC
SaveManager.cpp
Config.cpp
Metadata.cpp
- Scene.cpp
SceneManager.cpp
Camera.cpp
Animator.cpp
diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp
index 5835bdd..39d8ab0 100644
--- a/src/crepe/api/Camera.cpp
+++ b/src/crepe/api/Camera.cpp
@@ -1,3 +1,4 @@
+#include "types.h"
#include "util/Log.h"
#include "Camera.h"
@@ -6,9 +7,14 @@
using namespace crepe;
-Camera::Camera(game_object_id_t id, const Color & bg_color)
+Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
+ const vec2 & viewport_size, const double & zoom, const vec2 & offset)
: Component(id),
- bg_color(bg_color) {
+ bg_color(bg_color),
+ offset(offset),
+ screen(screen),
+ viewport_size(viewport_size),
+ zoom(zoom) {
dbg_trace();
}
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index e0cda34..2d8fa48 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -2,6 +2,7 @@
#include "Color.h"
#include "Component.h"
+#include "types.h"
namespace crepe {
@@ -20,33 +21,31 @@ public:
* \param id Unique identifier for the camera component.
* \param bg_color Background color for the camera view.
*/
- Camera(game_object_id_t id, const Color & bg_color);
+ Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
+ const vec2 & viewport_size, const double & zoom, const vec2 & offset = {0, 0});
~Camera(); // dbg_trace only
public:
//! Background color of the camera view.
- Color bg_color;
+ const Color bg_color;
- //! Aspect ratio height for the camera.
- double aspect_height = 480;
+ //! offset postion from the game object transform component
+ vec2 offset;
- //! Aspect ratio width for the camera.
- double aspect_width = 640;
+ //! screen the display size in pixels ( output resolution )
+ const ivec2 screen;
- //! X-coordinate of the camera position.
- double x = 0.0;
-
- //! Y-coordinate of the camera position.
- double y = 0.0;
+ //! viewport is the area of the world visible through the camera (in world units)
+ const vec2 viewport_size;
//! Zoom level of the camera view.
- double zoom = 1.0;
+ const double zoom;
public:
/**
* \brief Gets the maximum number of camera instances allowed.
* \return Maximum instance count as an integer.
*/
- virtual int get_instances_max() const { return 10; }
+ virtual int get_instances_max() const { return 1; }
};
} // namespace crepe
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 13eabd1..225e9b9 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -1,6 +1,8 @@
#pragma once
#include "../util/Log.h"
+#include "types.h"
+#include <string>
namespace crepe {
@@ -11,19 +13,18 @@ namespace crepe {
* modified *before* execution is handed over from the game programmer to the engine (i.e. the
* main loop is started).
*/
-class Config {
+class Config final {
public:
//! Retrieve handle to global Config instance
static Config & get_instance();
private:
Config() = default;
-
- // singleton
- Config(const Config &) = delete;
- Config(Config &&) = delete;
- Config & operator=(const Config &) = delete;
- Config & operator=(Config &&) = delete;
+ ~Config() = default;
+ Config(const Config &) = default;
+ Config(Config &&) = default;
+ Config & operator=(const Config &) = default;
+ Config & operator=(Config &&) = default;
public:
//! Logging-related settings
@@ -63,6 +64,14 @@ public:
double gravity = 1;
} physics;
+ //! default window settings
+ struct {
+ //TODO make this constexpr because this will never change
+ ivec2 default_size = {1080, 720};
+ std::string window_title = "Jetpack joyride clone";
+
+ } window_settings;
+
//! Asset loading options
struct {
/**
diff --git a/src/crepe/api/GameObject.cpp b/src/crepe/api/GameObject.cpp
index 3c36a21..9ef4682 100644
--- a/src/crepe/api/GameObject.cpp
+++ b/src/crepe/api/GameObject.cpp
@@ -30,3 +30,9 @@ void GameObject::set_parent(const GameObject & parent) {
RefVector<Metadata> parent_metadata = mgr.get_components_by_id<Metadata>(parent.id);
parent_metadata.at(0).get().children.push_back(this->id);
}
+
+void GameObject::set_persistent(bool persistent) {
+ ComponentManager & mgr = this->component_manager;
+
+ mgr.set_persistent(this->id, persistent);
+}
diff --git a/src/crepe/api/GameObject.h b/src/crepe/api/GameObject.h
index fcb8d9a..4cd2bc0 100644
--- a/src/crepe/api/GameObject.h
+++ b/src/crepe/api/GameObject.h
@@ -58,6 +58,15 @@ public:
*/
template <typename T, typename... Args>
T & add_component(Args &&... args);
+ /**
+ * \brief Components will not be deleted if this method is called
+ *
+ * This method sets the persistent flag of the GameObject to true. If the persistent
+ * flag is set to true, the GameObject will not be deleted when the scene is changed.
+ *
+ * \param persistent The persistent flag
+ */
+ void set_persistent(bool persistent = true);
public:
//! The id of the GameObject
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 35919cc..feb1338 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -63,7 +63,7 @@ void LoopManager::loop() {
void LoopManager::setup() {
this->game_running = true;
LoopTimer::get_instance().start();
- LoopTimer::get_instance().set_fps(60);
+ LoopTimer::get_instance().set_fps(200);
this->scene_manager.load_next_scene();
}
diff --git a/src/crepe/api/LoopTimer.cpp b/src/crepe/api/LoopTimer.cpp
index a9800b7..15a0e3a 100644
--- a/src/crepe/api/LoopTimer.cpp
+++ b/src/crepe/api/LoopTimer.cpp
@@ -47,7 +47,7 @@ double LoopTimer::get_fixed_delta_time() const { return this->fixed_delta_time.c
void LoopTimer::set_fps(int fps) {
this->fps = fps;
// target time per frame in seconds
- this->frame_target_time = std::chrono::seconds(1) / fps;
+ this->frame_target_time = std::chrono::duration<double>(1.0) / fps;
}
int LoopTimer::get_fps() const { return this->fps; }
diff --git a/src/crepe/api/LoopTimer.h b/src/crepe/api/LoopTimer.h
index f277d7b..9393439 100644
--- a/src/crepe/api/LoopTimer.h
+++ b/src/crepe/api/LoopTimer.h
@@ -130,9 +130,9 @@ private:
//! Delta time for the current frame in seconds
std::chrono::duration<double> delta_time{0.0};
//! Target time per frame in seconds
- std::chrono::duration<double> frame_target_time = std::chrono::seconds(1) / fps;
+ std::chrono::duration<double> frame_target_time = std::chrono::duration<double>(1.0) / fps;
//! Fixed delta time for fixed updates in seconds
- std::chrono::duration<double> fixed_delta_time = std::chrono::seconds(1) / 50;
+ std::chrono::duration<double> fixed_delta_time = std::chrono::duration<double>(1.0) / 50.0;
//! Total elapsed game time in seconds
std::chrono::duration<double> elapsed_time{0.0};
//! Total elapsed time for fixed updates in seconds
diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp
deleted file mode 100644
index 849945e..0000000
--- a/src/crepe/api/Scene.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-#include "Scene.h"
-
-using namespace crepe;
-
-Scene::Scene(ComponentManager & mgr) : component_manager(mgr) {}
diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h
index 869bf6f..f6fdb2a 100644
--- a/src/crepe/api/Scene.h
+++ b/src/crepe/api/Scene.h
@@ -2,6 +2,8 @@
#include <string>
+#include "../util/OptionalRef.h"
+
namespace crepe {
class SceneManager;
@@ -15,11 +17,8 @@ class ComponentManager;
*/
class Scene {
protected:
- //TODO: Use Loek's custom reference class to set ComponentManger via SceneManager instead of via constructor
- /**
- * \param mgr Reference to the ComponentManager
- */
- Scene(ComponentManager & mgr);
+ // NOTE: This must be the only constructor on Scene, see "Late references" below
+ Scene() = default;
//! SceneManager instances Scene
friend class SceneManager;
@@ -36,8 +35,20 @@ public:
virtual std::string get_name() const = 0;
protected:
+ /**
+ * \name Late references
+ *
+ * These references are set by SceneManager immediately after calling the constructor of Scene.
+ *
+ * \note Scene must have a constructor without arguments so the game programmer doesn't need to
+ * manually add `using Scene::Scene` to their concrete scene class, if they want to add a
+ * constructor with arguments (e.g. for passing references to their own concrete Scene classes).
+ *
+ * \{
+ */
//! Reference to the ComponentManager
- ComponentManager & component_manager;
+ OptionalRef<ComponentManager> component_manager;
+ //! \}
};
} // namespace crepe
diff --git a/src/crepe/api/SceneManager.h b/src/crepe/api/SceneManager.h
index 45ba668..f6f62cd 100644
--- a/src/crepe/api/SceneManager.h
+++ b/src/crepe/api/SceneManager.h
@@ -26,8 +26,8 @@ public:
*
* \tparam T Type of concrete scene
*/
- template <typename T>
- void add_scene();
+ template <typename T, typename... Args>
+ void add_scene(Args &&... args);
/**
* \brief Set the next scene
*
diff --git a/src/crepe/api/SceneManager.hpp b/src/crepe/api/SceneManager.hpp
index 94e5946..5c8e417 100644
--- a/src/crepe/api/SceneManager.hpp
+++ b/src/crepe/api/SceneManager.hpp
@@ -4,13 +4,17 @@
namespace crepe {
-template <typename T>
-void SceneManager::add_scene() {
+template <typename T, typename... Args>
+void SceneManager::add_scene(Args &&... args) {
using namespace std;
static_assert(is_base_of<Scene, T>::value, "T must be derived from Scene");
- Scene * scene = new T(this->component_manager);
- this->scenes.emplace_back(unique_ptr<Scene>(scene));
+ Scene * scene = new T(std::forward<Args>(args)...);
+ unique_ptr<Scene> unique_scene(scene);
+
+ unique_scene->component_manager = this->component_manager;
+
+ this->scenes.emplace_back(std::move(unique_scene));
// The first scene added, is the one that will be loaded at the beginning
if (next_scene.empty()) {
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index bd2d5cf..8647794 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -1,7 +1,7 @@
-#include <memory>
+#include <cmath>
+#include <utility>
#include "../util/Log.h"
-#include "facade/SDLContext.h"
#include "Component.h"
#include "Sprite.h"
@@ -10,16 +10,21 @@
using namespace std;
using namespace crepe;
-Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color & color,
- const FlipSettings & flip)
+Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color,
+ const FlipSettings & flip, int sort_layer, int order_layer, int height)
: Component(id),
color(color),
flip(flip),
- sprite_image(image) {
+ sprite_image(std::move(image)),
+ sorting_in_layer(sort_layer),
+ order_in_layer(order_layer),
+ height(height) {
+
dbg_trace();
- this->sprite_rect.w = sprite_image->get_width();
- this->sprite_rect.h = sprite_image->get_height();
+ this->mask.w = sprite_image.get_width();
+ this->mask.h = sprite_image.get_height();
+ this->aspect_ratio = static_cast<double>(this->mask.w) / this->mask.h;
}
Sprite::~Sprite() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 74a55d4..a0e90a0 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,6 +1,6 @@
#pragma once
-#include <memory>
+#include <cstdint>
#include "../Component.h"
@@ -9,18 +9,6 @@
namespace crepe {
-struct Rect {
- int w = 0;
- int h = 0;
- int x = 0;
- int y = 0;
-};
-
-struct FlipSettings {
- bool flip_x = false;
- bool flip_y = false;
-};
-
class SDLContext;
class Animator;
class AnimatorSystem;
@@ -34,6 +22,12 @@ class AnimatorSystem;
class Sprite : public Component {
public:
+ struct FlipSettings {
+ bool flip_x = false;
+ bool flip_y = false;
+ };
+
+public:
// TODO: Loek comment in github #27 will be looked another time
// about shared_ptr Texture
/**
@@ -42,9 +36,12 @@ public:
* \param image Shared pointer to the texture for this sprite.
* \param color Color tint applied to the sprite.
* \param flip Flip settings for horizontal and vertical orientation.
+ * \param order_layer decides the sorting in layer of the sprite.
+ * \param sort_layer decides the order in layer of the sprite.
+ * \param height the height of the image in game units
*/
- Sprite(game_object_id_t id, const std::shared_ptr<Texture> image, const Color & color,
- const FlipSettings & flip);
+ Sprite(game_object_id_t id, Texture & image, const Color & color,
+ const FlipSettings & flip, int sort_layer, int order_layer, int height);
/**
* \brief Destroys the Sprite instance.
@@ -52,38 +49,49 @@ public:
~Sprite();
//! Texture used for the sprite
- const std::shared_ptr<Texture> sprite_image;
+ const Texture sprite_image;
+
//! Color tint of the sprite
Color color;
+
//! Flip settings for the sprite
FlipSettings flip;
+
//! Layer sorting level of the sprite
- uint8_t sorting_in_layer = 0;
+ const int sorting_in_layer;
//! Order within the sorting layer
- uint8_t order_in_layer = 0;
+ const int order_in_layer;
+
+ //! height in world units
+ const int height;
-public:
/**
- * \brief Gets the maximum number of instances allowed for this sprite.
- * \return Maximum instance count as an integer.
+ * \aspect_ratio ratio of the img so that scaling will not become weird
*
- * For now is this number randomly picked. I think it will eventually be 1.
+ * cannot be const because if Animator component is addded then ratio becomes scuffed and
+ * does it need to be calculated again in the Animator
*/
- virtual int get_instances_max() const { return 10; }
+ double aspect_ratio;
private:
- //! Reads the sprite_rect of sprite
+ //! Reads the mask of sprite
friend class SDLContext;
- //! Reads the all the variables plus the sprite_rect
+ //! Reads the all the variables plus the mask
friend class Animator;
- //! Reads the all the variables plus the sprite_rect
+ //! Reads the all the variables plus the mask
friend class AnimatorSystem;
+ struct Rect {
+ int w = 0;
+ int h = 0;
+ int x = 0;
+ int y = 0;
+ };
//! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator
- // object is present in GameObject
- Rect sprite_rect;
+ // object is present in GameObject. this is in sprite pixels
+ Rect mask;
};
} // namespace crepe
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index 9be9421..e43bdaa 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -1,5 +1,3 @@
-#include <SDL2/SDL_render.h>
-
#include "../facade/SDLContext.h"
#include "../util/Log.h"
@@ -9,14 +7,9 @@
using namespace crepe;
using namespace std;
-Texture::Texture(unique_ptr<Asset> res) {
- dbg_trace();
- this->load(std::move(res));
-}
-
-Texture::Texture(const char * src) {
+Texture::Texture(const Asset & src) {
dbg_trace();
- this->load(make_unique<Asset>(src));
+ this->load(src);
}
Texture::~Texture() {
@@ -24,9 +17,18 @@ Texture::~Texture() {
this->texture.reset();
}
-void Texture::load(unique_ptr<Asset> res) {
+Texture::Texture(Texture && other) noexcept : texture(std::move(other.texture)) {}
+
+Texture & Texture::operator=(Texture && other) noexcept {
+ if (this != &other) {
+ texture = std::move(other.texture);
+ }
+ return *this;
+}
+
+void Texture::load(const Asset & res) {
SDLContext & ctx = SDLContext::get_instance();
- this->texture = std::move(ctx.texture_from_path(res->get_path()));
+ this->texture = ctx.texture_from_path(res.get_path());
}
int Texture::get_width() const {
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
index 6965223..7206a66 100644
--- a/src/crepe/api/Texture.h
+++ b/src/crepe/api/Texture.h
@@ -25,16 +25,10 @@ class Texture {
public:
/**
- * \brief Constructs a Texture from a file path.
- * \param src Path to the image file to be loaded as a texture.
- */
- Texture(const char * src);
-
- /**
* \brief Constructs a Texture from an Asset resource.
- * \param res Unique pointer to an Asset resource containing texture data.
+ * \param src Asset with texture data to load.
*/
- Texture(std::unique_ptr<Asset> res);
+ Texture(const Asset & src);
/**
* \brief Destroys the Texture instance, freeing associated resources.
@@ -42,6 +36,11 @@ public:
~Texture();
// FIXME: this constructor shouldn't be necessary because this class doesn't manage memory
+ Texture(Texture && other) noexcept;
+ Texture & operator=(Texture && other) noexcept;
+ Texture(const Texture &) = delete;
+ Texture & operator=(const Texture &) = delete;
+
/**
* \brief Gets the width of the texture.
* \return Width of the texture in pixels.
@@ -59,7 +58,7 @@ private:
* \brief Loads the texture from an Asset resource.
* \param res Unique pointer to an Asset resource to load the texture from.
*/
- void load(std::unique_ptr<Asset> res);
+ void load(const Asset & res);
private:
//! The texture of the class from the library