summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-15 19:32:52 +0200
committerkdx <kikoodx@paranoici.org>2023-07-15 19:32:52 +0200
commit7f098602cd322334d0a9e3333c5fc664ab42e9dc (patch)
treed50acffef2a32c54d84d8b8799d9ea6b7417b73a
parent2c9805b22860e32628cb7a0fe0b8a9ed854187aa (diff)
downloadfld-7f098602cd322334d0a9e3333c5fc664ab42e9dc.tar.gz
hd render with viewport
-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, 18 insertions, 57 deletions
diff --git a/demo.cpp b/demo.cpp
index 1312f5f..bab2123 100644
--- a/demo.cpp
+++ b/demo.cpp
@@ -5,6 +5,7 @@
static int
main_loop(FLD& fld, [[maybe_unused]] void *udata)
{
+ fld.viewport.zoom *= 0.99;
if (fld.drawBegin())
return 1;
@@ -13,6 +14,7 @@ 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 4e1b8bd..e430b19 100644
--- a/src/FLD.hpp
+++ b/src/FLD.hpp
@@ -46,12 +46,15 @@ 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:
@@ -87,6 +90,5 @@ 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 921dba8..90ac77d 100644
--- a/src/constructors.cpp
+++ b/src/constructors.cpp
@@ -1,4 +1,9 @@
#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 7f1c906..9863c70 100644
--- a/src/destructor.cpp
+++ b/src/destructor.cpp
@@ -3,10 +3,6 @@
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 2991917..fdc76ea 100644
--- a/src/drawBegin.cpp
+++ b/src/drawBegin.cpp
@@ -3,9 +3,9 @@
FLD::Error
FLD::drawBegin() const
{
- if (SDL_SetRenderTarget(_renderer, _target) < 0)
- return sdlError();
- if (SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND))
+ const auto width = config.width * viewport.zoom;
+ const auto height = config.height * viewport.zoom;
+ if (SDL_RenderSetLogicalSize(_renderer, width, height) < 0)
return sdlError();
return 0;
}
diff --git a/src/drawEnd.cpp b/src/drawEnd.cpp
index 1db58cd..6e46e61 100644
--- a/src/drawEnd.cpp
+++ b/src/drawEnd.cpp
@@ -4,35 +4,6 @@
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 2cd1b2f..a610c12 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -22,10 +22,8 @@ FLD::init()
return err;
if (auto err = initRenderer())
return err;
- if (auto err = initTarget())
- return err;
- SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
if (config.hide_cursor) {
if (SDL_ShowCursor(SDL_FALSE) < 0)
@@ -80,16 +78,3 @@ 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;
-}