SDL中的跳棋游戏

时间:2011-10-31 02:42:24

标签: c sdl game-engine

我正在尝试制作一个跳棋游戏,并且我正在使用SDL进行界面,但我只是在学习C和SDL,如何移动我添加到屏幕的表面?我希望它尽可能简单,只需从X中移除并在Y上显示,如何移除曲面使其出现在屏幕上的另一个位置?这是我的代码:

#include "SDL.h"
#define BRANCA 2
#define PRETA 1
#define DAMA 2
#define NORMAL 1

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *pecaPreta = NULL;
SDL_Surface *pecaBranca = NULL;
SDL_Surface *pecaDamaPreta = NULL;
SDL_Surface *pecaDamaBranca = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;

SDL_Surface *load_image(char * filename )
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP(filename);

    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        SDL_FreeSurface( loadedImage );

        if( optimizedImage != NULL )
        {
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
        }
    }

    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( source, NULL, destination, &offset );
}

void inserePeca(int tipo, int posX, int posY, int cor)
{
    switch(cor)
    {
    case 1:
        switch (tipo)
        {
        case 1:
            apply_surface(posX, posY, pecaPreta, screen);
        break;
        case 2:
            apply_surface(posX, posY, pecaDamaPreta, screen);
        break;  
        }
    break;
    case 2:
        switch (tipo)
        {
        case 1:
            apply_surface(posX, posY, pecaBranca, screen);
        break;
        case 2:
            apply_surface(posX, posY, pecaDamaBranca, screen);
        break;  
        }
    break;
    }
}

int main()
{
    int quit = 0;
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }

    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    if( screen == NULL )
    {
        return 1;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Jogo de Damas 0.1b", NULL );

    //Load the images
    pecaPreta = load_image( "pecapreta.bmp" );
    pecaBranca = load_image("pecabranca.bmp");
    pecaDamaPreta = load_image("pecadamapreta.bmp");
    pecaDamaBranca = load_image("pecadamabranca.bmp");
    background = load_image( "tabuleiro.bmp" );

    //Apply the background to the screen
    apply_surface( 0, 0, background, screen );

    inserePeca(DAMA, 0,0, BRANCA);
    inserePeca(NORMAL, 80,0, PRETA);

    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
    while( quit == 0 )
    {
        //While there's an event to handle
        while( SDL_PollEvent( &event ) )
        {
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = -1;
            }
        }
    }

    //Free the surfaces
    SDL_FreeSurface( pecaPreta );
    SDL_FreeSurface( background );

    //Quit SDL
    SDL_Quit();

    return 0;
}

你可以看到我在“inserePeca”上添加一个块,我想在创建后移动它

1 个答案:

答案 0 :(得分:0)

屏幕的缓冲区不会将您绘制的所有内容保存为单独的项目 - 它只保存所有绘图操作的最终结果。因此,您不仅可以绘制背景,然后在其上绘制一块,然后移动它 - 您需要使用所需的更改重绘屏幕的受影响部分。

你仍然有片断的图像,你仍然有背景图像;移动你绘制的棋子的方法只是通过再次将它渲染到旧位置,然后在新位置将棋子搞定。然而,不是再绘制整个屏幕和所有部分,而是可以绘制更改的区域:blit只是背景的一部分以擦除旧的正方形,然后将片段blit到新的正方形上。

以下函数类似于apply_surface()函数,但不是将整个源图像复制到目标的给定坐标,而是从给定的坐标复制给定宽度和高度的区域。源图像到目的地的相同坐标。然后可以使用它来恢复屏幕的一小部分背景。

/* Blit a region from src to the corresponding region in dest.  Uses the same
 * x and y coordinates for the regions in both src and dest.  w and h give the
 * width and height of the region, respectively.
 */
void erase_rect( int x, int y, int w, int h,  SDL_Surface *src, SDL_Surface *dest)
{
  SDL_Rect offset;
  offset.x = x;
  offset.y = y;
  offset.w = w;
  offset.h = h;
  SDL_BlitSurface( src, &offset, dest, &offset );
}

因此,如果您的方块是50x50,并且您需要将一块从(120,40)处的方块移动到(170,90)处的方块,您可以执行以下操作:

/* erase old 50x50 square at (120,40) (to background image) */
erase_rect( 120, 40, 50, 50, background, screen );
/* draw piece at new position of (170,90) */
inserePeca(NORMAL, 170, 90, PRETA);