-1
嘗試將圖像加載到SDL_Surface上。然而,表面總是黑色,儘管bmp顯然不是。SDL_LoadBMP()在引用表面時顯示黑色表面
注意
這是一個工作SDL_Window和創建面指針成功,什麼是成功加載其位於代碼:: Blocks的項目文件夾「Kassadin.bmp」。它顯示黑色表面。
在將此標記爲重複項之前,有關此特定問題的所有答案都未解決此問題。
#include <iostream>
#include <SDL.h>
#include <stdio.h>
using namespace std;
const int SCREEN_WIDTH = 700;
const int SCREEN_HEIGHT = SCREEN_WIDTH/12 * 9;
//Create an SDL_Window pointer
SDL_Window* window = NULL;
//Create an SDL_Surface pointer
SDL_Surface* surface = NULL;
//SDL_Surface for an image
SDL_Surface* imgSurface = NULL;
bool init(){
//try init SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0){
cout << "Failed init SDL" << endl;
return false;
}else{
//create window title x pos y pos width height flags
//This doesnt include a surface ie it will be a plain window
window = SDL_CreateWindow("An SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
}
if(window == NULL){
cout << "Failed creating SDL window" << endl;
return false;
}else{
// creates surface with an SDL_Window object
surface = SDL_GetWindowSurface(window);
}
return true;
}
bool loadMedia(){
imgSurface = SDL_LoadBMP("Kassadin.bmp");
return true;
}
void close(){
//Sets the SDL_Window pointer to NULL again
SDL_DestroyWindow(window);
window = NULL;
SDL_FreeSurface (imgSurface);
imgSurface = NULL;
//quits
SDL_Quit();
}
int main(int argc, char* argv[]){
if(!init()){
cout << "error init sdl" << endl;
}else{
if(!loadMedia){
cout << "Trouble loading media..." << endl;
}else{
SDL_BlitSurface(imgSurface, NULL, surface, NULL);
}
}
//This is so it's not just a static window ie it can update
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
close();
return 0;
}
固定,語法錯誤,但它仍然返回blac – felix9801
無法重現。在blitting之前輸出'imgSurface'值和'SDL_GetError()'(或者在調試器中檢查)。事件循環會很好,但它似乎並不是問題,儘管如果指針是非空的並且沒有錯誤,我會通過刷新事件隊列開始。 – keltar