-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphics.c
97 lines (83 loc) · 2.63 KB
/
graphics.c
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
#include "graphics.h"
//initialization of the SDL.
void initSDL (SDL_Surface **screen)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetIcon(IMG_Load("ressources/icon.png"), NULL); // The icon needs to be loaded before calling SDL_SetVideoMode
*screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption("JEU", NULL);
}
void game (SDL_Surface *screen, struct map* map)
{
//initialisation
static int init = 0;
static SDL_Surface *tile[NB_TILES_TYPES];
static SDL_Rect origin_tile[NB_TILES_TYPES];
static SDL_Surface *entitie[NB_ENTITIES];
static SDL_Rect origin_entitie[NB_ENTITIES];
//load ressources
int i;
if(!init)
{
init =1;
for(i=0; i<NB_TILES_TYPES; i++)
{
char name[24] = "ressources/tile_";
sprintf(name, "%s%03d%s", name, i, ".png");
printf("%s\n", name);//debug
tile[i] = IMG_Load(name);
SDL_SetColorKey(tile[i], SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 221, 0, 221));
origin_tile[i].x = 0;
origin_tile[i].y = tile[i]->h-TILE_HEIGHT;
}
for(i=0; i<NB_ENTITIES; i++)
{
char name[24] = "ressources/char_";
sprintf(name, "%s%03d%s", name, i, ".png");
printf("%s\n", name);//debug
entitie[i] = IMG_Load(name);
SDL_SetColorKey(entitie[i], SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 221, 0, 221));
origin_entitie[i].x = 0;
origin_entitie[i].y = entitie[i]->h;
}
printf("Init done \n");
}
// erasing the screen
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
//blitting the tiles
int x, y, n, numberOfLine = (map->sizeX+map->sizeY-1) ;
SDL_Rect position, position2;
for (n=1; n<=numberOfLine; n++)
{
if(n>=map->sizeY)
{
y=map->sizeY-1;
x=n-map->sizeY;
}
else
{
y=n-1;
x=0;
}
for(i=0; i<n && i<map->sizeX && i<map->sizeY && x<map->sizeX && y>=0; i++)
{
//first the tile
position.x = map->origin.x+(x-y)*(TILE_WIDTH/2)-origin_tile[map->tile[x][y].id].x;
position.y = map->origin.y+(y+x)*(TILE_HEIGHT/2)-origin_tile[map->tile[x][y].id].y; //we make the y and x position correspond with the origin of the tile.
SDL_BlitSurface(tile[map->tile[x][y].id], NULL, screen, &position);
//then the eventual entitie
struct entitie *current = map->tile[x][y].entitie;
while(current != NULL)
{
position2.x = position.x + current->x-origin_entitie[current->id].x;
position2.y = position.y + current->y-origin_entitie[current->id].y;
SDL_BlitSurface(entitie[current->id], NULL, screen, &position2);
current = current->next;
}
y--;
x++;
}
}
//updating the screen
SDL_Flip(screen);
}