Browse Source

makeRaiiPtr and makeDeferred

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
45f15cf3f9
2 changed files with 32 additions and 11 deletions
  1. 24
    0
      libswan/include/swan/util.h
  2. 8
    11
      src/main.cc

+ 24
- 0
libswan/include/swan/util.h View File

@@ -2,9 +2,33 @@

#include <optional>
#include <functional>
#include <memory>

namespace Swan {

template<typename Func>
class Deferred {
public:
Deferred(Func func): func_(func) {}
Deferred(const Deferred &def) = delete;
Deferred(const Deferred &&def) noexcept: func_(def.func_) { def.active = false; }
~Deferred() { if (active_) func_(); }

private:
Func func_;
bool active_ = true;
};

template<typename T, typename Del>
std::unique_ptr<T, Del> makeRaiiPtr(T *val, Del d) {
return std::unique_ptr<T, Del>(val, d);
}

template<typename Func>
Deferred<Func> makeDeferred(Func func) {
return Deferred(func);
}

// Ret can't be a reference, because C++ doesn't support optional<T&>.
template<typename Ret, typename Func = std::function<std::optional<Ret>()>>
class Iter {

+ 8
- 11
src/main.cc View File

@@ -9,11 +9,8 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include <swan/common.h>
#include <swan/World.h>
#include <swan/Game.h>
#include <swan/Timer.h>
#include <swan/Win.h>
#include <swan/swan.h>
#include <swan/util.h>

using namespace Swan;

@@ -32,13 +29,13 @@ using DeleteFunc = void (*)(T *);

int main() {
sdlassert(SDL_Init(SDL_INIT_VIDEO) >= 0, "Could not initialize SDL");
std::unique_ptr<void, DeleteFunc<void>> sdl(nullptr, [](void *){ SDL_Quit(); });
auto sdl = makeDeferred([] { SDL_Quit(); });

int imgflags = IMG_INIT_PNG;
imgassert(IMG_Init(imgflags) == imgflags, "Could not initialize SDL_Image");
std::unique_ptr<void, DeleteFunc<void>> sdl_image(nullptr, [](void *){ IMG_Quit(); });
auto sdl_image = makeDeferred([] { IMG_Quit(); });

std::unique_ptr<SDL_Window, DeleteFunc<SDL_Window>> window(
auto window = makeRaiiPtr(
SDL_CreateWindow(
"Project: SWAN",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
@@ -46,9 +43,9 @@ int main() {
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
SDL_DestroyWindow);

std::unique_ptr<SDL_Renderer, DeleteFunc<SDL_Renderer>> renderer(
auto renderer = makeRaiiPtr(
SDL_CreateRenderer(
window.get(), -1, SDL_RENDERER_ACCELERATED),
window.get(), -1, SDL_RENDERER_ACCELERATED),
SDL_DestroyRenderer);
sdlassert(renderer, "Could not create renderer\n");

@@ -96,7 +93,7 @@ int main() {
}

auto now = std::chrono::steady_clock::now();
std::chrono::duration<float> dur(prevTime - now);
std::chrono::duration<float> dur(now - prevTime);
prevTime = now;
float dt = dur.count();


Loading…
Cancel
Save