Displaying an .bmp image in C++/SDL2

陌路散爱 提交于 2019-12-24 00:54:07

问题


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

回答1:


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.

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

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



来源:https://stackoverflow.com/questions/47216338/displaying-an-bmp-image-in-c-sdl2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!