BareBones3DApp.c

/*
**  This is the simplest viewer application reasonable
**
**  Nick Thompson, nickt@apple.com
*/
 
 
/*------------------------------------------------------------*/
 
#include <CodeFragments.h>
#include <Dialogs.h>
#include <Fonts.h>
#include <Menus.h>
#include <QuickDraw.h>
#include <SegLoad.h>
#include <StandardFile.h>
#include <Windows.h>
 
#include "QD3DViewer.h"
 
/*------------------------------------------------------------*/
 
/* function prototypes for the application */
Boolean     IsQD3DViewerInstalled( void ) ;
void        MainEventLoop( void ) ;
 
 
/*------------------------------------------------------------*/
 
/* constants */
const   short   kWindHeight = 250 ;
const   short   kWindWidth = 200 ;
 
/*------------------------------------------------------------*/
 
/*
** this is a simple program, initialise the managers
** check that the viewer is installed
** ask for a 3DMF file
** create a window
** attach it to a viewer
** handle events until the window is closed
** Quit
**
** No menus, not really an app :-)
*/
 
void main(void)
{
 
    short               myNumTypes = 1 ; 
    SFTypeList          myTypeList = { '3DMF' } ;
    StandardFileReply   mySFReply ;
    OSErr               theErr = noErr ;
    short               myRefNum ;
    WindowPtr           myWind = nil ;
    Rect                myRect = { 0, 0, kWindHeight, kWindWidth } ;
    TQ3ViewerObject     myViewer ;
    
    /* Initialize all the needed managers. */
    InitGraf((Ptr)&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs((long)nil);
    InitCursor();
    
    /*
    ** must call this so that the heap is expanded to maximum
    ** size before calling any viewer routines
    */
    MaxApplZone() ;
    
    /* we weak linked against the Viewer, let's check that it is installed */
    
    if( (long) Q3ViewerNew != kUnresolvedCFragSymbolAddress ) {
        StandardGetFile( nil, myNumTypes, myTypeList, &mySFReply ) ;
        
        if( mySFReply.sfGood ) {
            theErr = FSpOpenDF( &mySFReply.sfFile,  
                                fsRdPerm, 
                                &myRefNum ) ;
                                
            OffsetRect( &myRect, 50, 50 ) ; 
                              
            myWind = NewCWindow( nil, 
                                 &myRect, 
                                 "\pBareBones3DApp", 
                                 true, 
                                 documentProc, 
                                 (WindowPtr)-1, 
                                 true, 
                                 0L ) ;
                                 
 
            if(myViewer = Q3ViewerNew( (CGrafPtr)myWind, &myWind->portRect, kQ3ViewerDefault )) 
            {
                /* if the viewer is not nil, we created it ok... */
                theErr = Q3ViewerUseFile( myViewer, myRefNum );
                SetWRefCon( myWind, (long)myViewer ) ;      /* stuff the reference in the refcon for later use */
                MainEventLoop() ;
            }
 
        }
    }
 
    ExitToShell();  
    
}
 
 
/*------------------------------------------------------------*/
 
/*
** we want to handle events until the fronmost window goes away
*/
 
void    MainEventLoop( void ) 
{
    WindowPtr       myWind ;
    Boolean         gotEvent ;
    TQ3ViewerObject theViewer ;
    OSErr           theErr ;
    RgnHandle       tempRgn ;
    Rect            dragRect ;
    EventRecord     theEvent ;
    GrafPtr         savedPort ;
    
    while(( myWind = FrontWindow()) != nil )
    {
        
        gotEvent = WaitNextEvent( everyEvent, &theEvent, GetCaretTime(), nil ) ;
        if( gotEvent )
        {
            switch( theEvent.what )
            {
              case updateEvt:
                myWind = (WindowPtr)theEvent.message ;
                
                theViewer = (TQ3ViewerObject)GetWRefCon( myWind ) ;
                BeginUpdate( myWind ) ;
                theErr = Q3ViewerDraw( theViewer );
                EndUpdate( myWind ) ;
                
                break ;
                
              case mouseDown:
                switch( FindWindow( theEvent.where, &myWind ) )
                {
                  case inGoAway:
                    theViewer = (TQ3ViewerObject)GetWRefCon( myWind ) ;
                    theErr = Q3ViewerDispose( theViewer);
                    DisposeWindow( myWind ) ;
                    break ;
                    
                  case inContent:
                    /*
                    ** There is a bug in versions 1.0.4 and earlier of the Viewer,
                    ** so the port has to be set and restored.
                    */
                    GetPort( &savedPort ) ;
                    SetPort((GrafPtr)myWind ) ;
                
                    Q3ViewerEvent( theViewer, &theEvent);
                    
                    SetPort( savedPort ) ;
                    break ;
                    
                  case inDrag:
                    tempRgn = GetGrayRgn() ;
                    dragRect = (**tempRgn).rgnBBox ;
                    DragWindow( myWind, theEvent.where, &dragRect ) ;
                    break ;
                }
                break ; 
            }
        }
        SetPort(savedPort ) ;
    }   
 
}