gpt4 book ai didi

c++ - 从 SDL_Rect 的 vector 在 SDL 中传输表面的问题

转载 作者:行者123 更新时间:2023-11-28 08:17:35 24 4
gpt4 key购买 nike

#include "main.h"

SDL_Surface* screen; //The screen Surface
SDL_Surface* Snake_Body; //The snake surface (shape to draw)

SDL_Rect rectangle1; //Red border
SDL_Rect rectangle2; //Black Board

SDL_Event event; //Keyboard handling
direction facing;

std::vector<SDL_Rect> snake (3); //Snake body locations



SDL_Surface *LoadSnake()
{
SDL_Surface* temporary;

Uint32 rmask,gmask,bmask,amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

temporary = SDL_CreateRGBSurface(SDL_SWSURFACE,5,5,32,rmask,gmask,bmask,amask); //Make an empty surface

SDL_FillRect(temporary,NULL,SDL_MapRGB(screen->format,0,255,0)); //Fill it with color

return temporary; //return it
}


bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1 )
return false;

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

if(!screen)
return false;

SDL_WM_SetCaption("Snake v0.1",NULL);

Snake_Body = LoadSnake();

rectangle1.x = 0; //Red border
rectangle1.y = 0;
rectangle1.w = 500;
rectangle1.h = 500;

rectangle2.x = 25; //Black background
rectangle2.y = 25;
rectangle2.w = 450;
rectangle2.h = 450;

return true;
}

SDL_Surface *loadImage(const char* filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;

loadedImage = SDL_LoadBMP(filename);

if (loadedImage)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}

return optimizedImage;
}

void pollInput()
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
end = true;
}

if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
end = true; break;
case SDLK_UP:
facing = up; break;
case SDLK_DOWN:
facing = down; break;
case SDLK_RIGHT:
facing = right; break;
case SDLK_LEFT:
facing = left; break;
}
}
}
}

void Update()
{
for(unsigned int i = 1; i < snake.size(); i++)
{
snake[i] = snake[i-1];
}

switch(facing)
{
case up:
snake[0].y -= 5; break;
case down:
snake[0].y += 5; break;
case left:
snake[0].x -= 5; break;
case right:
snake[0].x += 5; break;
}

}

void SetUp()
{
snake[0].x = 250;
snake[0].y = 250;


snake[1].x = 250 + 15;
snake[1].y = 250;

snake[2].x = 250 + 15 + 15;
snake[2].y = 250;
}

void Draw()
{
unsigned short i;
int x;

SDL_FillRect(screen,&rectangle1,SDL_MapRGB(screen->format,255,0,0)); //Red Border

SDL_FillRect(screen,&rectangle2,SDL_MapRGB(screen->format,0,0,0)); //Black Board

for(i = 0; i < snake.size(); i++)
{
assert(SDL_BlitSurface(Snake_Body,NULL,screen,&snake[i]) == 0);
}

SDL_Flip(screen);
}

void Finish()
{
SDL_FreeSurface(Snake_Body);

SDL_Quit();
}

我有这段代码,这是一个用 SDL 编写的贪吃蛇游戏,但我似乎遇到了问题。

SDL 可以很好地绘制背景,但由于某种原因它不会绘制蛇。我在那里设置了一个断言来检查 SDL_BlitSurface 函数是否成功,它返回一个正常值 (0),即使屏幕上没有任何显示。

我将蛇节点的纹理设置为单个绿色矩形 Snake_Body,并将需要在 std::vector 容器中绘制的位置编码,这样我就可以调用 SDL_BlitSurface() 函数 vector 中的每个元素,我都跟踪了值并且它们正在正确更改,但由于某种原因,图像仍然没有绘制。我还尝试在同一位置用 SDL_FillRect() 替换我的 SDL_BlitSurface() 函数,但它没有改变任何东西。

编辑:我在这个编辑下面添加了我的主循环:

#include "main.h"

bool end = false;
Uint32 time;

int main(int argc,char* argv[])
{
if(!init())
return 1;

SetUp();

time = SDL_GetTicks();

while(!end)
{
if(time+1000<SDL_GetTicks())
{
Update();
time = SDL_GetTicks();
}

pollInput();
Draw();
}

Finish();

return 0;
}

最佳答案

您似乎没有对蛇的位置进行任何边界检查以确保它停留在屏幕上。因此,当它离开屏幕时,它会保持关闭状态并且永远不会回来。我的猜测是你没有正确调节帧率,或者你根本没有调节它(我需要看到你的主循环),所以蛇离开屏幕的速度如此之快以至于你根本看不到它。

如果不调用 Update 函数会怎样?

关于c++ - 从 SDL_Rect 的 vector 在 SDL 中传输表面的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7176907/

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