SDL Guide 中文譯版(四) (轉)
第四章 樣例
注:重複的例子沒有列出
最快的影像平面塊傳送
將影像畫到螢幕上有三種方式:1.建立一個影像平面並用SDL_BlitSurface傳送到螢幕;2.在建立影片平面並SDL_UpdateRect;3.在視訊記憶體建立影片平面並呼叫SDL_LockSurface。最好的方法是混合方式:
#include #include SDL_AudioSpec wanted; extern void fill_audio(void *udata, Uint8 *stream, int len); /* Set the audio format */ wanted.freq = 22050; wanted.format = AUDIO_S16; wanted.channels = 2; /* 1 = mono, 2 = stereo */ wanted.samples = 1024; /* Good low-latency value for callback */ wanted.callback = fill_audio; wanted.userdata = NULL; /* Open the audio device, forcing the desired format */ if ( SDL_OpenAudio(&wanted, NULL) < 0 ) { fprintf(stderr, "Couldn't open audio: %sn", SDL_GetError()); return(-1); } return(0); static Uint8 *audio_chunk; static Uint32 audio_len; static Uint8 *audio_pos; /* The audio function callback takes the following parameters: stream: A pointer to the audio buffer to be filled len: The length (in bytes) of the audio buffer */ void fill_audio(void *udata, Uint8 *stream, int len) { /* Only play if we have data left */ if ( audio_len == 0 ) return; /* Mix as much data as possible */ len = ( len > audio_len ? audio_len : len ); SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME) audio_p+= len; audio_len -= len; } /* Load the audio data ... */ ;;;;; audio_pos = audio_chunk; /* Let the callback function play the audio chunk */ SDL_PauseAudio(0); /* Do some processing */ ;;;;; /* Wait for sound to complete */ while ( audio_len > 0 ) { SDL_Delay(100); /* Sleep 1/10 second */ } SDL_CloseAudio(); #include "SDL.h" /* Initialize SDL first */ if ( SDL_Init(SDL_INIT_CDROM) < 0 ) { fprintf(stderr, "Couldn't initialize SDL: %sn",SDL_GetError()); exit(1); } atexit(SDL_Quit); /* Find out how many CD-ROM drives are connected to the system */ printf("Drives available: %dn", SDL_CDNumDrives()); for ( i=0; i SDL_CD *cdrom; CDstatus status; char *status_str; cdrom = SDL_CDOpen(0); if ( cdrom == NULL ) { fprintf(stderr, "Couldn't open default CD-ROM drive: %sn", SDL_GetError()); exit(2); } status = SDL_CDStatus(cdrom); switch (status) { case CD_TRAYEMPTY: status_str = "tray empty"; break; case CD_STOPPED: status_str = "stopped"; break; case CD_PLAYING: status_str = "playing"; break; case CD_PAUSED: status_str = "paused"; break; case CD_ERROR: status_str = "error state"; break; } printf("Drive status: %sn", status_str); if ( status >= CD_PLAYING ) { int m, s, f; FRAMES_TO_M(cdrom->cur_frame, &m, &s, &f); printf("Currently playing track %d, %d:%2.2dn", cdrom->track[cdrom->cur_track].id, m, s); } SDL_CD *cdrom; /* Assuming this has already been set.. */ int i; int m, s, f; SDL_CDStatus(cdrom); printf("Drive tracks: %dn", cdrom->numtracks); for ( i=0; i SDL_CD *cdrom; /* Assuming this has already been set.. */ // Play entire CD: if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); // Play last track: if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) { SDL_CDPlayTracks(cdrom, cdrom->numtracks-1, 0, 0, 0); } // Play first and second track and 10 seconds of third track: if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) SDL_CDPlayTracks(cdrom, 0, 0, 2, 10); #define TICK_INTERVAL 30 Uint32 TimeLeft(void) { static Uint32 next_time = 0; Uint32 now; now = SDL_GetTicks(); if ( next_time <= now ) { next_time = now+TICK_INTERVAL; return(0); } return(next_time-now); } /* main game loop while ( game_running ) { UpdateGameState(); SDL_Delay(TimeLeft()); } 過濾和處理事件
開啟裝置
音訊
列出所有CDROM
開啟預設CDROM器
列出CD上所有音軌
播放CD
基於時間的遊戲主迴圈
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993821/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SDL Guide 中文譯版(一) (轉)GUIIDE
- SDL Guide 中文譯版(二) (轉)GUIIDE
- SDL Guide 中文譯版(三上) (轉)GUIIDE
- SDL Guide 中文譯版(三下) (轉)GUIIDE
- HTTPie 官方文件中文翻譯版HTTP
- 【翻譯】AwesomeAsyncio中文版
- 【譯Py】Awesome Asyncio 中文版
- 編譯Thelittlebookofredis中文版編譯Redis
- Ionic2系列——Ionic 2 Guide 官方文件中文版GUIIDE
- Emacs 教程中文版(轉)Mac
- iOS請求URL 中文轉譯iOS
- 【呆鳥譯Py】Awesome Asyncio 中文版
- 打造中文版Eclipse (轉)Eclipse
- Gnutella協議中文版 (轉)協議
- 《The Swift Programming Language》中文版翻譯完成Swift
- 中文版《計算機程式設計藝術》譯者的回憶 (轉)計算機程式設計
- RAC student guide 的中文筆記GUIIDE筆記
- C++11FAQ中文版–轉C++
- A Brief Look at C++ 中文版 (轉)C++
- Perl語言入門(第四版)(中文版) 下載
- 高效翻譯軟體:Easy Translator for mac 中文版Mac
- SDL簡介
- RAC student guide 的中文筆記(zt)GUIIDE筆記
- 中文RFC文件遠端COM選項(四) (轉)
- 《Head First C 中文版》審讀筆記(四)筆記
- 翻譯|A Visual Guide to State in ReactGUIIDEReact
- [轉貼]越獄所有中文譯名~~~噴飯!
- 實用的線上翻譯工具:Translatium for Mac中文版Mac
- (譯) JSON-RPC 2.0 規範(中文版)JSONRPC
- 《萬物簡史》中文版翻譯質量低劣
- Data Warehouse Guide文件筆記(四):dimensionGUIIDE筆記
- Object Pascal Style Guide (轉)ObjectGUIIDE
- JavaScript 權威指南第七版(GPT 重譯)(四)JavaScriptGPT
- 超好用的谷歌線上翻譯工具:Translatium Mac中文版谷歌Mac
- 超級好用的線上翻譯工具:Translatium Mac中文版Mac
- 我眼中的《WebGL:Up and Running》——中文版譯者序Web
- 《Emacs 快速指南》最新簡體中文版(轉)Mac
- TELNET協議規範(中文版) (轉)協議