libdragon
Loading...
Searching...
No Matches
graphics.h
Go to the documentation of this file.
1
6#ifndef __LIBDRAGON_GRAPHICS_H
7#define __LIBDRAGON_GRAPHICS_H
8
9#include <stdint.h>
10
41#ifdef __cplusplus
42extern "C" {
43#endif
44
46typedef struct surface_s surface_t;
47typedef struct sprite_s sprite_t;
49
51typedef struct __attribute__((packed))
52{
54 uint8_t r;
56 uint8_t g;
58 uint8_t b;
60 uint8_t a;
61} color_t;
62
63#ifndef __cplusplus
64_Static_assert(sizeof(color_t) == 4, "invalid sizeof for color_t");
65#endif
66
68#define RGBA16(rx,gx,bx,ax) ({ \
69 int rx1 = rx, gx1 = gx, bx1 = bx; \
70 (color_t){.r=(rx1<<3)|(rx1>>3), .g=(gx1<<3)|(gx1>>3), .b=(bx1<<3)|(bx1>>3), .a=ax ? 0xFF : 0}; \
71})
72
74#define RGBA32(rx,gx,bx,ax) ({ \
75 (color_t){.r=rx, .g=gx, .b=bx, .a=ax}; \
76})
77
79inline uint16_t color_to_packed16(color_t c) {
80 return (((int)c.r >> 3) << 11) | (((int)c.g >> 3) << 6) | (((int)c.b >> 3) << 1) | (c.a >> 7);
81}
82
84inline uint32_t color_to_packed32(color_t c) {
85 return *(uint32_t*)&c;
86}
88inline color_t color_from_packed16(uint16_t c) {
89 int r = (c >> 11) & 0x1F;
90 int g = (c >> 6) & 0x1F;
91 int b = (c >> 1) & 0x1F;
92 return (color_t){ .r=(uint8_t)((r << 3) | (r >> 2)), .g=(uint8_t)((g << 3) | (g >> 2)), .b=(uint8_t)((b << 3) | (b >> 2)), .a=(uint8_t)((c&0x1) ? 0xFF : 0) };
93}
94
96inline color_t color_from_packed32(uint32_t c) {
97 return (color_t){ .r=(uint8_t)(c>>24), .g=(uint8_t)(c>>16), .b=(uint8_t)(c>>8), .a=(uint8_t)c };
98}
99
123uint32_t graphics_make_color( int r, int g, int b, int a );
124
146uint32_t graphics_convert_color( color_t color );
147
164void graphics_draw_pixel( surface_t* surf, int x, int y, uint32_t color );
165
182void graphics_draw_pixel_trans( surface_t* surf, int x, int y, uint32_t color );
183
204void graphics_draw_line( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t color );
205
226void graphics_draw_line_trans( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t color );
227
248void graphics_draw_box( surface_t* surf, int x, int y, int width, int height, uint32_t color );
249
270void graphics_draw_box_trans( surface_t* surf, int x, int y, int width, int height, uint32_t color );
271
284void graphics_fill_screen( surface_t* surf, uint32_t c );
285
298void graphics_set_color( uint32_t forecolor, uint32_t backcolor );
299
303void graphics_set_default_font( void );
304
321
339void graphics_draw_character( surface_t* surf, int x, int y, char ch );
340
361void graphics_draw_text( surface_t* surf, int x, int y, const char * const msg );
362
383void graphics_draw_sprite( surface_t* surf, int x, int y, sprite_t *sprite );
384
421void graphics_draw_sprite_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
422
443void graphics_draw_sprite_trans( surface_t* surf, int x, int y, sprite_t *sprite );
444
481void graphics_draw_sprite_trans_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
482
483#ifdef __cplusplus
484}
485#endif
486
/* graphics */
488
489#endif
uint8_t a
Alpha component.
Definition graphics.h:60
uint8_t g
Green component.
Definition graphics.h:56
uint8_t b
Blue component.
Definition graphics.h:58
uint8_t r
Red component.
Definition graphics.h:54
color_t color_from_packed32(uint32_t c)
Create a color_t from the 32-bit packed format used by a FMT_RGBA32 surface (RGBA 8888)
Definition graphics.h:96
void graphics_draw_pixel(surface_t *surf, int x, int y, uint32_t color)
Draw a pixel to a given display context.
Definition graphics.c:145
void graphics_draw_line(surface_t *surf, int x0, int y0, int x1, int y1, uint32_t color)
Draw a line to a given display context.
Definition graphics.c:207
void graphics_draw_line_trans(surface_t *surf, int x0, int y0, int x1, int y1, uint32_t color)
Draw a line to a given display context with alpha support.
Definition graphics.c:265
void graphics_draw_sprite_trans_stride(surface_t *surf, int x, int y, sprite_t *sprite, int offset)
Draw a sprite from a spritemap to a display context.
Definition graphics.c:748
void graphics_set_font_sprite(sprite_t *font)
Set the current font. Should be set before using any of the draw function.
Definition graphics.c:436
void graphics_draw_pixel_trans(surface_t *surf, int x, int y, uint32_t color)
Draw a pixel to a given display context with alpha support.
Definition graphics.c:160
color_t color_from_packed16(uint16_t c)
Create a color_t from the 16-bit packed format used by a FMT_RGBA16 surface (RGBA 5551)
Definition graphics.h:88
void graphics_set_default_font(void)
Set the font to the default.
Definition graphics.c:429
void graphics_draw_box_trans(surface_t *surf, int x, int y, int width, int height, uint32_t color)
Draw a filled rectangle to a display context.
Definition graphics.c:354
uint32_t color_to_packed32(color_t c)
Convert a color_t to the 32-bit packed format used by a FMT_RGBA32 surface (RGBA 8888)
Definition graphics.h:84
void graphics_draw_sprite(surface_t *surf, int x, int y, sprite_t *sprite)
Draw a sprite to a display context.
Definition graphics.c:627
void graphics_fill_screen(surface_t *surf, uint32_t c)
Fill the entire screen with a particular color.
Definition graphics.c:417
void graphics_draw_sprite_trans(surface_t *surf, int x, int y, sprite_t *sprite)
Draw a sprite to a display context with alpha transparency.
Definition graphics.c:742
void graphics_draw_box(surface_t *surf, int x, int y, int width, int height, uint32_t color)
Draw a filled rectangle to a display context.
Definition graphics.c:323
uint16_t color_to_packed16(color_t c)
Convert a color_t to the 16-bit packed format used by a FMT_RGBA16 surface (RGBA 5551)
Definition graphics.h:79
uint32_t graphics_make_color(int r, int g, int b, int a)
Return a packed 32-bit representation of an RGBA color.
Definition graphics.c:86
void graphics_set_color(uint32_t forecolor, uint32_t backcolor)
Set the current forecolor and backcolor for text operations.
Definition graphics.c:112
uint32_t graphics_convert_color(color_t color)
Convert a color structure to a 32-bit representation of an RGBA color.
Definition graphics.c:98
void graphics_draw_character(surface_t *surf, int x, int y, char ch)
Draw a character to the screen using the built-in font.
Definition graphics.c:443
void graphics_draw_text(surface_t *surf, int x, int y, const char *const msg)
Draw a null terminated string to a display context.
Definition graphics.c:593
void graphics_draw_sprite_stride(surface_t *surf, int x, int y, sprite_t *sprite, int offset)
Draw a sprite from a spritemap to a display context.
Definition graphics.c:633
Generic color structure.
Definition graphics.h:52
Sprite structure.
Definition sprite.h:40
A surface buffer for graphics.
Definition surface.h:138