【发布时间】:2015-03-11 10:00:19
【问题描述】:
我目前正在学习如何使用 SDL 2。使用 Lazy Foo 的 SDL2 教程,显示为 here,我创建了一个脚本,该脚本应在关闭程序之前显示 2 秒钟的图像。这是脚本:
#include <SDL.h>
#include <stdio.h>
//Screen dimensions
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
SDL_Surface* surfaceImage = NULL;
bool init();
bool loadMedia();
void close();
bool init()
{
bool success = true;
//initializes
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else{
//creates the window
window = SDL_CreateWindow("Testing!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if( window = NULL )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
bool loadMedia()
{
bool success = true;
surfaceImage = SDL_LoadBMP( "test.bmp" );
if(surfaceImage = NULL)
{
printf(SDL_GetError());
success = false;
}
return success;
}
void close()
{
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main( int argc, char* args[] )
{
if(!init())
{
printf( "Failed to initialize!\n" );
}
else{
SDL_FillRect(screenSurface, NULL, ( screenSurface->format, 0xFF, 0xFF, 0xFF ));
if(!loadMedia())
{
printf(SDL_GetError());
}
else{
SDL_BlitSurface(surfaceImage, NULL, screenSurface, NULL);
SDL_UpdateWindowSurface(window);
}
SDL_Delay(2000);
close();
return 0;
}
return 0;
}
但是,图像未显示。没有出现错误,程序运行正常,除了图像不存在。我将 bmp 文件与 vcproj 文件放在同一目录中。代码有什么问题?
【问题讨论】: