Difference between revisions of "Pixmaps"

From uGFX Wiki
Jump to: navigation, search
(Example)
Line 17: Line 17:
  
 
== Example ==
 
== Example ==
 +
The following example will show how to create a pixmap and access it both as a virtual display and as a frame buffer surface. Once the pixmap is created it will be blitted incrementally across the entire screen (The pixmap will move across the screen).
 +
<syntaxhighlight lang="c">
 +
#include "gfx.h"
 +
 +
#define PIXMAP_WIDTH 40
 +
#define PIXMAP_HEIGHT 10
 +
 +
static GDisplay* pixmap;
 +
static pixel_t* surface;
 +
 +
int main(void) {
 +
coord_t width, height;
 +
coord_t i, j;
 +
 +
    // Initialize and clear the display
 +
    gfxInit();
 +
 +
    // Get the screen size
 +
    width = gdispGetWidth();
 +
    height = gdispGetHeight();
 +
 +
    // Create a pixmap and get a pointer to the bits
 +
    pixmap = gdispPixmapCreate(PIXMAP_WIDTH, PIXMAP_HEIGHT);
 +
    surface = gdispPixmapGetBits(pixmap);
 +
 +
    // A pixmap can be treated either as a virtual display or as a memory framebuffer surface.
 +
    // We demonstrate writing to it using both methods.
 +
 +
    // First demo drawing onto the surface directly
 +
    for(j = 0; j < PIXMAP_HEIGHT; j++)
 +
    for(i = 0; i < PIXMAP_WIDTH; i++)
 +
    surface[j*PIXMAP_WIDTH + i] = RGB2COLOR(0, 255-i*(256/PIXMAP_WIDTH), j*(256/PIXMAP_HEIGHT));
 +
 +
    // Secondly, show drawing a line on it like a virtual display
 +
    gdispGDrawLine(pixmap, 0, 0, gdispGGetWidth(pixmap)-1, gdispGGetHeight(pixmap)-1, White);
 +
   
 +
    i = j = 0;
 +
    while(TRUE) {
 +
    // Clear the old position
 +
    gdispFillArea(i, j, PIXMAP_WIDTH, PIXMAP_HEIGHT, Black);
 +
 +
    // Change the position
 +
    i += PIXMAP_WIDTH/2;
 +
    if (i >= width - PIXMAP_WIDTH/2) {
 +
    i %= width - PIXMAP_WIDTH/2;
 +
    j = (j + PIXMAP_HEIGHT/2) % (height - PIXMAP_HEIGHT/2);
 +
    }
 +
 +
    // Blit the pixmap to the real display at the new position
 +
    gdispBlitArea(i, j, PIXMAP_WIDTH, PIXMAP_HEIGHT, surface);
 +
 +
    // Wait
 +
    gfxSleepMilliseconds(100);
 +
    }
 +
 +
    // Clean up
 +
    gdispPixmapDelete(pixmap);
 +
}
 +
</syntaxhighlight>
  
 
[[Category:GDISP]]
 
[[Category:GDISP]]

Revision as of 15:12, 3 July 2015

Pixmaps provide dynamic frame buffers. A pixmap of any size can by dynamically created and destroyed during runtime. A pixmap represents itself as a virtual display to the user. Hence a pixmap can be treated like a regular display and all the available GDISP drawing routines can be performed inside the pixmap frame buffer. A pixmap can be rendered at any location of a real display at any time.

API reference

The API reference of the pixmap can be found here.

Creating and destroying a pixmap

Pixmaps can be created and destroyed at any time during runtime using gdispPixmapCreate() and gdispPixmapDelete().

Accessing the pixmap

A pixmap can be treated as either a virtual display or as a memory frame buffer surface.

Virtual display

Frame buffer surface

Example

The following example will show how to create a pixmap and access it both as a virtual display and as a frame buffer surface. Once the pixmap is created it will be blitted incrementally across the entire screen (The pixmap will move across the screen).

#include "gfx.h"
 
#define PIXMAP_WIDTH	40
#define PIXMAP_HEIGHT	10
 
static GDisplay* pixmap;
static pixel_t* surface;
 
int main(void) {
	coord_t		width, height;
	coord_t		i, j;
 
    // Initialize and clear the display
    gfxInit();
 
    // Get the screen size
    width = gdispGetWidth();
    height = gdispGetHeight();
 
    // Create a pixmap and get a pointer to the bits
    pixmap = gdispPixmapCreate(PIXMAP_WIDTH, PIXMAP_HEIGHT);
    surface = gdispPixmapGetBits(pixmap);
 
    // A pixmap can be treated either as a virtual display or as a memory framebuffer surface.
    // We demonstrate writing to it using both methods.
 
    // First demo drawing onto the surface directly
    for(j = 0; j < PIXMAP_HEIGHT; j++)
    	for(i = 0; i < PIXMAP_WIDTH; i++)
    		surface[j*PIXMAP_WIDTH + i] = RGB2COLOR(0, 255-i*(256/PIXMAP_WIDTH), j*(256/PIXMAP_HEIGHT));
 
    // Secondly, show drawing a line on it like a virtual display
    gdispGDrawLine(pixmap, 0, 0, gdispGGetWidth(pixmap)-1, gdispGGetHeight(pixmap)-1, White);
 
    i = j = 0;
    while(TRUE) {
    	// Clear the old position
    	gdispFillArea(i, j, PIXMAP_WIDTH, PIXMAP_HEIGHT, Black);
 
    	// Change the position
    	i += PIXMAP_WIDTH/2;
    	if (i >= width - PIXMAP_WIDTH/2) {
    		i %= width - PIXMAP_WIDTH/2;
    		j = (j + PIXMAP_HEIGHT/2) % (height - PIXMAP_HEIGHT/2);
    	}
 
    	// Blit the pixmap to the real display at the new position
    	gdispBlitArea(i, j, PIXMAP_WIDTH, PIXMAP_HEIGHT, surface);
 
    	// Wait
    	gfxSleepMilliseconds(100);
    }
 
    // Clean up
    gdispPixmapDelete(pixmap);
}