<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[TUTO] SDL2.0 #2]]></title><description><![CDATA[<p dir="auto">Bonjour à tous !</p>
<p dir="auto">ça fait un moment que j'en parle, voici la suite du premier cours sur la SDL2 !</p>
<p dir="auto">malheureusement le temps me manque, le peu de temps dont je dispose,  je n'ais pas de connexion internet.</p>
<p dir="auto">la solution la plus simple est donc d'écrire le code source du cours en le SURcommentant <img src="http://new.melinyel.net/assets/plugins/nodebb-plugin-emoji/emoji/android/1f61b.png?v=aa95655114f" class="not-responsive emoji emoji-android emoji--stuck_out_tongue" style="height:23px;width:auto;vertical-align:middle" title=":P" alt="😛" /></p>
<p dir="auto">le code source qui vas suivre vous montre étape par étape comment animer le personnage de rayman<img src="http://new.melinyel.net/assets/plugins/nodebb-plugin-emoji/emoji/android/00ae.png?v=aa95655114f" class="not-responsive emoji emoji-android emoji--registered" style="height:23px;width:auto;vertical-align:middle" title="®" alt="®" /> grâce a la SDL2.</p>
<pre><code>//pour le type bool
#include &lt;stdbool.h&gt;
//pour les atexit
#include &lt;stdlib.h&gt;
//pour les printf
#include &lt;stdio.h&gt;

//les includes SDL
#include &lt;SDL2/SDL.h&gt;
#include &lt;SDL2/SDL_image.h&gt;

int main ()
{
	//initialisation de la sdl
    if (SDL_Init (SDL_INIT_VIDEO) &lt; 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) &lt; 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 &lt; 6 ; index++)
    {
		raymantiles[index].x = index*60;
		raymantiles[index].y = 1100;
		raymantiles[index].w = 60;
		raymantiles[index].h = 68;
	}
	for(index = 0 ; index &lt; 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 &lt; 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 , &amp;pecran.w , &amp;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 &gt;= 7000) { break; }
		
		
		//test pour changer le sprite d'animation si "x" ms sont écoulées
		if (temps - tempsanim &gt;= 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 &gt;= 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 &gt;= pecran.w) { droite = false; }
			//si il touche la bordure gauche, il fait de même.
			else if (prayman.x &lt;= 0) {droite = true;}
			
			
			//vidage du tampon de rendu
			SDL_RenderClear(renderer);
			
			//copie du fond sur le tampon
			SDL_RenderCopy(renderer, fond, NULL, &amp;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, &amp;raymantiles[anim], &amp;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, &amp;raymantiles[anim], &amp;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;
}
</code></pre>
<p dir="auto">si vous compilez chez vous, vous devriez avoir un résultat s'approchant de ça:</p>
<p dir="auto"><a href="https://www.youtube.com/watch?v=fTFRiKdBPec" rel="nofollow ugc">https://www.youtube.com/watch?v=fTFRiKdBPec  (a noter qu'ici le déplacement était au clavier et non automatique)</a></p>
<p dir="auto">pour tout commentaire ou question, je suis présent pour vous répondre <img src="http://new.melinyel.net/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=aa95655114f" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:23px;width:auto;vertical-align:middle" title=":)" alt="🙂" /></p>
<p dir="auto">voici les fichiers pour ceux qui voudrais le compiler et le modifier pour s’entraîner ----&gt; <a href="https://drive.google.com/file/d/0BwiU5yPF-jlWdHJERFhwZnFycjg/edit?usp=sharing" rel="nofollow ugc">ICI</a></p>
]]></description><link>http://new.melinyel.net/topic/795/tuto-sdl20-2</link><generator>RSS for Node</generator><lastBuildDate>Thu, 21 May 2026 19:28:52 GMT</lastBuildDate><atom:link href="http://new.melinyel.net/topic/795.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 06 Jun 2014 19:51:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [TUTO] SDL2.0 #2 on Sun, 29 Jun 2014 13:26:20 GMT]]></title><description><![CDATA[<p dir="auto">c'est exactement comme la 1.2 il faut les initialiser et les fermer a la fin.</p>
<p dir="auto">par contre fait attention a initialiser la SDL en premier et a la fermer en dernier.</p>
<p dir="auto">si tu initialise img, ttf, net ou mixer avant la SDL, ça ne marche pas.</p>
]]></description><link>http://new.melinyel.net/post/8867</link><guid isPermaLink="true">http://new.melinyel.net/post/8867</guid><dc:creator><![CDATA[cegdd]]></dc:creator><pubDate>Sun, 29 Jun 2014 13:26:20 GMT</pubDate></item><item><title><![CDATA[Reply to [TUTO] SDL2.0 #2 on Sat, 28 Jun 2014 17:23:31 GMT]]></title><description><![CDATA[<p dir="auto">Je regarde ton code petit à petit <img src="http://new.melinyel.net/assets/plugins/nodebb-plugin-emoji/emoji/android/1f609.png?v=aa95655114f" class="not-responsive emoji emoji-android emoji--wink" style="height:23px;width:auto;vertical-align:middle" title=";)" alt="😉" /> .</p>
<p dir="auto">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><code>//initialisation de la sdl_image
    if (IMG_Init(IMG_INIT_PNG) &lt; 0)
    {   printf("erreur IMG_Init: %s\n", SDL_GetError());
		return EXIT_FAILURE;
	}
    atexit(IMG_Quit);
</code></pre>
]]></description><link>http://new.melinyel.net/post/8829</link><guid isPermaLink="true">http://new.melinyel.net/post/8829</guid><dc:creator><![CDATA[Alemort]]></dc:creator><pubDate>Sat, 28 Jun 2014 17:23:31 GMT</pubDate></item><item><title><![CDATA[Reply to [TUTO] SDL2.0 #2 on Sun, 08 Jun 2014 12:27:36 GMT]]></title><description><![CDATA[<p dir="auto">Merci du partage, pratique et bien utile quand on veut se mettre à la pratique. <img src="http://new.melinyel.net/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=aa95655114f" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:23px;width:auto;vertical-align:middle" title=":)" alt="🙂" /><br />
+1 rep.</p>
]]></description><link>http://new.melinyel.net/post/8471</link><guid isPermaLink="true">http://new.melinyel.net/post/8471</guid><dc:creator><![CDATA[Azad]]></dc:creator><pubDate>Sun, 08 Jun 2014 12:27:36 GMT</pubDate></item></channel></rss>