how to load images in sdl to a surface in c using IMG_Load?

1.2k Views Asked by At

When I run

SDL_Surface* surface  = IMG_Load("*image location*");

an error pops up saying

initialization of 'SDL_Surface *' {aka 'struct SDL_Surface *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]

I don't know why this happening because shouldn't IMG_load be returning a pointer

https://www.libsdl.org/projects/SDL_image/docs/SDL_image_11.html

here is the rest of the code

#include <stdio.h>
#include <windows.h>

#include <SDL2\SDL.h>

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    int result = SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window *window = SDL_CreateWindow("bruh",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,500,500,0);
    SDL_Renderer *rend  =  SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_Surface* surface  = IMG_Load("*image location*");
    SDL_Texture* texture = SDL_CreateTextureFromSurface(rend,surface);
    SDL_FreeSurface(surface);
    SDL_RenderClear(rend);
    SDL_RenderCopy(rend,texture,NULL,NULL);
    SDL_RenderPresent(rend);
    SDL_Delay(5000);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0; 
}

how do I fix this error?

1

There are 1 best solutions below

0
On

IMG_Load belongs to separate libary SDL_image. This function is declared in SDL_image.h, which you didn't include. Add that include and link with appropriate library. In C undeclared functions are allowed via implicit declarations, and implicitly declared function returns int, that's where your error comes from.

Sane compiler will issue a warning about implicit declaration. Warnings are there for a reason, read them and don't suppress warnings unless you're absolutely certain.