Write a cross-platform player with Cocos2d-x and libvlc

Source: Internet
Author: User
Tags visual studio 2010

Http://www.cnblogs.com/evan-cai/archive/2013/01/25/2876803.html

 

Write a cross-platform player with Cocos2d-x and libvlc

Introduction: This article uses cocos2d-x and libvlc two free open-source cross-platform framework, to achieve a video player. Visual Studio 2010 is used as a development tool and the testing platform is Windows (other platforms are not tested yet ). Cocos2d-x version 2.0.3, VLC version 2.0.5.

Currently, most game developers develop games on the following platforms: Windows, Mac OS X, IOS, and Android. The development of 2D games and cross-platform, cocos2d-x is a very good choice, and libvlc also provides the support of the major platforms, so the use of these two frameworks can basically achieve a write, the purpose of running the entire platform.

Open Visual Studio 2010, create a new Cocos2d-win32 application project, I'm called cocos2dplayer here, configure the entire project until it can be compiled and run (how to configure can refer to many post tutorials on the internet, sorry ). Add the VLC header file, library file, and dynamic link library file, I downloaded the installation version of the VLC player directly and copied the VLC folder to the Vc \ include directory in Visual Studio 2010 in the SDK directory, the library file and the dynamic link library file are placed in the debug of this project. win32 and release. win32 Directory.

Next, go to the topic:

Create files movieplayer. h and movieplayer. cpp

Movieplayer. h

#ifndef __MOVIEPLAYER_H__#define __MOVIEPLAYER_H__/****************************************************************************http://www.cnblogs.com/evan-cai/Author: Evan-CaiDate: 2013-01-25****************************************************************************/#include <vlc\vlc.h>#include "sprite_nodes\CCSprite.h"NS_CC_BEGINclass MoviePlayer : public CCSprite{public:    ~MoviePlayer();    static MoviePlayer * instance(void);    bool init(void);    void play(char *path);    void stop(void);    void pause(void);    void draw(void);protected:    MoviePlayer();private:    libvlc_instance_t *vlc;    libvlc_media_player_t *vlc_player;    unsigned int width;    unsigned int height;    static MoviePlayer * _instance; };NS_CC_END#endif

Movieplayer. cpp

#include "MoviePlayer.h"#include "CCDirector.h"NS_CC_BEGINMoviePlayer * MoviePlayer::_instance = 0;static char * videobuf = 0;static void *lock(void *data, void **p_pixels){    *p_pixels = videobuf;    return NULL;}static void unlock(void *data, void *id, void *const *p_pixels){    assert(id == NULL);}static void display(void *data, void *id){    (void) data;    assert(id == NULL);}MoviePlayer::MoviePlayer():vlc(0), vlc_player(0){    init();}MoviePlayer::~MoviePlayer(){    CCSprite::~CCSprite();    free(videobuf);    libvlc_media_player_stop(vlc_player);    libvlc_media_player_release(vlc_player);    libvlc_release(vlc);}bool MoviePlayer::init(void){    vlc = libvlc_new(0, NULL);    vlc_player = libvlc_media_player_new(vlc);    CCSize size = CCDirector::sharedDirector()->getWinSize();    width = size.width;    height = size.height;    videobuf = (char *)malloc((width * height) << 2);    memset(videobuf, 0, (width * height) << 2);    libvlc_video_set_callbacks(vlc_player, lock, unlock, display, NULL);    libvlc_video_set_format(vlc_player, "RGBA", width, height, width << 2);        CCTexture2D *texture = new CCTexture2D();    texture->initWithData(videobuf, kCCTexture2DPixelFormat_RGBA8888, width, height, size);    return initWithTexture(texture);}void MoviePlayer::play(char *path){    libvlc_media_t *media = libvlc_media_new_path(vlc, path);    libvlc_media_player_set_media(vlc_player, media);    libvlc_media_release(media);    libvlc_media_player_play(vlc_player);}void MoviePlayer::stop(void){    libvlc_media_player_stop(vlc_player);}void MoviePlayer::pause(void){    libvlc_media_player_pause(vlc_player);}void MoviePlayer::draw(void){    CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");    CCAssert(!m_pobBatchNode, "If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called");    CC_NODE_DRAW_SETUP();    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );    if (m_pobTexture != NULL)    {        ccGLBindTexture2D( m_pobTexture->getName() );        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,(uint8_t *) videobuf);    }    else    {        ccGLBindTexture2D(0);    }    //    // Attributes    //    ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );#define kQuadSize sizeof(m_sQuad.bl)    long offset = (long)&m_sQuad;    // vertex    int diff = offsetof( ccV3F_C4B_T2F, vertices);    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));    // texCoods    diff = offsetof( ccV3F_C4B_T2F, texCoords);    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));    // color    diff = offsetof( ccV3F_C4B_T2F, colors);    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);    CHECK_GL_ERROR_DEBUG();    CC_INCREMENT_GL_DRAWS(1);    CC_PROFILER_STOP_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw");}MoviePlayer * MoviePlayer::instance(){    if(_instance == 0)        _instance = new MoviePlayer();    return _instance;}NS_CC_END

Edit the init function of the helloworldscene. cpp file. The final function is as follows:
(Note: I changed the helloworld class to the cocos2dplayer class. Therefore, if you browse this article, you only need to change cocos2dplayer: init to helloworld: init to compile and run it, in addition, can't wait.mp4 is the MP4 file I downloaded from the Internet. Please change the name as needed. ^_^)

bool Cocos2dPlayer::init(){    bool bRet = false;    do     {        //////////////////////////////////////////////////////////////////////////        // super init first        //////////////////////////////////////////////////////////////////////////        CC_BREAK_IF(! CCLayer::init());        //////////////////////////////////////////////////////////////////////////        // add your codes below...        //////////////////////////////////////////////////////////////////////////        // 1. Add a menu item with "X" image, which is clicked to quit the program.        // Create a "close" menu item with close icon, it's an auto release object.        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(            "CloseNormal.png",            "CloseSelected.png",            this,            menu_selector(Cocos2dPlayer::menuCloseCallback));        CC_BREAK_IF(! pCloseItem);        // Place the menu item bottom-right conner.        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));        // Create a menu with the "close" menu item, it's an auto release object.        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);        pMenu->setPosition(CCPointZero);        CC_BREAK_IF(! pMenu);        // Add the menu to Cocos2dPlayer layer as a child layer.        this->addChild(pMenu, 1);        // 2. Add a label shows "Hello World".        // Create a label and initialize with string "Hello World".        //CCLabelTTF* pLabel = CCLabelTTF::create("Let's PLAY!", "Arial", 24);        //CC_BREAK_IF(! pLabel);        // Get window size and place the label upper.         CCSize size = CCDirector::sharedDirector()->getWinSize();        //pLabel->setPosition(ccp(size.width / 2, size.height - 50));        // Add the label to Cocos2dPlayer layer as a child layer.        //this->addChild(pLabel, 1);        // 3. Add add a splash screen, show the cocos2d splash image.        MoviePlayer* pPlayer = MoviePlayer::instance();        // Place the sprite on the center of the screen        pPlayer->setPosition(ccp(size.width/2, size.height/2));        // Add the sprite to Cocos2dPlayer layer as a child layer.        this->addChild(pPlayer, 0);        pPlayer->play("Can't Wait.mp4");        bRet = true;    } while (0);    return bRet;}

I have qq214390117. If you have any questions, please contact me :)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.