summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-15 21:42:19 +0200
committerkdx <kikoodx@paranoici.org>2023-07-15 21:42:19 +0200
commit8264ea7ae10e66cb1a0ccdecdfb59992fd823b05 (patch)
treea5ebecbd6a5aa9c20ec5319460ca514521b06f11
parent7f098602cd322334d0a9e3333c5fc664ab42e9dc (diff)
downloadfld-8264ea7ae10e66cb1a0ccdecdfb59992fd823b05.tar.gz
Revert "hd render with viewport"
This reverts commit 7f098602cd322334d0a9e3333c5fc664ab42e9dc.
-rw-r--r--demo.cpp2
-rw-r--r--src/FLD.hpp8
-rw-r--r--src/constructors.cpp9
-rw-r--r--src/destructor.cpp4
-rw-r--r--src/drawBegin.cpp6
-rw-r--r--src/drawEnd.cpp29
-rw-r--r--src/init.cpp17
7 files changed, 57 insertions, 18 deletions
diff --git a/demo.cpp b/demo.cpp
index bab2123..1312f5f 100644
--- a/demo.cpp
+++ b/demo.cpp
@@ -5,7 +5,6 @@
static int
main_loop(FLD& fld, [[maybe_unused]] void *udata)
{
- fld.viewport.zoom *= 0.99;
if (fld.drawBegin())
return 1;
@@ -14,7 +13,6 @@ main_loop(FLD& fld, [[maybe_unused]] void *udata)
fld.drawSetColor(FLD::WHITE);
fld.drawRectangle({ 16, 32, 64, 32 });
- fld.drawRectangle({ 64 + 16, 32, 64, 32 });
if (fld.drawEnd())
return 1;
diff --git a/src/FLD.hpp b/src/FLD.hpp
index e430b19..4e1b8bd 100644
--- a/src/FLD.hpp
+++ b/src/FLD.hpp
@@ -46,15 +46,12 @@ public:
static constexpr Color PURPLE {1.0f, 0.0f, 1.0f, -1.0f};
static constexpr Color CYAN {0.0f, 1.0f, 1.0f, -1.0f};
static constexpr Color WHITE {1.0f, 1.0f, 1.0f, -1.0f};
+ Point viewport = {0, 0};
- struct {
- int x = 0;
- int y = 0;
- double zoom = 1.0;
- } viewport;
const Config config;
READONLY(SDL_Window *, window, nullptr);
READONLY(SDL_Renderer *, renderer, nullptr);
+ READONLY(SDL_Texture *, target, nullptr);
READONLY(unsigned long, tick, 0);
READONLY(Color, drawColor, Color({0, 0, 0, 1}));
public:
@@ -90,5 +87,6 @@ private:
[[nodiscard]] Error initBasepath() const;
[[nodiscard]] Error initWindow();
[[nodiscard]] Error initRenderer();
+ [[nodiscard]] Error initTarget();
void cycleEvents();
};
diff --git a/src/constructors.cpp b/src/constructors.cpp
index 90ac77d..921dba8 100644
--- a/src/constructors.cpp
+++ b/src/constructors.cpp
@@ -1,9 +1,4 @@
#include "FLD.hpp"
-FLD::FLD(const Config& config) :
- config(config)
-{}
-
-FLD::FLD(const Config&& config) :
- config(std::move(config))
-{}
+FLD::FLD(const Config& config) : config(config) {}
+FLD::FLD(const Config&& config) : config(std::move(config)) {}
diff --git a/src/destructor.cpp b/src/destructor.cpp
index 9863c70..7f1c906 100644
--- a/src/destructor.cpp
+++ b/src/destructor.cpp
@@ -3,6 +3,10 @@
FLD::~FLD()
{
+ if (_target != nullptr) {
+ SDL_DestroyTexture(_target);
+ _target = nullptr;
+ }
if (_renderer != nullptr) {
SDL_DestroyRenderer(_renderer);
_renderer = nullptr;
diff --git a/src/drawBegin.cpp b/src/drawBegin.cpp
index fdc76ea..2991917 100644
--- a/src/drawBegin.cpp
+++ b/src/drawBegin.cpp
@@ -3,9 +3,9 @@
FLD::Error
FLD::drawBegin() const
{
- const auto width = config.width * viewport.zoom;
- const auto height = config.height * viewport.zoom;
- if (SDL_RenderSetLogicalSize(_renderer, width, height) < 0)
+ if (SDL_SetRenderTarget(_renderer, _target) < 0)
+ return sdlError();
+ if (SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND))
return sdlError();
return 0;
}
diff --git a/src/drawEnd.cpp b/src/drawEnd.cpp
index 6e46e61..1db58cd 100644
--- a/src/drawEnd.cpp
+++ b/src/drawEnd.cpp
@@ -4,6 +4,35 @@
FLD::Error
FLD::drawEnd()
{
+ if (SDL_SetRenderTarget(_renderer, nullptr) < 0)
+ return sdlError();
+ if (SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0xff) < 0)
+ return sdlError();
+ if (SDL_RenderClear(_renderer) < 0)
+ return sdlError();
+
+ int win_w, win_h;
+ SDL_GetWindowSize(_window, &win_w, &win_h);
+ win_w = win_w ? win_w : 1;
+ win_h = win_h ? win_h : 1;
+
+ const float ratio_w = win_w / static_cast<float>(config.width);
+ const float ratio_h = win_h / static_cast<float>(config.height);
+ transform.scale = (ratio_w < ratio_h) ? ratio_w : ratio_h;
+ if (transform.scale > 1.0f && config.pixel_perfect)
+ transform.scale = static_cast<int>(transform.scale);
+ transform.off_x = (win_w - config.width * transform.scale) / 2;
+ transform.off_y = (win_h - config.height * transform.scale) / 2;
+
+ const SDL_Rect dest = {
+ .x = transform.off_x,
+ .y = transform.off_y,
+ .w = static_cast<int>(config.width * transform.scale),
+ .h = static_cast<int>(config.height * transform.scale)
+ };
+ if (SDL_RenderCopy(_renderer, _target, nullptr, &dest) < 0)
+ return sdlError();
+
if (fps.managed) {
fps.next_time += fps.min_dt;
const Uint64 cur_time = SDL_GetTicks64();
diff --git a/src/init.cpp b/src/init.cpp
index a610c12..2cd1b2f 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -22,8 +22,10 @@ FLD::init()
return err;
if (auto err = initRenderer())
return err;
+ if (auto err = initTarget())
+ return err;
- SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
if (config.hide_cursor) {
if (SDL_ShowCursor(SDL_FALSE) < 0)
@@ -78,3 +80,16 @@ FLD::initRenderer()
return sdlError();
return 0;
}
+
+FLD::Error
+FLD::initTarget()
+{
+ _target = SDL_CreateTexture(_renderer,
+ SDL_PIXELFORMAT_RGB888,
+ SDL_TEXTUREACCESS_TARGET,
+ config.width,
+ config.height);
+ if (_target == nullptr)
+ return sdlError();
+ return 0;
+}