graphx.h
#include <graphx.h>
The graphx library implements efficient graphics routines from everything to drawing sprites, shapes, and tilemaps. This allows a programmer to easily add quality graphics to their program without needing to worry about the low-level implementation details.
Default Palette
This is the default palette used by graphx. This palette can be modified at any time with different colors.
Clipped vs. Unclipped
The graphx library contains many similar functions, e.g. gfx_Sprite
and gfx_Sprite_NoClip
.
As the name suggests, the _NoClip variant of a routine performs no clipping. Clipping is the process of checking if parts (or all) of an object to be drawn are outside the window bounds and skipping drawing these out of bounds parts. This checking process takes time. If you can be sure that an object to be drawn is fully within the bounds of the screen, the clipping process can be safely skipped by using the _NoClip variant of the routine.
Do not use the _NoClip variant of a routine if you can not be sure that the object to be drawn is fully within the bounds of the screen. Doing so may result in corrupted graphics or memory.
Partial Redraw
Partial redraw applies when you are moving a transparent sprite against a static background. This can be problematic, as you may not want to redraw the whole background each time. Partial redraw saves a copy of what is behind the transparent sprite, and draws it when the sprite moves again.
The Implementation
A brief summation of the paritial redraw using graphx is:
Allocate space for a temporary sprite.
When the background is drawn, a small copy of it is saved to the temporary spirte.
After the background is drawn, the sprite is drawn as normal.
Upon movement, the sprite is erased using the temporary sprite and the coordinates are updated.
The section of background with the updated coordinates is stored to the temporary sprite and the process repeated.
This is implemented using somewhat pseudo code:
int x = 0;
int y = 0;
gfx_sprite_t *behind_sprite = gfx_MallocSprite(SPRITE_WIDTH, SPRITE_HEIGHT);
// ...draw the background code here...
// set initial sprite state
gfx_GetSprite(behind_sprite, x, y);
gfx_TransparentSprite(sprite, x, y);
do {
// if we don't need to move, loop
if (no_move) continue;
// clear out the old sprite
gfx_Sprite(behind_sprite, x, y);
// ...movement code here...
// updates x and y
// get the new background and draw the moved sprite
gfx_GetSprite(behind_sprite, x, y);
gfx_TransparentSprite(sprite, x, y);
} while (moving);
Buffering Graphics
Buffering is a fancy method in order to prevent graphics from being displayed as they are being drawn. It is used primarily to eliminate visible draws which can make an application look amateurish, sluggish, or appear to flicker.
When graphics routines are buffered, they draw offscreen (nonvisible) portion of memory, so the user doesn’t see the partial drawing. This is accomplished in one line with the following routine, usually placed directly after calling gfx_Begin():
gfx_SetDrawBuffer();
The next part is deciding how the drawn graphics should be displayed to the user.
Method 1 (Copying/Blitting)
The first method is to copy (commonly referred to as ‘blitting’) whatever is in the buffer to the visible screen. The simplest way is to call:
gfx_BlitBuffer();
Which will copy/blit the entire buffer to the screen. Alternatively, gfx_BlitLines() and gfx_BlitRectangle() and the other variations are used to specify the blitting bounds.
Method 2 (Buffer Swapping)
Buffer swapping swaps the visible screen with an offscreen buffer, leaving the contents on both. Whatever is currently on the screen will become the graphics buffer, and whatever is in the graphics buffer will be displayed on the screen The code to swap the visible screen with nonvisibile buffer is:
gfx_SwapDraw();
What actually happens is shown below, ‘graphics’ is simply where the graphics routines will draw to.
This method is really useful when you are redrawing all of the graphics each frame, and requires more work to handle if you only wish to do a partial redraw.
Pros and Cons
Buffering is slower than drawing to the main screen, but greatly improves perceived performance which is extremely important in graphical applications.
Blitting: Easy, simply a copy to the screen.
Swapping: Requires more programmer management (sometimes), faster than blitting.
Creating Sprites
Sprites are images that contain pixel data that can be drawn via graphx functions. The CE C Toolchain includes convimg, which is used to convert images in PNG or similar formats into binary data that can be used by graphx.
Getting Started
We are going to begin by taking a look at an example program to familiarize ourselves with how to run and configure convimg.
Open the graphx/sprites example, and navigate to the src/gfx folder.
The image oiram.png
is the sprite that wants to be converted, and convimg.yaml
is used to configure how the image should be converted.
Open convimg.yaml
in a text editor, which contains the below lines.
Run the command convimg --help
. This outputs the readme for convimg, and what each of the various commands do.
You can also find the readme online, available here.
palettes:
- name: global_palette
fixed-entries:
- color: {index: 0, r: 255, g: 0, b: 128}
- color: {index: 1, r: 255, g: 255, b: 255}
images: automatic
converts:
- name: sprites
palette: global_palette
transparent-color-index: 0
images:
- oiram.png
outputs:
- type: c
include-file: gfx.h
palettes:
- global_palette
converts:
- sprites
Converting Sprites
Converting sprites requires you to type make gfx
instead of make
.
Any time you change an image or the convimg.yaml
, you will need to execute this command.
You can now make
the project as you would any of the other examples.
Explanation of output
Sprites used in the graphx library are allowed 8 bits per pixel (aka 8bpp), which allows for a total of 256
colors.
As shown in the previous image, convimg was able to create a suitable palette for oiram.png
with 15 colors.
Once the palette is generated, convimg outputs C headers and source files that are automatically picked up by the toolchain and compiled into the final program.
API Documentation
graphx contains optimized graphics operations and routines.
The below example template shows the best graphx buffer usage pattern:
// Standard #includes omitted
bool partial_redraw;
// Implement us!
void begin();
void end();
bool step();
void draw();
void main() {
begin(); // No rendering allowed!
gfx_Begin();
gfx_SetDrawBuffer(); // Draw to the buffer to avoid rendering artifacts
while (step()) { // No rendering allowed in step!
if (partial_redraw) // Only want to redraw part of the previous frame?
gfx_BlitScreen(); // Copy previous frame as a base for this frame
draw(); // As little non-rendering logic as possible
gfx_SwapDraw(); // Queue the buffered frame to be displayed
}
gfx_End();
end();
}
- Author
Matt “MateoConLechuga” Waltz
- Author
Jacob “jacobly” Young
- Author
Zachary “Runer112” Wassall
- Author
Patrick “tr1p1ea” Prendergast
- Author
”grosged”
Defines
-
gfx_MallocSprite(width, height)
Dynamically allocates memory for a sprite using
malloc
.width
andheight
will be set in the sprite. ReturnsNULL
upon allocation failure.See also
Note
If not used in a dynamic context and
width
andheight
are static, consider statically allocating the sprite instead with gfx_UninitedSprite() or gfx_TempSprite().- Parameters
width – Sprite width.
height – Sprite height.
- Returns
A pointer to the allocated sprite.
-
gfx_UninitedSprite(name, width, height)
Statically allocates uninitialized memory for a sprite.
Declares a
gfx_sprite_t *
with the givenname
pointing to the allocated memory.width
andheight
will not be set in the sprite, unlike gfx_TempSprite().See also
Warning
If used outside of a function body, the memory will be allocated in the global uninitialized data segment (BSS). If used inside a function body, the memory will be allocated on the stack. If the sprite is sufficiently large, usage inside a function body will overflow the stack, so it is recommended that this normally be used outside of a function body.
- Parameters
name – Name of declared
gfx_sprite_t *
.width – Sprite width.
height – Sprite height.
-
gfx_TempSprite(name, width, height)
Statically allocates memory for a sprite.
Declares a
gfx_sprite_t *
with the givenname
pointing to the allocated memory.width
andheight
will be set in the sprite, unlike gfx_UninitedSprite().See also
- Attention
Due to
width
andheight
being set, the memory will be allocated in the initialized data segment. If the compiled program is not compressed, then this could be a serious source of bloat and gfx_UninitedSprite() should be preferred.
- Parameters
name – Name of declared
gfx_sprite_t *
.width – Sprite width.
height – Sprite height.
-
gfx_AllocRLETSprite(data_size, malloc_routine)
Dynamically allocates memory for a sprite with RLE transparency.
Allocates the memory with
malloc_routine
. ReturnsNULL
upon allocation failure.data_size
is the maximum predicted/calculated size of the sprite data (excluding the width and height bytes) that will be stored in the allocated sprite. Sprite data size could be up to(width + 1) * height * 3 / 2
bytes in the worst case, in which pixels horizontally alternate between non-transparent and transparent and each row begins with a non-transparent pixel. But if the average length of a horizontal transparent run is at least 2, then the sprite data will be no larger than(width + 1) * height
bytes. The exact data size necessary is2 * hr + nont + rnont - ret
bytes, where:hr
: Number of horizontal transparent runs.nont
: Number of non-transparent runs.rnont
: Number of rows beginning with a non-transparent pixel.rbnont
: Number of rows beginning with a non-transparent pixel.ret
: Number of rows ending with a transparent pixel.Remark
If using
malloc
as themalloc_routine
, gfx_MallocRLETSprite() can be used as a shortcut.Note
If not used in a dynamic context and
data_size
is static, consider statically allocating the sprite instead with gfx_UninitedRLETSprite() or gfx_TempRLETSprite().- Parameters
data_size – (Maximum) sprite data size.
malloc_routine – Malloc implementation to use.
- Returns
A pointer to the allocated sprite.
-
gfx_MallocRLETSprite(data_size)
Dynamically allocates memory for a sprite with RLE transparency using
malloc
.data_size
is the size to allocate for sprite data; see gfx_AllocRLETSprite() for information. ReturnsNULL
upon allocation failure.See also
Note
If not used in a dynamic context and
data_size
is static, consider statically allocating the sprite instead with gfx_UninitedRLETSprite() or gfx_TempRLETSprite().- Parameters
data_size – (Maximum) sprite data size.
- Returns
A pointer to the allocated sprite.
-
gfx_UninitedRLETSprite(name, data_size)
Statically allocates uninitialized memory for a sprite with RLE transparency.
Declares a
gfx_rletsprite_t *
with the givenname
pointing to the allocated memory.data_size
is the size to allocate for sprite data; see gfx_AllocRLETSprite() for information.See also
Warning
If used outside of a function body, the memory will be allocated in the global uninitialized data segment (BSS). If used inside a function body, the memory will be allocated on the stack. If the sprite is sufficiently large, usage inside a function body will overflow the stack, so it is recommended that this normally be used outside of a function body.
- Parameters
name – Name of declared
gfx_rletsprite_t *
.data_size – (Maximum) sprite data size.
-
gfx_palette
Used for accessing the lcd palette directly.
This consists of 256 entries, each 2 bytes in size for a total of 512 bytes.
-
gfx_vram
Array of the LCD RAM.
Total of 153600 bytes in size.
See also
-
gfx_SetTile(tilemap, x_offset, y_offset, value)
Sets a particular tile’s value given an initialized tilemap structure and pixel offsets
- Parameters
tilemap – Pointer to initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
value – Value to set tile to.
-
gfx_GetTile(tilemap, x_offset, y_offset)
Gets a particular tile’s value given an initialized tilemap structure and pixel offsets.
- Parameters
tilemap – Pointer to an initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
-
gfx_SetTileMapped(tilemap, col, row, value)
Sets a particular tile’s value given an initialized tilemap structure and mapped offsets.
- Parameters
tilemap – Pointer to initialized tilemap structure.
col – Column of tile in tilemap.
row – Row of tile in tilemap.
value – Value to set tile to.
-
gfx_GetTileMapped(tilemap, col, row)
Gets a particular tile’s value given an initialized tilemap structure and mapped offsets.
- Parameters
tilemap – Pointer to an initialized tilemap structure.
col – Column of tile in tilemap.
row – Row of tile in tilemap.
-
gfx_Circle_NoClip(x, y, radius)
Draws an unclipped circle outline.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
radius – The radius of the circle.
-
gfx_SetDrawBuffer()
Makes the subsequent graphx drawing routines act on the buffer.
-
gfx_SetDrawScreen()
Makes the subsequent graphx drawing routines act on the screen.
-
gfx_BlitScreen()
Copies the screen to the buffer
-
gfx_BlitBuffer()
Copies the buffer to the screen
-
gfx_GetSprite_NoClip(sprite_buffer, x, y)
Grabs the background behind an unclipped sprite.
This is useful for partial redraw.
Note
sprite_buffer
must be pointing to a large enough buffer to hold (width * height + 2) number of bytes.- Parameters
sprite_buffer – Buffer used to store grabbed sprite.
x – X coordinate to grab sprite.
y – Y coordinate to grab sprite.
- Returns
A pointer to sprite_buffer.
-
gfx_RotatedTransparentSprite_NoClip(sprite, x, y, angle)
Helper macro to only perform rotation using gfx_RotatedScaledTransparentSprite_NoClip.
- Parameters
sprite – Input sprite to rotate/scale.
x – X coordinate position.
y – Y coordinate position.
angle – 256 position angular integer.
-
gfx_RotatedSprite_NoClip(sprite, x, y, angle)
Helper macro to only perform rotation using gfx_RotatedScaledSprite_NoClip.
- Parameters
sprite – Input sprite to rotate/scale.
x – X coordinate position.
y – Y coordinate position.
angle – 256 position angular integer.
-
gfx_RotateSprite(sprite_in, sprite_out, angle)
Helper macro to only perform rotation using gfx_RotateScaleSprite.
See also
- Parameters
sprite_in – Input sprite to rotate.
sprite_out – Pointer to where rotated sprite will be stored.
angle – 256 position angular integer.
- Returns
A pointer to
sprite_out
.
-
gfx_ConvertMallocRLETSprite(sprite_in)
Converts a sprite with normal transparency to a sprite with RLE transparency, allocating the exact amount of necessary space for the converted sprite using
malloc
.Width and height will be set in the converted sprite. Returns
NULL
upon allocation failure.The transparent color index in the input sprite is controlled by gfx_SetTransparentColor().
Remark
A gfx_sprite_t can be converted into an appropriately large, already-allocated gfx_rletsprite_t using gfx_ConvertToRLETSprite().
See also
- Parameters
sprite_in – Input sprite with normal transparency.
- Returns
A newly allocated converted sprite with RLE transparency.
-
gfx_RGBTo1555(r, g, b)
Converts an RGB value to a palette color.
Conversion is not 100% perfect, but is quite close.
-
gfx_CheckRectangleHotspot(master_x, master_y, master_width, master_height, test_x, test_y, test_width, test_height)
Checks if a rectangle intersects with another rectangle.
-
gfx_GetZX7SpriteWidth(zx7_sprite)
-
gfx_GetZX7SpriteHeight(zx7_sprite)
-
gfx_GetZX7SpriteSize(zx7_sprite)
Calculates the amount of memory that a zx7-compressed sprite would use when decompressed.
Sprite size is calculated as 2 + (width * height).
ZX7 data always starts with a literal, which is the sprite’s width. The next byte contains flags, which indicates if the following bytes are literals or codewords. If bit 7 of that is zero, the byte immediately after it is a literal and can be read in as sprite height. Otherwise, the bits that follows indicates a codeword, making sprite height the same as width.
- Parameters
zx7_sprite – ZX7-compressed sprite
- Returns
Size, in bytes, of decompressed sprite
Enums
-
enum gfx_mode_t
Stores operating modes of this library.
See also
Values:
-
enumerator gfx_8bpp = 0x27
Enable 8 bits per pixel mode.
-
enumerator gfx_8bpp = 0x27
-
enum gfx_tilemap_type_t
Used for defining tile types.
See also
Values:
-
enumerator gfx_tile_no_pow2 = 0
Use when the tile width/height is not a power of 2.
-
enumerator gfx_tile_2_pixel
Use when the tile width/height is 2 pixels.
-
enumerator gfx_tile_4_pixel
Use when the tile width/height is 4 pixels.
-
enumerator gfx_tile_8_pixel
Use when the tile width/height is 8 pixels.
-
enumerator gfx_tile_16_pixel
Use when the tile width/height is 16 pixels.
-
enumerator gfx_tile_32_pixel
Use when the tile width/height is 32 pixels.
-
enumerator gfx_tile_64_pixel
Use when the tile width/height is 64 pixels.
-
enumerator gfx_tile_128_pixel
Use when the tile width/height is 128 pixels.
-
enumerator gfx_tile_no_pow2 = 0
Functions
-
gfx_sprite_t *gfx_AllocSprite(uint8_t width, uint8_t height, void *(*malloc_routine)(size_t))
Dynamically allocates memory for a sprite.
Allocates the memory with
malloc_routine
.width
andheight
will be set in the allocated sprite. ReturnsNULL
upon allocation failure.Remark
If using
malloc
as themalloc_routine
, gfx_MallocSprite() can be used as a shortcut.Note
If not used in a dynamic context and
width
andheight
are static, consider statically allocating the sprite instead with gfx_UninitedSprite() or gfx_TempSprite().- Parameters
width – Sprite width.
height – Sprite height.
malloc_routine – Malloc implementation to use.
- Returns
A pointer to the allocated sprite.
-
void gfx_Begin()
Initializes the graphics library setup.
-
void gfx_End(void)
Closes the graphics setup
Restores the LCD to 16bpp preferred by the OS and clears the screen.
-
void gfx_Tilemap(const gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset)
Draws a tilemap given an initialized tilemap structure.
See also
- Parameters
tilemap – Pointer to initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
-
void gfx_Tilemap_NoClip(const gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset)
Draws an unclipped tilemap given an initialized tilemap structure.
See also
- Parameters
tilemap – Pointer to initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
-
void gfx_TransparentTilemap(const gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset)
Draws a transparent tilemap given an initialized tilemap structure.
See also
- Parameters
tilemap – Pointer to initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
-
void gfx_TransparentTilemap_NoClip(const gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset)
Draws an unclipped transparent tilemap given an initialized tilemap structure.
See also
- Parameters
tilemap – Pointer to initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
-
uint8_t *gfx_TilePtr(const gfx_tilemap_t *tilemap, uint24_t x_offset, uint24_t y_offset)
Gets a pointer to a particular tile given an initialized tilemap structure and pixel offsets. Measurement is done from the actual tilemap, not the drawn offset location.
- Parameters
tilemap – Pointer to initialized tilemap structure.
x_offset – Offset in pixels from the left of the tilemap.
y_offset – Offset in pixels from the top of the tilemap.
-
uint8_t *gfx_TilePtrMapped(const gfx_tilemap_t *tilemap, uint8_t col, uint8_t row)
Gets a pointer to a particular tile given an initialized tilemap structure and mapped offsets.
- Parameters
tilemap – Pointer to an initialized tilemap structure.
col – Column of tile in tilemap.
row – Row of tile in tilemap.
-
uint8_t gfx_SetColor(uint8_t index)
Sets the color index that drawing routines will use
This applies to lines, rectangles, circles, etc.
- Parameters
index – Color index to set.
- Returns
Previous set color index.
-
uint8_t gfx_SetTransparentColor(uint8_t index)
Sets the transparent index that drawing routines will use
This currently applies to transparent sprites, both scaled and unscaled
- Parameters
index – Transparent color index to set.
- Returns
The previous transparent color index.
-
void gfx_SetDefaultPalette(gfx_mode_t mode)
Sets up the default palette for the given mode
- Parameters
mode – Palette to use.
-
void gfx_SetPalette(const void *palette, uint24_t size, uint8_t offset)
Sets entries in the palette
- Parameters
palette – Pointer to palette to set.
size – Size of palette in bytes.
offset – Palette index to insert at.
-
void gfx_FillScreen(uint8_t index)
Fills the screen with a given palette index.
- Parameters
index – Color index to fill with.
-
void gfx_ZeroScreen(void)
Implements a faster version of gfx_FillScreen(0).
See also
-
void gfx_SetPixel(uint24_t x, uint8_t y)
Sets a pixel to the global color index
This is measured from the top left origin of the screen. Pixels are only clipped within the screen boundaries
See also
- Parameters
x – X coordinate location
y – Y coordinate location
-
uint8_t gfx_GetPixel(uint24_t x, uint8_t y)
Gets a pixel’s color index
This is measured from the top left origin of the screen. Pixels are only clipped within the screen boundaries
- Parameters
x – X coordinate location
y – Y coordinate location
-
void gfx_Line(int x0, int y0, int x1, int y1)
Draws a line
This is measured from the top left origin of the screen.
- Parameters
x0 – First X coordinate
y0 – First Y coordinate
x1 – Second X coordinate
y1 – Second Y coordinate
-
void gfx_Line_NoClip(uint24_t x0, uint8_t y0, uint24_t x1, uint8_t y1)
Draws an unclipped line.
This is measured from the top left origin of the screen.
- Parameters
x0 – First X coordinate.
y0 – First Y coordinate.
x1 – Second X coordinate.
y1 – Second Y coordinate.
-
void gfx_HorizLine(int x, int y, int length)
Draws a horizontal line.
This is measured from the top left origin of the screen. Performs faster than using gfx_Line.
- Parameters
x – X coordinate.
y – Y coordinate.
length – Length of line.
-
void gfx_HorizLine_NoClip(uint24_t x, uint8_t y, uint24_t length)
Draws an unclipped horizontal line.
This is measured from the top left origin of the screen. Performs faster than using gfx_Line.
- Parameters
x – X coordinate.
y – Y coordinate.
length – Length of line.
-
void gfx_VertLine(int x, int y, int length)
Draws a vertical line
This is measured from the top left origin of the screen. Performs faster than using
gfx_Line
- Parameters
x – X coordinate
y – Y coordinate
length – Length of line
-
void gfx_VertLine_NoClip(uint24_t x, uint8_t y, uint24_t length)
Draws an unclipped vertical line.
This is measured from the top left origin of the screen. Performs faster than using gfx_Line.
- Parameters
x – X coordinate.
y – Y coordinate.
length – Length of line.
-
void gfx_Rectangle(int x, int y, int width, int height)
Draws a rectangle outline.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
width – Width of rectangle.
height – Height of rectangle.
-
void gfx_Rectangle_NoClip(uint24_t x, uint8_t y, uint24_t width, uint8_t height)
Draws an unclipped rectangle outline.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
width – Width of rectangle.
height – Height of rectangle.
-
void gfx_FillRectangle(int x, int y, int width, int height)
Draws a filled rectangle.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
width – Width of rectangle.
height – Height of rectangle.
-
void gfx_FillRectangle_NoClip(uint24_t x, uint8_t y, uint24_t width, uint8_t height)
Draws an unclipped filled rectangle
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate
y – Y coordinate
width – Width of rectangle
height – Height of rectangle
-
void gfx_Circle(int x, int y, uint24_t radius)
Draws a circle outline.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
radius – The radius of the circle.
-
void gfx_FillCircle(int x, int y, uint24_t radius)
Draws a filled circle.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
radius – The radius of the circle.
-
void gfx_FillCircle_NoClip(uint24_t x, uint8_t y, uint24_t radius)
Draws an unclipped filled circle.
This is measured from the top left origin of the screen.
- Parameters
x – X coordinate.
y – Y coordinate.
radius – The radius of the circle.
-
void gfx_Polygon(const int *points, unsigned num_points)
Draws a clipped polygon outline.
Points are measured from the top left origin of the screen.
int points[6] = { 160, 1, // (x0, y0) 1, 238, // (x1, y1) 318,238, // (x2, y2) }; num_points = 3; gfx_Polygon(points, num_points);
- Parameters
points – Pointer to x and y pairs.
num_points – Number of x and y pairs.
-
void gfx_Polygon_NoClip(const int *points, unsigned num_points)
Draws an unclipped polygon outline
Points are measured from the top left origin of the screen.
int points[6] = { 160, 1, // (x0, y0) 1, 238, // (x1, y1) 318,238, // (x2, y2) }; num_points = 3; gfx_Polygon_NoClip(points, num_points)
- Parameters
points – Pointer to x and y pairs
num_points – Number of x and y pairs
-
void gfx_FillTriangle(int x0, int y0, int x1, int y1, int x2, int y2)
Draws a clipped filled triangle.
Points are measured from the top left origin of the screen.
- Parameters
x0 – First X coordinate.
y0 – First Y coordinate.
x1 – Second X coordinate.
y1 – Second Y coordinate.
x2 – Third X coordinate.
y2 – Third Y coordinate.
-
void gfx_FillTriangle_NoClip(int x0, int y0, int x1, int y1, int x2, int y2)
Draws a unclipped filled triangle.
Points are measured from the top left origin of the screen.
- Parameters
x0 – First X coordinate.
y0 – First Y coordinate.
x1 – Second X coordinate.
y1 – Second Y coordinate.
x2 – Third X coordinate.
y2 – Third Y coordinate.
-
void gfx_SetDraw(uint8_t location)
Forces all graphics routines draw location.
See also
- Parameters
location – Location routines should draw to.
-
uint8_t gfx_GetDraw(void)
Gets the current drawing buffer.
See also
- Returns
Location type enumeration.
-
void gfx_SwapDraw(void)
Swaps the roles of the screen and drawing buffers.
Does not wait for the old screen buffer to finish being displayed. Instead, the next invocation of a graphx drawing function will block, waiting for this event. To block and wait explicitly, use gfx_Wait().
The LCD driver maintains its own screen buffer pointer for the duration of a refresh. The swap performed by this function will only be picked up at a point between refreshes.
Remark
In practice, this function should be invoked immediately after finishing drawing a frame to the drawing buffer, and invocation of the first graphx drawing function for the next frame should be scheduled as late as possible relative to non-drawing logic. Non-drawing logic can execute during time when a drawing function may otherwise block.
-
void gfx_Wait(void)
Waits for the screen buffer to finish being displayed after gfx_SwapDraw().
Remark
In practice, this function should not need to be invoked by user code. It should be invoked by custom drawing functions (as late as reasonably possible) before writing to the drawing buffer, gfx_vbuffer.
-
void gfx_Blit(gfx_location_t src)
Copies the input buffer to the opposite buffer.
No clipping is performed; as it is a copy not a draw.
See also
- Parameters
src – drawing location to copy from.
-
void gfx_BlitLines(gfx_location_t src, uint8_t y_loc, uint8_t num_lines)
Copies lines from the input buffer to the opposite buffer.
No clipping is performed; as it is a copy not a draw.
See also
- Parameters
src – drawing location to copy from.
y_loc – Y Location to begin copying at.
num_lines – Number of lines to copy.
-
void gfx_BlitRectangle(gfx_location_t src, uint24_t x, uint8_t y, uint24_t width, uint24_t height)
Transfers a rectangle from the source graphics buffer to the opposite buffer.
No clipping is performed; as it is a copy not a draw.
See also
- Parameters
src – drawing location to copy from.
x – X coordinate.
y – Y coordinate.
width – Width of rectangle.
height – Height of rectangle.
-
void gfx_CopyRectangle(gfx_location_t src, gfx_location_t dst, uint24_t src_x, uint8_t src_y, uint24_t dst_x, uint8_t dst_y, uint24_t width, uint8_t height)
Copies a rectangle between graphics buffers or to the same graphics buffer.
No clipping is performed; as it is a copy not a draw.
See also
- Parameters
src – Graphics buffer to copy from.
dst – Graphics buffer to copy to.
src_x – X coordinate on src.
src_y – Y coordinate on src.
dst_x – X coordinate on dst.
dst_y – Y coordinate on dst.
width – Width of rectangle.
height – Height of rectangle.
-
void gfx_SetTextScale(uint8_t width_scale, uint8_t height_scale)
Sets the scaling for text. Scaling is performed by multiplying the width / height of the currently loaded text by the supplied scaling factors.
Values in the range from 1-5 are preferred.
- Parameters
width_scale – Text width scaling
height_scale – Text height scaling
-
void gfx_PrintChar(const char c)
Prints a single character.
Outputs a character at the current cursor position.
Note
By default, no text clipping is performed. See gfx_SetTextConfig.
- Parameters
c – Character to print
-
void gfx_PrintInt(int n, uint8_t length)
Prints a signed integer.
Outputs at the current cursor position. Padded with leading zeros if necessary to satisfy the specified minimum length.
Note
By default, no text clipping is performed. See gfx_SetTextConfig.
Note
length
must be between 1 and 8, inclusive- Parameters
n – Integer to print.
length – Minimum number of characters to print.
-
void gfx_PrintUInt(unsigned int n, uint8_t length)
Prints an unsigned integer.
Outputs at the current cursor position. Padded with leading zeros if necessary to satisfy the specified minimum length.
Note
By default, no text clipping is performed. See gfx_SetTextConfig.
Note
length
must be between 1 and 8, inclusive- Parameters
n – Unsigned integer to print.
length – Minimum number of characters to print.
-
void gfx_PrintString(const char *string)
Prints a string.
Outputs a string at the current cursor position.
Note
By default, no text clipping is performed. See gfx_SetTextConfig.
- Parameters
string – Pointer to string to print.
-
void gfx_PrintStringXY(const char *string, int x, int y)
Prints a string at a specific location.
Outputs a string at the supplied coordinates. Position is measured from top left origin of screen.
Note
By default, no text clipping is performed. See gfx_SetTextConfig.
- Parameters
string – Pointer to string to print.
x – X coordinate.
y – Y coordinate.
-
int gfx_GetTextX(void)
- Returns
The current text cursor X position.
-
int gfx_GetTextY(void)
- Returns
The current text cursor Y position.
-
void gfx_SetTextXY(int x, int y)
Sets the text cursor X and Y positions.
- Parameters
x – X coordinate
y – Y coordinate
-
void gfx_SetTextConfig(uint8_t config)
Sets the configuration for the text routines.
See also
Note
Scaled text does not clip
- Parameters
config – Set the options for the text mode.
-
uint8_t gfx_SetTextFGColor(uint8_t color)
Sets the text foreground color.
Note
Default text foreground color index: 0.
- Parameters
color – Color index to set.
- Returns
Previous text foreground color index.
-
uint8_t gfx_SetTextBGColor(uint8_t color)
Sets the text background color.
Note
Default text background color index: 255.
- Parameters
color – Color index to set.
- Returns
Previous text background color index.
-
uint8_t gfx_SetTextTransparentColor(uint8_t color)
Sets the text transparency color.
Note
Default text transparency color index: 255.
- Parameters
color – Color index to set.
- Returns
Previous text transparency color index.
-
void gfx_Sprite(const gfx_sprite_t *sprite, int x, int y)
Draws a sprite.
- Parameters
sprite – Pointer to an initialized sprite structure.
x – X coordinate.
y – Y coordinate.
-
void gfx_Sprite_NoClip(const gfx_sprite_t *sprite, uint24_t x, uint8_t y)
Draws an unclipped sprite.
- Parameters
sprite – Pointer to an initialized sprite structure.
x – X coordinate.
y – Y coordinate.
-
void gfx_TransparentSprite(const gfx_sprite_t *sprite, int x, int y)
Draws a transparent sprite.
- Parameters
sprite – Pointer to an initialized sprite structure.
x – X coordinate.
y – Y coordinate.
-
void gfx_TransparentSprite_NoClip(const gfx_sprite_t *sprite, uint24_t x, uint8_t y)
Draws an unclipped transparent sprite.
- Parameters
sprite – Pointer to an initialized sprite structure.
x – X coordinate.
y – Y coordinate.
-
gfx_sprite_t *gfx_GetSprite(gfx_sprite_t *sprite_buffer, int x, int y)
Grabs the background behind a sprite.
This is useful for partial redraw.
Note
sprite_buffer
must be pointing to a large enough buffer to hold (width * height + 2) number of bytes.- Parameters
sprite_buffer – Buffer used to store grabbed sprite.
x – X coordinate to grab sprite.
y – Y coordinate to grab sprite.
- Returns
A pointer to sprite_buffer.
-
void gfx_ScaledSprite_NoClip(const gfx_sprite_t *sprite, uint24_t x, uint8_t y, uint8_t width_scale, uint8_t height_scale)
Scales an unclipped sprite.
Scaling factors must be greater than or equal to 1, and an integer factor. Sprites are scaled by multiplying the dimensions by the respective factors.
Note
Usable with gfx_GetSprite in order to create clipped versions.
- Parameters
sprite – Pointer to an initialized sprite structure.
x – X coordinate.
y – Y coordinate.
width_scale – Width scaling factor.
height_scale – Height scaling factor.
-
void gfx_ScaledTransparentSprite_NoClip(const gfx_sprite_t *sprite, uint24_t x, uint8_t y, uint8_t width_scale, uint8_t height_scale)
Scales an unclipped transparent sprite.
Scaling factors must be greater than or equal to 1, and an integer factor. Sprites are scaled by multiplying the dimensions by the respective factors.
Note
Usable with gfx_GetSprite in order to create clipped versions.
- Parameters
sprite – Pointer to an initialized sprite structure.
x – X coordinate.
y – Y coordinate.
width_scale – Width scaling factor.
height_scale – Height scaling factor.
-
uint8_t gfx_RotatedScaledTransparentSprite_NoClip(const gfx_sprite_t *sprite, uint24_t x, uint8_t y, uint8_t angle, uint8_t scale)
Fixed Rotation with scaling factor for sprites.
Note
A scale factor of 64 represents 100% scaling.
Warning
This routine only accepts square input sprites.
- Parameters
sprite – Input sprite to rotate/scale.
x – X coordinate position.
y – Y coordinate position.
angle – 256 position angular integer.
scale – Scaling factor; range is about 1% to 400% scale.
- Returns
The size of the sprite after scaling. This can be used for centering purposes.
-
uint8_t gfx_RotatedScaledSprite_NoClip(const gfx_sprite_t *sprite, uint24_t x, uint8_t y, uint8_t angle, uint8_t scale)
Fixed Rotation with scaling fator for sprites without transparency.
Note
A scale factor of 64 represents 100% scaling.
Warning
This routine only accepts square input sprites.
- Parameters
sprite – Input sprite to rotate/scale.
x – X coordinate position.
y – Y coordinate position.
angle – 256 position angular integer.
scale – Scaling factor; range is about 1% to 400% scale. 64 represents 100% scaling.
- Returns
The size of the sprite after scaling. This can be used for centering purposes.
-
gfx_sprite_t *gfx_FlipSpriteX(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out)
Flips a sprite along the X axis.
Note
sprite_in and sprite_out cannot be the same.
- Parameters
sprite_in – Input sprite to flip.
sprite_out – Pointer to where flipped sprite will be stored.
- Returns
A pointer to sprite_out.
-
gfx_sprite_t *gfx_FlipSpriteY(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out)
Flips a sprite along the Y axis.
Note
sprite_in and sprite_out cannot be the same.
- Parameters
sprite_in – Input sprite to flip.
sprite_out – Pointer to where flipped sprite will be stored.
- Returns
A pointer to sprite_out.
-
gfx_sprite_t *gfx_RotateSpriteC(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out)
Rotates a sprite 90 degrees clockwise.
Note
sprite_in and sprite_out cannot be the same.
- Parameters
sprite_in – Input sprite to rotate.
sprite_out – Pointer to where rotated sprite will be stored.
- Returns
A pointer to sprite_out.
-
gfx_sprite_t *gfx_RotateSpriteCC(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out)
Rotates a sprite 90 degrees counter clockwise.
Note
sprite_in and sprite_out cannot be the same.
- Parameters
sprite_in – Input sprite to rotate.
sprite_out – Pointer to where rotated sprite will be stored.
- Returns
A pointer to sprite_out.
-
gfx_sprite_t *gfx_RotateSpriteHalf(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out)
Rotates a sprite 180 degrees.
- Parameters
sprite_in – Input sprite to rotate.
sprite_out – Pointer to where rotated sprite will be stored.
- Returns
A pointer to sprite_out.
-
gfx_sprite_t *gfx_ScaleSprite(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out)
Resizes a sprite to new dimensions.
Place new image dimensions in sprite_out; i.e. sprite_out->width = 80; sprite_out->height = 20.
- Parameters
sprite_in – Input sprite to scale.
sprite_out – Pointer to where scaled sprite will be stored.
- Returns
A pointer to
sprite_out
.
-
gfx_sprite_t *gfx_RotateScaleSprite(const gfx_sprite_t *sprite_in, gfx_sprite_t *sprite_out, uint8_t angle, uint8_t scale)
Fixed Rotation with scaling factor for sprites.
The output sprite is updated with the dimensions required for the implemented scaling factor. You must make sure that
sprite_out
has enough memory to store the needed output sprite. This can be found with the following formula: size = (max_scale / 64) * width * height + 2;Note
A scale factor of 64 represents 100% scaling.
Warning
This routine only accepts square input sprites.
- Parameters
sprite_in – Input sprite to rotate/scale.
sprite_out – Pointer to where rotated/scaled sprite will be stored.
angle – 256 position angular integer.
scale – Scaling factor; range is about 1% to 400% scale.
- Returns
A pointer to
sprite_out
.
-
gfx_sprite_t *gfx_GetSpriteChar(char c)
Creates a temporary character sprite.
This may be useful for performing rotations and other. operations on characters. The sprite returned is always 8x8 pixels.
- Parameters
c – Character to generate.
- Returns
A sprite of the character data.
-
uint8_t *gfx_SetFontData(const uint8_t *data)
Sets the font’s character data.
Fonts can be created manually or and exported to a C-style format using 8x8 Pixel ROM Font Editor: (https://www.min.at/prinz/o/software/pixelfont/#download)
Note
Format of font data is 8 bytes horizontally aligned.
- Parameters
data – Pointer to formatted 8x8 pixel font.
- Returns
Pointer to previous font data.
-
uint8_t *gfx_SetCharData(uint8_t index, const uint8_t *data)
Sets the font data for a specific character.
See also
Note
Format of font data is 8 bytes horizontally aligned.
- Parameters
index – Character index to modify. (if using default font, values range from 0-127, custom font can have indexes 0-255).
data – Pointer to formatted 8x8 pixel font.
- Returns
Pointer to current character data if
data
is NULL, otherwise a pointer to next character data.
-
void gfx_SetFontSpacing(const uint8_t *spacing)
Sets the font spacing for each character. Format is an array of bytes, where each index in the array corresponds to the character’s numerical value.
- Parameters
spacing – Pointer to array of character spacing.
-
uint8_t gfx_SetFontHeight(uint8_t height)
Sets the height in pixels of each character.
The default value is 8 pixels.
- Parameters
height – New font height in pixels.
- Returns
Previous height of font in pixels.
-
void gfx_SetMonospaceFont(uint8_t spacing)
Sets a monospaced font width.
Note
To disable monospaced font, set to 0.
- Parameters
spacing – Distance between characters.
-
unsigned int gfx_GetStringWidth(const char *string)
Gets the pixel width of the given string.
Note
Takes into account monospacing flag.
- Parameters
string – Pointer to a string.
-
unsigned int gfx_GetCharWidth(const char c)
Gets the pixel width of the given character.
Note
Takes into account monospacing flag.
- Parameters
c – Character to get width of.
- Returns
Width in pixels of character.
-
void gfx_SetClipRegion(int xmin, int ymin, int xmax, int ymax)
Sets the dimensions of the drawing window for all clipped routines.
- Parameters
xmin – Minimum x coordinate, inclusive (default 0).
ymin – Minimum y coordinate, inclusive (default 0).
xmax – Maximum x coordinate, exclusive (default 320).
ymax – Maximum y coordinate, exclusive (default 240).
-
bool gfx_GetClipRegion(gfx_region_t *region)
Clips a region to fit within the drawing window using Cohen-Sutherland.
- Returns
False if offscreen, true if onscreen.
-
void gfx_ShiftDown(uint8_t pixels)
Shifts/Slides the drawing window down.
Note
Remnant data after a shift is undefined.
- Parameters
pixels – Number of pixels to shift.
-
void gfx_ShiftUp(uint8_t pixels)
Shifts/Slides the drawing window up.
Note
Remnant data after a shift is undefined.
- Parameters
pixels – Number of pixels to shift.
-
void gfx_ShiftLeft(uint24_t pixels)
Shifts/Slides the drawing window left.
Note
Remnant data after a shift is undefined.
- Parameters
pixels – Number of pixels to shift.
-
void gfx_ShiftRight(uint24_t pixels)
Shifts/Slides the drawing window right.
Note
Remnant data after a shift is undefined.
- Parameters
pixels – Number of pixels to shift.
-
uint16_t gfx_Lighten(uint16_t color, uint8_t amount)
Lightens a given 1555 color; useful for palette color conversions.
Note
0 returns full white, 255 returns original color.
- Parameters
color – Original color input in 1555 format.
amount – Amount to lighten by.
- Returns
Lightened color.
-
uint16_t gfx_Darken(uint16_t color, uint8_t amount)
Darkens a given 1555 color; useful for palette color conversions.
Note
0 returns full black, 255 returns original color.
- Parameters
color – Original color input in 1555 format.
amount – Amount to darken by.
- Returns
Darkened color.
-
void gfx_FloodFill(unsigned int x, uint8_t y, uint8_t color)
Fills an area with a color.
Note
This routine performs clipping to stay within the window, but you must ensure it starts in the window.
- Parameters
x – X coordinate to begin filling at.
y – Y coordinate to begin filling at.
color – New color to fill with.
-
void gfx_RLETSprite(const gfx_rletsprite_t *sprite, int x, int y)
Draws a sprite with RLE transparency.
- Parameters
sprite – sprite to draw.
x – X coordinate.
y – Y coordinate.
-
void gfx_RLETSprite_NoClip(const gfx_rletsprite_t *sprite, uint24_t x, uint8_t y)
Draws an unclipped sprite with RLE transparency.
- Parameters
sprite – Sprite to draw.
x – X coordinate.
y – Y coordinate.
-
gfx_sprite_t *gfx_ConvertFromRLETSprite(const gfx_rletsprite_t *sprite_in, gfx_sprite_t *sprite_out)
Converts a sprite with RLE transparency to a sprite with normal transparency.
Width and height will be set in the converted sprite.
The transparent color index in the converted sprite is controlled by gfx_SetTransparentColor().
See also
See also
- Attention
The output sprite must have been allocated with a large enough
data
field to hold the converted sprite data, which will bewidth * height
bytes large.
- Parameters
sprite_in – [in] Input sprite with RLE transparency.
sprite_out – [out] Converted sprite with normal transparency.
- Returns
The converted sprite.
-
gfx_rletsprite_t *gfx_ConvertToRLETSprite(const gfx_sprite_t *sprite_in, gfx_rletsprite_t *sprite_out)
Converts a sprite with normal transparency to a sprite with RLE transparency.
Width and height will be set in the converted sprite.
The transparent color index in the input sprite is controlled by gfx_SetTransparentColor().
See also
- Attention
The output sprite must have been allocated with a large enough data field to hold the converted sprite data; see gfx_AllocRLETSprite() for information.
Note
To avoid needing to predict the output size and risking either the prediction being too high and wasting space, or being too low and corrupting memory, gfx_ConvertMallocRLETSprite() can be used instead to allocate the exact amount of necessary space for the converted sprite.
- Parameters
sprite_in – [in] Input sprite with normal transparency.
sprite_out – [out] Converted sprite with RLE transparency.
- Returns
The converted sprite.
-
gfx_rletsprite_t *gfx_ConvertToNewRLETSprite(const gfx_sprite_t *sprite_in, void *(*malloc_routine)(size_t))
Converts a sprite with normal transparency to a sprite with RLE transparency, allocating the exact amount of necessary space for the converted sprite.
Allocates the memory with
malloc_routine
. Width and height will be set in the converted sprite. ReturnsNULL
upon allocation failure.The transparent color index in the input sprite is controlled by gfx_SetTransparentColor().
Remark
If using
malloc
as themalloc_routine
, gfx_ConvertMallocRLETSprite() can be used as a shortcut.Remark
A gfx_sprite_t can be converted into an appropriately large, already-allocated gfx_rletsprite_t using gfx_ConvertToRLETSprite().
See also
- Parameters
sprite_in – Input sprite with normal transparency.
malloc_routine – Malloc implementation to use.
- Returns
A newly allocated converted sprite with RLE transparency.
-
struct gfx_sprite_t
- #include <>
Sprite (image) type.
Whether or not a sprite includes transparency is not explicitly encoded, and is determined only by usage. If used with transparency, transparent pixels are those with a certain color index, which can be set with gfx_SetTransparentColor().
Remark
Create at compile-time with a tool like convimg. Create at runtime (with uninitialized data) with gfx_MallocSprite(), gfx_UninitedSprite(), or gfx_TempSprite().
- Attention
Displaying a gfx_rletsprite_t (which includes transparency) is significantly faster than displaying a gfx_sprite_t with transparency, and should be preferred. However, gfx_rletsprite_t does not support transformations, such as flipping and rotation. Such transformations can be applied to a gfx_sprite_t, which can then be converted to a gfx_rletsprite_t for faster display using gfx_ConvertToNewRLETSprite() or gfx_ConvertToRLETSprite().
-
struct gfx_rletsprite_t
- #include <>
Sprite (image) type with RLE transparency.
Remark
Create at compile-time with a tool like convimg.
- Attention
Displaying a gfx_rletsprite_t (which includes transparency) is significantly faster than displaying a gfx_sprite_t with transparency, and should be preferred. However, gfx_rletsprite_t does not support transformations, such as flipping and rotation. Such transformations can be applied to a gfx_sprite_t, which can then be converted to a gfx_rletsprite_t for faster display using gfx_ConvertToNewRLETSprite() or gfx_ConvertToRLETSprite().
-
struct gfx_point_t
- #include <>
A structure for working with 2D points.
-
struct gfx_region_t
- #include <>
Defines a rectangular graphics region.
See also
-
struct gfx_tilemap_t
- #include <>
Defines tilemap structure.
See also
Public Members
-
uint8_t *map
Pointer to tilemap array.
-
gfx_sprite_t **tiles
Pointer to tileset sprites for the tilemap.
-
uint8_t tile_height
Individual tile height.
-
uint8_t tile_width
Individual tile width.
-
uint8_t draw_height
Number of tilemap rows to draw.
-
uint8_t draw_width
Number of tilemap columns to draw.
-
uint8_t type_width
Tile type height
See also
-
uint8_t type_height
Tile type width
See also
-
uint8_t height
Total number of rows in the tilemap.
-
uint8_t width
Total number of columns in the tilemap.
-
uint8_t y_loc
Y pixel location on the screen for the tilemap.
-
uint24_t x_loc
X pixel location on the screen for the tilemap.
-
uint8_t *map