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
|
#include <SDL/SDL.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
const unsigned int WIDTH=800;
const unsigned int HEIGHT=600;
using namespace std;
int main(int argc, char **argv)
{
SDL_Event event;
SDL_Surface *screen;
int done = 0, fps, last, now;
unsigned int checkTime;
const unsigned int wantedfps = 1;
//Initialisation
if(SDL_Init(SDL_INIT_VIDEO)!=0) {
cerr << "Probleme pour initialiser SDL: " << SDL_GetError() << endl;
return EXIT_FAILURE;
}
//Ouvrir une fenetre
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
if(screen==NULL)
done = 1;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
// recuperation de l'etat du parametre "double buffer"
int nValue;
if( SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &nValue) < 0)
{
std::cerr << "Echec de recuperation du parametre SDL_GL_DOUBLEBUFFER : " << SDL_GetError() << std::endl;
return (EXIT_FAILURE);
}
// assurons nous que le mode "double buffer" est bien actif
if(nValue != 1)
{
std::cerr << "Erreur : SDL_GL_DOUBLEBUFFER inactif" << std::endl;
return (EXIT_FAILURE);
}
// Mettre le systeme de coordonnees a zero avant de modifier
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Mettre la bonne perspective
glOrtho(0,WIDTH,HEIGHT,0,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Initialisation du moteur
//if(moteur.init()==false)
//done = 1;
//Pour le taux de rafraichissement
checkTime = SDL_GetTicks();
//Je crée et j'affiche mon carré que je copie d'un autre buffer
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(255, 0, 0); // unique définition de la couleur bleue
glBegin(GL_QUADS); // début du tracé
glVertex2i( 0, 50); // en bas à gauche
glVertex2i(50, 50); // en bas à droite
glVertex2i( 50, 0); // en haut à droite
glVertex2i( 0, 0); // en haut à gauche
glEnd();
glDrawBuffer(GL_AUX0);
glCopyPixels(0,0,WIDTH,HEIGHT,GL_COLOR);
SDL_GL_SwapBuffers();
while(!done)
{
//Traiter les evenements
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
done=1;
break;
case SDL_KEYUP:
if(event.key.keysym.sym==SDLK_q)
done=1;
break;
default:
break;
}
}
if(SDL_GetTicks() > (checkTime + 1000 / wantedfps) )
{
// On met a jour la variable checkTime
checkTime = SDL_GetTicks();
// Je copie le contenu de mon buffer auxiliaire et l'affiche
glClear(GL_COLOR_BUFFER_BIT);
glReadBuffer(GL_AUX0);
glCopyPixels(0,0,WIDTH,HEIGHT,GL_COLOR);
SDL_GL_SwapBuffers();
}
else
{
// Attendre 5 millisecondes
SDL_Delay(5);
}
}
SDL_Quit();
(void) argc;
(void) argv;
return EXIT_SUCCESS;
} |
Partager