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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <Plateau.h>
/* Dimensions initiales et titre de la fenetre */
static const unsigned int WINDOW_WIDTH = 800;
static const unsigned int WINDOW_HEIGHT = 600;
static const char WINDOW_TITLE[] = "TD04 Minimal";
static float aspectRatio;
/* Espace fenetre virtuelle */
static const float GL_VIEW_SIZE = 150.;
/* Nombre de bits par pixel de la fenetre */
static const unsigned int BIT_PER_PIXEL = 32;
/* Nombre minimal de millisecondes separant le rendu de deux images */
static const Uint32 FRAMERATE_MILLISECONDS = 1000 / 60;
SDL_Surface* ChargerImg(const char* filename){
return(IMG_Load(filename));
}
void LibererImg(SDL_Surface* surface){
SDL_FreeSurface(surface);
}
void reshape(SDL_Surface** surface, unsigned int width, unsigned int height)
{
SDL_Surface* surface_temp = SDL_SetVideoMode(
width, height, BIT_PER_PIXEL,
SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_RESIZABLE);
if(NULL == surface_temp)
{
fprintf(
stderr,
"Erreur lors du redimensionnement de la fenetre.\n");
exit(EXIT_FAILURE);
}
*surface = surface_temp;
aspectRatio = width / (float) height;
glViewport(0, 0, (*surface)->w, (*surface)->h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( aspectRatio > 1)
{
gluOrtho2D(
-GL_VIEW_SIZE / 2. * aspectRatio, GL_VIEW_SIZE / 2. * aspectRatio,
-GL_VIEW_SIZE / 2., GL_VIEW_SIZE / 2.);
}
else
{
gluOrtho2D(
-GL_VIEW_SIZE / 2., GL_VIEW_SIZE / 2.,
-GL_VIEW_SIZE / 2. / aspectRatio, GL_VIEW_SIZE / 2. / aspectRatio);
}
}
int main(int argc, char** argv)
{
/* Initialisation de la SDL */
if(-1 == SDL_Init(SDL_INIT_VIDEO))
{
fprintf(
stderr,
"Impossible d'initialiser la SDL. Fin du programme.\n");
exit(EXIT_FAILURE);
}
/* Ouverture d'une fenetre et creation d'un contexte OpenGL */
SDL_Surface* surface;
reshape(&surface, WINDOW_WIDTH, WINDOW_HEIGHT);
/* Initialisation du titre de la fenetre */
SDL_WM_SetCaption(WINDOW_TITLE, NULL);
/*Charger une image*/
SDL_Surface* Logo = ChargerImg("img/unite/V1_R_B.png");
if( Logo == NULL){
fprintf(
stderr,
"Le pointeur est NULL. Erreur. \n");
exit(EXIT_FAILURE);
}
/* Allouer la memoire texture*/
GLuint textures;
glGenTextures(1, &textures);
glBindTexture(GL_TEXTURE_2D, textures);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
/* Copie sur la carte graphique */
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,Logo->w,Logo->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Logo->pixels);
glBindTexture(GL_TEXTURE_2D, 0);
/* Boucle principale */
int loop = 1;
while(loop)
{
/* Recuperation du temps au debut de la boucle */
Uint32 startTime = SDL_GetTicks();
/* Placer ici le code de dessin */
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures);
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0,1);
glVertex2f( 0.0 , 0.0);
glTexCoord2f(1,1);
glVertex2f( 50.0 , 0.0);
glTexCoord2f(1,0);
glVertex2f( 50.0 , 50.0);
glTexCoord2f(0,0);
glVertex2f( 0.0 , 50.0);
glEnd();
glDisable(GL_BLEND);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
/* Echange du front et du back buffer : mise a jour de la fenetre */
SDL_GL_SwapBuffers();
/* Boucle traitant les evenements */
SDL_Event e;
while(SDL_PollEvent(&e))
{
/* L'utilisateur ferme la fenetre : */
if(e.type == SDL_QUIT)
{
loop = 0;
break;
}
if( e.type == SDL_KEYDOWN
&& (e.key.keysym.sym == SDLK_q || e.key.keysym.sym == SDLK_ESCAPE))
{
loop = 0;
break;
}
/* Quelques exemples de traitement d'evenements : */
switch(e.type)
{
/* Redimensionnement fenetre */
case SDL_VIDEORESIZE:
reshape(&surface, e.resize.w, e.resize.h);
break;
/* Clic souris */
case SDL_MOUSEBUTTONUP:
printf("clic en (%d, %d)\n", e.button.x, e.button.y);
break;
/* Touche clavier */
case SDL_KEYDOWN:
printf("touche pressee (code = %d)\n", e.key.keysym.sym);
break;
default:
break;
}
}
/* Calcul du temps ecoule */
Uint32 elapsedTime = SDL_GetTicks() - startTime;
/* Si trop peu de temps s'est ecoule, on met en pause le programme */
if(elapsedTime < FRAMERATE_MILLISECONDS)
{
SDL_Delay(FRAMERATE_MILLISECONDS - elapsedTime);
}
}
glDeleteTextures(1, &textures);
SDL_FreeSurface(Logo);
/* Liberation des ressources associees a la SDL */
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager