summaryrefslogtreecommitdiff
path: root/application.cpp
blob: 8cd9e9d1cce2861d78428c92248bd5d8e0800c7d (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
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
#include "application.h"

#include <SFML/Graphics/RenderWindow.hpp>

#include <stdexcept>

extern const sf::Input* input_backend;
sf::RenderWindow *renderwindow;

Application::Application() {
	please_quit = false;
}

Application::~Application() {
	
}

void Application::create_window(unsigned int w, unsigned int h, bool fs, const std::string& title) {
	window->Create(sf::VideoMode(w, h, 32), title, sf::Style::Resize | sf::Style::Close | (fs ? sf::Style::Fullscreen : 0));
	window->UseVerticalSync(true);
}

void Application::init_window(unsigned int w, unsigned int h, bool fs, const std::string& title) {
	renderwindow = new sf::RenderWindow();
	window = renderwindow;
	create_window(w, h, fs, title);
	
	input_backend = &window->GetInput();
	
	window_title = title;
}

void Application::run() {
	while(1) {
		sf::Event e;
		while(window->GetEvent(e)) {
			event(e);
		}
		
		if(please_quit) {
			return;
		}
		
		update();
		
		swap();
	}
}

void Application::swap() {
	window->Display();
}

void Application::quit() {
	please_quit = true;
}

void Application::event(const sf::Event& e) {
	switch(e.Type) {
		case sf::Event::Closed:
			quit();
			break;
		case sf::Event::KeyPressed:
			event_keypress(e.Key.Code);
			break;
		case sf::Event::Resized:
			event_reshape(e.Size.Width, e.Size.Height);
			break;
		default:
			break;
	}
}

void Application::event_keypress(sf::Key::Code key) {
	switch(key) {
		case sf::Key::Escape:
			quit();
			break;
		case sf::Key::F:
			fullscreen = !fullscreen;
			create_window(1024, 768, fullscreen, window_title);
			event_reshape(window->GetWidth(), window->GetHeight());
			init();
			break;
		default:
			break;
	}
}

void Application::event_reshape(int width, int height) {
	
}