http://blog.sina.com.cn/ufownl[订阅]
字体大小: 正文
第四章 样例(上)(2007-08-23 23:10:29)
 

注:重复的例子没有列出

最快的图像平面块传送

将图像画到屏幕上有三种方式: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);

}

加载中,请稍候...
  • 评论加载中,请稍候...

验证码:请点击后输入验证码  收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...