为什么我无法初始化 SDL_image?
Why am I failing to initialize SDL_image?
这是更新后的 post。有人让我做一个最小的可复制示例,这确实有很大帮助。此程序会打开 window,但不会像我希望的那样显示 PNG 图像。
编译器没有给我任何错误,但是 SDL_Init 的错误消息却有。
我无法初始化的原因可能是什么 SDL_Image?
// std
#include <stdio.h>
// sdl
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main (int argc, char* argv[])
{
// ----- Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "SDL could not initialize\n");
return 1;
}
if (IMG_Init(IMG_INIT_PNG) != 0)
{
fprintf(stderr, "SDL_image could not initialize\n");
/********
I GET THIS ERROR
********/
}
// ----- Create window
SDL_Window* window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
0);
if (!window)
{
fprintf(stderr, "Error creating window.\n");
return 2;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface *image_surface = NULL;
image_surface = IMG_Load("button_randomize.png");
if(image_surface == NULL)
{
printf("Cannot find image\n"); // This line is not executed
SDL_Quit();
}
SDL_Texture *image_texture = SDL_CreateTextureFromSurface(renderer, image_surface);
SDL_FreeSurface(image_surface);
// ----- Main loop
int quit = 0;
while (quit == 0)
{
SDL_Event windowEvent;
while (SDL_PollEvent(&windowEvent))
{
if (windowEvent.type == SDL_QUIT)
{
quit = 1;
break;
}
}
SDL_Rect image_rect = {50, 50, 120, 32};
SDL_RenderCopy(renderer, image_texture, NULL, &image_rect);
}
// ----- Clean up
IMG_Quit();
SDL_Quit();
return 0;
}
输出:
SDL_image could not initialize
构建命令:
gcc -std=c11 -Wall -o obj/main.o -c src/main.c
gcc -std=c11 -Wall -o my_program obj/main.o -lSDL2 -lGL -lGLEW -lm -lSDL2_image
我试过更改 SDL_Init 标志。我什至尝试将 IMG_Init 标志更改为 IMG_INIT_JPG(当然还使用 .jpg 图像进行测试),但没有成功。
这个例子有两个问题:
- IMG_Init() returns 成功时所有当前启动的图像加载器的位掩码而不是 0(这就是您收到错误消息的原因)
- 你在渲染器上绘制后没有调用 SDL_RenderPresent()(这就是你什么都看不到的原因)
这是一个关于如何从 SDL2_image documentation 初始化 SDL_Image 的例子:
// load support for the JPG and PNG image formats
int flags = IMG_INIT_JPG | IMG_INIT_PNG;
int initted = IMG_Init(flags);
if ((initted & flags) != flags) {
printf("IMG_Init: Failed to init required jpg and png support!\n");
printf("IMG_Init: %s\n", IMG_GetError());
// handle error
}
为了使图像可见,将此添加到循环的末尾:
SDL_RenderPresent(renderer);
这是更新后的 post。有人让我做一个最小的可复制示例,这确实有很大帮助。此程序会打开 window,但不会像我希望的那样显示 PNG 图像。
编译器没有给我任何错误,但是 SDL_Init 的错误消息却有。
我无法初始化的原因可能是什么 SDL_Image?
// std
#include <stdio.h>
// sdl
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main (int argc, char* argv[])
{
// ----- Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
fprintf(stderr, "SDL could not initialize\n");
return 1;
}
if (IMG_Init(IMG_INIT_PNG) != 0)
{
fprintf(stderr, "SDL_image could not initialize\n");
/********
I GET THIS ERROR
********/
}
// ----- Create window
SDL_Window* window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
0);
if (!window)
{
fprintf(stderr, "Error creating window.\n");
return 2;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface *image_surface = NULL;
image_surface = IMG_Load("button_randomize.png");
if(image_surface == NULL)
{
printf("Cannot find image\n"); // This line is not executed
SDL_Quit();
}
SDL_Texture *image_texture = SDL_CreateTextureFromSurface(renderer, image_surface);
SDL_FreeSurface(image_surface);
// ----- Main loop
int quit = 0;
while (quit == 0)
{
SDL_Event windowEvent;
while (SDL_PollEvent(&windowEvent))
{
if (windowEvent.type == SDL_QUIT)
{
quit = 1;
break;
}
}
SDL_Rect image_rect = {50, 50, 120, 32};
SDL_RenderCopy(renderer, image_texture, NULL, &image_rect);
}
// ----- Clean up
IMG_Quit();
SDL_Quit();
return 0;
}
输出:
SDL_image could not initialize
构建命令:
gcc -std=c11 -Wall -o obj/main.o -c src/main.c
gcc -std=c11 -Wall -o my_program obj/main.o -lSDL2 -lGL -lGLEW -lm -lSDL2_image
我试过更改 SDL_Init 标志。我什至尝试将 IMG_Init 标志更改为 IMG_INIT_JPG(当然还使用 .jpg 图像进行测试),但没有成功。
这个例子有两个问题:
- IMG_Init() returns 成功时所有当前启动的图像加载器的位掩码而不是 0(这就是您收到错误消息的原因)
- 你在渲染器上绘制后没有调用 SDL_RenderPresent()(这就是你什么都看不到的原因)
这是一个关于如何从 SDL2_image documentation 初始化 SDL_Image 的例子:
// load support for the JPG and PNG image formats
int flags = IMG_INIT_JPG | IMG_INIT_PNG;
int initted = IMG_Init(flags);
if ((initted & flags) != flags) {
printf("IMG_Init: Failed to init required jpg and png support!\n");
printf("IMG_Init: %s\n", IMG_GetError());
// handle error
}
为了使图像可见,将此添加到循环的末尾:
SDL_RenderPresent(renderer);