-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmap.c
151 lines (124 loc) · 3.35 KB
/
map.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
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
/*
Super Mario Bros
Menu
map.c
Antoine Drabble
Création: 07.01.13
*/
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include "game.h"
#include "char.h"
#include "event.h"
#include "map.h"
Sprites* ChargerImages()
{
Sprites* S;
int i;
S = malloc(NbSprites*sizeof(Sprites));
S[0].sprite = IMG_Load("images/sky.png");
S[1].sprite = IMG_Load("images/sol.png");
S[2].sprite = IMG_Load("images/block.png");
S[3].sprite = IMG_Load("images/boite.png");
S[4].sprite = IMG_Load("images/tuyau1.png");
S[5].sprite = IMG_Load("images/tuyau2.png");
S[6].sprite = IMG_Load("images/tuyau3.png");
S[7].sprite = IMG_Load("images/tuyau4.png");
S[8].sprite = IMG_Load("images/fin1.png");
S[9].sprite = IMG_Load("images/fin2.png");
S[10].sprite = IMG_Load("images/pique.png");
for(i=0;i<NbSprites;i++){
S[i].sprite = SDL_DisplayFormat(S[i].sprite);
}
S[0].traverser = 0;
S[1].traverser = 1;
S[2].traverser = 1;
S[3].traverser = 1;
S[4].traverser = 1;
S[5].traverser = 1;
S[6].traverser = 1;
S[7].traverser = 1;
S[8].traverser = 0;
S[9].traverser = 0;
S[10].traverser = 1;
return S;
}
Map* ChargerMap(char* level)
{
Map* m;
FILE* F;
F = fopen(level,"r");
m = malloc(sizeof(Map));
m->xscroll = 0;
m->yscroll = 0;
int i,j;
char buf[5000]; // Buffer pour le nom du niveau
fscanf(F,"%s",buf); // Nom du niveau
fscanf(F,"%d %d",&m->Nb_Block_W,&m->Nb_Block_H); //Nombre de block largeur et hauteur
m->LoadedMap = malloc(m->Nb_Block_W*sizeof(Uint16*)); //allocation mémoire pour le tableau
m->Used = malloc(m->Nb_Block_W*sizeof(Uint16*));
for(i=0;i<m->Nb_Block_W;i++)
{
m->LoadedMap[i] = malloc(m->Nb_Block_H*sizeof(Uint16));
m->Used[i] = malloc(m->Nb_Block_H*sizeof(Uint16));
}
for(i=0;i<m->Nb_Block_H;i++)
{
for(j=0;j<m->Nb_Block_W;j++)
{
int tmp;
fscanf(F,"%d",&tmp);
m->LoadedMap[j][i] = tmp;
m->Used[j][i] = 0;
}
}
fclose(F);
return m;
}
void AfficherMap(Map* m, SDL_Surface* screen, Sprites* S)
{
int i,j;
SDL_Rect Rect_dest;
int numero_tile;
int minx, maxx, miny, maxy;
minx = m->xscroll / Size_Sprite-1;
miny = m->yscroll / Size_Sprite-1;
maxx = (m->xscroll + WindowW)/Size_Sprite;
maxy = (m->yscroll + WindowH)/Size_Sprite;
for(i=minx;i<=maxx;i++)
{
for(j=miny;j<=maxy;j++)
{
Rect_dest.y = j*Size_Sprite - m->yscroll;
Rect_dest.x = i*Size_Sprite - m->xscroll;
if (i<0 || i>=m->Nb_Block_W || j<0 || j>=m->Nb_Block_H)
{
}
else
{
numero_tile = m->LoadedMap[i][j];
SDL_BlitSurface(S[numero_tile].sprite,NULL,screen,&Rect_dest);
}
}
}
}
void LibererMap(Map* m, Sprites* S)
{
int i;
SDL_FreeSurface(S[0].sprite);
SDL_FreeSurface(S[1].sprite);
SDL_FreeSurface(S[2].sprite);
SDL_FreeSurface(S[3].sprite);
SDL_FreeSurface(S[4].sprite);
SDL_FreeSurface(S[5].sprite);
SDL_FreeSurface(S[6].sprite);
SDL_FreeSurface(S[7].sprite);
SDL_FreeSurface(S[8].sprite);
SDL_FreeSurface(S[9].sprite);
SDL_FreeSurface(S[10].sprite);
free(m->LoadedMap);
free(m);
free(S);
}