libdragon
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
11#ifdef __cplusplus
12extern "C" {
13#endif
14
16typedef struct surface_s surface_t;
17typedef struct sprite_s sprite_t;
19
26typedef struct __attribute__((packed))
27{
29 uint8_t r;
31 uint8_t g;
33 uint8_t b;
35 uint8_t a;
36} color_t;
37
38_Static_assert(sizeof(color_t) == 4, "invalid sizeof for color_t");
39
41#define RGBA16(rx,gx,bx,ax) ({ \
42 int rx1 = rx, gx1 = gx, bx1 = bx; \
43 (color_t){.r=(rx1<<3)|(rx1>>3), .g=(gx1<<3)|(gx1>>3), .b=(bx1<<3)|(bx1>>3), .a=ax ? 0xFF : 0}; \
44})
45
47#define RGBA32(rx,gx,bx,ax) ({ \
48 (color_t){.r=rx, .g=gx, .b=bx, .a=ax}; \
49})
50
52inline uint16_t color_to_packed16(color_t c) {
53 return (((int)c.r >> 3) << 11) | (((int)c.g >> 3) << 6) | (((int)c.b >> 3) << 1) | (c.a >> 7);
54}
55
57inline uint32_t color_to_packed32(color_t c) {
58 return *(uint32_t*)&c;
59}
61inline color_t color_from_packed16(uint16_t c) {
62 return (color_t){ .r=((c>>11)&0x1F)<<3, .g=((c>>6)&0x1F)<<3, .b=((c>>1)&0x1F)<<3, .a=(c&0x1) ? 0xFF : 0 };
63}
64
66inline color_t color_from_packed32(uint32_t c) {
67 return (color_t){ .r=(c>>24)&0xFF, .g=(c>>16)&0xFF, .b=(c>>8)&0xFF, .a=c&0xFF };
68}
69
70uint32_t graphics_make_color( int r, int g, int b, int a );
71uint32_t graphics_convert_color( color_t color );
72void graphics_draw_pixel( surface_t* surf, int x, int y, uint32_t c );
73void graphics_draw_pixel_trans( surface_t* surf, int x, int y, uint32_t c );
74void graphics_draw_line( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t c );
75void graphics_draw_line_trans( surface_t* surf, int x0, int y0, int x1, int y1, uint32_t c );
76void graphics_draw_box( surface_t* surf, int x, int y, int width, int height, uint32_t color );
77void graphics_draw_box_trans( surface_t* surf, int x, int y, int width, int height, uint32_t color );
78void graphics_fill_screen( surface_t* surf, uint32_t c );
79void graphics_set_color( uint32_t forecolor, uint32_t backcolor );
80void graphics_set_default_font( void );
82void graphics_draw_character( surface_t* surf, int x, int y, char c );
83void graphics_draw_text( surface_t* surf, int x, int y, const char * const msg );
84void graphics_draw_sprite( surface_t* surf, int x, int y, sprite_t *sprite );
85void graphics_draw_sprite_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
86void graphics_draw_sprite_trans( surface_t* surf, int x, int y, sprite_t *sprite );
87void graphics_draw_sprite_trans_stride( surface_t* surf, int x, int y, sprite_t *sprite, int offset );
88
89#ifdef __cplusplus
90}
91#endif
92 /* graphics */
94
95#endif
uint8_t a
Alpha component.
Definition: graphics.h:35
uint8_t g
Green component.
Definition: graphics.h:31
uint8_t b
Blue component.
Definition: graphics.h:33
uint8_t r
Red component.
Definition: graphics.h:29
void graphics_draw_box(surface_t *disp, int x, int y, int width, int height, uint32_t color)
Draw a filled rectangle to a display context.
Definition: graphics.c:495
void graphics_draw_line(surface_t *disp, int x0, int y0, int x1, int y1, uint32_t color)
Draw a line to a given display context.
Definition: graphics.c:339
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:66
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:657
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:61
void graphics_set_default_font(void)
Set the font to the default.
Definition: graphics.c:636
void graphics_draw_box_trans(surface_t *disp, int x, int y, int width, int height, uint32_t color)
Draw a filled rectangle to a display context.
Definition: graphics.c:546
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:57
void graphics_draw_character(surface_t *disp, int x, int y, char ch)
Draw a character to the screen using the built-in font.
Definition: graphics.c:681
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:52
void graphics_draw_sprite(surface_t *disp, int x, int y, sprite_t *sprite)
Draw a sprite to a display context.
Definition: graphics.c:833
void graphics_draw_sprite_trans_stride(surface_t *disp, int x, int y, sprite_t *sprite, int offset)
Draw a sprite from a spritemap to a display context.
Definition: graphics.c:1047
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:136
void graphics_set_color(uint32_t forecolor, uint32_t backcolor)
Set the current forecolor and backcolor for text operations.
Definition: graphics.c:192
uint32_t graphics_convert_color(color_t color)
Convert a color structure to a 32-bit representation of an RGBA color.
Definition: graphics.c:166
void graphics_draw_text(surface_t *disp, int x, int y, const char *const msg)
Draw a null terminated string to a display context.
Definition: graphics.c:779
void graphics_draw_pixel_trans(surface_t *disp, int x, int y, uint32_t color)
Draw a pixel to a given display context with alpha support.
Definition: graphics.c:272
void graphics_draw_sprite_stride(surface_t *disp, int x, int y, sprite_t *sprite, int offset)
Draw a sprite from a spritemap to a display context.
Definition: graphics.c:875
void graphics_draw_sprite_trans(surface_t *disp, int x, int y, sprite_t *sprite)
Draw a sprite to a display context with alpha transparency.
Definition: graphics.c:1004
void graphics_draw_line_trans(surface_t *disp, 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:417
void graphics_draw_pixel(surface_t *disp, int x, int y, uint32_t color)
Draw a pixel to a given display context.
Definition: graphics.c:241
void graphics_fill_screen(surface_t *disp, uint32_t c)
Fill the entire screen with a particular color.
Definition: graphics.c:621
Generic color structure.
Definition: graphics.h:27
Sprite structure.
Definition: sprite.h:40
A surface buffer for graphics.
Definition: surface.h:135