Question

Hi there fellow Overflowers! I have recently begun learning SDL. I chose simple directmedia layer as my external API to my C++ knowledge because I found it to offer the most visually enhanced mechanics for game dev. Consider this code below:

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

using std::cerr;
using std::endl;

int main(int argc, char* args[])
{
// Initialize the SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
    cerr << "SDL_Init() Failed: " << SDL_GetError() << endl;
    exit(1);
}

// Set the video mode
SDL_Surface* display;
display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (display == NULL)
{
    cerr << "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl;
    exit(1);
}

// Set the title bar
SDL_WM_SetCaption("SDL Tutorial", "SDL Tutorial");

// Load the image
SDL_Surface* image;
image = SDL_LoadBMP("LAND.bmp");
if (image == NULL)
{
    cerr << "SDL_LoadBMP() Failed: " << SDL_GetError() << endl;
    exit(1);
}

// Main loop
SDL_Event event;
while(1)
{
    // Check for messages
    if (SDL_PollEvent(&event))
    {
        // Check for the quit message
        if (event.type == SDL_QUIT)
        {
            // Quit the program
            break;
        }
    }
    // Game loop will go here...
    // Apply the image to the display
    if (SDL_BlitSurface(image, NULL, display, NULL) != 0)
    {
        cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
        exit(1);
    }

    //Update the display
    SDL_Flip(display);

}

// Tell the SDL to clean up and shut down
SDL_Quit();

return 0;    
}

All I have done is just made a screen surface, Double buffered it, made another surface of an image, blit'ed the two together, and for some reason when I build the application, It closes instantly! The application build succeeds but then closes without a window opening! This is really frustrating.

I am using XCode5 and SDL 2.0.3 :) Help is needed!

EDIT: Turns out in the error log, it says SDL_LoadBMP(): Failed to load LAND.bmp. The bmp is saved in the root directory, the same folder as the main.cpp folder? Why doesn't this work?

Was it helpful?

Solution

You should be able to test your code by using the absolute (full) path to the image. That will verify that the code is actually working.

To be able to use resources with an absolute path you should create a Build Phase to Copy Files. The Destination should be set to 'Product Directory'. You can leave Subpath blank or provide a directory to place the resource in (this will be useful when you get a lot of resources) eg textures. If you supply a Subpath then alter your code so it would be textures/LAND.bmp

You would also use the build phase for packaging the SDL2.framework and any others e.g. SDL2_image etc with your final application. This would allow users who don't have SDL on their machines to run the app. To do this create another build phase with the Detination set to 'Frameworks' and leave the Subpath empty. Then just add any frameworks you want to package with the app. One other setting you will want to make in Build Settings is to change 'Runpath Search Paths' (found under 'Linking') to be @executeable_path/../Frameworks so that the application knows where to find packaged frameworks

I have a tutorial on configuring SDL2 in Xcode along with a template to make it quick

http://zamma.co.uk/how-to-setup-sdl2-in-xcode-osx/

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