libdragon
Loading...
Searching...
No Matches
graphics.h
Go to the documentation of this file.
1
8#ifndef __LIBDRAGON_GRAPHICS_H
9#define __LIBDRAGON_GRAPHICS_H
10
11#include <stdint.h>
12
43#ifdef __cplusplus
44extern "C" {
45#endif
46
48typedef struct surface_s surface_t;
49typedef struct sprite_s sprite_t;
51
53typedef struct __attribute__((packed))
54{
56 uint8_t r;
58 uint8_t g;
60 uint8_t b;
62 uint8_t a;
63} color_t;
64
65#ifndef __cplusplus
66_Static_assert(sizeof(color_t) == 4, "invalid sizeof for color_t");
67#endif
68
70#define RGBA16(rx, gx, bx, ax) \
71 (__builtin_constant_p(rx) && \
72 __builtin_constant_p(gx) && \
73 __builtin_constant_p(bx) && \
74 __builtin_constant_p(ax) ? \
75 ((color_t){ \
76 .r = ((rx) << 3) | ((rx) >> 3), \
77 .g = ((gx) << 3) | ((gx) >> 3), \
78 .b = ((bx) << 3) | ((bx) >> 3), \
79 .a = (ax) ? 0xFF : 0 \
80 }) : \
81 ( __extension__ ({ \
82 int _r = (rx); \
83 int _g = (gx); \
84 int _b = (bx); \
85 int _a = (ax); \
86 (color_t){ \
87 .r = (_r << 3) | (_r >> 3), \
88 .g = (_g << 3) | (_g >> 3), \
89 .b = (_b << 3) | (_b >> 3), \
90 .a = _a ? 0xFF : 0 \
91 }; \
92 }) ) \
93 )
94
96#define RGBA32(rx,gx,bx,ax) ((color_t){.r=rx, .g=gx, .b=bx, .a=ax})
97
99inline uint16_t color_to_packed16(color_t c) {
100 return (((int)c.r >> 3) << 11) | (((int)c.g >> 3) << 6) | (((int)c.b >> 3) << 1) | (c.a >> 7);
101}
102
104inline uint32_t color_to_packed32(color_t c) {
105 return *(uint32_t*)&c;
106}
108inline color_t color_from_packed16(uint16_t c) {
109 int r = (c >> 11) & 0x1F;
110 int g = (c >> 6) & 0x1F;
111 int b = (c >> 1) & 0x1F;
112 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) };
113}
114
116inline color_t color_from_packed32(uint32_t c) {
117 return (color_t){ .r=(uint8_t)(c>>24), .g=(uint8_t)(c>>16), .b=(uint8_t)(c>>8), .a=(uint8_t)c };
118}
119
143uint32_t graphics_make_color( int r, int g, int b, int a );
144
166uint32_t graphics_convert_color( color_t color );
167
184void graphics_draw_pixel( surface_t* surf, int x, int y, uint32_t color );
185
202void graphics_draw_pixel_trans( surface_t* surf, int x, int y, uint32_t color );
203
224void graphics_draw_line( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t color );
225
246void graphics_draw_line_trans( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t color );
247
268void graphics_draw_box( surface_t* surf, int x, int y, int width, int height, uint32_t color );
269
290void graphics_draw_box_trans( surface_t* surf, int x, int y, int width, int height, uint32_t color );
291
304void graphics_fill_screen( surface_t* surf, uint32_t c );
305
318void graphics_set_color( uint32_t forecolor, uint32_t backcolor );
319
323void graphics_set_default_font( void );
324
341
359void graphics_draw_character( surface_t* surf, int x, int y, char ch );
360
381void graphics_draw_text( surface_t* surf, int x, int y, const char * const msg );
382
403void graphics_draw_sprite( surface_t* surf, int x, int y, sprite_t *sprite );
404
441void graphics_draw_sprite_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
442
463void graphics_draw_sprite_trans( surface_t* surf, int x, int y, sprite_t *sprite );
464
501void graphics_draw_sprite_trans_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
502
503#ifdef __cplusplus
504}
505#endif
506
/* graphics */
508
509#endif
uint8_t a
Alpha component.
Definition graphics.h:62
uint8_t g
Green component.
Definition graphics.h:58
uint8_t b
Blue component.
Definition graphics.h:60
uint8_t r
Red component.
Definition graphics.h:56
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:116
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:146
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:211
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:269
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:752
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:440
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:163
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:108
void graphics_set_default_font(void)
Set the font to the default.
Definition graphics.c:433
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:358
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:104
void graphics_draw_sprite(surface_t *surf, int x, int y, sprite_t *sprite)
Draw a sprite to a display context.
Definition graphics.c:631
void graphics_fill_screen(surface_t *surf, uint32_t c)
Fill the entire screen with a particular color.
Definition graphics.c:421
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:746
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:327
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:99
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:87
void graphics_set_color(uint32_t forecolor, uint32_t backcolor)
Set the current forecolor and backcolor for text operations.
Definition graphics.c:113
uint32_t graphics_convert_color(color_t color)
Convert a color structure to a 32-bit representation of an RGBA color.
Definition graphics.c:99
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:447
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:597
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:637
Generic color structure.
Definition graphics.h:54
Sprite structure.
Definition sprite.h:42
A surface buffer for graphics.
Definition surface.h:140