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
| #include "cocos2d.h" #include "VideoSprite.h" #include "base/CCDirector.h"
NS_CC_BEGIN #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
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); }
VideoSprite* VideoSprite::create() { VideoSprite *video = new VideoSprite(); if (video && video->init()) { video->autorelease(); return video; } CC_SAFE_DELETE(video); return nullptr; }
bool VideoSprite::init(void) { vlc = libvlc_new(0, NULL); vlc_player = libvlc_media_player_new(vlc); vlc_eventManager = libvlc_media_player_event_manager(vlc_player);
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, (width * height) << 2, kCCTexture2DPixelFormat_RGBA8888, width, height, size); return initWithTexture(texture); }
void VideoSprite::vlcEvents(const libvlc_event_t *p_event, void *p_data) { if (libvlc_MediaPlayerEndReached == p_event->type) { VideoSprite *self = (VideoSprite *)p_data; if (self->m_isLoop) { self->play(); } } }
VideoSprite::VideoSprite() : vlc(0), vlc_player(0) { #if CC_SPRITE_DEBUG_DRAW _debugDrawNode = DrawNode::create(); addChild(_debugDrawNode); #endif }
VideoSprite::~VideoSprite(void) { libvlc_media_player_stop(vlc_player); libvlc_media_player_release(vlc_player); libvlc_release(vlc); free(videobuf); }
const std::string& VideoSprite::getFileName() { return _videoURL; } void VideoSprite::setFileName(const std::string& path) { _videoURL = path; }
void VideoSprite::play() { libvlc_media_t *media = libvlc_media_new_path(vlc, _videoURL.c_str()); libvlc_media_player_set_media(vlc_player, media); libvlc_media_release(media); libvlc_media_player_play(vlc_player); }
void VideoSprite::stop() { libvlc_media_player_stop(vlc_player); }
void VideoSprite::pause() { libvlc_media_player_pause(vlc_player); }
void VideoSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) {
CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprite, "CCSprite - draw"); CC_NODE_DRAW_SETUP();
ccGLBlendFunc(_blendFunc.src, _blendFunc.dst);
if (_texture != NULL) { ccGLBindTexture2D(_texture->getName()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (uint8_t *)videobuf); } else { ccGLBindTexture2D(0); }
ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
#define kQuadSize sizeof(_quad.bl) long offset = (long)&_quad;
int diff = offsetof(ccV3F_C4B_T2F, vertices); glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
diff = offsetof(ccV3F_C4B_T2F, texCoords); glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
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"); }
void VideoSprite::setLooping(bool looping) { m_isLoop = looping; }
#endif NS_CC_END
|