在 C++/SDL2 中显示 .bmp 图像 [英] Displaying an .bmp image in C++/SDL2

查看:52
本文介绍了在 C++/SDL2 中显示 .bmp 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been having trouble displaying an image in an SDL window and I am not quite sure what I am doing wrong. The code compiles just fine and the image has been placed in the debug folder with my .exe so I am not sure why it is not displaying. Is there something minor I may have missed?

#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;
}

推荐答案

如果您在调试器中逐步执行代码,并检查每一步都有返回值,你有没有得到意想不到的结果?比如空指针.这可能会帮助你缩小范围.

If you step through the code in a debugger, and inspect the return-values in each step, do you get something unexpected? Such as a nullpointer. That is probably going to help you narrow it down.

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

image has been placed in the debug folder with my .exe

如果从 IDE 运行,当前工作文件夹将成为项目文件夹.

If you run from the IDE, the current working folder is going to be the project folder.

这篇关于在 C++/SDL2 中显示 .bmp 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆