Bonjour,
J'essaye de réaliser un snake avec sdl cependant j'ai un segfault et je ne comprend pas pourquoi (Je vais vous indiquez dans le code la ligne qui segfault).
Lorsque je demande d'afficher une seul fois mon snake ça marche. Lorsque je demande de l'afficher une seconde fois lorsque je ferme le fenêtre il y à un segfault. Sinon lorsque je fait appel a la fonction Move_snake() lors de l'affichage mon programme segfault.
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 void My_SDL::pause_p() { int cont = 1; SDL_Event event; while (cont) { SDL_WaitEvent(&event); switch(event.type) { case SDL_QUIT: cont = 0; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_UP: // Flèche haut //Move_snake(); aff(); break; /*case SDLK_DOWN: // Flèche bas positionZozor.y++; break; case SDLK_RIGHT: // Flèche droite positionZozor.x++; break; case SDLK_LEFT: // Flèche gauche positionZozor.x--; break;*/ } break; } } } void My_SDL::aff(void) { int i(0); int j(0); std::list<candy> da = this->get_candy(); std::list<candy>::iterator it = da.begin(); positionFond.y = 0; std::cout << "prizeeeeeeee" << std::endl; while (i <= this->get_y()-1) { j=0; positionFond.x = 0; std::cout << "yo" << std::endl; while (j <= this->get_x()-1) { if (this->get_bric()[i][j].wall == 1) { std::cout << "fyyyyyyck" << std::endl; SDL_BlitSurface(wall, NULL, ecran, &positionFond);// c'est cette ligne qui segfault. std::cout << "fooooock" << std::endl; } else if (this->get_bric()[i][j].is_candy == 1) SDL_BlitSurface(s_candy, NULL, ecran, &positionFond); else SDL_BlitSurface(weed, NULL, ecran, &positionFond); positionFond.x += this->get_img_size(); j++; } std::cout << "fiiicttiioon" << std::endl; positionFond.y += this->get_img_size(); i++; } positionFond.x = it->x*20; positionFond.y = it->y*20; SDL_BlitSurface(head_down, NULL, ecran, &positionFond); it++; while (it != da.end()) { positionFond.x = it->x*20; positionFond.y = it->y*20; SDL_BlitSurface(piece, NULL, ecran, &positionFond); it++; } SDL_Flip(ecran); pause_p(); SDL_Quit(); }
Voila la fonction Move_snake:
Les coordonnées changent bien comme attendu mais lors de l'affichage segfault.
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 void SNAKE::Move_snake() { std::list<candy>::iterator it = snake.end(); std::list<candy>::iterator the = snake.end(); std::list<candy>::iterator tha = snake.begin(); std::cout << "pokemon" << std::endl; while (tha != snake.end()) { std::cout << tha->x << " " << tha->y << " " << std::endl; tha++; } --the; while (it != snake.begin()) { *it = *the; if (the != snake.begin()) the--; it--; } switch (snake.begin()->dir) { case DOWN:{ snake.begin()->y += 1; break; } case UP:{ snake.begin()->y -= 1; break; } case RIGHT:{ snake.begin()->x += 1; break; } case LEFT:{ snake.begin()->x -= 1; break; } } tha = snake.begin(); while (tha != snake.end()) { std::cout << tha->x << " " << tha->y << " " << std::endl; tha++; } }
Je remet la ligne qui segfault:
Elle est dans la fonction aff();
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 SDL_BlitSurface(wall, NULL, ecran, &positionFond);
Merci pour vos réponses.
Partager