在 C++/SDL2 中显示.bmp图像



我在 SDL 窗口中显示图像时遇到问题,我不太确定我做错了什么。代码编译得很好,图像已与我的.exe一起放置在调试文件夹中,所以我不确定为什么它不显示。我可能错过了什么小事吗?

#include <iostream>
#include <stdio.h>
#include <SDL.h>
#undef main
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
int main(int argc, char* args[]) {
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, 
SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
SDL_Surface* image = SDL_LoadBMP("image.bmp");
SDL_Renderer* render = SDL_CreateRenderer(window, -1, 0);
SDL_Texture* texture1 = SDL_CreateTextureFromSurface(render, image);
SDL_RenderCopy(render, texture1, NULL, NULL);
SDL_RenderPresent(render);
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
SDL_DestroyTexture(texture1);
SDL_DestroyRenderer(render);
SDL_FreeSurface(image);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

如果在调试器中单步执行代码,并检查每一步的返回值,你得到什么意想不到的东西吗?例如空指针。这可能会对你有所帮助。缩小范围。

图像已与我的.exe一起放置在调试文件夹中

如果从 IDE 运行,则当前工作文件夹将转到作为项目文件夹。

最新更新