⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl-player.c

📁 libFAD is a Flash Animation Decode library
💻 C
字号:
/** * libFAD - Flash Animation Decode library * Copyright (C) 2005-2006 VGSystem Technologies, Inc. * * libFAD is the legal property of its developers, whose names are too numerous * to list here.  Please refer to the COPYRIGHT file distributed with this * source distribution. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * $Id: sdl-player.c,v 1.1 2006/03/22 02:17:52 wrxzzj Exp $ */#include <errno.h>#include <signal.h>#include <string.h>#include <SDL/SDL.h>#include "fad.h"#define FAD_ONTIMER  SDL_USEREVENT+100#define DUMP(...) fprintf(stderr, "sdl-player: "__VA_ARGS__)SDL_Surface *sdl_screen = NULL, *sdl_surface = NULL;SDL_TimerID tid;static void _sdl_update_callback(void *surface, s32_t x, s32_t y, s32_t w, s32_t h) {	/**copy image buffer from surface to screen*/	SDL_BlitSurface(sdl_surface, NULL, sdl_screen, NULL);	SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);}static u32_t _sdl_on_timer(u32_t interval, void* param) {  SDL_Event event;  event.type = FAD_ONTIMER;  event.user.code = 2;  event.user.data1 = NULL;  event.user.data2 = NULL;  SDL_PushEvent(&event);  return interval;}int main(int argc, char* argv[]) {  fad_frame_t frame = {0};  fad_stream_t stream = {0};  FILE* fp = NULL;  u8_t *imgbuf = NULL, quit = 0;  SDL_Event event;  int bits_per_pixel, bytes_per_pixel, stride, width, height;  if(argc != 2) {    printf("\tFlash animation player base on libFAD-%d.%d.%d.\n", FAD_MAJOR_VER, FAD_MINOR_VER, FAD_MICRO_VER);    printf("\tCopyright (c) 2005 - 2006 RuXu W.(wrxzzj-AT-gmail-DOT-com)\n");    printf("\n\tUsage: \tfap <flash animation file>.swf\n");    return -1;  }  if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) {    DUMP("SDL system initialized failure.\n");    return -1;  }  atexit(SDL_Quit);  signal(SIGINT, SIG_DFL);  fp = fopen(argv[1], "rb");  if(fp == NULL) {    DUMP("open %s failure, err = %s.\n", argv[1], strerror(errno));    return -1;  }  fad_frame_init(&frame);  fad_stream_init(&stream, NULL);	fad_frame_set_update_callback(&frame, _sdl_update_callback, NULL);  fad_stream_stdio(&stream, fp);  /**decode flash file header*/  fad_frame_decode(&frame, &stream);  if(stream.err != FAD_ERROR_NONE) {    DUMP("decode flash file header failure.\n");    fclose(fp);    return -1;  }  width = (frame.size.x1-frame.size.x0)/20;  height = (frame.size.y1-frame.size.y0)/20;  sdl_screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);  if(sdl_screen == NULL)    goto error;  SDL_WM_SetCaption("Flash Animation Player", NULL);  stride = width*(SDL_GetVideoInfo()->vfmt->BytesPerPixel);  imgbuf = calloc(1, stride*height);  if(imgbuf == NULL)    goto error;  sdl_surface = SDL_CreateRGBSurfaceFrom((void* )imgbuf, width, height, SDL_GetVideoInfo()->vfmt->BitsPerPixel, stride, 0x00, 0x00, 0x00, 0xff<<24);  if(sdl_surface == NULL)    goto error;  {    cairo_surface_t *mysurface = NULL;    mysurface = cairo_image_surface_create_for_data(imgbuf, CAIRO_FORMAT_ARGB32, width, height, stride);    if(mysurface == NULL) {      DUMP("create cairo image surface failure.\n");      goto error;    }    frame.render->cr = cairo_create(mysurface);    if(frame.render->cr == NULL) {      DUMP("create cairo context failure.\n");      goto error;    }    cairo_surface_destroy(mysurface);  }  cairo_set_fill_rule(frame.render->cr, CAIRO_FILL_RULE_EVEN_ODD);  cairo_set_tolerance(frame.render->cr, 0.50);  tid = SDL_AddTimer(256000/frame.rate, _sdl_on_timer, NULL);  while(SDL_WaitEvent(&event)) {    switch(event.type) {      case FAD_ONTIMER:        /**TIPS:you can skip vector graphic movie render if your CPU is slower*/        /**if frame isn't render-ready mode, decode more flash stream*/        if(frame.sta == FAD_STA_END) {          //quit = 1;          break;        }        if(frame.sta == FAD_STA_STOP)          break;        while(frame.sta!=FAD_STA_DORENDER && frame.sta!=FAD_STA_FINISH && frame.sta!=FAD_STA_UPDATEBTN )          fad_frame_decode(&frame, &stream);        /**set background color for movie*/        SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format, frame.r, frame.g, frame.b));        /**render vector graphic movie*/        fad_frame_render_movie(&frame);        /**copy image buffer from surface to screen*/        SDL_BlitSurface(sdl_surface, NULL, sdl_screen, NULL);        SDL_UpdateRect(sdl_screen, 0, 0, 0, 0);        /**render audio in frame*/        fad_frame_render_audio(&frame, &stream);        break;      /**set mouse position & state in render public attribute*/      case SDL_MOUSEBUTTONUP:				if(frame.proc_mbevt)					frame.proc_mbevt(&frame, FAD_EVT_MBUP, event.button.x, event.button.y);				break;      case SDL_MOUSEBUTTONDOWN:				if(frame.proc_mbevt)					frame.proc_mbevt(&frame, FAD_EVT_MBDOWN, event.button.x, event.button.y);        break;      case SDL_MOUSEMOTION:				if(frame.proc_mbevt)					frame.proc_mbevt(&frame, FAD_EVT_MBMOTION, event.motion.x, event.motion.y);        break;      case SDL_QUIT:        quit = 1;        break;      default:        break;    }    if(quit)      break;  }/**while(poll_event)*/error:  fclose(fp);  fad_frame_finish(&frame);  fad_stream_finish(&stream);  if(imgbuf != NULL)    free(imgbuf);  if(sdl_screen != NULL)    SDL_FreeSurface(sdl_screen);  if(sdl_surface != NULL)    SDL_FreeSurface(sdl_surface);  if(tid != NULL)    SDL_RemoveTimer(tid);  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -