【问题标题】:C++/SDL need help getting an image on screenC++/SDL 需要帮助在屏幕上显示图像
【发布时间】:2014-12-05 20:10:54
【问题描述】:

我似乎无法将图像加载到 SDL 窗口中。

代码:

#include <iostream>
#include <SDL2/SDL.h>
#include <stdio.h>
using namespace std;

SDL_Window *window;
SDL_Surface *imageSurface;
SDL_Surface *image;

void createWindow() {

    SDL_INIT_EVERYTHING;

    if(SDL_INIT_EVERYTHING <0) {
       cout << "SDL failed to initialize!" << endl;
    }

    window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,0);

    if (window==NULL) {
        cout << "Window could not be made" << endl;
    }

    imageSurface = SDL_GetWindowSurface(window);
}

void close() {
    SDL_FreeSurface(image);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

void loadmedia() {

    image = SDL_LoadBMP("Test.bmp");

    if (image==NULL) {
        cout << "Image could not be loaded" << endl;
    }
}

int main() {

    createWindow();
    loadmedia();

    SDL_BlitSurface(image, NULL, imageSurface, NULL);
    SDL_UpdateWindowSurface(window);
    SDL_Delay(10000);

    close();
}

【问题讨论】:

  • Sooo,到底发生了什么? “似乎没有任何效果”并不是对您问题的真正有用描述
  • @unholysheep 窗口出现但保持空白
  • 请把相关代码也贴在这里。
  • 期望我们检查您的所有代码是不合理的。在没有具体说明您希望它做什么以及它正在做什么或没有做什么的情况下提出这个问题更加不合理。在此处发布代码的基本部分(作为编辑,并保留链接以供参考),并提供Minimal, Complete, and Verifiable example
  • 好的修复它,由于某种原因,xcode 无法识别文件名,所以我在图像加载函数中切换到绝对路径并且一切正常,如果你是 SDL 的初学者,我建议你不要遵循懒惰的 foos教程,直到您更有经验,因为它们针对有经验的 sdl 开发人员

标签: c++ sdl sdl-2


【解决方案1】:

您缺少消息循环。 基本上,您的应用程序正在接收消息以处理诸如绘制窗口等操作。

你缺少的是这样的:

while (!done) {
  while (SDL_PollEvent(&event)) {
    if (event.type == SDL_QUIT)
      done = true;
  }
}

您可以在此处找到示例:http://www.gamedev.net/topic/376858-a-proper-sdl-message-loop/

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 2015-08-06
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多