Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL_LoadBMP only returning NULL

I've been following this tutorial, and I've got to the point where we are instructed how to load and use bitmaps. Here is the current code:

#include "SDL/SDL.h"
#include <stdlib.h>

int main (int arg, char *argc[]) {
    SDL_Surface* screen = NULL;
    SDL_Surface* hello = NULL;

    SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode (256, 256, 32, SDL_SWSURFACE);
    hello = SDL_LoadBMP("hello world.png"); // Here
    if (hello == NULL) exit(0);

    SDL_BlitSurface(hello, NULL, screen, NULL);
    SDL_Flip(screen);

    SDL_Delay(2000);

    SDL_FreeSurface(hello);

    SDL_Quit();

    return 0;
}

hello, however, never gets any value other than NULL. I'm using Code::Blocks, and even if I scatter hello world.png through all possible directories of the project (be it inside bin, obj, either of the Debug's, the directory with the .cbp) or specify the whole path to the image in-code (as in SDL_LoadBMP("C:\Dir\hello world.png")) hello will get NULL.

What am I doing wrong?

OS is Windows


EDIT: Alright, apparently SDL_LoadBMP can only load .bmp files! How silly of me.

like image 664
Mutoh Avatar asked Jul 18 '26 16:07

Mutoh


1 Answers

Edit: I mixed SDL_LoadBMP with IMG_Load, here's my new answer:

You can use SDL Image (include SDL_Image.h, link SDL_image.lib and make sure that the correct DLL for you file type is with your binary, if you need one) to call IMG_Load. IMG_Load will take care to resolve your file type and use the appropriate loaded to create a new SDL Surface. Remember to Free your surface when you're done with it.

like image 96
emartel Avatar answered Jul 20 '26 07:07

emartel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!