-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgr.h
121 lines (100 loc) · 3.33 KB
/
pgr.h
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
/*
* LoD terrain generation and displaying
* Svoboda Petr, Vojvoda Jakub
*
* 2014
*
*/
#ifndef PGR_H
#define PGR_H
#include <cassert>
#include <string>
#include <exception>
#include <stdexcept>
#include <iostream>
#include <SDL/SDL.h>
#include <glm/glm.hpp>
// NUTNO PRO WINDOWS
#define GLEW_STATIC
#ifdef USE_GLEE
# include <GL/GLee.h>
#else
# include "GL/glew.h"
#endif
// Error handling
// Replacement for gluErrorString
const char * getGlErrorString(GLenum error);
struct SDL_Exception : public std::runtime_error
{
SDL_Exception() throw()
: std::runtime_error(std::string("SDL : ") + SDL_GetError()) {}
SDL_Exception(const char * text) throw()
: std::runtime_error(std::string("SDL : ") + text + " : " + SDL_GetError()) {}
SDL_Exception(const std::string & text) throw()
: std::runtime_error(std::string("SDL : ") + text + " : " + SDL_GetError()) {}
};
struct GL_Exception : public std::runtime_error
{
GL_Exception(const GLenum error = glGetError()) throw()
: std::runtime_error(std::string("OpenGL : ") + (const char*)getGlErrorString(error)) {}
GL_Exception(const char * text, const GLenum error = glGetError()) throw()
: std::runtime_error(std::string("OpenGL : ") + text + " : " + getGlErrorString(error)) {}
GL_Exception(const std::string & text, const GLenum error = glGetError()) throw()
: std::runtime_error(std::string("OpenGL : ") + text + " : " + getGlErrorString(error)) {}
};
// Event functions
// Send quit event
inline void quit()
{
SDL_Event event;
event.type = SDL_QUIT;
if(SDL_PushEvent(&event) < 0) throw SDL_Exception();
}
// Send redraw event
inline void redraw()
{
SDL_Event event;
event.type = SDL_VIDEOEXPOSE;
if(SDL_PushEvent(&event) < 0) throw SDL_Exception();
}
// Shaders
// Load whole file and return it as std::string
std::string loadFile(const char * const file);
// Common shader log code
#ifndef USE_GLEE
std::string getInfoLog(GLuint id, PFNGLGETSHADERIVPROC getLen, PFNGLGETSHADERINFOLOGPROC getLog);
#else
std::string getInfoLog(GLuint id, GLEEPFNGLGETSHADERIVPROC getLen, GLEEPFNGLGETSHADERINFOLOGPROC getLog);
#endif//USE_GLEE
// Info logs contain errors and warnings from shader compilation and linking
inline std::string getShaderInfoLog(GLuint shader)
{
assert(glIsShader(shader));
return getInfoLog(shader, glGetShaderiv, glGetShaderInfoLog);
}
inline std::string getProgramInfoLog(GLuint program)
{
assert(glIsProgram(program));
return getInfoLog(program, glGetProgramiv, glGetProgramInfoLog);
}
GLuint compileShader(const GLenum type, const char * source);
GLuint linkShader(size_t count, ...);
void SurfaceImage2D(GLenum target, GLint level, GLint internalformat, SDL_Surface * surface);
// Event handlers
void onInit();
void onWindowRedraw();
void onWindowResized(int w, int h);
void onKeyDown(SDLKey key, Uint16 mod);
void onKeyUp(SDLKey key, Uint16);
void onMouseMove(unsigned x, unsigned y, int, int, Uint8);
void onMouseDown(Uint8 button, unsigned x, unsigned y);
void onMouseUp(Uint8 button, unsigned x, unsigned y);
// Initialization + event loop
SDL_Surface * init(unsigned width, unsigned height, unsigned color, unsigned depth, unsigned stencil);
// Simple main loop
void mainLoop();
// Animation main loop
// period - rough time between redraws in ms
void mainLoop(unsigned period);
#undef main
#endif