Question

i am trying to learn SDL2 from lazyfoo.net and i have quite a few problems (i've also been learning c++ since January so i do know some of what is going on, but not all of it), the first and most obvious one is that the "Hello world" appears only when i start without debugging, if i do debug it gives me "the program can't start because SDL2.dll is missing from your computer". which is not true at all. i put the .dll file into my current project's folder (which i have called TEST), the site just told me to put it in the same area as the .vxcproj file is, which is what i did...

also the tutorial tells me to type #include <SDL.h>, and when i do it says a lot of the stuff is undefined (i just copied and pasted this directly from the zip file at the bottom of the tutorial page), so i have to use #include <SDL2/SDL.h>

and the third one (that i managed to fix, sorta) is when putting the image onto the screen, it showed that it could not find the .bmp file, so i had to get the file into the source files section of visual studio. it works for this test but if i start incorporating multiple .bmp files it might become confusing. was i supposed to do that?

here is my code, which is almost identical to the one in the zip file

#include <SDL2/SDL.h> //first modification i did
#include <stdio.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

bool init();
bool loadMedia();
void close();

SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;

bool init()
{
    bool success = true;

    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    }
    else
    {
        gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == NULL)
        {
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        {
            gScreenSurface = SDL_GetWindowSurface(gWindow);
        }
    }
    return success;
}

bool loadMedia()
{
    bool success = true;

    gHelloWorld = SDL_LoadBMP("hello_world.bmp"); //second modification
    if (gHelloWorld == NULL)
    {
        printf("Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError());
        success = false;
    }
    return success;
}
void close()
{
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;

    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    SDL_Quit();
}
int main(int argc, char* args[])
{
    if (!init())
    {
        printf("Failed to initialize!\n");
    }
    else
    {
        if (!loadMedia())
        {
            printf("Failed to load media!\n");
        }
        else
        {

            SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
            SDL_UpdateWindowSurface(gWindow);
            SDL_Delay(2000);
        }
    }
    close();

    return 0;
}
Was it helpful?

Solution

HMMM Your questions:

1

I'm also bothered, it would be better to use #including , and it would be better if you put those stuffs into the header file like stdafx or the header of your class to make it less confusing, there's a big chance you messed up on integrating your directories + your .dlls are not in a good place.

2

You're supposed to put all the dlls into the main project's folder's debug, the area where you see an exe, .pdb, and .ilk.

3

You used "hello_world.bmp", there's nothing wrong with that, it's your file's location, if you're going to run debug then place the files (the .bmp) inside... let's say you made a project named ABC, and placed it in a folder called ABC, there would be 3 folders called DEBUG (if you ran debug at least once), ipch, and ABC.

you're supposed to place the files inside the ABC folder which contains the .vcxproj, and other stuffs, then if you wanted to run your game using the .exe file in the mainfolder's debug folder then you're supposed to copy all the files you used (in this case, you used the .bmp) and paste them in that folder, the folder that contains the .exe, that way if you run debug, it would read in the ABC folder while if you personally go to the .exe file, it would read the files that are with it in it's folder.

OTHER TIPS

@Skalli he clearly stated Visual Studio which means he is running Windows,

please when incorporating images, place them at the same directory as the exe you built when using Release mode. Look at the SDL library files, go to bin and copy the SDL2.dll and place it where your EXE is. Also to be able to run the program from the IDE, go to your Program Files x86 folder and go to your IDE folder, should be called "Microsoft Visual Studio XX" and go to the bin and paste the SDL2.dll

comment on my answer if the problem persists

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top