gpt4 book ai didi

c++ - 在屏幕上的 block 状字符之间创建空间 - SDL

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:53:59 26 4
gpt4 key购买 nike

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

是多余的,blitting 时忽略目标的宽度和高度(至少 reference for SDL_BlitSurface 是这样说的)。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com