aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Camera.h30
-rw-r--r--src/crepe/api/Texture.cpp2
-rw-r--r--src/crepe/api/Vector2.cpp12
-rw-r--r--src/crepe/api/Vector2.h7
-rw-r--r--src/crepe/facade/SDLContext.cpp45
-rw-r--r--src/example/rendering.cpp7
6 files changed, 82 insertions, 21 deletions
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index e0cda34..73d4ef4 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -2,6 +2,7 @@
#include "Color.h"
#include "Component.h"
+#include "api/Vector2.h"
namespace crepe {
@@ -27,20 +28,35 @@ public:
//! Background color of the camera view.
Color bg_color;
- //! Aspect ratio height for the camera.
- double aspect_height = 480;
+ Vector2 pos = {0,0};
- //! Aspect ratio width for the camera.
- double aspect_width = 640;
+ Vector2 screen = {640,480};
- //! X-coordinate of the camera position.
+ Vector2 viewport = {500,500};
+
+ /*
+ //! screen width in pixel coordinates
+ double screen_w = 480;
+
+ //! screen height in pixel coordinates
+ double screen_h = 640;
+
+ //! screen widht in world units
+ double viewport_w = 500.0f;
+
+ //! screen height in world units
+ double viewport_h = 500.0f;
+
+ //! X-coordinate of the camera position. in world space
double x = 0.0;
- //! Y-coordinate of the camera position.
+ //! Y-coordinate of the camera position. in world space
double y = 0.0;
+ */
+
//! Zoom level of the camera view.
- double zoom = 1.0;
+ double zoom = 1.0f;
public:
/**
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index de0d0ea..734a5bb 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -35,5 +35,5 @@ int Texture::get_width() const {
}
int Texture::get_height() const {
if (this->texture == nullptr) return 0;
- return SDLContext::get_instance().get_width(*this);
+ return SDLContext::get_instance().get_height(*this);
}
diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp
index 30b968e..c3a49b7 100644
--- a/src/crepe/api/Vector2.cpp
+++ b/src/crepe/api/Vector2.cpp
@@ -8,11 +8,19 @@ Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y
Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; }
+Vector2 Vector2::operator*(const Vector2 & other) const{
+ return {this->x * other.x, this->y * other.y};
+}
Vector2 & Vector2::operator*=(const Vector2 & other) {
x *= other.x;
y *= other.y;
return *this;
}
+Vector2 & Vector2::operator*=(const double & other) {
+ x *= other;
+ y *= other;
+ return *this;
+}
Vector2 & Vector2::operator+=(const Vector2 & other) {
x += other.x;
@@ -26,6 +34,10 @@ Vector2 & Vector2::operator+=(double other) {
return *this;
}
+Vector2 Vector2::operator/(const Vector2 & other) const {
+ return {this->x / other.x, this->y / other.y};
+}
+
Vector2 Vector2::operator-() const { return {-x, -y}; }
bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; }
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
index 2fb6136..2a5db1d 100644
--- a/src/crepe/api/Vector2.h
+++ b/src/crepe/api/Vector2.h
@@ -18,9 +18,16 @@ struct Vector2 {
//! Multiplies this vector by a scalar and returns the result.
Vector2 operator*(double scalar) const;
+ Vector2 operator*(const Vector2 & other) const;
+
//! Multiplies this vector by another vector element-wise and updates this vector.
Vector2 & operator*=(const Vector2 & other);
+ //! Multiplies a scalar value to both components of this vector and updates this vector.
+ Vector2 & operator*=(const double & other);
+
+ Vector2 operator/(const Vector2 & other) const;
+
//! Adds another vector to this vector and updates this vector.
Vector2 & operator+=(const Vector2 & other);
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 83e91f8..5185adc 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -15,6 +15,7 @@
#include "../api/Texture.h"
#include "../api/Transform.h"
#include "../util/Log.h"
+#include "api/Vector2.h"
#include "SDLContext.h"
@@ -37,7 +38,7 @@ SDLContext::SDLContext() {
}
SDL_Window * tmp_window
= SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- this->viewport.w, this->viewport.h, 0);
+ this->viewport.w, this->viewport.h, SDL_WINDOW_RESIZABLE);
if (!tmp_window) {
// FIXME: throw exception
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
@@ -103,17 +104,30 @@ void SDLContext::handle_events(bool & running) {
void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); }
+
void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) {
SDL_RendererFlip render_flip
= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
| (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
+
+ double screen_aspect = cam.screen.x / cam.screen.y;
+ double viewport_aspect = cam.viewport.x / cam.viewport.y;
+ Vector2 scale;
+
+ if (screen_aspect > viewport_aspect) {
+ scale.x = scale.y = cam.screen.x / cam.viewport.x;
+ } else {
+ scale.y = scale.x = cam.screen.y / cam.viewport.y;
+ }
+
+ Vector2 zoomed_viewport = cam.viewport * cam.zoom;
+ Vector2 pixel_coord = (transform.position - cam.pos) * scale;
- double adjusted_x = (transform.position.x - cam.x) * cam.zoom;
- double adjusted_y = (transform.position.y - cam.y) * cam.zoom;
- double adjusted_w = sprite.sprite_rect.w * transform.scale * cam.zoom;
- double adjusted_h = sprite.sprite_rect.h * transform.scale * cam.zoom;
+ double pixel_w = sprite.sprite_rect.w * transform.scale * scale.x;
+ double pixel_h = sprite.sprite_rect.h * transform.scale * scale.y;
+ // decides which part of the sprite will be drawn
SDL_Rect srcrect = {
.x = sprite.sprite_rect.x,
.y = sprite.sprite_rect.y,
@@ -121,11 +135,12 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
.h = sprite.sprite_rect.h,
};
+ // decides where the clipped image is drawn
SDL_Rect dstrect = {
- .x = static_cast<int>(adjusted_x),
- .y = static_cast<int>(adjusted_y),
- .w = static_cast<int>(adjusted_w),
- .h = static_cast<int>(adjusted_h),
+ .x = static_cast<int>(pixel_coord.x),
+ .y = static_cast<int>(pixel_coord.y),
+ .w = static_cast<int>(pixel_w),
+ .h = static_cast<int>(pixel_h),
};
SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,
@@ -133,11 +148,19 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
}
void SDLContext::camera(const Camera & cam) {
- this->viewport.w = static_cast<int>(cam.aspect_width);
- this->viewport.h = static_cast<int>(cam.aspect_height);
+
+ if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) {
+ this->viewport.w = cam.screen.x;
+ this->viewport.h = cam.screen.y;
+ SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y);
+ }
+
+ /*
this->viewport.x = static_cast<int>(cam.x) - (SCREEN_WIDTH / 2);
this->viewport.y = static_cast<int>(cam.y) - (SCREEN_HEIGHT / 2);
+ */
+
SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
cam.bg_color.b, cam.bg_color.a);
}
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index c9e62f1..3631c76 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -23,7 +23,7 @@ int main() {
ComponentManager mgr{};
RenderSystem sys{mgr};
- GameObject obj = mgr.new_object("name", "tag", Vector2{0, 0}, 1, 1);
+ GameObject obj = mgr.new_object("name", "tag", Vector2{250, 0}, 0, 1);
GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1);
GameObject obj2 = mgr.new_object("name", "tag", Vector2{800, 0}, 1, 0.1);
@@ -32,13 +32,16 @@ int main() {
Color color(0, 0, 0, 0);
obj.add_component<Sprite>(make_shared<Texture>("../asset/texture/img.png"), color,
FlipSettings{false, false});
- obj.add_component<Camera>(Color::get_red());
+ Camera & cam = obj.add_component<Camera>(Color::get_red());
+
}
+ /*
{
Color color(0, 0, 0, 0);
obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color,
FlipSettings{true, true});
}
+ */
/*
{