[TUTO] SDL2.0 #2
-
<p>Bonjour à tous !</p>
<p> </p>
<p>ça fait un moment que j'en parle, voici la suite du premier cours sur la SDL2 !</p>
<p> </p>
<p>malheureusement le temps me manque, le peu de temps dont je dispose, je n'ais pas de connexion internet.</p>
<p> </p>
<p>la solution la plus simple est donc d'écrire le code source du cours en le SURcommentant
</p>
<p> </p>
<p>le code source qui vas suivre vous montre étape par étape comment animer le personnage de rayman<span style="font-size:12px;"><span style="font-family:arial, helvetica, sans-serif;">
</span></span> grâce a la SDL2.</p>
<pre class="ipsCode prettyprint">
//pour le type bool
#include <stdbool.h>
//pour les atexit
#include <stdlib.h>
//pour les printf
#include <stdio.h>//les includes SDL
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>int main ()
{
//initialisation de la sdl
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{ printf("erreur SDL_Init: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
atexit(SDL_Quit);
//initialisation de la sdl_image
if (IMG_Init(IMG_INIT_PNG) < 0)
{ printf("erreur IMG_Init: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
atexit(IMG_Quit);//contexte d'affichage SDL_Window *screen; //tampon de rendu SDL_Renderer *renderer; //position de l'écran, de rayman et des différents sprites de rayman SDL_Rect pecran, prayman; SDL_Rect raymantiles[16]; unsigned int temps, tempsbride = 0, tempsanim = 0, index, anim = 0; bool droite = true; //initialisation des positions pecran.x = 0; pecran.y = 0; prayman.w = 120; prayman.h = 136; for(index = 0 ; index < 6 ; index++) { raymantiles[index].x = index*60; raymantiles[index].y = 1100; raymantiles[index].w = 60; raymantiles[index].h = 68; } for(index = 0 ; index < 6 ; index++) { raymantiles[index+6].x = index*60; raymantiles[index+6].y = 68+1100; raymantiles[index+6].w = 60; raymantiles[index+6].h = 68; } for(index = 0 ; index < 4 ; index++) { raymantiles[index+12].x = index*60; raymantiles[index+12].y = 136+1100; raymantiles[index+12].w = 60; raymantiles[index+12].h = 68; } //suppression du curseur de souris SDL_ShowCursor(SDL_DISABLE); //création de la fenêtre screen = SDL_CreateWindow("Rayman_like", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP); //création du tampon de rendu renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED); //récupération de la taille de l'écran SDL_GetWindowSize(screen , &pecran.w , &pecran.h); //mise en position de rayman prayman.x = (pecran.w/2)-(prayman.w/2); prayman.y = (pecran.h*0.65)-(prayman.w/2); // chargement des images SDL_Texture *fond = IMG_LoadTexture(renderer, "fond.jpg"); SDL_Texture *rayman = IMG_LoadTexture(renderer, "rayman.png"); //test d'erreur a la création de la fenêtre, du tampon de rendu et des images. (fonction critiques qui peuvent rencontrer des erreurs if (screen == NULL || renderer == NULL || fond == NULL || rayman == NULL) { printf("erreur: %s\n", SDL_GetError()); return EXIT_FAILURE; } //entrée en boucle principale while(1) { //récupération du temps temps = SDL_GetTicks(); //si le programme tourne depuis "x" ms, on quitte; if (temps >= 7000) { break; } //test pour changer le sprite d'animation si "x" ms sont écoulées if (temps - tempsanim >= 60) { //sauvegarde du temps actuel pour la prochaine itération tempsanim = temps; //sprite suivant anim++; //si le dernier sprite est atteint, on reviens au premier if(anim == 16){anim = 0;} } //bride pour sinchro ecran ~60fps if (temps - tempsbride >= 16) { //assignation du temps actuel pour la prochaine itération tempsbride = temps; //si rayman touche la bordure d'écran a droite, il fait demi tour ! if (prayman.x + prayman.w >= pecran.w) { droite = false; } //si il touche la bordure gauche, il fait de même. else if (prayman.x <= 0) {droite = true;} //vidage du tampon de rendu SDL_RenderClear(renderer); //copie du fond sur le tampon SDL_RenderCopy(renderer, fond, NULL, &pecran); //si rayman vas à gauche if (!droite) { //décrémentation horizontale de la position de rayman. prayman.x -= 5; //copie de rayman (en inversant le sens de l'image) sur le tampon SDL_RenderCopyEx(renderer, rayman, &raymantiles[anim], &prayman, 0,NULL, SDL_FLIP_HORIZONTAL); } //sinon else { //incrémentation horizontale de la position de rayman. prayman.x += 5; //copie de rayman sur le tampon SDL_RenderCopy(renderer, rayman, &raymantiles[anim], &prayman); } //affichage du tampon de rendu sur l'écran. SDL_RenderPresent(renderer); } else { // si l'écran ne suis pas, ont fait une pause. // cela permet de grandement libérer le CPU. SDL_Delay(5); } } //programme terminé return EXIT_SUCCESS;}
</pre>
<p>si vous compilez chez vous, vous devriez avoir un résultat s'approchant de ça:</p>
<p><a href="https://www.youtube.com/watch?v=fTFRiKdBPec" rel="external nofollow">https://www.youtube.com/watch?v=fTFRiKdBPec (a noter qu'ici le déplacement était au clavier et non automatique)</a></p>
<p> </p>
<p>pour tout commentaire ou question, je suis présent pour vous répondre
</p>
<p> </p>
<p>voici les fichiers pour ceux qui voudrais le compiler et le modifier pour s’entraîner ----> <a href="https://drive.google.com/file/d/0BwiU5yPF-jlWdHJERFhwZnFycjg/edit?usp=sharing" rel="external nofollow">ICI</a></p> -
<p> Je regarde ton code petit à petit
.</p>
<p>Une simple question: Par rapport à la SDL 1.2, il faut initialiser les bibliothèques extérieurs comme la sdl_image ou bien c'est pour que ton code soit plus "propre" ?</p>
<pre class="ipsCode prettyprint">
//initialisation de la sdl_image
if (IMG_Init(IMG_INIT_PNG) < 0)
{ printf("erreur IMG_Init: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
atexit(IMG_Quit);
</pre> -
<p>c'est exactement comme la 1.2 il faut les initialiser et les fermer a la fin.</p>
<p> </p>
<p>par contre fait attention a initialiser la SDL en premier et a la fermer en dernier.</p>
<p> </p>
<p>si tu initialise img, ttf, net ou mixer avant la SDL, ça ne marche pas.</p>
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login