<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://doc.qtfr.org/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Documentation Qtfr - SDL</title>
  <link>http://doc.qtfr.org/</link>
  <description>La documentation francophone sur Qt</description>
  <language>fr</language>
  <pubDate>Thu, 03 Sep 2009 16:42:30 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Intégration de SDL</title>
    <link>http://doc.qtfr.org/post/2007/02/21/Integration-de-SDL</link>
    <guid isPermaLink="false">urn:md5:117b6f909d42194f825db6c03e8c805c</guid>
    <pubDate>Wed, 21 Feb 2007 13:11:00 +0100</pubDate>
    <dc:creator>IrmatDen</dc:creator>
        <category>Tutoriels</category>
        <category>class_QWidget</category><category>intégration</category><category>SDL</category><category>version_Qt3</category><category>version_Qt4</category>    
    <description>&lt;ul&gt;
&lt;li&gt;Comment intégrer SDL dans un widget Qt&amp;nbsp;?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ce tutoriel va explorer l'intégration de la bibliothèque SDL dans Qt, le rendu étant effectué dans un widget Qt.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;Version&lt;/strong&gt;&amp;nbsp;: Qt4 (Qt3 possible avec quelques ajustements) &lt;br /&gt;
&lt;strong&gt;Auteur&lt;/strong&gt;&amp;nbsp;: Denys Bulant (&lt;a href=&quot;http://forum.qtfr.org/profile.php?id=291&quot;&gt;IrmatDen&lt;/a&gt;)&lt;/p&gt;    &lt;h3&gt;Pré requis:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.libsdl.org/&quot; hreflang=&quot;en&quot;&gt;La librairie SDL&lt;/a&gt;&amp;nbsp;: le site contient une section documentation pour débuter&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;Un widget Qt comme cible de rendu pour SDL&lt;/h3&gt;


&lt;p&gt;Les moteurs graphiques des différents Qt ne sont souvent pas adaptés au travail graphique intense (sauf à passer par OpenGL, mais on détourne le problème - c'est d'ailleurs ce que l'on va faire ici).&lt;/p&gt;


&lt;h4&gt;Signifier à SDL la fenêtre à utiliser&lt;/h4&gt;


&lt;p&gt;Se servir d'un widget comme zone de rendu cible pour SDL est très simple: il suffit d'une variable d'environnement. Cette variable est &lt;code&gt;SDL_WINDOWID&lt;/code&gt; et peut être de 2 formats:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;un entier, tel quel&lt;/li&gt;
&lt;li&gt;une valeur hexadécimale, préfixé par '0x'&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Une variable valide est un handle de fenêtre (dont le format peut varier en fonction de l'environnement utilisé). Ce handle s'obtient par la méthode &lt;code&gt;&lt;a href=&quot;http://doc.trolltech.com/4.2/qwidget.html#winId&quot; hreflang=&quot;en&quot;&gt;QWidget::winId()&lt;/a&gt;&lt;/code&gt;, qui est une méthode publique.&lt;/p&gt;


&lt;p&gt;Une fois la zone de rendu définie par cette variable, on peut initialiser le moteur graphique de SDL.
Voici par exemple le code permettant de réaliser cette intégration&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;char&lt;/span&gt; windowid&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#ifdef Q_WS_WIN&lt;/span&gt;
	&lt;span style=&quot;color: #0000dd;&quot;&gt;sprintf&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowid, &lt;span style=&quot;color: #666666;&quot;&gt;&amp;quot;SDL_WINDOWID=0x%lx&amp;quot;&lt;/span&gt;, reinterpret_cast&amp;lt;qlonglong&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;winId&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#elif defined Q_WS_X11&lt;/span&gt;
	&lt;span style=&quot;color: #0000dd;&quot;&gt;sprintf&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowid, &lt;span style=&quot;color: #666666;&quot;&gt;&amp;quot;SDL_WINDOWID=0x%lx&amp;quot;&lt;/span&gt;, winId&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#else&lt;/span&gt;
	qFatal&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #666666;&quot;&gt;&amp;quot;Fatal: cast du winId() inconnu pour votre plate-forme; toute information est la bienvenue!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#endif&lt;/span&gt;
SDL_putenv&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowid&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #ff0000;&quot;&gt;// Initialisation du système vidéo de SDL&lt;/span&gt;
SDL_Init&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;SDL_INIT_VIDEO&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
screen = SDL_SetVideoMode&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;width&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, height&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #0000dd;&quot;&gt;32&lt;/span&gt;, SDL_SWSURFACE&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/pre&gt;


&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Attention&lt;/strong&gt;&amp;nbsp;: ne le faîtes pas l'initialisation du système vidéo de SDL avant que la création réelle de votre widget (le moment le plus sûr est lors du premier showEvent), sinon une seconde fenêtre sera créée par et pour SDL.&lt;/p&gt;&lt;/blockquote&gt;


&lt;h4&gt;Configuration du widget de rendu&lt;/h4&gt;


&lt;p&gt;Le dessin direct par SDL sur un widget Qt implique une petite configuration des attributs de dessin du dit widget:&lt;/p&gt;
&lt;pre class=&quot;cpp&quot;&gt;setAttribute&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;Qt::&lt;span style=&quot;color: #00eeff;&quot;&gt;WA_PaintOnScreen&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
setAttribute&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;Qt::&lt;span style=&quot;color: #00eeff;&quot;&gt;WA_NoSystemBackground&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/pre&gt;


&lt;p&gt;A faire lors de l'utilisation de SDL, sinon, vous aurez une bouillie de pixels &quot;inspirée&quot; par le fond de l'écran à cet emplacement ;)&lt;/p&gt;


&lt;h4&gt;Conflit de main&lt;/h4&gt;


&lt;p&gt;Il y a par contre un conflit entre SDL et Qt au niveau de la fonction &lt;code&gt;main&lt;/code&gt;. Lorsque &lt;code&gt;main&lt;/code&gt; n'est pas le point d'entrée d'un programme (ce qui est le cas sur win32 par exemple), Qt appelle main à partir de son implémentation de &lt;code&gt;WinMain&lt;/code&gt;. SDL quant à lui fait un define (code extrait de &lt;code&gt;SDL_main.h&lt;/code&gt;)&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp&quot;&gt;&lt;span style=&quot;color: #339900;&quot;&gt;#define main SDL_main&lt;/span&gt;
&lt;span style=&quot;color: #ff0000; font-style: italic;&quot;&gt;/* The prototype for the application's main() function */&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;extern&lt;/span&gt; C_LINKAGE &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; SDL_main&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; argc, &lt;span style=&quot;color: #0000ff;&quot;&gt;char&lt;/span&gt; *argv&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/pre&gt;


&lt;p&gt;Pour supprimer ce problème, ils vous faut annuler la définition de SDL, juste après l'inclusion de &lt;code&gt;SDL.h&lt;/code&gt;&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp&quot;&gt;&lt;span style=&quot;color: #339900;&quot;&gt;#include &amp;quot;SDL.h&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#undef main&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Une fois ceci fait, vous pouvez utiliser sereinement le système vidéo de SDL au sein de votre application Qt :)&lt;/p&gt;



&lt;h3&gt;Modification du fichier projet&lt;/h3&gt;


&lt;p&gt;Comme toute librairie, son utilisation avec votre programme doit être expressément signalée sous peine d'erreurs de compilation et/ou de liaison. Les informations à ajouter dans le fichier projet sont&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;le chemin vers les en-têtes SDL (ex: &lt;code&gt;INCLUDEPATH += /usr/local/include/&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;la librairie avec laquelle lier (et éventuellement son chemin) (ex: &lt;code&gt;LIBS += -L/usr/local/lib/ -lSDL&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;Exemple complet&lt;/h3&gt;


&lt;p&gt;Vous trouverez associé à ce tutoriel &lt;a href=&quot;http://doc.qtfr.org/public/2006/qt_sdl.tar.gz&quot;&gt;un exemple&lt;/a&gt; illustrant l'utilisation de la SDL pour un starfield - qui rappellera des souvenirs aux (ex-)possesseurs de vieilles machines et qui ont codé avant l’arrivée du S-VGA (définit comme étant haute-résolution à l’époque) ;-)&lt;/p&gt;


&lt;p&gt;J'ai repris ici les points principaux de la classe&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;...&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #339900;&quot;&gt;#ifdef Q_WS_WIN&lt;/span&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#include &amp;lt;SDL.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#elif defined Q_WS_X11&lt;/span&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#include &amp;lt;SDL/SDL.h&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#endif&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff0000; font-style: italic;&quot;&gt;/* PIEGE: main est redéfini par SDL.h comme SDL_Main.
                On retire donc la définition puisque Qt le gère
*/&lt;/span&gt;
&lt;span style=&quot;color: #339900;&quot;&gt;#undef main&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;...&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; SDLWidget : &lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; QWidget
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
    Q_OBJECT
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt;:
    SDLWidget&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
    :refreshTimer&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, windowInitialized&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, screen&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, StarNumbers&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;...&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;virtual&lt;/span&gt; ~SDLWidget&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
        SDL_Quit&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;protected&lt;/span&gt;:
    &lt;span style=&quot;color: #0000ff;&quot;&gt;virtual&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; showEvent&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;QShowEvent *e&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;!windowInitialized&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
        &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
            &lt;span style=&quot;color: #ff0000;&quot;&gt;// C'est ici qu'on dis à SDL d'utiliser notre widget&lt;/span&gt;
            &lt;span style=&quot;color: #0000ff;&quot;&gt;char&lt;/span&gt; windowid&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#ifdef Q_WS_WIN&lt;/span&gt;
&amp;nbsp;
			&lt;span style=&quot;color: #0000dd;&quot;&gt;sprintf&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowid, &lt;span style=&quot;color: #666666;&quot;&gt;&amp;quot;SDL_WINDOWID=0x%lx&amp;quot;&lt;/span&gt;, reinterpret_cast&amp;lt;qlonglong&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;winId&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#elif defined Q_WS_X11&lt;/span&gt;
			&lt;span style=&quot;color: #0000dd;&quot;&gt;sprintf&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowid, &lt;span style=&quot;color: #666666;&quot;&gt;&amp;quot;SDL_WINDOWID=0x%lx&amp;quot;&lt;/span&gt;, winId&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#else&lt;/span&gt;
			qFatal&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #666666;&quot;&gt;&amp;quot;Fatal: cast du winId() inconnu pour votre plate-forme; toute information est la bienvenue!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #339900;&quot;&gt;#endif&lt;/span&gt;
            SDL_putenv&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowid&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&amp;nbsp;
            &lt;span style=&quot;color: #ff0000;&quot;&gt;// Initialisation du système vidéo de SDL&lt;/span&gt;
            SDL_Init&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;SDL_INIT_VIDEO&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
            screen = SDL_SetVideoMode&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;width&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, height&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #0000dd;&quot;&gt;32&lt;/span&gt;, SDL_SWSURFACE&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
            windowInitialized = &lt;span style=&quot;color: #0000ff;&quot;&gt;true&lt;/span&gt;;
        &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;:
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;...&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;:
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;...&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt; slots:
    &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; onRefresh&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;windowInitialized &amp;amp;&amp;amp; screen&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
        &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
            SDL_LockSurface&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;screen&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
                &lt;span style=&quot;color: #ff0000;&quot;&gt;// Nettoyage de l'écran&lt;/span&gt;
                SDL_FillRect&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;screen, &lt;span style=&quot;color: #0000ff;&quot;&gt;NULL&lt;/span&gt;, &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
                &lt;span style=&quot;color: #ff0000;&quot;&gt;// Dessin du starfield&lt;/span&gt;
                drawStarfield&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
            SDL_UnlockSurface&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;screen&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&amp;nbsp;
            &lt;span style=&quot;color: #ff0000;&quot;&gt;// Rafraîchissement...&lt;/span&gt;
            SDL_UpdateRect&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;screen, &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&amp;nbsp;
            &lt;span style=&quot;color: #ff0000;&quot;&gt;// Et enfin, mise à jour des positions des étoiles&lt;/span&gt;
            updateStarfield&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
        &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;:
    &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;...&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;/pre&gt;


&lt;p&gt;N’oubliez pas d’ajouter les références d’inclusion et de bibliothèques de SDL au .pro, et laissez vous aller à un peu de nostalgie ;-)&lt;/p&gt;


&lt;h3&gt;Utilisation avancée&lt;/h3&gt;


&lt;p&gt;L'ensemble du code illustrant l'utilisation décrite ici est disponible sur &lt;a href=&quot;http://doc.qtfr.org/public/2006/sdl_adv.tar.gz&quot;&gt;la seconde pièce jointe&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;La méthode décrite précédemment est suffisante si vous n'avez besoin de SDL que dans un seul widget. Mais si vous voulez utiliser SDL comme système d'affichage pour plusieurs widgets, alors il faut changer de stratégie. En effet, SDL ayant pris le parti d'utiliser une seule et unique variable d'environnement pointant vers un et un seul identifiant de fenêtre, il va nous falloir utiliser ce système différemment.&lt;/p&gt;


&lt;p&gt;Nous allons donc devoir créer un widget invisible servant à initialiser le système SDL. (Vous trouverez une classe remplissant cette fonctionnalité dans les fichiers tools.h et tools.cpp (classe &lt;code&gt;SDLContext&lt;/code&gt;) fournis avec l'archive.) Le dessin se fera alors sur diverses surfaces SDL (au moins 1 par widget où vous comptez l'utiliser en fait).&lt;/p&gt;


&lt;p&gt;Il nous font donc aussi des fonctions de conversion entre &lt;code&gt;QImage&lt;/code&gt; et &lt;code&gt;SDL_Surface&lt;/code&gt;, ainsi qu'une fonction de création de &lt;code&gt;SDL_Surface&lt;/code&gt;. Cette dernière est nécessaire afin de paramétrer les surfaces SDL de façon à ce que leur format soit compatible à 100% avec les formats proposés par &lt;code&gt;QImage&lt;/code&gt;. Dans l'exemple, ces fonctions ne couvrent pas tout les cas nécessaires, il ne tient qu'à vous de les compléter selon vos besoins:&lt;/p&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;// Création d'une surface SDL rapidement convertible en QImage&lt;/span&gt;
SDL_Surface* createQImageCompliantSurface&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;Uint32 flags, &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; width, &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; height&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
	&lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; Rmask = 0x00FF0000;
	&lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; Gmask = 0x0000FF00;
	&lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; Bmask = 0x000000FF;
	&lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;int&lt;/span&gt; Amask = 0xFF000000;
&amp;nbsp;
	&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; SDL_CreateRGBSurface&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;flags, width, height, &lt;span style=&quot;color: #0000dd;&quot;&gt;32&lt;/span&gt;, Rmask, Gmask, Bmask, Amask&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #ff0000; font-style: italic;&quot;&gt;/* Fonctions de conversion entre SDL_Surface et QImage
   SDLSurfaceToQImage **DOIT** recevoir une surface
   construite avec createQImageCompliantSurface
*/&lt;/span&gt;
QImage SDLSurfaceToQImage&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;SDL_Surface *s&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
	SDL_LockSurface&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;s&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
		QImage im&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;static_cast&amp;lt;uchar*&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;s-&amp;gt;pixels&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, s-&amp;gt;w, s-&amp;gt;h, QImage::&lt;span style=&quot;color: #00eeff;&quot;&gt;Format_RGB32&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
        SDL_UnlockSurface&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;s&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
&amp;nbsp;
	&lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; im;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;2 améliorations conseillées:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;il est intéressant de définir notre propre type encapsulant ceci afin de réduire à 0 le risque de passer une mauvaise surface.&lt;/li&gt;
&lt;li&gt;on peut ajouter une méthode SDLSurfaceToQImage prenant en paramètre une référence vers un QImage afin de modifier les données d'un QImage précédemment créé. Dans les scènes requierant un affichage rapide, cela peut apporter une différence&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;br /&gt;
Nous avons donc un système SDL fonctionnel pour l'appli ainsi que la possibilité d'avoir des surfaces dont la transformation en QImage est extrêmement rapide (puisqu'il s'agit de la copie brute des octets formant la surface). A la différence de la première partie du tutoriel, nous ne dessinons plus sur une surface affichée à l'écran (que nous utilisions ou non le double buffering ne change rien). Il va donc nous falloir surcharger &lt;a href=&quot;http://doc.trolltech.com/4.4/qwidget.html#paintEvent&quot;&gt;paintEvent&lt;/a&gt; pour gérer l'affichage. La mise à jour de notre QImage se fera à chaque mise à jour de la surface. Inutile de le faire dans le paintEvent puisque certains de ces événements seront générés par le système sans qu'il y ait obligatoirement eu de modifications de la surface, par exemple le fait que le widget soit minimisé puis restauré/maximisé.&lt;/p&gt;

&lt;pre class=&quot;cpp&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; Widget : &lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt; QWidget
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;protected&lt;/span&gt;:
   &lt;span style=&quot;color: #0000ff;&quot;&gt;virtual&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; paintEvent&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;QPaintEvent *e&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
   &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
      QPainter p&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
      p.&lt;span style=&quot;color: #00eeff;&quot;&gt;drawImage&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;displayBuffer&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
   &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; modificationSurfaceSDL&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;
   &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #ff0000; font-style: italic;&quot;&gt;/* on modifie la surface renderer par diverses opérations de dessin */&lt;/span&gt;
      displayBuffer = SDLSurfaceToQImage&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;renderer&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;
      update&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #ff0000;&quot;&gt;// on demande par la même occasion un rafraichissement du widget&lt;/span&gt;
   &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;:
   SDL_Surface *renderer;
   QImage displayBuffer;
&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;/pre&gt;


&lt;p&gt;Voici le screenshot obligatoire présentant l'exemple en cours d'exécution:
&lt;a href=&quot;http://doc.qtfr.org/public/2006/sdl_adv.png&quot;&gt;&lt;img src=&quot;http://doc.qtfr.org/public/2006/.sdl_adv_m.jpg&quot; alt=&quot;Illustration SDL multi-widget&quot; style=&quot;display:block; margin:0 auto;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description>
    
          <enclosure url="http://doc.qtfr.org/public/2006/sdl_qt.tar.gz"
      length="2169" type="application/x-gzip" />
          <enclosure url="http://doc.qtfr.org/public/2006/sdl_adv.tar.gz"
      length="3667" type="application/x-gzip" />
    
    
      </item>
    
</channel>
</rss>