summaryrefslogtreecommitdiff
path: root/application.cpp
blob: 07f4411ef621daeb54766f908f5f5f796c9e2e27 (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
93
94
95
96
#include "application.h"

#include <stdexcept>

Application::Application() {
	// Initialize SDL
	if(SDL_Init(SDL_INIT_VIDEO)) {
		throw(std::runtime_error("SDL initialization failed"));
	}
	
	please_quit = false;
}

Application::~Application() {
	
}

void Application::init_window(unsigned int w, unsigned int h, bool fs) {
	// Fetch the video info
	const SDL_VideoInfo *info = SDL_GetVideoInfo();
	if(!info) {
		throw(std::runtime_error("SDL info query failed"));
	}
	// The SDL mode-flags
	int flags = SDL_OPENGL;       // OpenGL in SDL
	flags |= SDL_GL_DOUBLEBUFFER; // Double buffering
	flags |= SDL_HWPALETTE;       // Hardware palette
	// Check for hardware surface aviability
	if(info->hw_available) {
		flags |= SDL_HWSURFACE;
	} else {
		flags |= SDL_SWSURFACE;
	}
	// Check for hardware blit ability
	if(info->blit_hw) {
		flags |= SDL_HWACCEL;
	}
	// Setup double buffering
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
	
	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
	
	if(fs) {
		flags |= SDL_FULLSCREEN;
	}
	
	// Get our surface
	surface = SDL_SetVideoMode(w, h, 32, flags);
	if(!surface) {
		throw(std::runtime_error("Video mode set failed"));
	}
}

void Application::run() {
	while(1) {
		SDL_Event e;
		while(SDL_PollEvent(&e)) {
			event(e);
		}
		
		if(please_quit) {
			return;
		}
		
		update();
	}
}

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

void Application::event(const SDL_Event& e) {
	switch(e.type) {
		case SDL_QUIT:
			quit();
			break;
		case SDL_KEYDOWN:
			event_keypress(e.key.keysym.sym);
			break;
		default:
			break;
	}
}

void Application::event_keypress(SDLKey key) {
	switch(key) {
		case SDLK_ESCAPE:
			quit();
			break;
		default:
			break;
	}
}