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
| #ifdef WIN32
#include <GL/glew.h>
#else
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>
#endif
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char **argv)
{
// fenêtre pricipal
SDL_Window* fenetre(0);
SDL_GLContext contexteOpengl(0); //contexte opengl
SDL_Event evenements;
bool terminer(false);
// 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;
}
//vérification de la version opengl
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//création du Double Buffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
// Création de la fenêtre
fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
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();
}
#ifdef WIN32
// On initialise GLEW
GLenum initialisationGLEW( glewInit() );
// Si l'initialisation a échouée :
if(initialisationGLEW != GLEW_OK)
{
// On affiche l'erreur grâce à la fonction : glewGetErrorString(GLenum code)
std::cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(initialisationGLEW) << std::endl;
// On quitte la SDL
SDL_GL_DeleteContext(contexteOpengl);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
#endif
//vertices et coordonnées
float vertices[] = {-0.5, 0.0, 0.5, 0.5, 0.5, -0.5};
// Boucle principale
while(!terminer)
{
SDL_WaitEvent(&evenements);
if(evenements.window.event == SDL_WINDOWEVENT_CLOSE)
terminer = true;
// Nettoyage de l'écran
glClear(GL_COLOR_BUFFER_BIT);
//remplissage et activation du tableau Vertex Attrib 0
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
//affichage Opengl
glDrawArrays(GL_TRIANGLES_, 0, 3);
//désactivation du tableau Vertex Attrib
glDisableVertexAttribArray(0);
//actualisation de la fenetre
SDL_GL_SwapWindow(fenetre);
}
// destruction de la fenetre
SDL_GL_DeleteContext(contexteOpengl);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return 0;
} |
Partager