summaryrefslogtreecommitdiff
path: root/engine/engine.cpp
blob: eccf495b2bf28c61683f993a3998e7fd1a53e9e1 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "engine.h"

#include "config.h"

#include <wriggle/texturesdl.h>

#include <SDL/SDL_opengl.h>

#include <iostream>

#include <cmath>

#include <boost/format.hpp>

void Engine::event_keypress(SDLKey key) {
	switch(key) {
		case SDLK_SPACE:
			paused = !paused;
			break;
		default:
			Application::event_keypress(key);
	}
}

Engine::Engine() {
	init_window(Config::window_w, Config::window_h, Config::fullscreen);
	
	// Texturing
	glEnable(GL_TEXTURE_2D);
	// Blending
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
	// Smooth shading
	glShadeModel(GL_SMOOTH);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	
	glEnable(GL_POINT_SPRITE);
	glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
	
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	
	glClearColor(0, 0, 0, 0);
	glClearDepth(1);
	
	font = new FTTextureFont("fonts/VeraMono.ttf");
	font->FaceSize(20);
	background = new TextureSDL("textures/background.png");
	
	paused = false;
}

void Engine::update() {
	if(!paused) {
		stage->update();
	}
	
	glClearColor(0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glViewport(0, 0, Config::window_w, Config::window_h);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	float xo = float(Config::window_w)/float(Config::window_h)/2.5 - 0.5;
	glOrtho(-xo, 1 + xo, 0, 0.8, 0, 10);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	background->bind();
	glBegin(GL_QUADS);
		glTexCoord2f(0, 1);
		glVertex2f(-1.9/9.0, 0);
		glTexCoord2f(0, 0);
		glVertex2f(-1.9/9.0, 0.8);
		glTexCoord2f(1, 0);
		glVertex2f(10.9/9.0, 0.8);
		glTexCoord2f(1, 1);
		glVertex2f(10.9/9.0, 0);
	glEnd();
	
	glDisable(GL_TEXTURE_2D);
	glColor4f(1, 1, 0, 1);
	
	unsigned int tick = SDL_GetTicks();
	if(tick - lastframes >= 1000) {
		fps = (float)frames * ((float)(tick - lastframes) / 1000.0f);
		frames = 1;
		lastframes = tick;
	} else {
		frames++;
	}
	
	glScalef(0.0005, 0.0005, 0.0005);
	font->Render((boost::format("FPS: %.2f") % fps).str().c_str());
	
	float v_x = Config::window_w * (Config::viewport_x + xo) / (1 + 2 * xo);
	float v_y = Config::window_h * Config::viewport_y;
	float v_w = Config::window_h * Config::viewport_w;
	float v_h = Config::window_h * Config::viewport_h;
	
	glViewport(v_x - v_w * 0.5, v_y - v_h * 0.5, v_w * 2, v_h * 2);
	glScissor(v_x, v_y, v_w, v_h);
	glEnable(GL_SCISSOR_TEST);
	
	stage->draw();
	
	glDisable(GL_SCISSOR_TEST);
	
	SDL_GL_SwapBuffers();
}