summaryrefslogtreecommitdiff
path: root/ui/window.h
blob: 62c34de52e87a66e49642fd12f8874a8bdb9452b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once

#include "chronology.h"
#include "config/types.h"
#include "ptr.h"
#include "special_members.h"
#include "windowContent.h"
#include <SDL2/SDL.h>
#include <cstddef>
#include <string>

using SDL_WindowPtr = wrapped_ptrt<SDL_Window, SDL_CreateWindow, SDL_DestroyWindow>;
using GL_Context = std::remove_pointer_t<SDL_GLContext>;
using SDL_GLContextPtr = wrapped_ptrt<GL_Context, SDL_GL_CreateContext, SDL_GL_DeleteContext>;

class Window {
public:
	Window(size_t width, size_t height, const std::string & title, Uint32 flags);
	virtual ~Window() = default;

	NO_COPY(Window);
	NO_MOVE(Window);

	template<typename C, typename... P>
	void
	setContent(P &&... p)
	{
		glm::ivec2 size {};
		SDL_GetWindowSizeInPixels(m_window, &size.x, &size.y);
		content = std::make_unique<C>(size.x, size.y, std::forward<P>(p)...);
	}

	void tick(TickDuration elapsed);
	void refresh() const;
	bool handleInput(const SDL_Event & e);

	void swapBuffers() const;

protected:
	void clear(float r, float g, float b, float a) const;

	const ScreenAbsCoord size;
	SDL_WindowPtr m_window;
	SDL_GLContextPtr glContext;
	WindowContent::Ptr content;
};