第四章 样例
注:重复的例子没有列出
最快的图像平面块传送
将图像画到屏幕上有三种方式: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");
}