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
| #include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <GL/glu.h>
#include <GL/gl.h>
/**
* Initialisation de la SDL, création du contexte OpenGL et ouverture de la fenetre.
*/
void init_SDL() {
SDL_Init(SDL_INIT_VIDEO); //BLOQUE ICI!!!!
if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) == -1) {
fprintf(stderr, "Impossible d'activer le double buffer\n");
SDL_Quit();
exit(EXIT_FAILURE);
}
SDL_WM_SetCaption("SDL B-A-BA",NULL);
if (SDL_SetVideoMode(640, 640, 32, SDL_OPENGL) == NULL) {
fprintf(stderr, "Impossible de passer en mode OpenGL : %s\n", SDL_GetError());
SDL_Quit();
exit(EXIT_FAILURE);
}
}
/**
* Initialisation des états du contexte OpenGL.
*/
void init_GL(void) {
glClearColor(1., 1., 1., 0.);
}
/**
* Affichage de la scène OpenGL.
*/
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// On dessine un carré rouge dans le plan Oxy
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex3f(-.2, -.2, 0.);
glVertex3f(.2, -.2, 0.);
glVertex3f(.2, .2, 0.);
glVertex3f(-.2, .2, 0.);
glEnd();
SDL_GL_SwapBuffers();
GLenum erreur;
if ((erreur = glGetError()) != GL_NO_ERROR) {
fprintf(stderr, "Erreur OpenGL dans display : %s\n", gluErrorString(erreur));
}
}
/**
* Gestion du clavier.
* @param event l'évènement SDL ayant déclenché l'appel à la fonction
* (doit etre de type SDL_KEYDOWN).
*/
int keyboard(SDL_Event * event) {
if (event->type == SDL_KEYDOWN) {
switch(event->key.keysym.sym) {
case SDLK_ESCAPE:
return 0;
default:
break;
}
}
return 1;
}
int WinMain(int argc, char *argv[]) {
printf("toto");
init_SDL();
init_GL();
int continuer = 1;
SDL_Event event;
// Boucle traitant les évènements de la fenetre
while (continuer) {
// On attend le prochain évènement
SDL_WaitEvent(&event);
// On traite l'évènement
switch (event.type) {
case SDL_QUIT:
// On a fermé la fenetre
continuer = 0;
break;
case SDL_KEYDOWN:
// On a appuyé sur une touche
continuer = keyboard(&event);
break;
}
display();
}
// C'est fini : on libère les ressources proprement
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager