Bonjour,
j'ai un petit problème avec la création de mon éditeur de carte en 2D, j'aurais besoin d'explication alors voila mon code source :
main.cpp:
main.h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <iostream> #include "main.h" using namespace std; int main(int argc, char **argv) { SDL_Event event; struct_map map; Init(); int offset_X=0; int offset_Y=0; Map(&map,16,20,"mchip1.bmp"); Map_Affiche(&map,offset_X,offset_Y); SDL_EnableKeyRepeat(100,10); while(1){ while(SDL_PollEvent(&event)){ if(event.type == SDL_KEYDOWN){ if(event.key.keysym.sym == SDLK_ESCAPE) return 0; if(event.key.keysym.sym == SDLK_LEFT) offset_X ++; if(event.key.keysym.sym == SDLK_RIGHT) offset_X --; if(event.key.keysym.sym == SDLK_UP) offset_Y ++; if(event.key.keysym.sym == SDLK_DOWN) offset_Y --; } } SDL_FillRect(SDL_GetVideoSurface(),NULL,0); Map_Affiche(&map,offset_X,offset_Y); Map_Affichage_Chipset(&map,500,500); SDL_Flip(SDL_GetVideoSurface()); } SDL_Quit(); return 0; } void Init() { SDL_Surface *screen; screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_WM_SetCaption("EDITEUR CARTE SDL","THORN"); } SDL_Rect Rect(int x,int y,int w,int h) //pour creer des rectangles plus vite :p { SDL_Rect r; r.x=x; r.y=y; r.w=w; r.h=h; return r; }
map.cpp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 #ifndef MAIN_H # define MAIN_H #include <sdl/sdl.h> #include "map.h" void Init(); #endif
map.h :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <iostream> #include <stdlib.h> #include "map.h" #define TILE_TAILLE 32 using namespace std; void InitMap(struct_map *map) { map->longueur=0; map->largeur=0; map->offsetX=0; map->offsetY=0; map->nb_tile=0; } void Map(struct_map *map, int largeur, int longueur, char *chipset) { InitMap(map); int i,j,x,y; map->s_chipset=SDL_LoadBMP(chipset); map->nb_tile=(map->s_chipset->w/TILE_TAILLE)*(map->s_chipset->h/TILE_TAILLE); map->longueur=longueur; map->largeur=largeur; x=y=0; //Pour mettre les chipsets dans un tableau. for(i=0; i<map->s_chipset->h/TILE_TAILLE; i++){ map->chipset[i].pos_y = y; for(j=0; j<map->s_chipset->w/TILE_TAILLE; j++){ map->chipset[j].pos_x = x; x += TILE_TAILLE; } y += TILE_TAILLE; x = 0; } } void Map_Affiche(struct_map *map, int Xoffset, int Yoffset) { SDL_Rect src=Rect(0,0,32,32); SDL_Rect dest=Rect(0,0,32,32); int y,x; int x1,y1; map->offsetX = Xoffset; map->offsetY = Yoffset; x1 = map->offsetX+160; y1 = map->offsetY; for(y=0; y<map->largeur; y++){ for(x=0; x<map->longueur; x++){ dest=Rect(x*TILE_TAILLE+x1 , y*TILE_TAILLE+y1 , TILE_TAILLE, TILE_TAILLE); SDL_BlitSurface(map->s_chipset,&src,SDL_GetVideoSurface(),&dest); } } src=Rect(0,0,32,32); dest=Rect(0,0,32,32); for(y=0; y<(map->s_chipset->h/TILE_TAILLE); y++){ for(x=0; x<(map->s_chipset->w/TILE_TAILLE); x++){ src=Rect(x*TILE_TAILLE , y*TILE_TAILLE, TILE_TAILLE, TILE_TAILLE); dest=Rect(x*TILE_TAILLE , y*TILE_TAILLE, TILE_TAILLE, TILE_TAILLE); SDL_BlitSurface(map->s_chipset,&src,SDL_GetVideoSurface(),&dest); } } } void Map_Affichage_Chipset(struct_map *map, int x, int y) { SDL_Rect src=Rect(0,0,32,32); SDL_Rect dest=Rect(0,0,32,32); src.x = map->chipset[1].pos_x; src.y = map->chipset[2].pos_y; dest.x = (24-((800-x)/TILE_TAILLE))*TILE_TAILLE; dest.y = (18-((608-y)/TILE_TAILLE))*TILE_TAILLE; SDL_BlitSurface(map->s_chipset,&src,SDL_GetVideoSurface(),&dest); }
voila bon ce n'est juste que le début, alors voila mon probleme j ai une carte de 10carreau * 32pixels(chaques carreaux) au debut ils sont bleu mais par exemple si je veux en mettre un vert au milieu je voudrais qu'il le mette sur la carte et non sur le SDL_GetVideoSurface(), sinon quand je deplace la carte, il me refait la carte en bleu a chaque fois et le carreau vert reste a sa place, j'espère que vous avez compris :/.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #ifndef MAP_H # define MAP_H #include <sdl/sdl.h> #define GET_TILE(x,y,map) map->data[x+y*map->longueur] struct struct_chipset { unsigned int pos_x; unsigned int pos_y; }; struct struct_map { unsigned int nb_tile; struct struct_chipset *chipset; SDL_Surface *s_chipset; unsigned int longueur; unsigned int largeur; unsigned int offsetX; unsigned int offsetY; unsigned int *data; }; void InitMap(struct_map *map); void Map(struct_map *map, int largeur, int longueur, char *chipset); void Map_Affiche(struct_map *map, int Xoffset, int Yoffset); void Map_Affichage_Chipset(struct_map *map, int x, int y); SDL_Rect Rect(int x,int y,int w,int h); #endif
merci de votre aide .
Partager