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
| #ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
#define LARGEUR_ECRAN 640
#define HAUTEUR_ECRAN 480
#define PIXEL_IMAGE 32 //les tiles mesurent 32 pixels de largeur et 32 pixels de hauteur
#define CIEL a
#define HERBE b
#define TERRE c
#define CAISSE d
int main ( int argc, char** argv )
{
SDL_Init(SDL_INIT_VIDEO);
char tile[15][20];
FILE *map1 = NULL;
SDL_Surface *ecran = NULL, *ciel = NULL, *herbe = NULL, *terre = NULL, *caisse = NULL;
ciel = SDL_LoadBMP("ciel.bmp");
if(ciel == NULL)
return EXIT_FAILURE;
herbe = SDL_LoadBMP("herbe.bmp");
if(herbe == NULL)
return EXIT_FAILURE;
terre = SDL_LoadBMP("terre.bmp");
if(terre == NULL)
return EXIT_FAILURE;
caisse = SDL_LoadBMP("caisse.bmp");
if(caisse == NULL)
return EXIT_FAILURE;
map1 = fopen("map.txt", "r"); //map.txt contient la suite de 0, 1, 2, 3, qui forment la map.
if(map1 == NULL)
return EXIT_FAILURE;
SDL_WM_SetCaption("TMT: Tile Mapping Test", NULL);
ecran = SDL_SetVideoMode(LARGEUR_ECRAN, HAUTEUR_ECRAN, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_Event evenement;
int continuer = 1;
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
SDL_Rect posHerbe;
int posTileHerbex = 0, posTileHerbey = 0;
SDL_Rect posTerre;
int posTileTerrex = 0, posTileTerrey = 0;
SDL_Rect posCaisse;
int posTileCaissex = 0, posTileCaissey = 0;
SDL_Rect posCiel;
int posTileCielx = 0, posTileCiely = 0;
int i;
int j;
for(i = 0; i <= LARGEUR_ECRAN / PIXEL_IMAGE; i++)
{
for(j = 0; j <= HAUTEUR_ECRAN / PIXEL_IMAGE; j++)
{
fscanf(map1, "%c", &tile[i][j]);
switch(tile[i][j])
{
case 'b':
{
posTileHerbex = j * 32;
posTileHerbey = i * 32;
posHerbe.x = posTileHerbex;
posHerbe.y = posTileHerbey;
SDL_BlitSurface(herbe, NULL, ecran, &posHerbe);
}
break;
case 'c':
{
posTileTerrex = j * 32;
posTileTerrey = i * 32;
posTerre.x = posTileTerrex;
posTerre.y = posTileTerrey;
SDL_BlitSurface(terre, NULL, ecran, &posTerre);
}
break;
case 'd':
{
posTileCaissex = j * 32;
posTileCaissey = i * 32;
posCaisse.x = posTileCaissex;
posCaisse.y = posTileCaissey;
SDL_BlitSurface(caisse, NULL, ecran, &posCaisse);
}
break;
case 'a':
{
posTileCielx = j * 32;
posTileCiely = i * 32;
posCiel.x = posTileCielx;
posCiel.y = posTileCiely;
SDL_BlitSurface(ciel, NULL, ecran, &posCiel);
}
break;
}
}
}
SDL_Flip(ecran);
while(continuer)
{
while(SDL_PollEvent(&evenement)) // pas de ;
{
switch(evenement.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_MOUSEBUTTONDOWN:
{
switch(evenement.button.button)
{
case SDL_BUTTON_RIGHT:
continuer = 0;
break;
default:
break;
}
}
break;
} // fin switch
} // fin Poll
}
SDL_FreeSurface(ciel);
SDL_FreeSurface(terre);
SDL_FreeSurface(herbe);
SDL_FreeSurface(caisse);
SDL_FreeSurface(ecran);
fclose(map1);
SDL_Quit();
return 0;
} |
Partager