c++ - 在屏幕上位图传输的字符之间创建空间 - SDL

标签 c++ sdl blit

我有一个包含数字和字符的 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;
}

最佳答案

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

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

另外,这个:

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

是多余的,位图传输时会忽略目标的宽度和高度(至少 reference for SDL_BlitSurface 是这么说的)。

关于c++ - 在屏幕上位图传输的字符之间创建空间 - SDL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638306/

相关文章:

c++ - 有进度的快速文件复制

python - 如何在 pygame 中渲染/blit 文本以获得良好的性能

linux - OpenGL 全屏纹理使帧率降至 12fps

c++ - 做复合 C++ 时成员函数重载错误

c++将单个变量作为引用和常量引用传递

c++ - 使用最少的代码行读取 C++ 中的表格数字数据

python - 为什么这段代码不起作用? (Python 和 PyGame)

c++ - Foreach 和 For 循环的区别?

c++ - 使用 clang++ 和 SDL 包含路径

python - 提高 Sprite 渲染性能?