📄 gameswf_test_ogl.cpp
字号:
// gameswf_test_ogl.cpp -- Thatcher Ulrich <tu@tulrich.com> 2003 -*- coding: utf-8;-*-// This source code has been donated to the Public Domain. Do// whatever you want with it.// A minimal test player app for the gameswf library.#include "SDL.h"#include "SDL_thread.h"#include "gameswf.h"#include <stdlib.h>#include <stdio.h>#include "base/ogl.h"#include "base/utility.h"#include "base/container.h"#include "base/tu_file.h"#include "base/tu_types.h"#include "gameswf_xmlsocket.h"#ifdef HAVE_LIBXMLextern int xml_fd; // FIXME: this is the file descriptor // from XMLSocket::connect(). This // needs to be propogated up through // the layers properly, but first I // want to make sure it all works.#endif // HAVE_LIBXMLvoid print_usage()// Brief instructions.{ printf( "gameswf_test_ogl -- a test player for the gameswf library.\n" "\n" "This program has been donated to the Public Domain.\n" "See http://tulrich.com/geekstuff/gameswf.html for more info.\n" "\n" "usage: gameswf_test_ogl [options] movie_file.swf\n" "\n" "Plays a SWF (Shockwave Flash) movie, using OpenGL and the\n" "gameswf library.\n" "\n" "options:\n" "\n" " -h Print this info.\n" " -s <factor> Scale the movie up/down by the specified factor\n" " -c Produce a core file instead of letting SDL trap it\n" " -d num Number of milli-seconds to delay in main loop\n" " -a Turn antialiasing on/off. (obsolete)\n" " -v Be verbose; i.e. print log messages to stdout\n" " -va Be verbose about movie Actions\n" " -vp Be verbose about parsing the movie\n" " -ml <bias> Specify the texture LOD bias (float, default is -1)\n" " -p Run full speed (no sleep) and log frame rate\n" " -e Use SDL Event thread\n" " -1 Play once; exit when/if movie reaches the last frame\n" " -r <0|1|2> 0 disables renderering & sound (good for batch tests)\n" " 1 enables rendering & sound (default setting)\n" " 2 enables rendering & disables sound\n" " -t <sec> Timeout and exit after the specified number of seconds\n" " -b <bits> Bit depth of output window (16 or 32, default is 16)\n" "\n" "keys:\n" " CTRL-Q Quit/Exit\n" " CTRL-W Quit/Exit\n" " ESC Quit/Exit\n" " CTRL-P Toggle Pause\n" " CTRL-R Restart the movie\n" " CTRL-[ or kp- Step back one frame\n" " CTRL-] or kp+ Step forward one frame\n" " CTRL-A Toggle antialiasing (doesn't work)\n" " CTRL-T Debug. Test the set_variable() function\n" " CTRL-G Debug. Test the get_variable() function\n" " CTRL-M Debug. Test the call_method() function\n" " CTRL-B Toggle background color\n" );}#define OVERSIZE 1.0fstatic int runThread(void *nothing);static int doneYet = 0;static float s_scale = 1.0f;static bool s_antialiased = false;static int s_bit_depth = 16;static bool s_verbose = false;static bool s_background = true;static bool s_measure_performance = false;static bool s_event_thread = false;static void message_log(const char* message)// Process a log message.{ if (s_verbose) { fputs(message, stdout); fflush(stdout); // needed on osx for some reason }}static void log_callback(bool error, const char* message)// Error callback for handling gameswf messages.{ if (error) { // Log, and also print to stderr. message_log(message); fputs(message, stderr); } else { message_log(message); }}static tu_file* file_opener(const char* url)// Callback function. This opens files for the gameswf library.{ return new tu_file(url, "rb");}static void fs_callback(gameswf::movie_interface* movie, const char* command, const char* args)// For handling notification callbacks from ActionScript.{ message_log("fs_callback: '"); message_log(command); message_log("' '"); message_log(args); message_log("'\n");}static void key_event(SDLKey key, bool down)// For forwarding SDL key events to gameswf.{ gameswf::key::code c(gameswf::key::INVALID); if (key >= SDLK_a && key <= SDLK_z) { c = (gameswf::key::code) ((key - SDLK_a) + gameswf::key::A); } else if (key >= SDLK_F1 && key <= SDLK_F15) { c = (gameswf::key::code) ((key - SDLK_F1) + gameswf::key::F1); } else if (key >= SDLK_KP0 && key <= SDLK_KP9) { c = (gameswf::key::code) ((key - SDLK_KP0) + gameswf::key::KP_0); } else { // many keys don't correlate, so just use a look-up table. struct { SDLKey sdlk; gameswf::key::code gs; } table[] = { { SDLK_RETURN, gameswf::key::ENTER }, { SDLK_ESCAPE, gameswf::key::ESCAPE }, { SDLK_LEFT, gameswf::key::LEFT }, { SDLK_UP, gameswf::key::UP }, { SDLK_RIGHT, gameswf::key::RIGHT }, { SDLK_DOWN, gameswf::key::DOWN }, // @@ TODO fill this out some more { SDLK_UNKNOWN, gameswf::key::INVALID } }; for (int i = 0; table[i].sdlk != SDLK_UNKNOWN; i++) { if (key == table[i].sdlk) { c = table[i].gs; break; } } } if (c != gameswf::key::INVALID) { gameswf::notify_key_event(c, down); }}int main(int argc, char *argv[]){ assert(tu_types_validate()); const char* infile = NULL; float exit_timeout = 0; bool do_render = true; bool do_sound = true; bool do_loop = true; bool sdl_abort = true; int delay = 31; float tex_lod_bias; // -1.0 tends to look good. tex_lod_bias = -1.2f; for (int arg = 1; arg < argc; arg++) { if (argv[arg][0] == '-') { // Looks like an option. if (argv[arg][1] == 'h') { // Help. print_usage(); exit(1); } if (argv[arg][1] == 'c') { sdl_abort = false; } else if (argv[arg][1] == 's') { // Scale. arg++; if (arg < argc) { s_scale = fclamp((float) atof(argv[arg]), 0.01f, 100.f); } else { fprintf(stderr, "-s arg must be followed by a scale value\n"); print_usage(); exit(1); } } else if (argv[arg][1] == 'a') { // Set antialiasing on or off. arg++; if (arg < argc) { s_antialiased = atoi(argv[arg]) ? true : false; } else { fprintf(stderr, "-a arg must be followed by 0 or 1 to disable/enable antialiasing\n"); print_usage(); exit(1); } } else if (argv[arg][1] == 'b') { // Set default bit depth. arg++; if (arg < argc) { s_bit_depth = atoi(argv[arg]); if (s_bit_depth != 16 && s_bit_depth != 32) { fprintf(stderr, "Command-line supplied bit depth %d, but it must be 16 or 32", s_bit_depth); print_usage(); exit(1); } } else { fprintf(stderr, "-b arg must be followed by 16 or 32 to set bit depth\n"); print_usage(); exit(1); } } else if (argv[arg][1] == 'd') { // Set a delay arg++; if (arg < argc) { delay = atoi(argv[arg]); } else { fprintf(stderr, "-d arg must be followed by number of milli-seconds to del in the main loop\n"); print_usage(); exit(1); } } else if (argv[arg][1] == 'p') { // Enable frame-rate/performance logging. s_measure_performance = true; } else if (argv[arg][1] == 'e') { // Use an SDL thread to handle events s_event_thread = true; } else if (argv[arg][1] == '1') { // Play once; don't loop. do_loop = false; } else if (argv[arg][1] == 'r') { // Set rendering on/off. arg++; if (arg < argc) { const int render_arg = atoi(argv[arg]); switch (render_arg) { case 0: // Disable both do_render = false; do_sound = false; break; case 1: // Enable both do_render = true; do_sound = true; break; case 2: // Disable just sound do_render = true; do_sound = false; break; default: fprintf(stderr, "-r must be followed by 0, 1 or 2 (%d is invalid)\n", render_arg); print_usage(); exit(1); break; } } else { fprintf(stderr, "-r must be followed by 0 an argument to disable/enable rendering\n"); print_usage(); exit(1); } } else if (argv[arg][1] == 't') { // Set timeout. arg++; if (arg < argc) { exit_timeout = (float) atof(argv[arg]); } else { fprintf(stderr, "-t must be followed by an exit timeout, in seconds\n"); print_usage(); exit(1); } } else if (argv[arg][1] == 'v') { // Be verbose; i.e. print log messages to stdout. s_verbose = true; if (argv[arg][2] == 'a') { // Enable spew re: action. gameswf::set_verbose_action(true); } else if (argv[arg][2] == 'p') { // Enable parse spew. gameswf::set_verbose_parse(true); } // ... } else if (argv[arg][1] == 'm') { if (argv[arg][2] == 'l') { arg++; tex_lod_bias = (float) atof(argv[arg]); //printf("Texture LOD Bais is no %f\n", tex_lod_bias); } else { fprintf(stderr, "unknown variant of -m arg\n"); print_usage(); exit(1); } } } else { infile = argv[arg]; } } if (infile == NULL) { printf("no input file\n"); print_usage(); exit(1); } gameswf::register_file_opener_callback(file_opener); gameswf::register_fscommand_callback(fs_callback); if (s_verbose == true) { gameswf::register_log_callback(log_callback); } //gameswf::set_antialiased(s_antialiased); gameswf::sound_handler* sound = NULL; gameswf::render_handler* render = NULL; if (do_render) { if (do_sound) { sound = gameswf::create_sound_handler_sdl(); gameswf::set_sound_handler(sound); } render = gameswf::create_render_handler_ogl(); gameswf::set_render_handler(render); } // Get info about the width & height of the movie. int movie_version = 0; int movie_width = 0; int movie_height = 0; float movie_fps = 30.0f; gameswf::get_movie_info(infile, &movie_version, &movie_width, &movie_height, &movie_fps, NULL, NULL); if (movie_version == 0) { fprintf(stderr, "error: can't get info about %s\n", infile); exit(1); } int width = int(movie_width * s_scale); int height = int(movie_height * s_scale); if (do_render) { // Initialize the SDL subsystems we're using. Linux // and Darwin use Pthreads for SDL threads, Win32 // doesn't. Otherwise the SDL event loop just polls. if (sdl_abort) { // Other flags are SDL_INIT_JOYSTICK | SDL_INIT_CDROM#ifdef _WIN32 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))#else if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTTHREAD ))#endif { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } } else { fprintf(stderr, "warning: SDL won't trap core dumps \n");#ifdef _WIN32 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE | SDL_INIT_EVENTTHREAD))#else if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -