在 C++/SDL2 中显示 .bmp 图像
Displaying an .bmp image in C++/SDL2
我一直无法在 SDL 中显示图像 window,我不太确定我做错了什么。代码编译得很好,图像已经和我的 .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;
}
如果您在调试器中单步执行代码,并检查
return-每一步的值,你有没有得到意想不到的东西?
比如空指针。这可能会对你有所帮助
缩小范围。
image has been placed in the debug folder with my .exe
如果您 运行 来自 IDE,则当前工作文件夹将
成为项目文件夹。
我一直无法在 SDL 中显示图像 window,我不太确定我做错了什么。代码编译得很好,图像已经和我的 .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;
}
如果您在调试器中单步执行代码,并检查 return-每一步的值,你有没有得到意想不到的东西? 比如空指针。这可能会对你有所帮助 缩小范围。
image has been placed in the debug folder with my .exe
如果您 运行 来自 IDE,则当前工作文件夹将 成为项目文件夹。