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 int rx1 = rx, gx1 = gx, bx1 = bx; \
72 (color_t){.r=(rx1<<3)|(rx1>>3), .g=(gx1<<3)|(gx1>>3), .b=(bx1<<3)|(bx1>>3), .a=ax ? 0xFF : 0}; \
73})
74
76#define RGBA32(rx,gx,bx,ax) ({ \
77 (color_t){.r=rx, .g=gx, .b=bx, .a=ax}; \
78})
79
81inline uint16_t color_to_packed16(color_t c) {
82 return (((int)c.r >> 3) << 11) | (((int)c.g >> 3) << 6) | (((int)c.b >> 3) << 1) | (c.a >> 7);
83}
84
86inline uint32_t color_to_packed32(color_t c) {
87 return *(uint32_t*)&c;
88}
90inline color_t color_from_packed16(uint16_t c) {
91 int r = (c >> 11) & 0x1F;
92 int g = (c >> 6) & 0x1F;
93 int b = (c >> 1) & 0x1F;
94 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) };
95}
96
98inline color_t color_from_packed32(uint32_t c) {
99 return (color_t){ .r=(uint8_t)(c>>24), .g=(uint8_t)(c>>16), .b=(uint8_t)(c>>8), .a=(uint8_t)c };
100}
101
125uint32_t graphics_make_color( int r, int g, int b, int a );
126
148uint32_t graphics_convert_color( color_t color );
149
166void graphics_draw_pixel( surface_t* surf, int x, int y, uint32_t color );
167
184void graphics_draw_pixel_trans( surface_t* surf, int x, int y, uint32_t color );
185
206void graphics_draw_line( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t color );
207
228void graphics_draw_line_trans( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t color );
229
250void graphics_draw_box( surface_t* surf, int x, int y, int width, int height, uint32_t color );
251
272void graphics_draw_box_trans( surface_t* surf, int x, int y, int width, int height, uint32_t color );
273
286void graphics_fill_screen( surface_t* surf, uint32_t c );
287
300void graphics_set_color( uint32_t forecolor, uint32_t backcolor );
301
305void graphics_set_default_font( void );
306
323
341void graphics_draw_character( surface_t* surf, int x, int y, char ch );
342
363void graphics_draw_text( surface_t* surf, int x, int y, const char * const msg );
364
385void graphics_draw_sprite( surface_t* surf, int x, int y, sprite_t *sprite );
386
423void graphics_draw_sprite_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
424
445void graphics_draw_sprite_trans( surface_t* surf, int x, int y, sprite_t *sprite );
446
483void graphics_draw_sprite_trans_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
484
485#ifdef __cplusplus
486}
487#endif
488
/* graphics */
490
491#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:98
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:208
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:266
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:749
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:437
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:161
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:90
void graphics_set_default_font(void)
Set the font to the default.
Definition graphics.c:430
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:355
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:86
void graphics_draw_sprite(surface_t *surf, int x, int y, sprite_t *sprite)
Draw a sprite to a display context.
Definition graphics.c:628
void graphics_fill_screen(surface_t *surf, uint32_t c)
Fill the entire screen with a particular color.
Definition graphics.c:418
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:743
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:324
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:81
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:444
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:594
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:634
Generic color structure.
Definition graphics.h:54
Sprite structure.
Definition sprite.h:42
A surface buffer for graphics.
Definition surface.h:140