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?