Why am I failing to initialize SDL_image? - c

This is an updated post. Someone asked me to do a minimal reproducible example, which did help a lot. This program opens a window, but does not display a PNG image as I would like it to do.
The compiler gives me no errors, however my error message for SDL_Init does.
What could be the reason that I am failing to initialize 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;
}
Output:
SDL_image could not initialize
build command:
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
I have tried changing the SDL_Init flags. I even tried to change the IMG_Init flag to IMG_INIT_JPG (and testing with a .jpg image of course), but no luck.

There are two issues in this example:
IMG_Init() returns a bitmask of all the currently initted image loaders instead of 0 on success (this is why you get the error message)
You do not call SDL_RenderPresent() after drawing on the renderer (this is why you can't see anything)
Here is an example on how to init SDL_Image from the SDL2_image documentation:
// 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
}
And to make the image visible add this to the end of the loop:
SDL_RenderPresent(renderer);

Related

Sdl2 window is either transparent or rendering whats behind it on arch linux

The code looks like this:
#include"global.h"
static const int width = 800;
static const int height = 600;
int main (int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO);
// Create the window
SDL_Window *window = SDL_CreateWindow("Ferengine",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,width,height, SDL_WINDOW_OPENGL);
// renderer
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// settings
SDL_SetRenderDrawColor(renderer,255,0,0,255);
SDL_SetWindowOpacity(window,1.0f);
// loop
bool running = true;
SDL_Event event;
while(running)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
// release resources
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
global.h looks like this:
// SDL
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
my make file looks like this
CC=gcc
S_DIR=Source
B_DIR=Build
build:
$(CC) \
-o \
$(B_DIR)/Ferengine \
$(S_DIR)/main.c \
-lSDL2
clean:
rm -f $(B_DIR)/*
file structure:
main folder
Makefile
source | build/executable
source/main.c | source/global.h
when I run make and then run the executable either the window is rendering whats behind it or it is transparent I saw another question like this but its answer did not work. I am using sdl2 version 2.0.22-2.
I read the tutorial wrong it should be
while(running)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}

C SDL2 Error: No hardware accelerated renderers available

I'm trying to create a pong game with sdl2 in c. I'm able to make it work in a laptop running UBUNTU 20.04 (gtx 1650 mobile) with nvidia driver 470. But running it on the same setup in ubuntu 21.10 with nvidia 470 on a different laptop (rtx 3060 mobile) gives me this error.
Here's the code (stripped off any game logic to only part where it crashes).
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "SDL2/SDL.h"
const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 800;
int main(int argc, char** argv)
{
int sdl_init_status = SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER);
if (sdl_init_status != 0)
{
printf("Initialization failed. Error: %s \n", SDL_GetError());
return 1;
}
SDL_Window* window = SDL_CreateWindow("PONG", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);
if (!window)
{
printf("Window initialization error: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
puts("Window created");
// // create renderer
Uint32 rflags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, rflags);
if (!renderer)
{
printf("renderer initialization error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
puts("Renderer created");
SDL_Surface* screen_surface = SDL_GetWindowSurface(window);
if (!screen_surface)
{
printf("surface initialization error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
puts("Surface created");
SDL_Delay(1000);
// cleanup and quit
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
This is the output I get:
gcc -std=c99 -g3 -Wall -o1 -o main main.c `sdl2-config --libs --cflags` -lm -lSDL2_image
./main
Window created
Renderer created
surface initialization error: No hardware accelerated renderers available
make: *** [Makefile:10: test] Error 1
You have
Uint32 rflags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, rflags);
Your error states No hardware accelerated renderers available.
The documentation for SDL_CreateRenderer refers 4 flags you can use.
I was having this issue on Android devices and solved with replacing the flags in SDL_CreateRenderer with only SDL_RENDERER_SOFTWARE. So use
Uint32 rflags = SDL_RENDERER_SOFTWARE;
instead.

Problem with SDL2 not running after compiling successfully

I'm having trouble running an SDL2 program, I compiled the program successfully using this command.
gcc -g main.c -o main.exe -IC:\\SDL2\\i686-w64-mingw32\\include -LC:\\SDL2\\i686-w64-mingw32\\lib -lmingw32 -lSDL2main -lSDL2 -Wall -Werror
Now that I'm trying to run it, I expect to get a window and to log hello in the console but nothing happens and the program exits immediately.
I'm using windows 10 and using the MinGw compiler.
This is the code.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
static const int width = 800;
static const int height = 600;
int main(int argc, char **argv)
{
SDL_Log("hello");
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
bool running = true;
SDL_Event event;
while(running)
{
// Process events
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
running = false;
}
}
// Clear screen
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

Basic C SDL2 program won't work with TCC while it does work with GCC (Linux)

I have the following C code (for clarity, I know it's not complete code and it should handle events and stuff like that):
#!/usr/bin/tcc -run -L/usr/lib/x86_64-linux-gnu -D_REENTRANT -DSDL_MAIN_HANDLED -I/usr/include/SDL2 -lSDL2 -lGL -lGLEW
#include <GL/glew.h>
#include <SDL.h>
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window *win = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext glContext = SDL_GL_CreateContext(win);
while (1)
{
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(win);
}
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(win);
return 0;
}
If I compile this with GCC (without the shebang line), and start the executable, it works fine, but I like the simplicity of the compile and run functionality of TCC and its speed.
However, the compiled executable gives me the following console output and then hangs forever and can't even be killed with CTRL-C:
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
I added the SDL_MAIN_HANDLED as suggested here but that didn't change anything.
Anyone?
How about proper handling of the possible errors of the SDL calls and also initializing glew?
#include <stdlib.h>
#include <SDL.h>
#include <GL/glew.h>
void exit_sdl_error(const char *message) {
SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "%s failed with SDL error %s", message, SDL_GetError());
exit(EXIT_FAILURE);
}
int main() {
const Uint32 init_flags = SDL_INIT_VIDEO;
const int gl_major = 4, gl_minor = 0;
if (!SDL_WasInit(init_flags)) {
if (SDL_Init(init_flags) != 0)
exit_sdl_error("Unable to initialize SDL");
if (SDL_InitSubSystem(init_flags) != 0)
exit_sdl_error("Unable to initialize sub system of SDL");
}
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, gl_major))
exit_sdl_error("Unable to set gl major version");
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_minor))
exit_sdl_error("Unable to set gl minor version");
if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE))
exit_sdl_error("Unable to set gl profile");
SDL_Window *window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!window) {
SDL_QuitSubSystem(init_flags);
exit_sdl_error("Unable to create window");
}
SDL_GLContext glContext = SDL_GL_CreateContext(window);
if (!glContext) {
SDL_DestroyWindow(window);
SDL_QuitSubSystem(init_flags);
exit_sdl_error("Unable to create context");
}
glewInit();
while (1)
{
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_QuitSubSystem(init_flags);
}

SDL2 Window lacking window bar

I'm trying to build an SDL2 app on OSX El Capitan, and I am running into an issue where the window dressing is not appearing. Things like the exit button and the resizing bar are not appearing. I've compiled on Windows and it all works fine there.
To make it a bit easier this link to Lazy Foo's tutorial replicates the issue: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php
My make file is pretty simple (I've stolen it off his website as well)
COMPILER_FLAGS = -w
LINKER_FLAGS = -framework SDL2
#OBJS specifies which files to compile as part of the project
OBJS = main.c
#CC specifies which compiler we're using
CC = g++
OBJ_NAME = main
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
If anyone knows what I am doing wrong, I'd love to find out.
UPDATE: I pulled out an old laptop, fresh installed to El Capitan and it is having the same issue based on the Lazy Foo code.
UPDATE:
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Destroy window
SDL_DestroyWindow( window );
//Quit SDL subsystems
SDL_Quit();
return 0;
}
Not sure of the root cause but making a loop that polls events seems to do the trick.
bool loop = true;
while(loop)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
loop = false;
}
}
}

Resources