C++ 位图赢了';SDL中的t负载
******修复了********* 原来我的位图文件不知怎么被破坏了。我可以打开文件查看它,但我的应用程序无法。我制作了一个新图像,效果很好 我在sdl中显示位图图像时遇到问题。程序以代码2退出,因此我知道位图未加载。我正在windows 7 64位上使用vc2010。BMP的名称是hello.BMP,我尝试将它放在应用程序文件、源文件和项目文件的旁边,但它仍然无法加载它。我也尝试过把它放在c:\中,然后从那里加载,但运气不好。这是我第一次尝试使用SDL。代码如下:C++ 位图赢了';SDL中的t负载,c++,sdl,C++,Sdl,******修复了********* 原来我的位图文件不知怎么被破坏了。我可以打开文件查看它,但我的应用程序无法。我制作了一个新图像,效果很好 我在sdl中显示位图图像时遇到问题。程序以代码2退出,因此我知道位图未加载。我正在windows 7 64位上使用vc2010。BMP的名称是hello.BMP,我尝试将它放在应用程序文件、源文件和项目文件的旁边,但它仍然无法加载它。我也尝试过把它放在c:\中,然后从那里加载,但运气不好。这是我第一次尝试使用SDL。代码如下: #include "std
#include "stdlib.h"
#include "SDL.h"
int main(int argc, char *argv[])
{
SDL_Surface *screen; //This pointer will reference the backbuffer
SDL_Surface *image; //This pointer will reference our bitmap sprite
SDL_Surface *temp; //This pointer will temporarily reference our bitmap sprite
SDL_Rect src, dest; //These rectangles will describe the source and destination regions of our blit
//We must first initialize the SDL video component, and check for success
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//When this program exits, SDL_Quit must be called
atexit(SDL_Quit);
//Set the video mode to fullscreen 640x480 with 16bit colour and double-buffering
screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF | SDL_FULLSCREEN);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
//Load the bitmap into a temporary surface, and check for success
temp = SDL_LoadBMP("hello.bmp");
if (temp == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 2;
}
//Convert the surface to the appropriate display format
image = SDL_DisplayFormat(temp);
//Release the temporary surface
SDL_FreeSurface(temp);
//Construct the source rectangle for our blit
src.x = 0;
src.y = 0;
src.w = image->w; //Use image->w to display the entire width of the image
src.h = image->h; //Use image->h to display the entire height of the image
//Construct the destination rectangle for our blit
dest.x = 100; //Display the image at the (X,Y) coordinates (100,100)
dest.y = 100;
dest.w = image->w; //Ensure the destination is large enough for the image's entire width/height
dest.h = image->h;
//Blit the image to the backbuffer
SDL_BlitSurface(image, &src, screen, &dest);
//Flip the backbuffer to the primary
SDL_Flip(screen);
//Wait for 2500ms (2.5 seconds) so we can see the image
SDL_Delay(2500);
//Release the surface
SDL_FreeSurface(image);
//Return success!
return 0;
}
您正在加载位图。因此,如果您正在使用VisualStudio,有一个简单的修复方法。转到
添加新文件
,然后单击资源
。您可以在visual studio中创建位图文件。所以,复制位图,然后在VisualStudio上单击“编辑”,打开位图并单击“粘贴”。Boom done,在代码中将图像的名称更改为图像,这样就可以正常工作,因为没有链接问题。SDL_GetError()怎么说?您尝试过这个吗<代码>SDL_加载bmp(“c:\\hello.bmp”)注意这个双“\\”。哈哈,这个双“\\”对我很有用。我仍然不明白,当文件在应用程序目录中时,为什么它不能工作。