Search code examples
c++sdl

C++ SDL2 can't load any file


I'm using SLD2 in order to make a game, All was working perfectly since now. I think there is a problem the loading file system. for example, when i tried to load a bmp :

#include <iostream>
# include "SDL.h"

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);  // Initialize SDL2
    SDL_Window* window = SDL_CreateWindow("game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                          1000, 500, SDL_WINDOW_OPENGL);

    SDL_Renderer* render = SDL_CreateRenderer(window, -1, 0);

    SDL_Surface* image = SDL_LoadBMP("../background.bmp");
    printf(SDL_GetError());

    return 0;
}

the SLD_GetError() function return : couldn't load ( some random weird chars) and it's the same for SDL mixer

it seems that no file (or any type of file) can be load by any SDL2 features... Why ?

(the exe is in a subfolder, so the background path is correct) here is a screenshot


Solution

  • There is nothing wrong with SDL2. The path you are entering is probably invalid because it says "couldn't load ( some random weird chars )", that means SDL couldn't find the file. You are using ../ to indicate that your background.bmp file is in the parent directory of the current working directory. If you are using an IDE and then build it, SDL expects the file to be in the parent directory of your project folder. If you then run the executable, it expects the file to be in the parent directory of your executable. So try using an absolute path, for example, C:\users\path-to-file\background.bmp. If you use that path system, SDL always search for the background.bmp in the given path no matter where your executable is located or where you built the program.

    Also be aware of escape sequences. use \\ for backslash. Or you can just use /.