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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
#include "PAppSdlOpenGl3.h"
///constante qui permet que le dt ne soit pas trop grand
const float DTFACTOR = 0.01;
PAppSdlOpenGl3::PAppSdlOpenGl3(const std::string& titreFenetre, int largeurFenetre, int hauteurFenetre){
this->initialisationPAppSdlOpenGl3(titreFenetre, largeurFenetre, hauteurFenetre);
}
PAppSdlOpenGl3::PAppSdlOpenGl3(const PAppSdlOpenGl3 & other){
}
PAppSdlOpenGl3::~PAppSdlOpenGl3(){
SDL_Quit();
}
int PAppSdlOpenGl3::executer(){
if(!p_isOpenGlSdlInit) return 1;
p_isRun = true;
while(p_isRun){
p_start_time = SDL_GetTicks();
this->clearKeyStatesNoRepeat();
while(SDL_PollEvent(&p_evenements)){
this->updateWithEvent(&p_evenements);
this->getEvent(&p_evenements);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->update(p_timeForOneFrame*DTFACTOR);
p_matModelview.loadIdentity();
p_cameraGL.see(p_matModelview);
this->draw3d();
SDL_GL_SwapWindow(p_fenetre); p_ellapsed_time = SDL_GetTicks() - p_start_time;
if(p_ellapsed_time < p_timeForOneFrame){SDL_Delay(p_timeForOneFrame - p_ellapsed_time);}
}
return 0;
}
void PAppSdlOpenGl3::updateWithEvent(SDL_Event* event){
}
void PAppSdlOpenGl3::addKeyEventRepeat(const std::string & action, SDL_Keycode key, bool initValue){
p_keyconfRepeat[action] = key;
p_keystatesRepeat[p_keyconfRepeat[action]] = initValue;
}
void PAppSdlOpenGl3::addKeyEventNoRepeat(const std::string & action, SDL_Keycode key, bool initValue){
p_keyconfNoRepeat[action] = key;
p_keystatesNoRepeat[p_keyconfNoRepeat[action]] = initValue;
}
void PAppSdlOpenGl3::clearKeyStatesNoRepeat(){
for(KeyStates::iterator it = p_keystatesNoRepeat.begin(); it != p_keystatesNoRepeat.end(); it++){
it->second = false;
}
}
void PAppSdlOpenGl3::stop(){p_isRun = false;}
bool PAppSdlOpenGl3::isOpenGlSdlInit() const{return p_isOpenGlSdlInit;}
void PAppSdlOpenGl3::setFPS(Uint32 framePerSecond){
p_timeForOneFrame = 1000.0/framePerSecond;
}
float PAppSdlOpenGl3::getRatio() const{
return p_largeurFenetre / p_hauteurFenetre;
}
void PAppSdlOpenGl3::setPerpectiveDeg(float angleDeg, float near, float far){
p_matProjection.perspectiveDeg(angleDeg, getRatio(), near, far);
}
void PAppSdlOpenGl3::setPerpectiveRad(float angleRad, float near, float far){
p_matProjection.perspectiveRad(angleRad, getRatio(), near, far);
}
void PAppSdlOpenGl3::dontRun(){
p_isOpenGlSdlInit = false;
}
void PAppSdlOpenGl3::initialisationPAppSdlOpenGl3(const std::string& titreFenetre, int largeurFenetre, int hauteurFenetre){
p_titreFenetre = titreFenetre;
p_largeurFenetre = largeurFenetre;
p_hauteurFenetre = hauteurFenetre;
if(SDL_Init(SDL_INIT_VIDEO) < 0){ std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
p_isOpenGlSdlInit = false;
return;
}else{
std::cout << "Mode vidéo de SDL initialisé." << std::endl;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
p_fenetre = SDL_CreateWindow(p_titreFenetre.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, p_largeurFenetre, p_hauteurFenetre, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if(p_fenetre == NULL){
std::cerr << "Impossible de créer la fenêtre : " << SDL_GetError() << std::endl;
p_isOpenGlSdlInit = false;
dontRun();
return;
}else{
std::cout << "Création de la fenêtre." << std::endl;
}
p_contexteOpenGL = SDL_GL_CreateContext(p_fenetre);
glEnable(GL_DEPTH_TEST);
p_isOpenGlSdlInit = true;
p_matModelview.loadIdentity();
p_matProjection.loadIdentity();
setFPS(30.0);
p_cameraGL.setWindowID(p_fenetre);
}
void PAppSdlOpenGl3::getEvent(SDL_Event* event){
for (KeyStates::iterator it = p_keystatesRepeat.begin();it != p_keystatesRepeat.end();it++){
if (event->key.keysym.sym == it->first){
it->second = (event->motion.type == SDL_KEYDOWN); break; }
}
for (KeyStates::iterator it = p_keystatesNoRepeat.begin();it != p_keystatesNoRepeat.end();it++){
if (event->key.keysym.sym == it->first){
it->second = (event->motion.type == SDL_KEYUP); break; }
}
}
|