c++ - SDL2不使用纹理绘制图像

标签 c++ c image sdl sdl-2

我一直在尝试创建一个简单的 SDL2 程序来显示图像然后退出。我有这个代码:

/* compile with `gcc -lSDL2 -o main main.c` */

#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_timer.h>

int main(void){
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window * w = SDL_CreateWindow("Hi", 0, 0, 640, 480, 0);
    SDL_Renderer * r = SDL_CreateRenderer(w, -1, 0);

    SDL_Surface * s = SDL_LoadBMP("../test.bmp");
    SDL_Texture * t = SDL_CreateTextureFromSurface(r, s);
    SDL_FreeSurface(s);

    SDL_RenderClear(r);
    SDL_RenderCopy(r, t, 0, 0);
    SDL_RenderPresent(r);

    SDL_Delay(2000);

    SDL_DestroyTexture(t);
    SDL_DestroyRenderer(r);
    SDL_DestroyWindow(w);

    SDL_Quit();
}

我知道我省略了每个函数成功的正常检查 - 它们都成功了,为了便于阅读,它们被删除了。我也知道我使用了 0 而不是空指针或正确的枚举值,这也不是问题的原因(因为当我正确构造程序时会发生相同的问题,这是一个为测试而编写的快速测试用例最简单的情况)

目的是出现一个窗口并显示图像(肯定位于该目录中),等待几秒钟并退出。然而,结果是窗口显示正确,但窗口被黑色填充。

额外的注释SDL_ShowSimpleMessageBox()似乎工作正常。但我不知道这与框架的其余部分有何关系。

最佳答案

我会清楚这一点,您想要制作纹理,直接执行它以方便控制,另外这可以让您更好地控制图像,尝试使用此代码,经过全面测试并工作,您想要的只是为了窗口显示图像并在 2 秒内关闭,对吧?如果图像未加载,则表明这是您图像的位置。

/* compile with `gcc -lSDL2 -o main main.c` */

#include <SDL.h>
#include <SDL_image.h>  
#include <iostream> //I included it since I used cout


int main(int argc, char* argv[]){
bool off = false;
int time;

SDL_Init(SDL_INIT_VIDEO);
SDL_Window * w = SDL_CreateWindow("Hi", 0, 0, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer * r = SDL_CreateRenderer(w, -1, SDL_RENDERER_ACCELERATED);

SDL_Texture * s = NULL;
s = IMG_LoadTexture(r, "../test.bmp"); // LOADS A TEXTURE DIRECTLY FROM THE IMAGE
if (s == NULL)
{
cout << "FAILED TO FIND THE IMAGE" << endl; //we did this to check if IMG_LoadTexture found the image, if it showed this message in the cmd window (the black system-like one) then it means that the image can't be found
}
SDL_Rect s_rect; // CREATES THE IMAGE'S SPECS
s_rect.x = 100;    // just like the window, the x and y values determine it's displacement from the origin which is the top left of your window
s_rect.y = 100;
s_rect.w = 640; //width of the texture
s_rect.h = 480; // height of the texture

time = SDL_GetTicks(); //Gets the current time

while (time + 2000 < SDL_GetTicks()) //adds 2 seconds to the past time you got and waits for the present time to surpass that
{
SDL_RenderClear(r);
SDL_RenderCopy(r, s, NULL, &s_rect); // THE NULL IS THE AREA YOU COULD USE TO CROP THE IMAGE
SDL_RenderPresent(r);

}

SDL_DestroyTexture(s);
SDL_DestroyRenderer(r);
SDL_DestroyWindow(w);

return 0;   //returns 0, closes the program.
}

如果你想在窗口上看到一个关闭按钮并希望它生效,那么创建一个事件,然后将其添加到 while 区域以检查它是否等于 SDL_Quit();,我没有包含它,因为你希望它在 2 秒内立即关闭,这样关闭按钮就没用了。

快乐编码:D

关于c++ - SDL2不使用纹理绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22627779/

相关文章:

c++ - 获取类成员变量的内存地址(非动态)

双向循环链表删除节点的正确方法

python - 导入天气快照/图像

c++ - Windows 上的命名管道

c++ - 生长球体的交集

c++ - GDB 无法显示 Boost uBLAS 矩阵?

C程序在运行时崩溃

c - 在这个对矩阵进行不同操作的程序中,如果没有产生正确的输出,则 saddlepoint() 函数。为什么?

c# - 如何将 48bpp 图像的原始数据加载到位图中?

java - 缓存数据库中存在的图像