-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenUnit.cpp
More file actions
78 lines (62 loc) · 1.99 KB
/
Copy pathScreenUnit.cpp
File metadata and controls
78 lines (62 loc) · 1.99 KB
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
#include "ScreenUnit.hpp"
#include "Logger.hpp"
#include <iostream>
TScreen::TScreen(const std::string& aTitle, int aWidth, int aHeight, int aBpp, bool aFullscreen, float afarclip)
: fTitle(aTitle), fWidth(aWidth), fHeight(aHeight), fBpp(aBpp), fFarClip(afarclip),
fFullScreen(aFullscreen), fWindow(nullptr), fContext(nullptr) {}
TScreen::~TScreen() {
Finalize();
}
bool TScreen::InitSdl() {
Log("Initializing SDL");
if (SDL_Init(SDL_INIT_VIDEO) < 0) return false;
uint32_t flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
if (fFullScreen) flags |= SDL_WINDOW_FULLSCREEN;
fWindow = SDL_CreateWindow(fTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
fWidth, fHeight, flags);
if (!fWindow) return false;
fContext = SDL_GL_CreateContext(fWindow);
SDL_GL_SetSwapInterval(1); // VSync
return true;
}
void TScreen::InitGL() {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
SetUpWindow(fWidth, fHeight, fFarClip);
float color[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
}
bool TScreen::SetUpWindow(int width, int height, float farclip) {
if (height == 0) height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)width / (double)height, 2.0, farclip);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return true;
}
bool TScreen::Initialize() {
if (InitSdl()) {
InitGL();
return true;
}
return false;
}
void TScreen::Finalize() {
Log("Finalizing SDL");
if (fContext) {
SDL_GL_DeleteContext(fContext);
fContext = nullptr;
}
if (fWindow) {
SDL_DestroyWindow(fWindow);
fWindow = nullptr;
}
SDL_Quit();
}