aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp102
-rw-r--r--src/crepe/facade/SDLContext.h43
2 files changed, 109 insertions, 36 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index b3298a7..de7d08f 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -8,9 +8,9 @@
#include <cmath>
#include <cstddef>
#include <functional>
+#include <iostream>
#include <memory>
#include <stdexcept>
-#include <string>
#include "../api/Camera.h"
#include "../api/Sprite.h"
@@ -18,6 +18,7 @@
#include "../api/Transform.h"
#include "../api/Vector2.h"
#include "../util/Log.h"
+#include "api/Vector2.h"
#include "SDLContext.h"
@@ -93,7 +94,10 @@ void SDLContext::handle_events(bool & running) {
*/
}
-void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
+void SDLContext::clear_screen() {
+ SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255);
+ SDL_RenderClear(this->game_renderer.get());
+}
void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); }
SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {
@@ -104,57 +108,111 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {
.h = sprite.sprite_rect.h,
};
}
-SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos,
- const double & scale, const Camera & cam) const {
+SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos,
+ const Vector2 & cam_pos, const double & img_scale,
+ const Vector2 & cam_scale) const {
+
+ int pixel_width, pixel_height;
+
+ if (sprite.sprite_rect.w > sprite.sprite_rect.h) {
+ pixel_width = static_cast<int>(sprite.width * cam_scale.x);
+ pixel_height = static_cast<int>(pixel_width / sprite.aspect_ratio);
+ } else {
+ pixel_height = static_cast<int>(sprite.height * cam_scale.y);
+ pixel_width = static_cast<int>(pixel_height * sprite.aspect_ratio);
+ }
+
+ pixel_width *= img_scale;
+ pixel_height *= img_scale;
- double adjusted_w = sprite.sprite_rect.w * scale * cam.zoom;
- double adjusted_h = sprite.sprite_rect.h * scale * cam.zoom;
- double adjusted_x = (pos.x - cam.x) * cam.zoom - adjusted_w / 2;
- double adjusted_y = (pos.y - cam.y) * cam.zoom - adjusted_h / 2;
+ int pixel_x = static_cast<int>((pos.x - cam_pos.x + this->viewport.w / 2 - pixel_width / 2));
+ int pixel_y = static_cast<int>((pos.y - cam_pos.y + this->viewport.h / 2 - pixel_height / 2));
return SDL_Rect{
- .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 = pixel_x,
+ .y = pixel_y,
+ .w = pixel_width,
+ .h = pixel_height,
};
}
-void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle,
- const double & scale, const Camera & camera) {
+void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos,
+ const double & angle, const Vector2 & cam_pos,
+ const double & img_scale, const Vector2 & cam_scale) {
SDL_RendererFlip render_flip
= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
| (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
SDL_Rect srcrect = this->get_src_rect(sprite);
- SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale, camera);
+ SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam_pos, img_scale, cam_scale);
SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,
&dstrect, angle, NULL, render_flip);
}
-void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) {
+void SDLContext::draw(const Sprite & sprite, const Transform & transform,
+ const Vector2 & cam_pos, const Vector2 & cam_scale) {
SDL_RendererFlip render_flip
= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
| (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
SDL_Rect srcrect = this->get_src_rect(sprite);
- SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, transform.scale, cam);
+ SDL_Rect dstrect
+ = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale);
SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,
&dstrect, transform.rotation, NULL, render_flip);
}
-void SDLContext::set_camera(const Camera & cam) {
- this->viewport.w = static_cast<int>(cam.aspect_width);
- this->viewport.h = static_cast<int>(cam.aspect_height);
- this->viewport.x = static_cast<int>(cam.x) - (this->viewport.w / 2);
- this->viewport.y = static_cast<int>(cam.y) - (this->viewport.h / 2);
+void SDLContext::set_camera(const Camera & cam, Vector2 & scale) {
+
+ // resize window
+ if (this->viewport.w != (int) cam.screen.x || this->viewport.h != (int) cam.screen.y) {
+ SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y);
+ this->viewport.h = (int)cam.screen.y;
+ this->viewport.w = (int)cam.screen.x;
+ }
+
+ double screen_aspect = cam.screen.x / cam.screen.y;
+ double viewport_aspect = cam.viewport.x / cam.viewport.y;
+
+ // decide scaling factor for world to screen
+ scale = cam.screen / cam.viewport * cam.zoom;
+
+ SDL_Rect view;
+
+ // calculate black bars
+ if (screen_aspect > viewport_aspect) {
+ // lettorboxing
+ view.w = static_cast<int>(cam.screen.y * viewport_aspect);
+ view.x = static_cast<int>(cam.screen.x - view.w) / 2;
+ view.y = 0;
+ } else {
+ // pillarboxing
+ view.h = static_cast<int>(cam.screen.x / viewport_aspect);
+ view.w = static_cast<int>(cam.screen.x / cam.zoom);
+ view.x = 0;
+ view.y = static_cast<int>(cam.screen.y - view.h) / 2;
+ }
+ // set drawing area
+ SDL_RenderSetViewport(this->game_renderer.get(), &view);
+
+ SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y);
+ // set bg color
SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
cam.bg_color.b, cam.bg_color.a);
+
+ SDL_Rect bg = {
+ .x = 0,
+ .y = 0,
+ .w = static_cast<int>(cam.viewport.x),
+ .h = static_cast<int>(cam.viewport.y),
+ };
+ // fill bg color
+ SDL_RenderFillRect(this->game_renderer.get(), &bg);
}
uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 20e30b3..aed5797 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -117,12 +117,25 @@ private:
* \brief Draws a sprite to the screen using the specified transform and camera.
* \param sprite Reference to the Sprite to draw.
* \param transform Reference to the Transform for positioning.
- * \param camera Reference to the Camera for view adjustments.
+ * \param cam_pos position of the current camera in the scene
+ * \param cam_scale multiplier for the world to screen
*/
- void draw(const Sprite & sprite, const Transform & transform, const Camera & camera);
+ void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos,
+ const Vector2 & cam_scale);
- void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle,
- const double & scale, const Camera & camera);
+ /**
+ * \brief Draws a particle to the screen using the specified parameters
+ *
+ * \param sprite Referenceto the sprite to draw
+ * \param pos particle position in world units
+ * \param angle particle angle in degrees
+ * \param cam_pos camera position in world units
+ * \param img_scale scalar multiplier to increase image size
+ * \param cam_scale camera scalar for world to screen
+ */
+ void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle,
+ const Vector2 & cam_pos, const double & img_scale,
+ const Vector2 & cam_scale);
//! Clears the screen, preparing for a new frame.
void clear_screen();
@@ -134,7 +147,7 @@ private:
* \brief sets the background of the camera (will be adjusted in future PR)
* \param camera Reference to the Camera object.
*/
- void set_camera(const Camera & camera);
+ void set_camera(const Camera & camera, Vector2 & scale);
private:
/**
@@ -144,18 +157,19 @@ private:
* \return sdl rectangle to draw a src image
*/
SDL_Rect get_src_rect(const Sprite & sprite) const;
+
/**
- * \brief calculates the sqaure size of the image for an destination
+ * \brief calculates the sqaure size of the image for destination
*
- * \param sprite Reference to the sprite to calculate the rectangle
- * \param pos the pos in pixel positions
- * \param scale the multiplier to increase of decrease for the specified sprite
- * \param cam Reference to the current camera in the scene to calculate the position based
- * on the camera
+ * \param sprite Reference to the sprite to calculate rectangle
+ * \param pos the pos in world units
+ * \param cam_pos the camera position in world units
+ * \param img_scale the image multiplier for increasing img size
+ * \param scale the multiplier for world to screen
* \return sdl rectangle to draw a dst image to draw on the screen
*/
- SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const double & scale,
- const Camera & cam) const;
+ SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos,
+ const double & img_scale, const Vector2 & scale) const;
private:
//! sdl Window
@@ -165,7 +179,8 @@ private:
std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
//! viewport for the camera window
- SDL_Rect viewport = {0, 0, 640, 480};
+ //todo change this so that it becomes a vec2 for only width and height
+ SDL_Rect viewport = {0, 0, 1200, 1200};
};
} // namespace crepe