SDL Guide 中文译版(四)
2008-04-09 04:27:34来源:互联网 阅读 ()
第四章 样例
注:重复的例子没有列出
最快的图像平面块传送
将图像画到屏幕上有三种方式:1.创建一个图像平面并用SDL_BlitSurface传送到屏幕;2.在系统内存创建视频平面并调用SDL_UpdateRect;3.在显存创建视频平面并调用SDL_LockSurface。最好的方法是混合方式:
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_timer.h"
void ComplainAndExit(void)
{
fprintf(stderr, "Problem: %s\n", SDL_GetError());
exit(1);
}
int main(int argc, char *argv[])
{
SDL_PixelFormat fmt;
SDL_Surface *screen, *locked;
SDL_Surface *imagebmp, *image;
SDL_Rect dstrect;
int i;
Uint8 *buffer;
/* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
ComplainAndExit();
}
atexit(SDL_Quit);
/* Load a BMP image into a surface */
imagebmp = SDL_LoadBMP("image.bmp");
if ( imagebmp == NULL ) {
ComplainAndExit();
}
/* Set the video mode (640x480 at native depth) */
screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE|SDL_FULLSCREEN);
if ( screen == NULL ) {
ComplainAndExit();
}
/* Set the video colormap */
if ( imagebmp->format->palette != NULL ) {
SDL_SetColors(screen,
imagebmp->format->palette->colors, 0,
imagebmp->format->palette->ncolors);
}
/* Convert the image to the video format (maps colors) */
image = SDL_DisplayFormat(imagebmp);
SDL_FreeSurface(imagebmp);
if ( image == NULL ) {
ComplainAndExit();
}
/* Draw bands of color on the raw surface */
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 )
ComplainAndExit();
}
buffer=(Uint8 *)screen->pixels;
for ( i=0; ih; i ) {
memset(buffer,(i*255)/screen->h,
screen->w*screen->format->BytesPerPixel);
buffer = screen->pitch;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
/* Blit the image to the center of the screen */
dstrect.x = (screen->w-image->w)/2;
dstrect.y = (screen->h-image->h)/2;
dstrect.w = image->w;
dstrect.h = image->h;
if ( SDL_BlitSurface(image, NULL, screen, &dstrect) < 0 ) {
SDL_FreeSurface(image);
ComplainAndExit();
}
SDL_FreeSurface(image);
/* Update the screen */
SDL_UpdateRects(screen, 1, &dstrect);
SDL_Delay(5000); /* Wait 5 seconds */
exit(0);
}
过滤和处理事件
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
/* This function may run in a separate event thread */
int FilterEvents(const SDL_Event *event) {
static int boycott = 1;
/* This quit event signals the closing of the window */
if ( (event->type == SDL_QUIT) && boycott ) {
printf("Quit event filtered out -- try again.\n");
boycott = 0;
return(0);
}
if ( event->type == SDL_MOUSEMOTION ) {
printf("Mouse moved to (%d,%d)\n",
event->motion.x, event->motion.y);
return(0); /* Drop it, we''''ve handled it */
}
return(1);
}
int main(int argc, char *argv[])
{
SDL_Event event;
/* Initialize the SDL library (starts the event loop) */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn''''t initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* Clean up on exit, exit on window close and interrupt */
atexit(SDL_Quit);
/* Ignore key events */
SDL_EventState(SDL_KEYDOWN, SDL_IGNORE);
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
/* Filter quit and mouse motion events */
SDL_SetEventFilter(FilterEvents);
/* The mouse isn''''t much use unless we have a display for reference */
if ( SDL_SetVideoMode(640, 480, 8, 0) == NULL ) {
fprintf(stderr, "Couldn''''t set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
/* Loop waiting for ESC Mouse_Button */
while ( SDL_WaitEvent(&event) >= 0 ) {
switch (event.type) {
case SDL_ACTIVEEVENT: {
if ( event.active.state & SDL_APPACTIVE ) {
if ( event.active.gain ) {
printf("App activated\n");
} else {
printf("App iconified\n");
}
}
}
break;
case SDL_MOUSEBUTTONDOWN: {
Uint8 *keys;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
printf("Bye bye...\n");
exit(0);
}
printf("Mouse button pressed\n");
}
break;
case SDL_QUIT: {
printf("Quit requested, quitting.\n");
exit(0);
}
break;
}
}
/* This should never happen */
printf("SDL_WaitEvent error: %s\n", SDL_GetError());
exit(1);
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
