summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-12 06:42:44 +0200
committerkdx <kikoodx@paranoici.org>2023-07-12 06:44:33 +0200
commit42e295485cd2ed62b773de3fd6ec57b875033978 (patch)
treed52138a98ce1d4e6281deb592a0374708ed49aec
parentecdc7bd890b250a2030e4c7537b29bd0a4784b5d (diff)
downloadfld-42e295485cd2ed62b773de3fd6ec57b875033978.tar.gz
create context
-rw-r--r--demo.cpp9
-rw-r--r--src/FLD.cpp41
-rw-r--r--src/FLD.hpp20
-rw-r--r--src/constructors.cpp4
-rw-r--r--src/destructor.cpp19
-rw-r--r--src/init.cpp80
6 files changed, 129 insertions, 44 deletions
diff --git a/demo.cpp b/demo.cpp
index b8a014f..ea5d525 100644
--- a/demo.cpp
+++ b/demo.cpp
@@ -3,9 +3,12 @@
int
main()
{
- FLD fld;
- if (fld.init()) {
+ FLD fld (FLD::Config{
+ .width=256,
+ .height=256,
+ .title="uwu",
+ });
+ if (fld.init())
return 1;
- }
return 0;
}
diff --git a/src/FLD.cpp b/src/FLD.cpp
deleted file mode 100644
index 319357e..0000000
--- a/src/FLD.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-#include "FLD.hpp"
-#include <SDL2/SDL.h>
-
-FLD::Error
-FLD::init()
-{
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
- return sdl_error();
-
- _window = SDL_CreateWindow("uwu",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- 128,
- 128,
- SDL_WINDOW_RESIZABLE);
- if (_window == nullptr)
- return sdl_error();
-
-#ifdef __EMSCRIPTEN__
- _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_SOFTWARE);
-#else
- _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
-#endif
- if (_renderer == nullptr)
- return sdl_error();
-
- return 0;
-}
-
-FLD::~FLD()
-{
- if (_renderer != nullptr) {
- SDL_DestroyRenderer(_renderer);
- _renderer = nullptr;
- }
- if (_window != nullptr) {
- SDL_DestroyWindow(_window);
- _window = nullptr;
- }
- SDL_Quit();
-}
diff --git a/src/FLD.hpp b/src/FLD.hpp
index d4947d6..cae4cf8 100644
--- a/src/FLD.hpp
+++ b/src/FLD.hpp
@@ -1,16 +1,36 @@
#pragma once
#include <SDL2/SDL_render.h>
+#include <string>
#define READONLY(T, X, V) private: T _##X = V; public: T const& X = _##X
class FLD {
+public:
+ struct Config {
+ int width = 640;
+ int height = 480;
+ int scale = 1;
+ int target_fps = 60;
+ bool pixel_perfect = true;
+ bool hide_cursor = true;
+ std::string title = "FLD project";
+ };
+
+ const Config config;
READONLY(SDL_Window *, window, nullptr);
READONLY(SDL_Renderer *, renderer, nullptr);
+ READONLY(SDL_Texture *, target, nullptr);
public:
using Error = int;
+ FLD(const Config& config);
+ FLD(const Config&& config);
[[nodiscard]] Error init();
~FLD();
private:
Error sdl_error() const;
+ [[nodiscard]] Error initBasepath() const;
+ [[nodiscard]] Error initWindow();
+ [[nodiscard]] Error initRenderer();
+ [[nodiscard]] Error initTarget();
};
diff --git a/src/constructors.cpp b/src/constructors.cpp
new file mode 100644
index 0000000..921dba8
--- /dev/null
+++ b/src/constructors.cpp
@@ -0,0 +1,4 @@
+#include "FLD.hpp"
+
+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
new file mode 100644
index 0000000..7f1c906
--- /dev/null
+++ b/src/destructor.cpp
@@ -0,0 +1,19 @@
+#include "FLD.hpp"
+#include <SDL2/SDL.h>
+
+FLD::~FLD()
+{
+ if (_target != nullptr) {
+ SDL_DestroyTexture(_target);
+ _target = nullptr;
+ }
+ if (_renderer != nullptr) {
+ SDL_DestroyRenderer(_renderer);
+ _renderer = nullptr;
+ }
+ if (_window != nullptr) {
+ SDL_DestroyWindow(_window);
+ _window = nullptr;
+ }
+ SDL_Quit();
+}
diff --git a/src/init.cpp b/src/init.cpp
new file mode 100644
index 0000000..a6689a3
--- /dev/null
+++ b/src/init.cpp
@@ -0,0 +1,80 @@
+#include "FLD.hpp"
+#include <SDL2/SDL.h>
+#include <unistd.h>
+
+FLD::Error
+FLD::init()
+{
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
+ return sdl_error();
+ if (auto err = initBasepath())
+ return err;
+ if (auto err = initWindow())
+ return err;
+ if (auto err = initRenderer())
+ return err;
+ if (auto err = initTarget())
+ return err;
+
+ SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
+
+ if (config.hide_cursor) {
+ if (SDL_ShowCursor(SDL_FALSE) < 0)
+ sdl_error();
+ }
+
+ return 0;
+}
+
+FLD::Error
+FLD::initBasepath() const
+{
+ char *const basepath = SDL_GetBasePath();
+ if (basepath == nullptr)
+ return sdl_error();
+ const int chdir_rv = chdir(basepath);
+ SDL_free(basepath);
+ if (chdir_rv < 0)
+ return perror("chdir"), -1;
+ return 0;
+}
+
+FLD::Error
+FLD::initWindow()
+{
+ _window = SDL_CreateWindow(config.title.c_str(),
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ config.width * config.scale,
+ config.height * config.scale,
+ SDL_WINDOW_RESIZABLE);
+ if (_window == nullptr)
+ return sdl_error();
+ return 0;
+}
+
+FLD::Error
+FLD::initRenderer()
+{
+#ifdef __EMSCRIPTEN__
+ _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_SOFTWARE);
+#else
+ _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
+#endif
+ if (_renderer == nullptr)
+ return sdl_error();
+ return 0;
+}
+
+FLD::Error
+FLD::initTarget()
+{
+ _target = SDL_CreateTexture(_renderer,
+ SDL_PIXELFORMAT_RGB888,
+ SDL_TEXTUREACCESS_TARGET,
+ config.width,
+ config.height);
+ if (_target == nullptr)
+ return sdl_error();
+ return 0;
+}