2

我有一个包含数字和字符的 bmp 文件。我的程序读取按键并将位图中的适当字符/数字传送到屏幕上。唯一的问题是如果我按'A' - 'A' 将显示在屏幕上,但如果我按'B',它将取代'A'。所以我永远不会在屏幕上拥有一个以上的角色。

这是我的代码:

#include <SDL/SDL.h>
#include <iostream>
#include <string>
#include <vector>
#include <SDL_image/SDL_image.h>

using namespace std;

const Uint32 FRAMES_PER_SECOND = 60;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int WIDTH = 21;
const int HEIGHT = 25;

SDL_Event sdlEvent;


char getAsciiKey(const SDL_Event &sdlEvent);

int main( int argc, char* args[] )
{
    //The surfaces
    SDL_Surface* image[2]   = {NULL};
    SDL_Surface* screen     = NULL;

    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );

    //Set up screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE | SDL_DOUBLEBUF );

char filename[256];    //create a large enough C-string

for(int index = 0; index < 2; index++)
{
    sprintf(filename, "Text-types/Arial%d.bmp", index); //Arial0 and Arial1

    //Load the image
    image[index] = IMG_Load( filename );

    if(image == NULL)
    {
        cout << "Error loading image: " << filename << endl;
        exit(1);
    }
}

Uint32 color = SDL_MapRGB(screen->format, 0xff,0xff,0xff);  //White
SDL_FillRect(screen, &screen->clip_rect, color);        //Backgroundcolor -> White

bool quit = false;    


//Main program loop
while(quit == false)
{

    //Process any events from the event queue - such as key presses or mouse movements
    while(SDL_PollEvent(&sdlEvent))
    {
        if(sdlEvent.type == SDL_QUIT)
        {
            quit = true;
        }
        if (sdlEvent.type == SDL_KEYDOWN)   //A key has just pressed
        {   
            int x, y, temp;
            SDL_Rect srcRect;
            SDL_Rect dstRect;

            //char letter = getAsciiKey(sdlEvent);    //Getting keypress


            temp = (static_cast<int>(getAsciiKey(sdlEvent)) - 12);

            x = (temp % 20); //X-axis postion
            y = (temp / 20); //Y-axis position

            srcRect.x = (x - 1) * WIDTH;
            srcRect.y = (y - 1) * HEIGHT;

            //PIXELSIZE
            srcRect.w = WIDTH;
            srcRect.h = HEIGHT;

            dstRect.x = 0;
            dstRect.y = 0;

            dstRect.w = WIDTH;
            dstRect.h = HEIGHT;

            SDL_BlitSurface(image[1] , &srcRect, screen, &dstRect );    //Putting image up                   on screen

            //Update Screen
            SDL_Flip( screen );

            //Pause
            SDL_Delay( 2000 );

        }
    }

}
//Free any loaded images
for(int index = 0; index < 2; index++)
{
    SDL_FreeSurface( image[index] );
}


//Quit SDL
SDL_Quit();

return 0;

}

char getAsciiKey(const SDL_Event &sdlEvent)
{
    char key = 0;
    if(sdlEvent.key.keysym.sym >= SDLK_a && sdlEvent.key.keysym.sym <= SDLK_z)
    {
        if(sdlEvent.key.keysym.mod & KMOD_SHIFT)
        {
            key = 'A' + char(sdlEvent.key.keysym.sym - SDLK_a);
        }

        else
        {
            key = 'a' + char(sdlEvent.key.keysym.sym - SDLK_a);
        }
    }

    else if(sdlEvent.key.keysym.sym >= SDLK_0 && sdlEvent.key.keysym.sym <= SDLK_9)
    {
        key = '0' + char(sdlEvent.key.keysym.sym - SDLK_0);
    }

    return key;
}
4

1 回答 1

2
dstRect.x = 0;
dstRect.y = 0;

这是你要转移到的位置,我看不到你在任何地方改变它。为每个新角色更新它。

另外,这个:

dstRect.w = WIDTH;
dstRect.h = HEIGHT;

是多余的,目的地的宽度和高度在传输时被忽略(至少是reference for SDL_BlitSurface这样说的)。

于 2012-09-28T10:48:45.417 回答