Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8700223
  • 博文数量: 1413
  • 博客积分: 11128
  • 博客等级: 上将
  • 技术积分: 14685
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-13 10:03
个人简介

follow my heart...

文章分类

全部博文(1413)

文章存档

2013年(1)

2012年(5)

2011年(45)

2010年(176)

2009年(148)

2008年(190)

2007年(293)

2006年(555)

分类: C/C++

2006-11-24 22:26:03

#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <string.h>
#i nclude <SDL.h>
#i nclude <windows.h>

/*
 PLEASE NOTE: the program will require SDL.dll which is located in
              dev-c++'s dll directory. You have to copy it to you
     program's home directory or the path.
 */


/* The screen surface */

SDL_Surface *screen = NULL;//屏幕表面

void display_bmp(char *file_name);

/* This function draws to the screen; replace this with your own code! */
//绘制屏幕

static void
draw ()
{
    static int direction = 0;
    static int value = 0;
    static int which = 0;
    SDL_Rect rect;//定义一个矩形区域

    Uint32 color;

    /* Create a black background */
 //创建黑色背景

    color = SDL_MapRGB (screen->format, 0, 0, 0);//映射一个颜色值为像素

    SDL_FillRect (screen, NULL, color);//使用某种颜色快速填充指定矩形


    /* Determine which color the layer should have */
    if (direction == 0)
    {
        value += 2;
        if (value >= 256)
        {
            value = 255;
            direction = 1;
        }
    }
    else
    {
        value -= 2;
        if (value <= 5)
        {
            value = 0;
            direction = 0;
            which++;
            if (which == 5)
                which = 0;
        }
    }

    /* Draw a layer with variable color */
 //使用指定颜色在层上显示

    switch (which)
    {
      case 0:
          color = SDL_MapRGB (screen->format, value, 0, 0);
          break;
      case 1:
          color = SDL_MapRGB (screen->format, 0, value, 0);
          break;
      case 2:
          color = SDL_MapRGB (screen->format, 0, 0, value);
          break;
      case 3:
          color = SDL_MapRGB (screen->format, value, value, value);
          break;
      case 4:
          color = SDL_MapRGB (screen->format, value, 0, value);
          break;
    }

    rect.w = screen->w / 2;
    rect.h = screen->h / 2;
    rect.x = (screen->w / 2) - (rect.w / 2);
    rect.y = (screen->h / 2) - (rect.h / 2);
    SDL_FillRect (screen, &rect, color);


    /* Make sure everything is displayed on screen */
 //翻转屏幕,要翻转必须在设置视频模式时加上SDL_DOUBLEBUF选项

    SDL_Flip (screen);
    /* Don't run too fast */
 //延迟1毫秒

    SDL_Delay (1);
}


int
main (int argc, char *argv[])//必须要加参数

{
    char *msg;
    int done;

    /* Initialize SDL */
    if (SDL_Init (SDL_INIT_VIDEO) < 0)//初始化SDL

    {
        sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
        MessageBox (0, msg, "Error", MB_ICONHAND);
        free (msg);
        exit (1);
    }
    atexit (SDL_Quit);//释放


    /* Set 640x480 16-bits video mode */
 //设置视频模式640*480

    screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
    if (screen == NULL)
    {
        sprintf (msg, "Couldn't set 640x480x16 video mode: %s\n",
          SDL_GetError ());
        MessageBox (0, msg, "Error", MB_ICONHAND);
        free (msg);
        exit (2);
    }
 //设置窗口标题

    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);

 //通过这种方式设置全屏方式在win32下出现错误.建议使用在setvideomode中使用sdl_fullscreen参数

 //if(SDL_WM_ToggleFullScreen(screen)==0)

 // MessageBox(NULL,"设置全屏错误!","设置全屏错误!",0);


    //事件循环

 done = 0;
    while (!done)
    {
        SDL_Event event;

        /* Check for events */
  //检测事件

        while (SDL_PollEvent (&event))
        {
            switch (event.type)
            {
            case SDL_KEYDOWN:
                break;
            case SDL_QUIT:
                done = 1;
                break;
            default:
                break;
            }
        }

        /* Draw to screen */
  //绘制屏幕

        //draw ();//绘制不同色彩的矩形

  display_bmp("123.bmp");//显示图片

  
    }

    return 0;
}

void display_bmp(char *file_name)
{
    SDL_Surface *image;

    /* Load the BMP file into a surface */
 //加载位图至屏幕表面

    image = SDL_LoadBMP(file_name);
    if (image == NULL) {
        fprintf(stderr, "Couldn't load %s: %s\n", file_name, SDL_GetError());
        return;
    }

    /*
     * Palettized screen modes will have a default palette (a standard
     * 8*8*4 colour cube), but if the image is palettized as well we can
     * use that palette for a nicer colour matching
     */

 //如果图像调色板与屏幕调色板一致的话,设置屏幕的一部分色彩为图像的色彩

    if (image->format->palette && screen->format->palette) {
    SDL_SetColors(screen, image->format->palette->colors, 0,
                  image->format->palette->ncolors);
    }

    /* Blit onto the screen surface */
 //迅速传输image表面至于screen表面

    if(SDL_BlitSurface(image, NULL, screen, NULL) < 0)
        fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());

    //更新指定区域

 SDL_UpdateRect(screen, 0, 0, image->w, image->h);

    /* Free the allocated BMP surface */
 //清除所分配的bmp表面

    SDL_FreeSurface(image);
 
 //翻页

 SDL_Flip(screen);
}

阅读(3318) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~