Need a bit of porting help

Member
Posts: 156
Joined: 2002.11
Post: #31
You can read files with fopen, but it's convenient to convert file paths to CFURLRefs and use those instead. Also, you can open the application's bundle and get the CFURLs to all .jpg files stored on a certain folder, for example.

Code:
void LoadMainBundleResources()
{
    //----------------------------------------------------------------------
    // Get the main bundle for the app:
    
    //CFBundleRef    gMainBundle_ref;
    
    gMainBundle_ref = CFBundleGetMainBundle();

    //----------------------------------------------------------------------
    // Look for the bundle’s version number:

    UInt32      bundleVersion;
    
    bundleVersion = CFBundleGetVersionNumber( gMainBundle_ref );

    //----------------------------------------------------------------------
    // Check the bundle version for compatibility with our app:

    if ( bundleVersion < kMyBundleVersion1 )
    {
        FatalAlert( "Error on LoadMainBundleResources(): CFBundleGetVersionNumber() bundle version too old", -1 );    
    }

    //----------------------------------------------------------------------
    
    RetrieveMainBundleInfo();
}

//----------------------------------------------------------------------

void RetrieveMainBundleInfo()
{
    //----------------------------------------------------------------------
    // Retrieving info from the bundle (get an instance of the info.plist):
    // Use CFBundleCopyInfoDictionaryInDirectory() if you don't want to open the bundle:

    CFDictionaryRef    bundleInfo_CFDictionaryRef;

    bundleInfo_CFDictionaryRef = CFBundleGetInfoDictionary( gMainBundle_ref );

    //----------------------------------------------------------------------
    // If we succeeded, look for our property:

    if ( bundleInfo_CFDictionaryRef != NULL )
    {
        CFStringRef    applicationID_CFStringRef;

        applicationID_CFStringRef = CFDictionaryGetValue( bundleInfo_CFDictionaryRef, CFSTR( "CFBundleIdentifier" ) );

        // CFRelease( bundleInfo_CFDictionaryRef ); if you release this from memory before you open windows and stuff, the app will crash then.
        // Only release CF Objects that you own, i.e. you've created with a function that has Create or Copy in its name.

    CFShow( applicationID_CFStringRef );    // Debug tool;
    }    
}

//----------------------------------------------------------------------

void ReleaseMainBundleResources()
{
    //----------------------------------------------------------------------
    // Always remember to release any CF Objects created/copied or returned as a value by a function:

    CFRelease( gMainBundle_ref );
    gMainBundle_ref = NULL;
}


Code:
void LoadOneGLTextureFromBundle( CFBundleRef bundle_ref, CFStringRef fileName, CFStringRef fileExtension, CFStringRef subDirName, TextureImageRef texture_ref )
{
    //----------------------------------------------------------------------
    // Variables:
    
    Boolean        didLoad;
    CFURLRef    url_ref;
    
    //----------------------------------------------------------------------
    // Get the URL for that file:
    
    url_ref = CFBundleCopyResourceURL( bundle_ref, fileName, fileExtension, subDirName );
    
    //theURLs[0] = CFBundleCopyResourceURLsOfType( bundle_ref, CFSTR( "jpg" ), subDirName );
    
    //----------------------------------------------------------------------
    
    if ( url_ref == NULL )
    {
        FatalAlert( "Error on LoadOneGLTextureFromBundle(): CFBundleCopyResourceURL() file doesn't exist.", -1 );
    }
    
    //----------------------------------------------------------------------
    // Display the file path for the current URL (debug):
    
    didLoad = CFURLGetFileSystemRepresentation( url_ref, TRUE, texture_ref->filePath, 256 );
    
    if ( didLoad )
    {
        //printf( "\nTexture file path: %s \n", (char *) texture_ref->filePath );
    }
    
    //----------------------------------------------------------------------
    // Set texture's name:
    
    ConvertCFStringtoCString( fileName, texture_ref->name, 256 );
    
    //----------------------------------------------------------------------
    // Create a texture from the URL:
    
    if ( kCFNotFound != ( CFStringFind( fileExtension, CFSTR( "jpg" ), kCFCompareCaseInsensitive ) ).location )
    {
        LoadGLTextureFrom_JPG_CGImageURL( url_ref, texture_ref );
    }
    else if ( kCFNotFound != ( CFStringFind( fileExtension, CFSTR( "png" ), kCFCompareCaseInsensitive ) ).location )
    {
        LoadGLTextureFrom_PNG_CGImageURL( url_ref, texture_ref );
    }
    else if ( kCFNotFound != ( CFStringFind( fileExtension, CFSTR( "tga" ), kCFCompareCaseInsensitive ) ).location )
    {
        LoadGLTextureFrom_TGA_CFDataURL( url_ref, texture_ref );
    }
    
    //----------------------------------------------------------------------
    // Release the URL:
    
    CFRelease( url_ref );
    url_ref = NULL;
}
Quote this message in a reply
Post Reply