Glut+ initialisation graphisme et son

Description

Routine simplifiée d installation du son et des graphismes sous GLUT en 2D,
en utilisant DEVIL(image) et OPENAL(son) ,
avec une routine de gestion simplifiée de SPRITE et detection de collision
attention c est un code de debutant, qui permet de programmer des jeux simples en beneficiant de l acceleration opengl.
n hesitez pas a ameliorer ce code.

// MAIN.CPP
#include"simple_final.h"

struct IMAGE imag,imgsouris ;
struct SOUND son ;
struct SPRITE sprimag,sprsouris ;

int main(int argc,char **argv)
{
ini_simple(argc,argv);

son.load("c:/exemple.wav");
son.run();

imag.load("c:/exemple.png");
imgsouris.load("c:/souris.png");
imag.texturage();
imgsouris.texturage();
sprimag.ini(&imag); // initialisation des sprites (adresse de l'image)
sprsouris.ini(&imgsouris);

int mmm=0 ;
while(climouse!=2)
{glutMainLoopEvent();
cls();
sprimag.affiche(400,400);
sprsouris.affiche(xmouse,ymouse);
affichage();

if (collision(sprsouris,sprimag))
printf ("collision %i \n",mmm++);
}

printf("appuyer ESC\n");
while(keyboard!=27) glutMainLoopEvent();

return 0;

}

//simple_final.h

//------------------------------------------------
// SIMPLE. instructions simplifiées OPENGL
//------------------------------------------------
using namespace std ;
unsigned char keyboard ;
int xmouse,ymouse,climouse=0 ;
const int resx=800,resy=800; //resolution// 0,0 en bas a gauche //resx,resy en haut a droite

// FreeGlut
//#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/freeglut.h>

//DEVIL graphisme load image
#include <IL/il.h>
#include <IL/ilu.h>
#include <IL/ilut.h>

// Son OpenAL
#include<AL/al.h>
#include<AL/alc.h>
#include <AL/alut.h>

#include<iostream>
#include<stdio.h>
#include <conio.h>

//------------------------
// ROUTINES GLUT
//------------------------
void clavier(unsigned char key,int xsouris, int ysouris)
{ keyboard=key ;}

void souris1 (int xsouris,int ysouris)
{ xmouse=xsouris;ymouse=resy-ysouris;climouse=0;}

void souris2(int boutton, int etatsouris,int xsouris,int ysouris)
{ xmouse=xsouris ; ymouse=ysouris ; climouse=boutton ;}

void affichage()
{ glutSwapBuffers();
glutPostRedisplay();
}

void cls() {glClear(GL_COLOR_BUFFER_BIT);}

void ini_glut(int argc,char **argv)
{
glutInit(&argc,argv) ;
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(resx,resy);
glutInitWindowPosition(800,100);
glutCreateWindow("window");
// glutDisplayFunc(affichage);
glutKeyboardFunc(clavier);
glutPassiveMotionFunc(souris1) ; // bouton relaché
glutMouseFunc(souris2);// bouton appuyer
glutSetCursor(GLUT_CURSOR_NONE);

glEnable( GL_TEXTURE_2D );
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // transparence

glMatrixMode(GL_PROJECTION); // mode 2 dimensions
glLoadIdentity();
//gluOrtho2D(-1*resx/resy,1*resx/resy,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClearColor(0,0,0,0); // definit couleur d effacement
glClear(GL_COLOR_BUFFER_BIT);
//glutFullScreen();
}

//-------------------------------------------------------
// routines IMAGE //Bibliotheque DEVIL
//-------------------------------------------------------

//convertion coordonnees pixels GL
void screen_gl(float* x, float* y)
{ *x=2*(*x)/float(resx)-1.0;*y=(*y)*2.0/float(resy)-1.0;}// converti coordonnées ecran(-resolution/2,+resolution/2) en coordonnees gl(-1,1)

void gl_screen(float *x, float*y) //converti gl float (-1,1) en screen pixels(resx,resy)
{ *x=float(resx)*(*x+1.0)/2.0 ;*y=float(resy)*(*y+1.0)/2.0;}

void point(double x, double y , double R, double G, double B)
{ glColor3d(R,G,B); // entre 0 et 1
glBegin(GL_POINTS);
glVertex2d(x,y);
glEnd();
}

struct IMAGE
{
BYTE* adress=NULL;
ILuint imageID;
ILinfo ImageInfo;
int w,h;
GLuint textureID;
bool flag_texture;

void load(const char *filename)
{flag_texture=0;
ilGenImages(1, &imageID); // Generate the image ID
ilBindImage(imageID); // Bind the image
if(ilLoadImage(filename)) printf("%s LOAD IMAGE OK\n",filename) ; else printf("%s LOAD IMAGE ERROR\n",filename); // Load the image file
adress=ilGetData();
iluGetImageInfo(&ImageInfo);
w = ImageInfo.Width;
h = ImageInfo.Height;
printf("w=%i h=%i\n",w,h);
if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT) { iluFlipImage();printf("IMAGE FLIP OK\n");}
if (ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE)) printf("IMAGE CONVERT OK\n") ; else printf("ERROR CONVERT\n");

return ;

}

void texturage()
{flag_texture=1;
// TEXTURE IS MORE FASTER
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
ilGetInteger(IL_IMAGE_BPP), // Image colour depth
w, // Image width
h, // Image height
0, // Border width in pixels (can either be 1 or 0)
ilGetInteger(IL_IMAGE_FORMAT), // Image format (i.e. RGB, RGBA, BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
adress);

printf("TEXTURE OK\n");
}

void affiche(float x, float y)
{ screen_gl(&x,&y) ;
if(flag_texture) //FAST
{float u1=float(w-1)*2/float(resx),u2=float(h-1)*2/float(resy);
glBindTexture(GL_TEXTURE_2D,textureID);
glBegin(GL_QUADS);
glTexCoord2i(0,0);glVertex2f(x,y);
glTexCoord2i(1,0);glVertex2f(x+u1,y);
glTexCoord2i(1,1);glVertex2f(x+u1,y+u2);
glTexCoord2i(0,1);glVertex2f(x,y+u2);
glEnd();
}
else // SLOW
{ glRasterPos2f(x,y); // -1,1
glDrawPixels(w,h,ImageInfo.Format,GL_UNSIGNED_BYTE,adress);
}
}

void over() { ilDeleteImages(1,&imageID) ;}
};

struct SPRITE
{
struct IMAGE *image;
float x,y ; // 0,0 au centre, ecran [-1,1]
int w,h ; // taille image

void ini( struct IMAGE *i)
{ image=i;
w=i->w;
h=i->h;
}
void ini( float xx,float yy)
{x=xx;y=yy;}

void affiche(float xx,float yy)
{ x=xx ;y=yy;
image->affiche(x,y) ;}

void affiche()
{ image->affiche(x,y);}

};

bool collision(struct SPRITE spr1 , struct SPRITE spr2)
{ // xpixelecran=sprite1.x+colonne1=sprite2.x+colonne2
int c1,l1,c2,l2,largeurspr1,hauteurspr1,largeurspr2,hauteurspr2,t;
int spr1c,spr1l,spr2c,spr2l;
float pixel1,pixel2 ; // 4 octets rgba
bool inter =0 ;
largeurspr1=spr1.w ; hauteurspr1=spr1.h ;
largeurspr2=spr2.w ; hauteurspr2=spr2.h ;

for(c1=0;c1<largeurspr1;c1=c1+2) for(l1=0;l1<hauteurspr1;l1=l1+2)
{ c2=spr1.x+c1-spr2.x;
l2=spr1.y+l1-spr2.y;
if(c2>=0 && l2>=0 && c2<largeurspr2 && l2<hauteurspr2)
{ pixel1=0;pixel2=0;
for(t=0;t<4;t++)
{ pixel1=pixel1 + *(spr1.image->adress+t+4*(c1+l1*largeurspr1));
pixel2=pixel2 + *(spr2.image->adress+t+4*(c2+l2*largeurspr2));
}
if (pixel1!=0 && pixel2!=0) inter=1;
}
}
return inter ;
}

void ini_graphisme()
{
// ini graphisme chargeur
ilInit();
iluInit();
ilutInit();
ilutRenderer(ILUT_OPENGL);
}

//----------------------------------------------------------------------
// ROUTINES SON
//----------------------------------------------------------------------
struct SOUND
{
ALuint Buffer;
ALuint Source;
ALfloat SourcePos[3]={0,0,0};
ALfloat SourceVel[3]={0,0,0};
ALfloat ListenerPos[3]={0,0,0};
ALfloat ListenerVel[3]={0,0,0} ;
ALfloat ListenerOri[6]={0,0,-1,0,1,0} ;
ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;

bool load( char *name)
{
// Load wav data into a buffer.
alGenBuffers(1, &Buffer);
if(alGetError() != AL_NO_ERROR) {printf("%s LOAD SOUND ERROR 1\n",name);return 0 ;}
// Load sound
alutLoadWAVFile( name , &format, &data, &size, &freq, &loop);
alBufferData(Buffer, format, data, size, freq);
alutUnloadWAV(format, data, size, freq);
alGenSources(1, &Source);
if(alGetError() != AL_NO_ERROR){ printf("%s LOAD SOUND ERROR 2\n",name);return 0;}
alSourcei (Source, AL_BUFFER, Buffer );
alSourcef (Source, AL_PITCH, 1.0 );
alSourcef (Source, AL_GAIN, 1.0 );
alSourcefv(Source, AL_POSITION, SourcePos);
alSourcefv(Source, AL_VELOCITY, SourceVel);
alSourcei (Source, AL_LOOPING, loop );
// Do another error check and return.
if(alGetError() == AL_NO_ERROR) {printf ("%s LOAD SOUND OK\n",name); return 1;}
else {printf("%s LOAD SOUND ERROR 3\n",name);return 0;}
}
bool run() { alSourcePlay(Source); }
bool stop() { alSourceStop(Source); }
bool pause() { alSourcePause(Source); }
void over() { alDeleteBuffers(1, &Buffer); alDeleteSources(1, &Source); }
} ;

void ini_son()
{ // initialisation openal son
alutInit(0,0);
alGetError();
}

//--------------------------------------------------------------------
//--------------------------------------------------------------------
// INITIALISATION
//------------------------------------------------------------------------
void ini_simple(int argc , char **argv)
{
ini_glut(argc,argv);
ini_graphisme();
ini_son();
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.