【问题标题】:Having trouble building SDL Project构建 SDL 项目时遇到问题
【发布时间】: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 ,它仍然什么都不做。我实际上为图像添加了一个带有新目录的构建阶段,并以这种方式放置它们:理论上,这应该都可以工作。

标签: c++ xcode macos build sdl


【解决方案1】:

我在您的代码中发现了一些问题。它们可能不都是错误。

1) 当您对图像进行 blit 时,它不会直接进入屏幕。它保存在缓冲区中,直到您调用 SDL_Flip(screen);

所以最后几行应该是这样的

...
apply_surface( 320, 240, background, screen );
SDL_Flip(screen);//Display the results of the blits
SDL_Delay(5000);
SDL_Quit();
return 0;

2) 对图像进行 blit 时不要使用 Rect offset。它将始终显示在 0,0 位置。

//temporary rectangle to hold offsets
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, NULL);

3) 您永远不会释放您加载的图像。您需要在 SDL_Quit 之前添加一些 SDL_FreeSurface(...);

4) 你加载但从不使用hello.bmp

另外,我不明白您为什么将表面声明为全局。但也许你有充分的理由。

你也可以尝试用

替换你的SDL_Delay(5000);
bool done = false;
while (!done)//we wait until done == true
{

    SDL_Event event;
    while (SDL_WaitEvent(&event))//we get all the events
    {
        switch (event.type) //we look what type of event we received
        {
        case SDL_QUIT:  //We received a quit event
            done = true;//we want to exit the loop
        break;
        }
    }
}

因为 SDL_Delay 告诉您的计算机,您的应用程序在一定时间内不必更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多