-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWindow.cpp
123 lines (106 loc) · 3.59 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// Window.cpp
// KSDL
//
// Created by Kyle Koser on 8/5/14.
// Copyright (c) 2014 Kyle Koser. All rights reserved.
//
#include "Window.h"
Window::Window( const char *title, int aX, int aY, int aHeight, int aWidth, Uint32 aFlags ) {
static bool sInitialized = false;
if( !sInitialized ) {
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
printf("initialized SDL");
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
else {
printf("Intiitlaized texture filtering");
sInitialized = true;
}
}
}
window = SDL_CreateWindow(title, aX, aY, aHeight, aWidth, aFlags);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
printf("created renderer with error: %s\n", SDL_GetError() );
SDL_Surface *winSurface = SDL_GetWindowSurface(window);
SDL_SetSurfaceBlendMode(winSurface, SDL_BLENDMODE_MOD);
SDL_UpdateWindowSurface(window);
SDL_FreeSurface(winSurface);
}
Window::~Window() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
// delete all of the ViewControllers.
while (!viewControllerStack.empty()) {
ViewController* vc = viewControllerStack.top();
viewControllerStack.pop();
delete vc;
}
}
SDL_Renderer* Window::getRenderer() {
return renderer;
}
ViewController* Window::getRootViewController() {
return viewControllerStack.top();
}
void Window::run() {
if (viewControllerStack.empty()) {
return;
}
ViewController *vc = viewControllerStack.top();
if(vc != NULL) {
SDL_Event e;
SDL_Event empty; // Needed to clear the event
bool running = true;
while( running )
{
// Handle events on queue
SDL_PollEvent( &e );
// Check for quit.
if( e.type == SDL_QUIT )
{
running = false;
}
vc->handleEvent(e);
// Need to update the active ViewController here so that if the last one gets popped, we have something to draw
vc = viewControllerStack.top();
vc->draw();
//Update screen
SDL_RenderPresent( renderer );
SDL_Delay(4);
e = empty;
}
}
}
// This method clears the current ViewController stack and then pushes the given
// ViewController onto the bottom of that stack
// NOTE: This method will destroy the current ViewController stack
void Window::setRootViewController(ViewController *aViewController) {
viewControllerStack = std::stack<ViewController *>();
viewControllerStack.push(aViewController);
aViewController->setWindow(this);
}
void Window::pushViewController(ViewController *aViewController) {
viewControllerStack.push(aViewController);
aViewController->setWindow(this);
}
// Removes the top ViewController from the navigation stack
// The top ViewController will be destroyed and deallocated
// If there is only one ViewController on the stack, this does nothing
void Window::popViewController() {
// We cannot allow there to be no vc on the stack
if (viewControllerStack.size() <= 1) {
return;
}
ViewController *vc = viewControllerStack.top();
viewControllerStack.pop();
delete vc;
}