Need a bit of porting help

Puzzler183
Unregistered
 
Post: #16
I should point out that SDL image works great, and fits with the rest of the SDL framework (which makes porting applications ridiculously easy).
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #17
Puzzler183 Wrote:I should point out that SDL image works great, and fits with the rest of the SDL framework (which makes porting applications ridiculously easy).
I'm with him...
couldn't you simply export all the textures to other formats, then load them through your own code, or libjpg/libpng?

It's not magic, it's Ruby.
Quote this message in a reply
Lindley
Unregistered
 
Post: #18
I'll try SDL. I've looked into just about everything else, and each one is either too complicated for my needs (libgd doesn't even give an image reading example, just writing!) or won't work for some other reason.

My current problem: fopen is failing for no good reason, when it worked just before that on another file. Apparently fopen can handle text files, but not jpegs......

This is vaguely ridiculous. It should not be so difficult to figure out how to load image files.
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #19
Uh, for fopen, are you doing: FILE *myFile = fopen("file", "rb"); and not just "r"? Binary reading is important if you plan to use binary files. See http://www.cplusplus.com/ref/cstdio/fopen.html for details.

Anyhow, SDL is really easy and you can use it for windowing, input and maybe sound (although the SDL_mixer extension or FMOD are both nicer). Then you get the SDL_image extension which reads images really easily..
Quote this message in a reply
Lindley
Unregistered
 
Post: #20
Not quite. The actual failure is in jpeg_read (in jpeg.c):

if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return 0;
}

This works perfectly when I compile it for X11 using my Makefile. This fails when I compile it for Cocoa using Xcode. Frustrating as hell.
Quote this message in a reply
Lindley
Unregistered
 
Post: #21
Now I'm getting a linker warning claiming the directory containing the project does not exist!

I'm starting to suspect Xcode is not entirely bug-free....
Quote this message in a reply
Lindley
Unregistered
 
Post: #22
You know what? Screw it. Thanks to everyone who tried to help. The program works under X11, and that's going to have to be good enough, I guess. It's just not worth the hassel of figuring out what's going wrong.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #23
Remember, you have no guarantees what your current working directory is when your application is run. You should always make sure to set it to a known place before using relative paths to load files.
Quote this message in a reply
Lindley
Unregistered
 
Post: #24
OneSadCookie Wrote:Remember, you have no guarantees what your current working directory is when your application is run. You should always make sure to set it to a known place before using relative paths to load files.

Normally I'd agree, but there are other files in the same directory which are being loaded correctly. Only the texture files are failing.

EDIT: I think I solved it. I moved the readTextures() call before any of the glut function calls. For some strange reason, that seems to have worked.

I'm not complaining, though.....
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #25
glut changes the working directory to the Resources folder within your application's bundle for you. This is normally a good thing Smile

I think you'll find that if moving the readTextures() before your glut call worked, that your app won't work if double-clicked in the finder.

And of course, be careful not to make any OpenGL calls before glutCreateWindow, or you'll crash.
Quote this message in a reply
Lindley
Unregistered
 
Post: #26
So, should I just use absolute paths, or somehow put the textures inside the .app, or what? How do I make the app work as a standalone?
Quote this message in a reply
Member
Posts: 156
Joined: 2002.11
Post: #27
With C and Carbon:

Code:
void LoadGLTextureFrom_PNG_CGImageURL( CFURLRef currURL_ref, TextureImageRef texImage_ref )
{
    //----------------------------------------------------------------------
    // Variables:
    
    CGDataProviderRef    dataProvider_ref;
    CGImageRef        image_ref;
    
    //----------------------------------------------------------------------
    // Create a CFDataProviderRef and a CGImageRef to hold the PNG image data:
    
    dataProvider_ref    = CGDataProviderCreateWithURL( currURL_ref );
    image_ref            = CGImageCreateWithPNGDataProvider( dataProvider_ref, NULL, FALSE, kCGRenderingIntentDefault );
    
    //----------------------------------------------------------------------
    // Load the texture from the CGImage:
    
    LoadGLTextureFromCGImage( image_ref, texImage_ref );
    
    //----------------------------------------------------------------------
    // Release the CGDataProvider and the CGImageRef:
    
    CGDataProviderRelease( dataProvider_ref );
    dataProvider_ref = NULL;
    
    CFRelease( image_ref );
    image_ref = NULL;
}



void LoadGLTextureFromCGImage( CGImageRef image_ref, TextureImageRef texImage_ref )
{
    //----------------------------------------------------------------------
    // Get image data for OpenGL texture (images should be at least 16 X 16 pxl, up to 256 X 256 pxl for best results):
    
    CGContextRef        context_ref = NULL;
    CGColorSpaceRef        colorspace_ref;
    CGImageAlphaInfo    alpha_info;
    
    CGSize        image_size;
    size_t        image_depth;
    size_t        buffer_rowbytes;
    int            bitmap_byteCount;
    GLubyte        *data_buffer;
    
    //----------------------------------------------------------------------
    // Get original image's size and bit depth:
    
    image_size  = CGSizeMake( CGImageGetWidth( image_ref ), CGImageGetHeight( image_ref ) );
    image_depth = CGImageGetBitsPerPixel( image_ref );
    
    //----------------------------------------------------------------------
    // Determine the size of the CGContext bitmap buffer (RGBA_32):
    
    //buffer_rowbytes = CGImageGetBytesPerRow( image_ref );
    //printf( "1) %s : W = %.2f, H = %.2f, R = %d \n", texImage_ref->name, image_size.width, image_size.height, (int) buffer_rowbytes );
    
    buffer_rowbytes  = (size_t) ( ( ( image_size.width * 32 ) + 7 ) / 8 );    // Do not use CGImageGetBytesPerRow( image_ref ); // (((width)*(bits per component)*(number of components per pixel))+7)/8;
    bitmap_byteCount = buffer_rowbytes * image_size.height;
    
    //printf( "2) %s : W = %.2f, H = %.2f, R = %d \n\n", texImage_ref->name, image_size.width, image_size.height, (int) buffer_rowbytes );
    
    //----------------------------------------------------------------------
    // Allocate memory block for bitmap buffer:
    
    data_buffer = (GLubyte *) calloc( bitmap_byteCount, sizeof( GLubyte ) );
    
    if ( data_buffer == NULL )
    {
        FatalAlert( "Error on LoadGLTextureFromCGImage(): Not enough memory to allocate for data_buffer.\n", -1 );
    }
    else
    {
        //----------------------------------------------------------------------
        // Create bitmap graphics colorspace (Monitor == RGB):
        
        colorspace_ref = CGColorSpaceCreateDeviceRGB();
        
        if ( !colorspace_ref )
        {
            FatalAlert( "Error on LoadGLTextureFromCGImage(): Could not create CGColorSpace.\n", -1 );
        }
        
        //----------------------------------------------------------------------
        // Specify the context's alpha info (RGBA_32 or RGBX_32):
        
        alpha_info = ( image_depth > 24 ) ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast; //kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast;
        
        //----------------------------------------------------------------------
        // Create bitmap graphics context:
        
        context_ref = CGBitmapContextCreate( data_buffer, (size_t) image_size.width, (size_t) image_size.height, ( 8 ), buffer_rowbytes, colorspace_ref, alpha_info );
        
        if ( context_ref )
        {
            CGContextSetFillColorSpace( context_ref, colorspace_ref );
            CGContextSetStrokeColorSpace( context_ref, colorspace_ref );
            CGContextTranslateCTM( context_ref, 0.0, image_size.height );
            CGContextScaleCTM( context_ref, 1.0, -1.0);
        }
        else
        {
            FatalAlert( "Error on LoadGLTextureFromCGImage(): Could not create CGContext.\n", -1 );
        }
        
        //CGContextSetRenderingIntent( context_ref, kCGRenderingIntentPerceptual ); crashes on Panther.
        
        CGColorSpaceRelease( colorspace_ref );
        colorspace_ref = NULL;
        
        //----------------------------------------------------------------------
        // Draw the image into the bitmap graphics context:
        
        CGRect        picRect;
        
        picRect = CGRectMake( 0.0f, 0.0f, image_size.width, image_size.height );
        
        CGContextClearRect( context_ref, picRect );
        //CGContextSetRGBFillColor( context_ref, 0.0f, 0.0f, 0.0f, 0.0f );
        //CGContextFillRect( context_ref, picRect );
        
        CGContextDrawImage( context_ref, picRect, image_ref );
        
        //----------------------------------------------------------------------
        // Create OpenGL texture from the bitmap data:
        
        texImage_ref->bpp    = (GLuint) 32;    // image_depth;
        texImage_ref->width  = (GLuint) image_size.width;
        texImage_ref->height = (GLuint) image_size.height;
        
        //printf( "Texture: %s, bpp: %d, width: %d, height: %d\n", texImage_ref->name, (int) image_depth, (int) image_size.width, (int) image_size.height );
        
        //----------------------------------------------------------------------
        // Generate OpenGL texture (it returns texID):
        
        CreateGLTexture( texImage_ref, data_buffer );
        
        //----------------------------------------------------------------------
        // Release data_buffer, context_ref:
        
        CGContextRelease( context_ref );
        context_ref = NULL;
        
        free( data_buffer );
        data_buffer = NULL;
    }
}
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #28
No offense, but the problem here is not bugs in XCode.

The problem is you don't understand enough about what you're doing. fopen() does not start randomly failing for no reason.

Quote:So, should I just use absolute paths, or somehow put the textures inside the .app, or what? How do I make the app work as a standalone?

You should read and understand the bundle functions (NSBundle for Cocoa or CFBundle for Carbon) and use those to find the location of your main bundle. From there, you can find the Resources folder in your bundle or anything else.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #29
Lindley Wrote:So, should I just use absolute paths, or somehow put the textures inside the .app, or what? How do I make the app work as a standalone?

I think (someone correct me on this) that if you add your textures to the project and group them in a folder called "Resources", XCode will copy them into the .app (which is really a directory) it creates for you. Then you can get the main bundle and from that, get the path to your resources directory.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Lindley
Unregistered
 
Post: #30
wadesworld Wrote:No offense, but the problem here is not bugs in XCode.

The problem is you don't understand enough about what you're doing. fopen() does not start randomly failing for no reason.

I know, but it certainly seemed that way for a while. How was I know that that Apple's version of GLUT did something different than libglut (the version I had been working with before)? It's supposed to be seamlessly cross-platform.

As I said at the start, long-time programmer, long time Mac user, but almost never both at the same time. Most of the actual programming I've done has been on other systems.

Besides which, this past semester was the first time I've taken a Computer Graphics course. Before now I haven't had to worry about reading in images. And the course provided a simple-to-use interface for reading based only on filename, so I just assumed (stupidly, I suppose) that there was no more to it than that.

Quote:You should read and understand the bundle functions (NSBundle for Cocoa or CFBundle for Carbon) and use those to find the location of your main bundle. From there, you can find the Resources folder in your bundle or anything else.

I'll look into it. I haven't done much with that sort of stuff so far.

Does this apply to all files read in by the program?
Quote this message in a reply
Post Reply