IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Qt Discussion :

Intégrer SDL dans Qt


Sujet :

Qt

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 8
    Par défaut Intégrer SDL dans Qt
    Salut,

    Je souhaite intégré la SDL dans Qt en me servent de ce tuto, mon code est donc le même que dans le tuto:

    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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
     
    #include <QApplication>
    #include <QWidget>
    #include <QShowEvent>
    #include <QTimer>
     
    #ifdef Q_WS_WIN
    #include <SDL.h>
    #elif defined Q_WS_X11
    #include <SDL/SDL.h>
    #endif
     
    /* PIEGE: main est redéfini par SDL.h comme SDL_Main.
    		  On retire donc la définition puisque Qt le gère
    */
    #undef main
     
    #include <vector>
    #include <cstdlib>
    #include <ctime>
     
    class SDLWidget : public QWidget
    {
    	Q_OBJECT
     
    public:
    	SDLWidget()
    	:refreshTimer(0), windowInitialized(false), screen(0), StarNumbers(100)
    	{
    		setAttribute(Qt::WA_PaintOnScreen);
    		setAttribute(Qt::WA_NoSystemBackground);
     
    		starfield.resize(100);
    		initStarfield();
     
    		// Le bon vieux mode 13h (simulé, c'est sûr :p )
    		resize(320, 200);
     
    		refreshTimer = new QTimer(this);
    		connect(refreshTimer, SIGNAL(timeout()), this, SLOT(onRefresh()));
     
    		// simulons la vitesse de l'époque... ;)
    		refreshTimer->start(70);
    	}
     
    	virtual ~SDLWidget()
    	{
    		SDL_Quit();
    	}
     
    protected:
    	virtual void showEvent(QShowEvent *e)
    	{
    		(void)e;
     
    		if(!windowInitialized)
    		{
    			// C'est ici qu'on dis à SDL d'utiliser notre widget
    			char windowid[64];
    #ifdef Q_WS_WIN
    			sprintf(windowid, "SDL_WINDOWID=0x%lx", reinterpret_cast<qlonglong>(winId()));
    #elif defined Q_WS_X11
    			sprintf(windowid, "SDL_WINDOWID=0x%lx", winId());
    #else
    			qFatal("Fatal: cast du winId() inconnu pour votre plate-forme; toute information est la bienvenue!");
    #endif
    			SDL_putenv(windowid);
     
    			// Initialisation du système vidéo de SDL
    			SDL_Init(SDL_INIT_VIDEO);
    			screen = SDL_SetVideoMode(width(), height(), 32, SDL_SWSURFACE);
    			windowInitialized = true;
    		}
    	}
     
    private:
    	struct Star
    	{
    		float x, y, z;
    	};
     
    private:
    	Star generateStar()
    	{
    		Star s = {(::rand() / (static_cast<double>(RAND_MAX) + 1.0)) * 20-10,
    				  (::rand() / (static_cast<double>(RAND_MAX) + 1.0)) * 20-10,
    				  (::rand() / (static_cast<double>(RAND_MAX) + 1.0)) * 100};
    		return s;
    	}
     
    	void initStarfield()
    	{
    		for(StarField::iterator it = starfield.begin();
    			it != starfield.end();
    			++it)
    		{
    			*it = generateStar();
    		} 
    	}
     
    	void updateStarfield()
    	{
    		for(StarField::iterator it = starfield.begin();
    			it != starfield.end();
    			++it)
    		{
    			--it->z;
     
    			if(it->z <= 0)
    			{
    				*it = generateStar();
    			}
    		}
    	}
     
    	void drawStarfield()
    	{
    		int w = width();
    		int hw = w >> 1;
    		int h = height();
    		int hh = h >> 1;
     
    		for(StarField::iterator it = starfield.begin();
    			it != starfield.end();
    			++it)
    		{
    			int screenX = static_cast<int>((it->x / it->z) * w + hw);
    			int screenY = static_cast<int>((it->y / it->z) * h + hh);
     
    			// ignore out of view stars
    			if(screenX < 0 || screenX > 319 || screenY < 0 || screenY > 199)
    				continue;
     
    			putpixel(screen, screenX, screenY, SDL_MapRGBA(screen->format,
    				0xfe, 0xef, 0xcc, 0xff));
    		}
    	}
     
    	// ripped off SDL doc
    	void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    	{
    		int bpp = surface->format->BytesPerPixel;
    		/* Here p is the address to the pixel we want to set */
    		Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
     
    		switch(bpp) {
    		case 1:
    			*p = pixel;
    			break;
     
    		case 2:
    			*(Uint16 *)p = pixel;
    			break;
     
    		case 3:
    			if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
    				p[0] = (pixel >> 16) & 0xff;
    				p[1] = (pixel >> 8) & 0xff;
    				p[2] = pixel & 0xff;
    			} else {
    				p[0] = pixel & 0xff;
    				p[1] = (pixel >> 8) & 0xff;
    				p[2] = (pixel >> 16) & 0xff;
    			}
    			break;
     
    		case 4:
    			*(Uint32 *)p = pixel;
    			break;
    		}
    	}
     
    private slots:
    	void onRefresh()
    	{
    		if(windowInitialized && screen)
    		{
    			SDL_LockSurface(screen);
    				// Nettoyage de l'écran
    				SDL_FillRect(screen, NULL, 0);
    				// Dessin du starfield
    				drawStarfield();
    			SDL_UnlockSurface(screen);
     
    			// Rafraîchissement...
    			SDL_UpdateRect(screen, 0, 0, 0, 0);
     
    			// Et enfin, mise à jour des positions des étoiles
    			updateStarfield();
    		}
    	}
     
    private:
    	QTimer *refreshTimer;
    	bool windowInitialized;
    	SDL_Surface *screen;
     
    	typedef std::vector<Star> StarField;
    	StarField starfield;
    	const int StarNumbers;
    };
     
    int main(int argc, char **argv)
    {
    	QApplication app(argc, argv);
     
    	::srand(::time(NULL));
     
    	SDLWidget *sdlw = new SDLWidget;
    	sdlw->show();
     
    	return app.exec();
    }
     
    #include "main.moc"



    Seulement quand je compile il y a plein d'erreur:

    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
    mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory `C:/Users/Nicolas/qt-sdl'
    C:/Qt/2009.04/qt/bin\moc.exe -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_
    LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\Qt\2009.04\qt
    \include\QtCore" -I"..\..\..\Qt\2009.04\qt\include\QtGui" -I"..\..\..\Qt\2009.04
    \qt\include" -I"..\..\..\Program Files\Microsoft Visual Studio 9.0\VC\include\SD
    L" -I"..\..\..\Qt\2009.04\qt\include\ActiveQt" -I"debug" -I"..\..\..\Qt\2009.04\
    qt\mkspecs\win32-g++" -D__GNUC__ -DWIN32 main.cpp -o debug\main.moc
    g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -
    DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..
    \..\Qt\2009.04\qt\include\QtCore" -I"..\..\..\Qt\2009.04\qt\include\QtGui" -I"..
    \..\..\Qt\2009.04\qt\include" -I"..\..\..\Program Files\Microsoft Visual Studio
    9.0\VC\include\SDL" -I"..\..\..\Qt\2009.04\qt\include\ActiveQt" -I"debug" -I"..\
    ..\..\Qt\2009.04\qt\mkspecs\win32-g++" -o debug\main.o main.cpp
    In file included from ../../../Program Files/Microsoft Visual Studio 9.0/VC/incl
    ude/SDL/SDL.h:28,
    from main.cpp:7:
    ../../../Program Files/Microsoft Visual Studio 9.0/VC/include/SDL/SDL_main.h:50:
    1: warning: "main" redefined
    In file included from ../../../Qt/2009.04/qt/include/QtGui/qwindowdefs.h:1,
    from ../../../Qt/2009.04/qt/include/QtGui/../../src/gui/kernel/
    qapplication.h:46,
    from ../../../Qt/2009.04/qt/include/QtGui/qapplication.h:1,
    from ../../../Qt/2009.04/qt/include/QtGui/QApplication:1,
    from main.cpp:1:
    ../../../Qt/2009.04/qt/include/QtGui/../../src/gui/kernel/qwindowdefs.h:147:1: w
    arning: this is the location of the previous definition
    main.cpp:15:2: #endif without #if
    main.cpp: In member function `virtual void SDLWidget::showEvent(QShowEvent*)':
    main.cpp:60: warning: long unsigned int format, different type arg (arg 3)
    mingw32-make[1]: *** [debug/main.o] Error 1
    mingw32-make[1]: Leaving directory `C:/Users/Nicolas/qt-sdl'
    mingw32-make: *** [debug] Error 2
    Apparemment il y a un problème avec la redéfinition de main, pourtant, dans le tuto, il dit de justement mettre "#undef main" pour ne pas qu'il y est ce problème. D'où viennent ces erreurs et comment les régler?

  2. #2
    Membre expérimenté

    Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2006
    Messages
    281
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 281
    Par défaut
    Bonjour,

    peut être qu'en plaçant la classe SDLWidget dans un duo .h/.cpp avec protections #ifdef / #define approprié et le main dans .cpp à part...
    En gros :
    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
     
    // sdlwidget.h
    #ifndef SDLWIDGET_H
    #define SDLWIDGET_H
     
    #include <SDL/SDL.h>
    #undef main
     
    class SDLWidget {... } ;
     
    #endif //SDLWIDGET_H
     
    // sdlwidget.cpp
    #include "sdlwidget.h"
    ...
     
    // main.cpp
    #include "sdlwidget.h"
     
    int main(int argc, char *argv)
    {
    ...
    }
    J'ai moi même suivi ce tuto et aucun souci. La grosse différence _peut être_ c'est que je n'utilisais pas visual studio...

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 8
    Par défaut
    Même séparent en plusieur fichier, il y a les même erreurs...
    Quand toi tu as suivi le tuto, cela date de longtemps? Car peut être que ça marche uniquement avec une version de Qt antérieur.

  4. #4
    Membre expérimenté

    Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2006
    Messages
    281
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 281
    Par défaut
    Quand toi tu as suivi le tuto, cela date de longtemps? Car peut être que ça marche uniquement avec une version de Qt antérieur.
    L'année dernière (version 2009.04 ou 4.5) et aujourdhui (version 4.6.2)... Et aucun souci...
    Quelques questions (peut être bếtes...)
    - Tu utilise la version 2009.04, recompilé pour visual ?
    - SDL est compilé pour visual ?

    Je pense que le problème se situe autour de visual car c'est le seul environnement que je n'ai pas

    Et vu la version ancienne de Qt que tu utilise, il se peut qu'elle passe mal avec Vista ou Seven...

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    106
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2007
    Messages : 106
    Par défaut
    peut etre tu doit inclure des fichiers comme :
    libsdl.a ou sdl.dll ....etc
    en tout cas j ai compiler ce code avec code_blocks et ça a compiler mais a l affichage 2 fenetres sont afficher une fenetre de Qt ou rien ne c est passer et une autre de SDL_APP ou le code s est executer (ERROR....) peut etre il y a un expert qui c est corriger , merci a bientot !

Discussions similaires

  1. intégrer SDL dans Qt
    Par chabeka dans le forum SDL
    Réponses: 4
    Dernier message: 27/01/2009, 14h39
  2. Intégrer SDL dans Qt
    Par Fullmetal82 dans le forum Qt
    Réponses: 9
    Dernier message: 02/11/2008, 18h07
  3. Comment intégrer SDL dans un prog C
    Par Leguerinos dans le forum SDL
    Réponses: 8
    Dernier message: 15/12/2006, 13h44
  4. [CKEditor] Comment intégrer fckeditor dans ma page Web
    Par Kylen dans le forum Bibliothèques & Frameworks
    Réponses: 3
    Dernier message: 22/12/2005, 19h13
  5. [SDL] Integration fenetre SDL dans fenetre C# ?
    Par salammbo dans le forum OpenGL
    Réponses: 3
    Dernier message: 07/02/2005, 09h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo