Carbon OpenGL help, anyone?

Discussion in 'Mac Programming' started by Kopachris, Oct 20, 2008.

  1. Kopachris macrumors newbie

    Joined:
    Feb 12, 2007
    Location:
    under your bed
    #1
    I want to make an OpenGL program with Carbon because I want to use C++ instead of the mandatory Objective-C that you have to use with Cocoa (I don't want to learn Objective-C just yet (I want my software to be easily portable to Win and Lin)). So opened XCode and made a new C++ Carbon Application project. For what I'm doing (essentially a physics simulator (don't ask me to explain it)), I need to not have the OpenGL area the size of the entire window. I need some room for boxes to put attributes of objects in. I'm totally new to Carbon programming. Can someone help me figure out what to do? Thanks!

    P.S. This does not help me any. I'm not a big fan of "conceptual" learning.

    UPDATE: I managed to make the OpenGL window appear alongside the .nib window. The only problem now is that the menubar at the top of the screen has an  application menu, then the "Ninja Physics" application menu.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    What's wrong with that menu bar? If you don't want a menu bar, make it go full screen.

    You can still write a C++ OpenGL app using Cocoa. The only thing you'd use Cocoa for is setting up the window and OpenGL environment, and then after that you can use C++ to your heart's content. I've done it before :)
     
  3. Kopachris thread starter macrumors newbie

    Joined:
    Feb 12, 2007
    Location:
    under your bed
    #3
    The thing about the menu bar is that there's two application menus. If I didn't want a menu bar, there's a function to get rid of it. The menu bar goes:

    File Edit Window Planets File Edit Window

    The "Quit" item in both menus work the same. Anyway, this is the code so far:
    Code:
    //
    //  main.cpp
    //  Planets
    //
    //  Created by Christopher Koch on 10/20/08.
    //  © Copyright NinjaSoft Entertainment 2008. All rights reserved.
    //
    
    #include <Carbon/Carbon.h>
    #include "TApplication.h"
    #include "TWindow.h"
    #include <OpenGL/OpenGL.h>
    #include <OpenGL/gl.h>
    #include <OpenGL/glu.h>
    #include <OpenGL/glext.h>
    //#include <OpenGL/wglext.h>
    //#include <OpenGL/CGLRenderers.h>
    #include <GLUT/glut.h>
    
    // Our custom application class
    class CarbonApp : public TApplication
    {
        public:
    							
                                CarbonApp() {}
            virtual             ~CarbonApp() {}
            
        protected:
            virtual Boolean     HandleCommand( const HICommandExtended& inCommand );
    };
    
    // Our main window class
    class MainWindow : public TWindow
    {
        public:
                                MainWindow() : TWindow( CFSTR("MainWindow") ) {}
            virtual             ~MainWindow() {}
            
            static void         Create();
            
        protected:
            virtual Boolean     HandleCommand( const HICommandExtended& inCommand );
    };
    
    //--------------------------------------------------------------------------------------------
    
    void display() {
    	glClearColor(0,0,0,1);
    	glClear(GL_COLOR_BUFFER_BIT);
    	glLoadIdentity();
    	gluLookAt(0,0,5,0,0,0,0,1,0);
    	glFlush();
    }
    
    int main(int argc, char* argv[])
    {
        CarbonApp app;
        
        // Create a new window. A full-fledged application would do this from an AppleEvent handler
        // for kAEOpenApplication.
    	MainWindow::Create();
    	
    	glutInit(&argc,argv);
    	glutInitDisplayMode(GLUT_SINGLE);
    	glutInitWindowSize(500,500);
    	glutInitWindowPosition(100,100);
    	glutCreateWindow("Ninja Physics Viewer");
    	glutDisplayFunc(display);
    	glutMainLoop();
    	
    	app.Run();
        
        return 0;
    }
    
    //--------------------------------------------------------------------------------------------
    Boolean
    CarbonApp::HandleCommand( const HICommandExtended& inCommand )
    {
        switch ( inCommand.commandID )
        {
            case kHICommandNew:
                MainWindow::Create();
                return true;
                
            // Add your own command-handling cases here
            
            default:
                return false;
        }
    }
    
    //--------------------------------------------------------------------------------------------
    void
    MainWindow::Create()
    {
        MainWindow* wind = new MainWindow();
    
        // Position new windows in a staggered arrangement on the main screen
        RepositionWindow( *wind, NULL, kWindowCascadeOnMainScreen );
        
        // The window was created hidden, so show it
        wind->Show();
    	
    }
    
    //--------------------------------------------------------------------------------------------
    Boolean
    MainWindow::HandleCommand( const HICommandExtended& inCommand )
    {
        switch ( inCommand.commandID )
        {
            // Add your own command-handling cases here
            
            default:
                return false;
        }
    }
    
    So, let me see if I've got this right... You can set up a Cocoa project, which will use Objective-C for the GUI handling. But other than that, you can use C++ for everything else? That might be a good alternative for me if I can figure out how to get and set the properties of the NS controls.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Here's a small Xcode project that demonstrates using OpenGL within C++ inside a Cocoa application. I pretty much just stripped down my old OpenGL project and simplified it as best I could, adding in a few bits of code provided by Apple.

    A lot of the app is automatically handled from within MainMenu.xib. There the menu bar, main window and OpenGL view are created. I created an NSOpenGLView subclass called GLView which creates an instance of the C++ class and passes all its functionality to this which does the drawing.

    I'm not an OpenGL expert by any means (pretty much forgot all that I learned) but if you have questions let me know :)
     
  5. Kopachris thread starter macrumors newbie

    Joined:
    Feb 12, 2007
    Location:
    under your bed
    #5
    Thank you very much. It looks like I could do that, use Cocoa for the UI and C++ for the calculations and OpenGL. I just have a couple questions/problems. First, I'm stuck with OSX Tiger, so I can't build and run the program to see how it works (I think I just need to find the path/name of the SDK to use instead of "macosx10.5"). Second, is plain text the only way to edit the .xib file, or is it compatible with the Interface Builder in XCode 2 (again, I'm limited to Tiger, so no XCode 3)?

    Danke!
     
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Ah I'll convert the project so it'll run on Xcode 2.5 when I get a chance.
     
  7. Kopachris thread starter macrumors newbie

    Joined:
    Feb 12, 2007
    Location:
    under your bed
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    Ok I converted the project and put it up on my Google code account. You can check it out with Subversion via:
    Code:
    svn co http://kaincode.googlecode.com/svn/trunk/CocoaGL
     
  9. Kopachris thread starter macrumors newbie

    Joined:
    Feb 12, 2007
    Location:
    under your bed
    #9
    This is just what I needed. I didn't know you could do C++ in a Cocoa project (I'm coming from Visual Studio Express on the PC (monitor died, Dino PC useless)). Again, Thanks!:)
     
  10. physicsdude1 macrumors member

    Joined:
    Aug 30, 2006
    #10
    Any chance this code is posted somewhere? I'd like to see the example too. Thanks!
     

Share This Page