Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3423836
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: C/C++

2010-12-06 09:25:04

我的代码如下,如果把winId()传给SDL_putenv,结果是能够在整个widget中显示的,但我要想新建一个小widgt,并通过widget_player->width()将窗口传给SDL_putenv就会出错。不知道什么地方出了问题。我是想把FFPLAY嵌入到QT中,这只是一个实验的例子。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifdef Q_WS_WIN
#include
#elif defined Q_WS_X11
#include
#endif

#undef main

#include
#include
#include

class SDLWidget : public QWidget
{
    Q_OBJECT

public:
    SDLWidget()
    :refreshTimer(0), windowInitialized(false), screen(0), StarNumbers(100)
    {
   
      //setAttribute(Qt::WA_PaintOnScreen);
        //setAttribute(Qt::WA_NoSystemBackground);
     if (this->objectName().isEmpty())
        this->setObjectName(QString::fromUtf8("QT_player"));

    this->resize(358, 339);
    this->setWindowIcon(QIcon());
#if 1
    vboxLayout = new QVBoxLayout(this);
    vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
    widget_player = new QWidget(this);
     setObjectName(QString::fromUtf8("widget_player"));
widget_player->setAttribute(Qt::WA_PaintOnScreen);
        widget_player->setAttribute(Qt::WA_NoSystemBackground);
   
    vboxLayout->addWidget(widget_player);

    progressBar_play = new QProgressBar(this);
    progressBar_play->setObjectName(QString::fromUtf8("progressBar_play"));
    progressBar_play->setWindowModality(Qt::NonModal);
    progressBar_play->setValue(0);
    progressBar_play->setTextVisible(false);

    vboxLayout->addWidget(progressBar_play);

    hboxLayout = new QHBoxLayout();
    hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
    pushButton_play = new QPushButton(this);
    pushButton_play->setObjectName(QString::fromUtf8("pushButton_play"));

    hboxLayout->addWidget(pushButton_play);

    pushButton_pause = new QPushButton(this);
    pushButton_pause->setObjectName(QString::fromUtf8("pushButton_pause"));

    hboxLayout->addWidget(pushButton_pause);

    pushButton_fseek = new QPushButton(this);
    pushButton_fseek->setObjectName(QString::fromUtf8("pushButton_fseek"));

    hboxLayout->addWidget(pushButton_fseek);

    pushButton_bseek = new QPushButton(this);
    pushButton_bseek->setObjectName(QString::fromUtf8("pushButton_bseek"));

    hboxLayout->addWidget(pushButton_bseek);

    vboxLayout->addLayout(hboxLayout);
    this->setWindowTitle(QApplication::translate("player", "player", 0, QApplication::UnicodeUTF8));
    pushButton_play->setText(QApplication::translate("player", "play", 0, QApplication::UnicodeUTF8));
    pushButton_pause->setText(QApplication::translate("player", "pause", 0, QApplication::UnicodeUTF8));
    pushButton_fseek->setText(QApplication::translate("player", ">>", 0, QApplication::UnicodeUTF8));
    pushButton_bseek->setText(QApplication::translate("player", "<<", 0, QApplication::UnicodeUTF8));
    Q_UNUSED(this);

   

    QMetaObject::connectSlotsByName(this);
#endif
         
        starfield.resize(100);
        initStarfield();
         
        resize(320, 200);

        refreshTimer = new QTimer(this);
        connect(refreshTimer, SIGNAL(timeout()), this, SLOT(onRefresh()));
         
        refreshTimer->start(70);
    }

    virtual ~SDLWidget()
    {
        SDL_Quit();
    }

protected:
    virtual void showEvent(QShowEvent *e)
    {
        (void)e;
         
        if(!windowInitialized)
        {
            char windowid[64];

            sprintf(windowid, "SDL_WINDOWID=0x%lx",  widget_player->winId());
        printf( "SDL_WINDOWID=0x%lx",  widget_player->winId());
        printf( "SDL_WINDOWID=0x%lx",  winId());

        SDL_putenv(windowid);

            SDL_Init(SDL_INIT_VIDEO);
            screen = SDL_SetVideoMode( widget_player->width(),  widget_player->height(), 0, SDL_SWSURFACE);
            windowInitialized = true;
        }
    }

private:
    struct Star
    {
        float x, y, z;
    };
     
private:
    Star generateStar()
    {
        Star s = {(::rand() / (static_cast(RAND_MAX) + 1.0)) * 20-10,
                  (::rand() / (static_cast(RAND_MAX) + 1.0)) * 20-10,
                  (::rand() / (static_cast(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 = widget_player->width()/2;
        int hw = w >> 1;
        int h = height()/2;
        int hh = h >> 1;

        for(StarField::iterator it = starfield.begin();
            it != starfield.end();
            ++it)
        {
            int screenX = static_cast((it->x / it->z) * w + hw);
            int screenY = static_cast((it->y / it->z) * h + hh);

            if(screenX < 0 || screenX > 319 || screenY < 0 || screenY > 199)
                continue;

            putpixel(screen, screenX, screenY, SDL_MapRGBA(screen->format,
                0xfe, 0xef, 0xcc, 0xff));
        }
    }

    void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
    {
        int bpp = surface->format->BytesPerPixel;
        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);
                SDL_FillRect(screen, NULL, 0);
                drawStarfield();
            SDL_UnlockSurface(screen);

            SDL_UpdateRect(screen, 0, 0, 0, 0);

            updateStarfield();
        }
    }

private:
    QTimer *refreshTimer;
    bool windowInitialized;
    SDL_Surface *screen;
QVBoxLayout *vboxLayout;
    QWidget *widget_player;
    QProgressBar *progressBar_play;
    QHBoxLayout *hboxLayout;
    QPushButton *pushButton_play;
    QPushButton *pushButton_pause;
    QPushButton *pushButton_fseek;
    QPushButton *pushButton_bseek;

    typedef std::vector 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"
阅读(1333) | 评论(0) | 转发(0) |
0

上一篇:QT中嵌入SDL

下一篇:using SDL with OpenGL

给主人留下些什么吧!~~