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
   |  
/* afficher l'univers
Fonction_afficher_univers.c
 
Fonction: permet d'afficher l'univers en quelques tuiles
Par: Maxime
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_rotozoom.h>
 
void Afficher(SDL_Surface* screen,SDL_Surface* tileset,int **table,int HAUTEUR_TILES,int LARGEUR_TILES,int nombre_blocs_largeur,int nombre_blocs_hauteur);
double frand_a_b(double a, double b);
int **remplir_tableau(int nbtiles_largeur_monde,int nbtiles_hauteur_monde);
 
int main(int argc,char** argv)
{
    int LARGEUR_TILE=240;
    int HAUTEUR_TILE=160;
    int nbtiles_largeur_monde=4;
    int nbtiles_hauteur_monde=4;
 
	SDL_Surface* screen,*tileset;
	SDL_Event event;
	SDL_Init(SDL_INIT_VIDEO);
 
	screen = SDL_SetVideoMode(LARGEUR_TILE*nbtiles_largeur_monde, HAUTEUR_TILE*nbtiles_hauteur_monde, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);
	tileset = IMG_Load("environnement.png");
	if (!tileset)
	{
		printf("Echec de chargement tileset1.bmp\n");
		exit(-1);
	}
	SDL_Flip(screen);
    int **table= remplir_tableau(nbtiles_largeur_monde,nbtiles_hauteur_monde);
	Afficher(screen,tileset,table,HAUTEUR_TILE,LARGEUR_TILE,nbtiles_largeur_monde,nbtiles_hauteur_monde);
 
	do
	{
		SDL_WaitEvent(&event);
	} while (event.type!=SDL_KEYDOWN);
 
	SDL_FreeSurface(tileset);
	SDL_Quit();
	free(table);
	return 0;
}
void Afficher(SDL_Surface* screen,SDL_Surface* tileset,int **table,int HAUTEUR_TILE,int LARGEUR_TILE,int nbtiles_largeur_monde,int nbtiles_hauteur_monde)
{
	int i,j;
	SDL_Rect Rect_dest;
	SDL_Rect Rect_source;
	Rect_source.w = LARGEUR_TILE;
	Rect_source.h = HAUTEUR_TILE;
	for(i=0;i<nbtiles_largeur_monde;i++)
	{
		for(j=0;j<nbtiles_hauteur_monde;j++)
		{
			Rect_dest.x = i*LARGEUR_TILE;
			Rect_dest.y = j*HAUTEUR_TILE;
			Rect_source.x = (table[j][i]-'0')*LARGEUR_TILE;
			Rect_source.y = 0;
			SDL_BlitSurface(tileset,&Rect_source,screen,&Rect_dest);
		}
	}
	SDL_Flip(screen);
}
 
int **remplir_tableau(int nbtiles_largeur_monde,int nbtiles_hauteur_monde)
{
    int i,j;
    tab= calloc(nbtiles_largeur_monde, sizeof(int));
    if(tab == NULL)
        {
            SDL_Quit();
            exit(-3);
        }
 
    for(i=0 ; i < nbtiles_largeur_monde  ; i++)
    {
      tab[i] = malloc(nbtiles_hauteur_monde  * sizeof( **tab) );
      if(tab[i] == NULL)
        {
            SDL_Quit();
            exit(-4);
        }
 
    }
 
	for(i=0;i<nbtiles_largeur_monde;i++)
	{
		for(j=0;j<nbtiles_hauteur_monde;j++)
		{
			int a=0;
			int b=4;
			int nombre=0;
			nombre = frand_a_b(a,b);
			if(nombre == 0)
            {
                SDL_Quit();
                exit(-5);
            }
			tab[i][j]=nombre;
		}
	}
	return tab ;
}
 
double frand_a_b(double a, double b)
{
    return (rand()/(double)RAND_MAX)*(b-a) +a;
} | 
Partager