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/Camera.cpp10
-rw-r--r--src/crepe/api/Camera.h25
-rw-r--r--src/crepe/api/Config.h10
-rw-r--r--src/crepe/api/Sprite.cpp19
-rw-r--r--src/crepe/api/Sprite.h64
-rw-r--r--src/crepe/api/Texture.cpp11
-rw-r--r--src/crepe/api/Texture.h5
9 files changed, 100 insertions, 63 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/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 0c9d116..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 {
@@ -62,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/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 264d7b1..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"
@@ -19,6 +17,15 @@ Texture::~Texture() {
this->texture.reset();
}
+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 = ctx.texture_from_path(res.get_path());
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
index b4f7d07..7206a66 100644
--- a/src/crepe/api/Texture.h
+++ b/src/crepe/api/Texture.h
@@ -36,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.