Saturday, September 17, 2005

QuickTime on Windows - Movies

Apple has a really good example class for MFC programming - CQuickTime -- very helpful!

I wanted to load a movie with a URL. There are a few good examples out there... so here's what I learned.

Movie LoadMovieFromURL( const UInt8* url, CFIndex len, Rect* bounds )
{ // you have to set the port first - MacSetPort, CreatePortAssociation
CFURLRef urlRef = ::CFURLCreateWithBytes( null, url, len, kCFStringEncodingUTF8, null );
OSErr error;

if( null == urlRef )
return null;

Movie movie = null;
Handle dataRef;
OSType dataRefType;
error = ::QTNewDataReferenceFromCFURL( urlRef, 0, &dataRef, &dataRefType );

if( noErr == error )
{
short movieId;
error = ::NewMovieFromDataRef( &movie, 0, &movieId, dataRef, dataRefType );

if( noErr == error )
{
::GetMovieBox( movie, bounds );
}
}

::CFRelease( urlRef );

return movie;
}


Before calling this routine, you need to set the graphics port. You can create a graphics port for your Windows HWND with CreatePortAssociation

With loading images, I didn't use a CFURLRef. I am here. Why? Just practicing how to skin cats many ways. Handy to know when things don't work.

I make the movie controller with the following routine:
MovieController MakeMovieController( Movie movie, CWnd* wnd, Rect* bounds )
{
Rect theMovieRect;
CRect box;

::SetMovieBox( movie, bounds );

wnd->GetClientRect( box );

theMovieRect.left = 0;
theMovieRect.top = 0;
theMovieRect.right = box.Width();
theMovieRect.bottom = box.Height();

// Attach a movie controller
return ::NewMovieController( movie, &theMovieRect, mcTopLeftMovie );
}


This will put the movie in the top left corner of the CWnd. This works for me for now. I'm going to center the movie eventually.

QuickTime on Windows - Images

I've decided to use QuickTime for importing images, video and sound into the Windows application. The API is easy, familiar and cross platform. Plus, I get access to a wide range of media resource formats.

Loading an Image from a URL
GraphicsImportComponent LoadGraphicsImportFromURL( char* url, Rect* bounds )
{ // you need to make sure that you've set the port before calling
Handle urlRef = NULL;
long urlLen = (long) strlen( url );
OSErr error = noErr;
GraphicsImportComponent gi = NULL;

error = PtrToHand( url, &urlRef, urlLen +1 );
if( noErr == error )
{
error = GetGraphicsImporterForDataRef( urlRef, URLDataHandlerSubType, &gi );
if( noErr == error )
{
GraphicsImportGetNaturalBounds( gi, bounds );

CGrafPtr port;
GDHandle gd;

GetGWorld( &port, &gd );
GraphicsImportSetGWorld( gi, port, gd );
GraphicsImportSetGraphicsMode( gi, graphicsModeStraightAlpha, NULL );
}
DisposeHandle( urlRef );
}

return gi;
// you'll need to CloseComponent with this returned value
// when you are done with it.
}


Before calling this routine, you need to set the graphics port. You can create a graphics port for your Windows HWND with CreatePortAssociation

I use graphicsModeStraightAlpha so that png files (and other formats) draw well (for my purposes).

Drawing an Image
bool DrawGraphicsImport( GraphicsImportComponent gi, RECT* clientBox, Rect* bounds )
{
if( (null == gi) || (null == clientBox) || (null == bounds) )
{
return false;
}

MatrixRecord matrix;
Rect dstRect;
float width = (float) ( clientBox->right - clientBox->left );
float height = (float) ( clientBox->bottom - clientBox->top );

if( ( 0.0f == width ) || ( 0.0f == height ) )
return false;

float boundsWidth = (float) ( bounds->right - bounds->left );
float boundsHeight = (float) ( bounds->bottom - bounds->top );

if( ( 0.0f == boundsWidth ) || ( 0.0f == boundsHeight ) )
return false;
float boundsAspect = boundsWidth / boundsHeight;

float dstWidth;
float dstHeight;


dstWidth = height * boundsAspect;

if( dstWidth > width )
{
dstWidth = width;
dstHeight = width / boundsAspect;
}
else
{
dstHeight = height;
}

dstRect.left = (short) (clientBox->left + (long) ( ( width - dstWidth ) * 0.5 ));
dstRect.right = (short) (dstRect.left + (long) dstWidth);
dstRect.top = (short) (clientBox->top + (long) ( ( height - dstHeight ) * 0.5 ));
dstRect.bottom = (short) (dstRect.top + (long) dstHeight);

::RectMatrix( &matrix, bounds, &dstRect );
::GraphicsImportSetMatrix( gi, &matrix );
::GraphicsImportDraw( gi );
return true;
}


The clientBox is the HWND's client rect. Bounds is the natural bounds of the image. All I wanted to do here is to scale and center the image in an HWND. You'll notice that I don't need to pass the display context. If you want the image to draw to different contexts, you'll have to set the port for the image before drawing.

Saturday, September 10, 2005

Objective-C

In my recent update to Town Generator, I wanted to export an attributed string as html. Internally, Core Data stores the string as NSData which I convert into an NSAttributedString using an NSValueTransformer, NSUnarchiveFromDataTransformerName.

Nothing too unusual about all that - could easily been done with C or C++.

A cool thing about Objective-C is that you can extend any existing class by using categories - without subclassing the class.

So, I wrote a category for NSAttributedString as so:

@interface NSAttributedString ( TGDocumentAdditions )
- (NSString*) toHTML:(NSString*)pathToAttachments;
- (NSString*) toText;
@end


In the code that exports the comments to HTML, I don't need to do anything special to get the additional functionality. For example, with C++, I would have to subclass NSAttributedString and then I'd have to have a constructor that could restore the class from NSData... It starts to get messy (but not impossible).

NSAttributedString*   comments  = 
[transformer transformedValue:[power valueForKey:@"comments"]];
[self appendTag:@"td"
contents:[comments toHTML:path]
to:html
attributes:nil];


By extending NSAttributedString by using a category, I can gain increased functionality without introducing a lot of coding overhead.