无法在 CodeBlocks 中加载 SDL_LoadBMP 的 bmp 文件
can't load bmp file with SDL_LoadBMP in CodeBlocks
我正在学习 LazyFoo 的 SDL2.0 教程,使用 Code::Blocks 16.01。我无法使用 SDL_LoadBMP().
加载图像
这是我第一次在这里提问。我做了 Google 的答案并在此处搜索现有的 questions/answers 但无法解决问题。
这是我的代码:
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial - Lesson 02", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP( "/02_getting_an_image_on_the_screen/hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Free resources and close SDL
close();
return 0;
}
当我指定整个路径时,这段代码工作正常。但是有没有比总是指定整个路径更简单的方法呢?
我把图片放在哪里,如何指定它的路径?
删除 hello_world.bmp
中的 /02_getting_an_image_on_the_screen/
。
如果您不在 IDE.
范围内,则必须将文件放在项目文件夹中/与可执行文件位于同一目录中
我用 ../02_getting_an_image_on_the_screen/hello_world.bmp
替换了 /02_getting_an_image_on_the_screen/hello_world.bmp
,它也能正常工作。 :)
我回答的有点晚了,但这也是我在学习 LazyFoo 的 SDL 教程后发生的事情,这是第一个出现在 google 中的 Whosebug post主题所以这是我的解决方案:
在包含 .bmp 文件的文件夹前加一个点。
“。”是.cpp文件的当前目录:
//Load splash image
gHelloWorld = SDL_LoadBMP("./02_getting_an_image_on_the_screen/hello_world.bmp");
我正在学习 LazyFoo 的 SDL2.0 教程,使用 Code::Blocks 16.01。我无法使用 SDL_LoadBMP().
加载图像这是我第一次在这里提问。我做了 Google 的答案并在此处搜索现有的 questions/answers 但无法解决问题。
这是我的代码:
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial - Lesson 02", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP( "/02_getting_an_image_on_the_screen/hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//Update the surface
SDL_UpdateWindowSurface( gWindow );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Free resources and close SDL
close();
return 0;
}
当我指定整个路径时,这段代码工作正常。但是有没有比总是指定整个路径更简单的方法呢?
我把图片放在哪里,如何指定它的路径?
删除 hello_world.bmp
中的 /02_getting_an_image_on_the_screen/
。
如果您不在 IDE.
我用 ../02_getting_an_image_on_the_screen/hello_world.bmp
替换了 /02_getting_an_image_on_the_screen/hello_world.bmp
,它也能正常工作。 :)
我回答的有点晚了,但这也是我在学习 LazyFoo 的 SDL 教程后发生的事情,这是第一个出现在 google 中的 Whosebug post主题所以这是我的解决方案: 在包含 .bmp 文件的文件夹前加一个点。 “。”是.cpp文件的当前目录:
//Load splash image
gHelloWorld = SDL_LoadBMP("./02_getting_an_image_on_the_screen/hello_world.bmp");