[C] the ScreenCapture program

Discussion in 'Mac Programming' started by piotrek321, Oct 2, 2010.

  1. macrumors newbie

    Joined:
    Oct 2, 2010
    #1
    Hi all!
    I'm trying to understand this code:
    http://developer.apple.com/library/mac/#samplecode/OpenGLScreenSnapshot/Listings/OpenGLScreenReader_m.html%23//apple_ref/doc/uid/DTS10004288-OpenGLScreenReader_m-DontLinkElementID_6

    Unfortunately, I'm not acquainted with Objective-C (to be honest, it seems to be extemely unintuitive), but I know how to write programs in C/C++. As this program uses openGL I suppose it is not difficult to write a similar one in C using gl.h library. Am I right?
    Can anyone write me a scheme what my openGL program needs to do before calling glReadPixels() routine? Or just translate this into C, if possible:
    Code:
    // Create a full-screen OpenGL graphics context
            
            // Specify attributes of the GL graphics context
            NSOpenGLPixelFormatAttribute attributes[] = {
                NSOpenGLPFAFullScreen,
                NSOpenGLPFAScreenMask,
                CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
                (NSOpenGLPixelFormatAttribute) 0
                };
     
            NSOpenGLPixelFormat *glPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
            if (!glPixelFormat)
            {
                return nil;
            }
     
            // Create OpenGL context used to render
            mGLContext = [[[NSOpenGLContext alloc] initWithFormat:glPixelFormat shareContext:nil] autorelease];
     
            // Cleanup, pixel format object no longer needed
            [glPixelFormat release];
        
            if (!mGLContext)
            {
                [self release];
                return nil;
            }
            [mGLContext retain];
     
            // Set our context as the current OpenGL context
            [mGLContext makeCurrentContext];
            // Set full-screen mode
            [mGLContext setFullScreen];
     
            NSRect mainScreenRect = [[NSScreen mainScreen] frame];
            mWidth = mainScreenRect.size.width;
            mHeight = mainScreenRect.size.height;
    I have never used the openGL before, but now I need only one function from that huge library: the function that will allow me to grab the current content of the screen - one that puts it somewhere in RAM as a table of RGB ints and returns me the pointer to the first byte.

    thanks in advance!
     
  2. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    You haven't indicated what you are trying to accomplish so before pointing in some random direction would you give us an idea as to what you plan on building.

    Would GLUT be something you could use?
     
  3. thread starter macrumors newbie

    Joined:
    Oct 2, 2010
    #3
    To start learning openGL, I'm just trying to create very simple program: capture the content of the screen and write it into the bmp file.
    I know that it is quite pointless, because the file will be huge, but still it is good to learn how to do it.
    It can use glut as well, if necessary.
     
  4. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Well, if you don't mind a starter project I've attached a minimal working example.

    It's Carbon based using C++.

    It started life by selecting "New Project" from Xcode "File" menu.

    Selecting "Application" from the "Mac OS X" on the left portion of the "New Project" template select dialog window.

    Selecting "C++ stdc++" item form the "Type" pop-up menu.

    Clicking the "Choose" menu.

    Naming the project and saving the project.

    I customized it by adding both the "GLUT.framework" and the "OpenGL.framework".

    And provided some possibly useful function stubs.

    As it exists here it simply created a Window draws a line and nothing more.
     

    Attached Files:

Share This Page