【发布时间】:2014-06-12 17:05:46
【问题描述】:
我已经开始研究 SDL。我在使用基本程序时遇到问题,每次编译时都没有错误,但程序完全是空白的。构建成功,应用程序图标弹出到我的 Dock 中,但保持打开 5 秒钟,没有窗口。代码如下:
//
// main.cpp
// HelloSDL
//
// Created by Liam Tan
// Copyright 2014 Liam Tan. All rights reserved.
//
#include <stdlib.h>
#include <SDL/SDL.h>
#include <string>
#include <iostream>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//Declares surfaces
SDL_Surface* message = NULL;
SDL_Surface* background = NULL;
SDL_Surface* screen = NULL;
SDL_Surface *load_image( std::string filename) {
//declaration
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
//takes arguement and loads that BMP
loadedImage = SDL_LoadBMP(filename.c_str() );
//Checks, swaps opti with loaded then frees loaded
if (loadedImage!= NULL) {
optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
//temporary rectangle to hold offsets
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, NULL);
}
int SDL_main (int argc, char **argv) {
const std::string errorMsg = "failed to initialize. ";
//Initialise
if( SDL_Init(SDL_INIT_EVERYTHING) == -1 ) {
std::cout << "SDL " << errorMsg << std::endl;
SDL_GetError();
return 1;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL) {
std::cout << "Video mode " << errorMsg << std::endl;
SDL_GetError();
return 1;
}
SDL_WM_SetCaption("Programming", NULL);
//loads message
message = load_image("textures/hello.bmp");
//loads background
background = load_image("textures/background.bmp");
//blits 4 times!
apply_surface(0,0,background,screen);
apply_surface( 320, 0, background, screen );
apply_surface( 0, 240, background, screen );
apply_surface( 320, 240, background, screen );
SDL_Delay(5000);
SDL_Quit();
return 0;
}
我正在使用 Xcode 5 和 SDL 1.2。
【问题讨论】:
-
您确定 SDL 能找到您的图像吗?尝试在
if (loadedImage!= NULL){}之后添加和else并添加调试消息,以便您知道是否出现问题。我还建议学习 SDL2 而不是 SDL1.2。 SDL2 使用起来要好得多。例如,您不必使用SDL_BlitSurface()来渲染 -
@olevegard 我添加了一个 SDL_GetError else ,它仍然什么都不做。我实际上为图像添加了一个带有新目录的构建阶段,并以这种方式放置它们:理论上,这应该都可以工作。