Bonjour voila je me suis fortement inspiré du tuto de loka , mais ma camera ne suit pas mon sprite , voici mon code :
Mon main.cpp
et mon fichier Point.cpp ou est créer ma fonction camera :
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
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 #include <iostream> #include <string> #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <SDL/SDL_ttf.h> #include "Point.h" #include "map.h" #include "constante.h" //Les dimensions du point (l'image qu'on va deplacer) const int POINT_WIDTH = 40; const int POINT_HEIGHT = 80; int main(int argc, char *argv[]) { int continuer =1; int map[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR]; int x,y,i,j; Point monPoint; SDL_Event event2; SDL_Surface *sprite = NULL; SDL_Surface *screen,*texture1,*texture2,*texture3 = NULL; SDL_Rect position; SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE|SDL_DOUBLEBUF ); SDL_WM_SetCaption( "Bouge le point", NULL ); sprite = SDL_LoadBMP( "SiOu_Left_1.bmp" ); sprite = SDL_DisplayFormat(sprite); SDL_SetColorKey( sprite, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( sprite->format, 255, 255, 255 ) ); //chargement des textures //texture1 = SDL_LoadBMP("tn225.bmp"); texture1 = load_image("tn225.bmp"); //texture2 = SDL_LoadBMP("tn226.bmp"); texture2 = load_image("tn226.bmp"); //texture3 = SDL_LoadBMP("tn227.bmp"); texture3 = load_image("tn227.bmp"); while (continuer) { while( SDL_PollEvent( &event2 ) ) { //On remplit l'ecran de blanc //SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0, 0, 0) ); monPoint.handle_input(event2,sprite, screen); if( event2.type == SDL_QUIT ) { //On quitte the programme continuer = 0; } } if (!chargerMap(map)) { exit(EXIT_FAILURE); } //Met la camera monPoint.set_camera(); //Le point qu'on va utilise x = 0; y = 0; for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++) { for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) { position.x = i * TAILLE_BLOC; position.y = j * TAILLE_BLOC; switch(map[i][j]) { case 0: SDL_BlitSurface(texture1, NULL, screen, &position); break; case 1: SDL_BlitSurface(texture2, NULL, screen, &position); break; case 2: SDL_BlitSurface(texture3, NULL, screen, &position); break; } } } //SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); monPoint.move(sprite,screen); monPoint.show(sprite,screen); if( SDL_Flip( screen ) == -1 ) { return 1; } } //Libèration des surfaces SDL_FreeSurface( sprite ); //On quitte SDL SDL_Quit(); return EXIT_SUCCESS; }
Voila je ne comprends pas je n'ai pas de scrolling =(
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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 #include <iostream> #include <string> #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <SDL/SDL_ttf.h> #include "Point.h" #include "constante.h" //Les dimensions du point (l'image qu'on va deplacer) const int POINT_WIDTH = 20; const int POINT_HEIGHT = 120; SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) { SDL_Rect offset; //Recuperation des coordonnées offset.x = x; offset.y = y; //On blitte la surface SDL_BlitSurface( source, clip, destination, &offset ); } Point::Point() { //Initialisation des coordonnees x = 0; y = 0; //Initialisation de la vitesse xVel = 0; yVel = 0; } void Point::handle_input(SDL_Event event, SDL_Surface* affichePoint,SDL_Surface* screen) { if( event.type == SDL_KEYDOWN ) { //ajustement de la vitesse switch( event.key.keysym.sym ) { case SDLK_UP: yVel -= POINT_HEIGHT / 2; break; case SDLK_DOWN: yVel += POINT_HEIGHT / 2; break; case SDLK_LEFT: xVel -= POINT_WIDTH / 2; break; case SDLK_RIGHT: xVel += POINT_WIDTH / 2; break; default: break; } } //Si une touche a ete relachee else if( event.type == SDL_KEYUP ) { //ajustement de la vitesse switch( event.key.keysym.sym ) { case SDLK_UP: yVel += POINT_HEIGHT / 2; break; case SDLK_DOWN: yVel -= POINT_HEIGHT / 2; break; case SDLK_LEFT: xVel += POINT_WIDTH / 2; break; case SDLK_RIGHT: xVel -= POINT_WIDTH / 2; break; default: break; } } } void Point::move(SDL_Surface* source, SDL_Surface* destination) { //Bouge le point à gauche ou à droite x += xVel; //Si le point se rapproche trop des limites(gauche ou droite) de l'ecran if( ( x < 0 ) || ( x + POINT_WIDTH > LEVEL_WIDTH ) ) { //On revient x -= xVel; } //Bouge le point en haut ou en bas y += yVel; //Si le point se rapproche trop des limites(haute ou basse) de l'ecran if( ( y < 0 ) || ( y + POINT_HEIGHT > LEVEL_HEIGHT ) ) { //On revient y -= yVel; } } void Point::set_camera() { //Centre la camera sur le point camera.x = ( x + POINT_WIDTH / 2 ) - SCREEN_WIDTH / 2; camera.y = ( y + POINT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2; //Garde la camera sur les bords if( camera.x < 0 ) { camera.x = 0; } if( camera.y < 0 ) { camera.y = 0; } if( camera.x > LEVEL_WIDTH - camera.w ) { camera.x = LEVEL_WIDTH - camera.w; } if( camera.y > LEVEL_HEIGHT - camera.h ) { camera.y = LEVEL_HEIGHT - camera.h; } } void Point::show(SDL_Surface* source, SDL_Surface* destination) { //On blitte la surface SDL_BlitSurface( source, 0, destination, &camera ); SDL_Flip(destination); } SDL_Surface *load_image( std::string filename ) { //L'image qui est chargée SDL_Surface* loadedImage = NULL; //L'image optimisée qu'on va utiliser SDL_Surface* optimizedImage = NULL; //Chargement de l'image loadedImage = SDL_LoadBMP( filename.c_str() ); //Si l'image a bien chargée if( loadedImage != NULL ) { //Création de l'image optimisée optimizedImage = SDL_DisplayFormat( loadedImage ); //Libération de l'ancienne image SDL_FreeSurface( loadedImage ); //Si la surface a bien été optimisée if( optimizedImage != NULL ) { //transparence SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 255, 255, 255 ) ); } } //On retourne l'image optimisée return optimizedImage; }
Partager