[Previous]
[Bookset]
[Next]

PmMemCreateMC()

Create a memory context

Synopsis:

PmMemoryContext_t * PmMemCreateMC( 
                        PhImage_t *mc_image, 
                        PhDim_t *dim, 
                        PhPoint_t *translation );

Description:

This function creates a memory context. A memory context is used to draw into a local memory image buffer. You must create a memory context before calling any other Photon Memory (Pm) functions. The memory context provides definition, control, and access to the memory image.

The parameters for this function are:

mc_image
The resulting image's type and dimensions.

If the image member of this PhImage_t structure (i.e. mc_image->image) is NULL, PmMemCreateMC() allocates its own buffer. PmMemReleaseMC() will free the allocated image buffer.

If the mc_image->image isn't NULL, PmMemCreateMC() uses it instead of allocating its own buffer. The size of the buffer depends on the type and dimensions specified for mc_image. PmMemReleaseMC() won't free the buffer.

The image member of the PhImage_t structure filled in by PmMemFlush() is a pointer to the mc_image->image buffer.

dim
The source size of the draw stream. If the image dimension is different from the source dimension, any drawing done to the memory context will be scaled as necessary to fit the source dimension exactly into the image dimension.
translation
The amount the draw stream will be translated when being rendered into the memory image buffer.

Returns:

A pointer to the new memory context, or NULL if there isn't enough memory to allocate one.

Examples:

/* pmmemtobutton.c                                                
                                                                  
This demonstrates how to draw into an image.  This example
uses the PmMem*() functions to draw into a memory context. 
When finished drawing, the memory context is then dumped
into an image. The image is then used as the image
displayed on a button.                                                        

To compile, you must link with phrender_s.lib.  For example:   
                                                                  
cc -w3 -opmmemtobutton -lphrender_s -lphoton_s pmmemtobutton.c

*/

#include <mem.h>
#include <photon/PhRender.h>
#include <Pt.h>

main( int argc, char *argv[] )
{
    PhArea_t    area = { 80, 20, 80, 40 };
    PhDim_t     dim = { 240, 80 };
    PhImage_t   image;
    PtArg_t     arg[3];
    PtWidget_t  *button, *window;
    
    PtSetArg( &arg[0], Pt_ARG_WINDOW_TITLE, 
              "Memory Context Sample", 0 );
    PtSetArg( &arg[1], Pt_ARG_DIM, &dim, 0 );
    window = PtAppInit( NULL, &argc, argv, 2, arg );

    create_image( &image, &area.size );

    PtSetArg( &arg[0], Pt_ARG_LABEL_TYPE, Pt_IMAGE, 0 );
    PtSetArg( &arg[1], Pt_ARG_AREA, &area, 0 );
    PtSetArg( &arg[2], Pt_ARG_LABEL_DATA, 
              &image, sizeof(PhImage_t) );
    button = PtCreateWidget( PtButton, NULL, 3, arg );
    
    PtRealizeWidget( window );
    PtMainLoop();
}

void
create_image( PhImage_t *image, PhDim_t *dim )
{
    PhPoint_t           translation = { 0, 0 }, center, radii;
    PmMemoryContext_t   *mc; 
    short               bytes_per_pixel = 3;
    
    memset( image, 0, sizeof(PhImage_t) );
    image->type = Pg_IMAGE_DIRECT_888; // 3 bytes per pixel 
                                          // with this type
    image->size = *dim;
    image->image = PgShmemCreate( 
                        dim->w * dim->h * bytes_per_pixel, 
                        NULL );
    
    mc = PmMemCreateMC( image, dim, &translation );
    
    PmMemStart( mc );
    // now all drawing goes into the memory context
    
    // draw whatever we want to appear in the image
    center.x = dim->w / 2;
    center.y = dim->h / 2;
    radii = center;
    PgSetFillColor( Pg_WHITE );
    PgSetStrokeColor( Pg_RED );
    PgDrawEllipse( &center, &radii, Pg_DRAW_FILL_STROKE );
    PgSetStrokeColor( Pg_GREEN );
    PgDrawILine( 0, 0, dim->w-1, dim->h-1 );
    
    PmMemFlush( mc, image ); // get the image
    PmMemStop( mc );
    // now all drawing goes to the default drawing context
    
    PmMemReleaseMC( mc );
}

Classification:

Photon

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

PhImage_t


[Previous]
[Bookset]
[Next]