-
Notifications
You must be signed in to change notification settings - Fork 7
/
window.cpp
93 lines (70 loc) · 2.16 KB
/
window.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 Red Blob Games <[email protected]>
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
#include "window.h"
#include "glwrappers.h"
#include <vector>
// NOTE: I need this implementation to be in a cpp file and not an h
// file so that the compiler knows where to put the vtable
IRenderLayer::~IRenderLayer() {}
struct WindowImpl {
SDL_Window* window;
bool context_initialized;
GlContext context;
std::vector<IRenderLayer*> layers;
WindowImpl(SDL_Window* window_);
~WindowImpl();
};
int Window::FRAME = 0;
Window::Window(int width_, int height_)
:visible(true), width(width_), height(height_),
self(new WindowImpl(SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
)))
{
HandleResize();
}
Window::~Window() {}
void Window::AddLayer(IRenderLayer* layer) {
self->layers.push_back(layer);
}
void Window::HandleResize() {
self->context_initialized = false;
SDL_GL_GetDrawableSize(self->window, &width, &height);
glViewport(0, 0, width, height);
}
void Window::Render() {
if (visible) {
glClear(GL_COLOR_BUFFER_BIT);
for (auto layer : self->layers) {
layer->Render(self->window, !self->context_initialized);
}
self->context_initialized = true;
SDL_GL_SwapWindow(self->window);
FRAME++;
}
}
void Window::ProcessEvent(SDL_Event* event) {
if (event->type == SDL_WINDOWEVENT) {
switch (event->window.event) {
case SDL_WINDOWEVENT_SHOWN: { visible = true; break; }
case SDL_WINDOWEVENT_HIDDEN: { visible = false; break; }
case SDL_WINDOWEVENT_SIZE_CHANGED: { HandleResize(); break; }
}
}
for (auto layer : self->layers) {
layer->ProcessEvent(event);
}
}
WindowImpl::WindowImpl(SDL_Window* window_)
:window(window_), context_initialized(false), context(window)
{
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
WindowImpl::~WindowImpl() {
SDL_DestroyWindow(window);
}