Hello world,
je vous sollicite pour un problème que je rencontre en utilisant la SDL2 pour bouger une image. (avec les flèches.)
L'image doit pouvoir bouger dans 4 directions, haut bas gauche droite.
En vérité, l'image va toujours vers le bas et produit une sorte de trainée lorsqu'elle bouge. J'admets ne pas comprendre pourquoi. (j'ai du mal à passé de la 1.2 à la version 2 de la SDL...)
Voici le main.
Voici le fichier input (gère les contrôles)
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 #include <iostream> #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include "header.hpp" using namespace std; int main(){ Player_control player; SDL_Window *window = NULL; SDL_Renderer *renderer = NULL; SDL_Surface *screenSurface = NULL, *helloWorld = NULL; if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0 ){ cout << "SDL could not be initialize ! SDL_Error: " << SDL_GetError() << endl; return EXIT_FAILURE; } SDL_CreateWindowAndRenderer( 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer ); if ( window /* && renderer */ == NULL ){ cout << "A window could not be created ! SDL_Error: " << SDL_GetError() << endl; return EXIT_FAILURE; } helloWorld = SDL_LoadBMP( "random picture.bmp" ); if ( helloWorld == NULL ){ cout << "The picture cannot be used ! SDL_Error: " << SDL_GetError() << endl; return EXIT_FAILURE; } screenSurface = SDL_GetWindowSurface( window ); //SDL_BlitSurface( helloWorld, NULL, screenSurface, NULL ); /* if ( SDL_UpdateWindowSurface( window ) < 0 ){ cout << "The window could not be update ! SDL_Error: " << SDL_GetError() << endl; return EXIT_FAILURE; } */ player.SDL_GetControl( window, screenSurface, helloWorld ); SDL_Close(window, screenSurface); return EXIT_SUCCESS; } void SDL_Close( SDL_Window *ptrWindow, SDL_Surface *ptrSurface ){ //Dealocate the surface SDL_FreeSurface( ptrSurface ); //Destroy the window SDL_DestroyWindow( ptrWindow ); //Quit SDL subsystems SDL_Quit(); }
et Voici le header.
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 #include <SDL2/SDL.h> #include <iostream> #include "header.hpp" void Player_control::SDL_GetControl( SDL_Window *ptrWindow, SDL_Surface *ptrWindowSurface, SDL_Surface *ptrSurface ){ bool foo = true; SDL_Event event; SDL_FillRect( ptrWindowSurface ,NULL,SDL_MapRGB(ptrWindowSurface->format,255,255,255)); SDL_Rect position; position.x = 300; position.y = 300; SDL_BlitSurface( ptrSurface, NULL, ptrWindowSurface,&position); //SDL_EnableKeyRepeat(10,10); SDL_UpdateWindowSurface( ptrWindow ); while (foo){ SDL_WaitEvent(&event); switch(event.type) { case SDL_QUIT: foo = false; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: foo = false; break; } case SDLK_UP: position.y++; break; case SDLK_DOWN: position.y--; break; case SDLK_RIGHT: position.x++; break; case SDLK_LEFT: position.x--; break; } SDL_BlitSurface( ptrSurface, NULL, ptrWindowSurface, &position ); SDL_UpdateWindowSurface( ptrWindow ); } }
Voici ce que j'obtiens:
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 #ifndef header_hpp #define header_hpp void SDL_Close( SDL_Window *ptrWindow, SDL_Surface *ptrSurface ); class Player_control { public: void SDL_GetControl( SDL_Window *ptrWindow, SDL_Surface *ptrWindowSurface, SDL_Surface *ptrSurface ); private: }; #endif
Bref, je suis ouvert à la critique.
Merci de bien vouloir éclairer ma lanterne, cordialement.
Partager