1

so i've been trying out some super basic stuff with SDL 2 in Xcode. I can't quite figure out how to get the image file path correct. I've done some searching on the subject that led to advice for telling the build phase what files to copy over. That hasn't helped me though. Here's what I currently in terms of file structure & settings:

enter image description here

enter image description here

However, when i try to grab the file like so:

//
//  Image.h
//  SDLTest
//
//  Created by Aaron McLeod on 2013-10-17.
//  Copyright (c) 2013 Aaron McLeod. All rights reserved.
//

#ifndef SDLTest_Image_h
#define SDLTest_Image_h

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

class Image {
public:
    static SDL_Texture* load_image(const char * filename, SDL_Renderer* renderer) {
        SDL_Surface* loaded_image = nullptr;
        SDL_Texture* texture = nullptr;

        loaded_image = SDL_LoadBMP(filename);
        if(loaded_image != nullptr) {
            texture = SDL_CreateTextureFromSurface(renderer, loaded_image);
            SDL_FreeSurface(loaded_image);
            std::cout << "loaded image" << std::endl;
        }
        else {
            std::cout << SDL_GetError() << std::endl;
        }

        return texture;
    }
};

#endif

The else condition gets hit, and i see the error that it could not find the file. The string i pass is "resources/paddle.png"

Martin G
  • 17,357
  • 9
  • 82
  • 98
agmcleod
  • 13,321
  • 13
  • 57
  • 96

1 Answers1

1

You're trying to load a PNG file from SDL_LoadBMP(). That's not going to work because SDL_LoadBMP() can load only BMP files.

If you want to load PNG files in an app that uses SDL2, use the SDL2_image framework. The Mac version of SDL2_image is set to look for image files in the Resources folder of the app bundle. Place your image files in the Copy Bundle Resources build phase, and SDL2_image should be able to load the files. You won't need the Copy Files build phase shown in your screenshot.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • The bitmap thing makes sense, grabbed SDL2_image. So i renamed the folder in my screenshot to be capitalized `Resources` to be safe. Added the build phase to copy bundle resources. However, the path is still not resolving. I've tried both `"resources/paddle.png"` and`"paddle.png"`. Any ideas? Thanks again :) – agmcleod Oct 22 '13 at 03:05
  • Looking at your project navigator screenshot, it looks like you created a command-line project that doesn't generate an app bundle. I recommend creating a Cocoa application project, removing the .h, .m, and .xib files, and adding the SDL frameworks, source code files, and image files to the project. Cocoa projects generate an app bundle. With a proper app bundle and the paddle.png file in the Copy Bundle Resources build phase, you should be able to load the file as paddle.png. – Swift Dev Journal Oct 22 '13 at 04:12