diff options
author | kdx <kikoodx@paranoici.org> | 2023-07-12 06:13:09 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-07-12 06:13:09 +0200 |
commit | ecdc7bd890b250a2030e4c7537b29bd0a4784b5d (patch) | |
tree | aa9768e98b7b5bc5698b465e0028a9e353590845 | |
download | fld-ecdc7bd890b250a2030e4c7537b29bd0a4784b5d.tar.gz |
initial commit
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | build.sh | 9 | ||||
-rw-r--r-- | compile_flags.txt | 4 | ||||
-rw-r--r-- | demo.cpp | 11 | ||||
-rw-r--r-- | src/FLD.cpp | 41 | ||||
-rw-r--r-- | src/FLD.hpp | 16 | ||||
-rw-r--r-- | src/sdl_throw.cpp | 9 |
7 files changed, 91 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cba7efc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +a.out diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..8802526 --- /dev/null +++ b/build.sh @@ -0,0 +1,9 @@ +#!/bin/sh +CFLAGS="-std=c++20 -Wall -Wextra -iquotesrc" +LIBS="-lSDL2" +SRC="src/*.cpp demo.cpp" + +g++ $CFLAGS $SRC $LIBS +echo g++ +clang++ $CFLAGS $SRC $LIBS +echo clang++ diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..a2f670f --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,4 @@ +-Wall +-Wextra +-std=c++20 +-iquotesrc diff --git a/demo.cpp b/demo.cpp new file mode 100644 index 0000000..b8a014f --- /dev/null +++ b/demo.cpp @@ -0,0 +1,11 @@ +#include "FLD.hpp" + +int +main() +{ + FLD fld; + if (fld.init()) { + return 1; + } + return 0; +} diff --git a/src/FLD.cpp b/src/FLD.cpp new file mode 100644 index 0000000..319357e --- /dev/null +++ b/src/FLD.cpp @@ -0,0 +1,41 @@ +#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 new file mode 100644 index 0000000..d4947d6 --- /dev/null +++ b/src/FLD.hpp @@ -0,0 +1,16 @@ +#pragma once +#include <SDL2/SDL_render.h> + +#define READONLY(T, X, V) private: T _##X = V; public: T const& X = _##X + +class FLD { + READONLY(SDL_Window *, window, nullptr); + READONLY(SDL_Renderer *, renderer, nullptr); +public: + using Error = int; + + [[nodiscard]] Error init(); + ~FLD(); +private: + Error sdl_error() const; +}; diff --git a/src/sdl_throw.cpp b/src/sdl_throw.cpp new file mode 100644 index 0000000..4d66e23 --- /dev/null +++ b/src/sdl_throw.cpp @@ -0,0 +1,9 @@ +#include "FLD.hpp" +#include <SDL2/SDL_log.h> + +FLD::Error +FLD::sdl_error() const +{ + SDL_Log("SDL runtime error: %s", SDL_GetError()); + return -1; +} |