[C] the ScreenCapture program

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

  1. piotrek321 macrumors newbie

    Oct 2, 2010
    Hi all!
    I'm trying to understand this code:

    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:
    // Create a full-screen OpenGL graphics context
            // Specify attributes of the GL graphics context
            NSOpenGLPixelFormatAttribute attributes[] = {
                (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. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    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. piotrek321 thread starter macrumors newbie

    Oct 2, 2010
    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. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    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