我只是在尝试一个教程,如:
- http://lazyfoo.net/SDL_tutorials/lesson03/windows/msvsnet0508e/index.php
- http://lazyfoo.net/SDL_tutorials/lesson02/index.php
并尝试.png
通过简单的代码片段在应用程序中加载和显示我的便携式网络图形()文件:
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include <stdio.h>
#include <string>
//The attributes of the screen
const int screen_width = 640;
const int screen_height = 480;
const int screen_bpp = 32;
//The surfaces that will be used
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *message = NULL;
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
SDL_RWops *rwop;
rwop=SDL_RWFromFile(filename.c_str(), "rb");
if(IMG_isPNG(rwop))
printf("%s is a PNG file.\n", filename.c_str());
else
printf("%s is not a PNG file, or PNG support is not available.\n", filename.c_str());
//Load the image using SDL_image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface *source_surface, SDL_Surface *destintion_Surface)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect rectangle;
//Give the offsets to the rectangle
rectangle.x = x;
rectangle.y = y;
//Blit the surface
SDL_BlitSurface(source_surface, NULL, destintion_Surface, &rectangle);
}
int main(int argc, char** argv)
{
//Initialize all SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
return 1;
//Set up the screen
screen = SDL_SetVideoMode(screen_width, screen_height, screen_bpp, SDL_SWSURFACE);
//If there was an error in setting up the screen
if(screen == NULL)
return 1;
//Set the window caption
SDL_WM_SetCaption("Surface Bliting", NULL);
//Load the images
background = load_image("cute2.png");
message = load_image("cute4.png");
//Apply the background to the screen
apply_surface(0, 0, background, screen);
apply_surface(320, 0, background, screen);
apply_surface(0, 240, background, screen);
apply_surface(320, 240, background, screen);
//Apply the message to the screen
apply_surface( 180, 140, message, screen );
//Update the screen
if(SDL_Flip(screen) == -1)
return 1;
SDL_Delay(12000);
SDL_FreeSurface(background);
SDL_FreeSurface(message);
//Quit SDL
SDL_Quit();
return 0;
}
现在在 Visual Stdio 2008 中,应用程序运行得非常好。
但是当我试图.exe
直接从我的应用程序运行时:
E:\SDL_sample\SDL Image Extension Libraries\Release\"SDL Image Extension Libraries.exe"
正在stdout.txt
显示消息:
cute2.png 不是 PNG 文件,或者 PNG 支持不可用。
cute4.png 不是 PNG 文件,或者 PNG 支持不可用。
并且窗口关闭,甚至没有显示/渲染任何东西。
我不明白当我在 Visual Studio 2008 中构建/运行.exe
应用程序时图像如何成功加载,但是当我运行时,图像没有加载,图像文件、dll 和所有东西在它们的位置都是一样的。