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
|
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QShowEvent>
#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()
: 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);
}
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:
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);
SDLWidget *sdlw = new SDLWidget;
sdlw->show();
return app.exec();
} |
Partager