gpt4 book ai didi

c++ - 在 SDL 中移动位图

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:47 25 4
gpt4 key购买 nike

我的图片无法加载 谁能告诉我为什么吗?我试图在按下 WASD 时更改偏移量,它将在 Code::Blocks 中起作用,但在 .exe 文件中不起作用。

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>


int WIDTH = 800;
int HEIGHT = 600;

int main (int argc, char** argv)
{

SDL_Init(SDL_INIT_EVERYTHING);

SDL_Surface *screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_SWSURFACE|SDL_DOUBLEBUF);
if (!screen){
printf("Unable to set 800x600 video: %s\n", SDL_GetError());
return 1;
}
SDL_WM_SetCaption("The Killer Of The Night Pre-Release 0.0.1", NULL);

SDL_Surface *player = SDL_LoadBMP("lol.bmp");
if (!player){
printf("Unable to load the image here's the error: %s\n", SDL_GetError());
}

SDL_Rect offset;
offset.x = 100;
offset.y = 200;

bool done = false;
while (!done)
{
SDL_Event event;
SDL_PollEvent(&event);

if (event.type == SDL_QUIT)
{
done = true;
}

if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{

case SDLK_ESCAPE:

done = true;
break;

case SDLK_w:
offset.y -= 1;
break;

case SDLK_a:
offset.x -= 1;
break;

case SDLK_s:
offset.y += 1;
break;

case SDLK_d:
offset.x += 1;
break;

}

}



SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 25,25,255));

SDL_BlitSurface(player, 0, screen, &offset);

SDL_Flip(screen);

}



return 0;
}

它在 Code::Blocks 中工作正常,但是当我运行 .exe 文件时它不工作了!?!

最佳答案

你需要把lol.bmp放在你的exe所在的目录下。

当您在 CodeBlocks 中运行可执行文件时,工作目录是代码块项目文件 (.cbp) 的目录,它是您的 lol.bmp 所在的位置。您需要将该文件放在 bin/Debug/ 文件夹或 bin/Release/ 文件夹中,具体取决于您的构建目标。

关于c++ - 在 SDL 中移动位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21517830/

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