-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (134 loc) · 3.94 KB
/
main.cpp
File metadata and controls
181 lines (134 loc) · 3.94 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Constantes.h"
#include "Bloc.h"
#include "BlocMultiTexture.h"
#include "Texture.h"
#include "CameraVolante.h"
#include "Input.h"
#include "Perso.h"
bool pause=false,
debug=true;
SDL_Window* fenetre(0);
SDL_GLContext contexteOpenGL(0);
Perso joueur;
Texture terre;
Texture herbeDessus;
Texture herbeCote;
vector<Bloc*> carte;
void dessineScene(){
// Nettoyage de l'écran
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
glMatrixMode(GL_MODELVIEW);
for(unsigned int i=0;i<carte.size();i++){
carte[i]->affiche();
}
if(joueur.getBlocVise()!=NULL){
glDepthFunc(GL_ALWAYS);
glLineWidth(2);
joueur.getBlocVise()->surligneCote(joueur.getCoteVise());
glLineWidth(1);
glDepthFunc(GL_LESS);
}
}
void jeu(){
CameraVolante cam(0,0,0);
Input input;
Uint32 debutImage, tempsImage; //nouvelle variable
SDL_Event evenements;
while(!input.quit)
{
debutImage=SDL_GetTicks();
input.updateInput(evenements);
if(input.key[SDL_SCANCODE_ESCAPE]){
pause=SDL_GetRelativeMouseMode();
SDL_SetRelativeMouseMode(SDL_bool(!SDL_GetRelativeMouseMode()));
input.key[SDL_SCANCODE_ESCAPE]=false;
if(pause){
SDL_WarpMouseInWindow(fenetre, LARGEUR_ECRAN/2, HAUTEUR_ECRAN/2);
}
}
if(input.key[SDL_SCANCODE_F3]){
debug=!debug;
input.key[SDL_SCANCODE_F3]=false;
}
if(!pause){
joueur.gestionEvenements(input);
cam.gestionEvenements(input);
dessineScene();
cam.affiche();
SDL_GL_SwapWindow(fenetre);
}
tempsImage=SDL_GetTicks()-debutImage;
if(tempsImage<10){
SDL_Delay(10 - tempsImage);
}
}
}
void initScene(){
carte.push_back(new Bloc(0,0,0,1,1,1, terre.getID()));
carte.push_back(new BlocMultiTexture(3,0,0,1,1,1, herbeCote.getID(), herbeDessus.getID(), terre.getID()));
}
void initTexture(){
terre.charger("dirt.png");
herbeDessus.charger("grass_top.png");
herbeCote.charger("grass_side.png");
}
int init(){
// Initialisation de la SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
// Version d'OpenGL
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
// Double Buffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
// Création de la fenêtre
fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LARGEUR_ECRAN, HAUTEUR_ECRAN, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
SDL_SetRelativeMouseMode(SDL_TRUE);
if(fenetre == 0)
{
std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
// Création du contexte OpenGL
contexteOpenGL = SDL_GL_CreateContext(fenetre);
if(contexteOpenGL == 0)
{
std::cout << SDL_GetError() << std::endl;
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(70,(double)LARGEUR_ECRAN/HAUTEUR_ECRAN, 0.01, 1000);
glClearColor(0.42,0.796,1,1);
glClearColor(1,1,1,1);
return 0;
}
void quit(){
for(unsigned int i=0;i<carte.size();i++){
delete carte[i];
}
}
int main(int argc, char **argv)
{
init();
initTexture();
initScene();
jeu();
quit();
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return 0;
}