sf2dlib
Simple and Fast 2D library for the Nintendo 3DS
sf2d.h
Go to the documentation of this file.
1 
8 #ifndef SF2D_H
9 #define SF2D_H
10 
11 #include <3ds.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 // Defines
18 
26 #define RGBA8(r, g, b, a) ((((a)&0xFF)<<24) | (((b)&0xFF)<<16) | (((g)&0xFF)<<8) | (((r)&0xFF)<<0))
27 
28 #define RGBA8_GET_R(c) (((c) >> 0) & 0xFF)
29 #define RGBA8_GET_G(c) (((c) >> 8) & 0xFF)
30 #define RGBA8_GET_B(c) (((c) >> 16) & 0xFF)
31 #define RGBA8_GET_A(c) (((c) >> 24) & 0xFF)
32 
36 #define SF2D_GPUCMD_DEFAULT_SIZE 0x80000
37 
41 #define SF2D_TEMPPOOL_DEFAULT_SIZE 0x80000
42 
46 #define SF2D_DEFAULT_DEPTH 0.5f
47 
48 // Enums
49 
54 typedef enum {
55  TEXFMT_RGBA8 = 0,
56  TEXFMT_RGB8 = 1,
57  TEXFMT_RGB5A1 = 2,
58  TEXFMT_RGB565 = 3,
59  TEXFMT_RGBA4 = 4,
60  TEXFMT_IA8 = 5,
61 
62  TEXFMT_I8 = 7,
63  TEXFMT_A8 = 8,
64  TEXFMT_IA4 = 9,
65  TEXFMT_I4 = 10,
66  TEXFMT_A4 = 11,
67  TEXFMT_ETC1 = 12,
68  TEXFMT_ETC1A4 = 13
69 } sf2d_texfmt;
70 
71 
76 typedef enum {
80 } sf2d_place;
81 
82 // Structs
83 
88 typedef struct {
89  float u;
90  float v;
92 
97 typedef struct {
98  float x;
99  float y;
100  float z;
102 
107 typedef struct {
108  unsigned char r;
109  unsigned char g;
110  unsigned char b;
111  unsigned char a;
113 
119 typedef struct {
123 
128 typedef struct {
132 
137 typedef struct {
139  int tiled;
141  u32 params;
142  int width;
143  int height;
144  int pow2_w;
145  int pow2_h;
146  int data_size;
147  void *data;
148 } sf2d_texture;
149 
150 // Basic functions
151 
156 int sf2d_init();
157 
164 int sf2d_init_advanced(int gpucmd_size, int temppool_size);
165 
170 int sf2d_fini();
171 
176 void sf2d_set_3D(int enable);
177 
183 void sf2d_start_frame(gfxScreen_t screen, gfx3dSide_t side);
184 
188 void sf2d_end_frame();
189 
193 void sf2d_swapbuffers();
194 
199 void sf2d_set_vblank_wait(int enable);
200 
205 float sf2d_get_fps();
206 
211 void *sf2d_pool_malloc(u32 size);
212 
218 void *sf2d_pool_memalign(u32 size, u32 alignment);
219 
227 void *sf2d_pool_calloc(u32 nmemb, u32 size);
228 
233 unsigned int sf2d_pool_space_free();
234 
238 void sf2d_pool_reset();
239 
244 void sf2d_set_clear_color(u32 color);
245 
246 // Draw functions
255 void sf2d_draw_line(int x0, int y0, int x1, int y1, u32 color);
256 
265 void sf2d_draw_rectangle(int x, int y, int w, int h, u32 color);
266 
276 void sf2d_draw_rectangle_rotate(int x, int y, int w, int h, u32 color, float rad);
277 
285 void sf2d_draw_fill_circle(int x, int y, int radius, u32 color);
286 
287 // Texture
288 
304 sf2d_texture *sf2d_create_texture(int width, int height, sf2d_texfmt pixel_format, sf2d_place place);
305 
310 void sf2d_free_texture(sf2d_texture *texture);
311 
319 void sf2d_fill_texture_from_RGBA8(sf2d_texture *dst, const void *rgba8, int source_w, int source_h);
320 
332 sf2d_texture *sf2d_create_texture_mem_RGBA8(const void *src_buffer, int src_w, int src_h, sf2d_texfmt pixel_format, sf2d_place place);
333 
339 void sf2d_bind_texture(const sf2d_texture *texture, GPU_TEXUNIT unit);
340 
347 void sf2d_bind_texture_color(const sf2d_texture *texture, GPU_TEXUNIT unit, u32 color);
348 
355 void sf2d_bind_texture_parameters(const sf2d_texture *texture, GPU_TEXUNIT unit, unsigned int params);
356 
364 void sf2d_texture_set_params(sf2d_texture *texture, u32 params);
365 
371 int sf2d_texture_get_params(const sf2d_texture *texture);
372 
379 void sf2d_draw_texture(const sf2d_texture *texture, int x, int y);
380 
388 void sf2d_draw_texture_blend(const sf2d_texture *texture, int x, int y, u32 color);
389 
399 void sf2d_draw_texture_rotate_hotspot(const sf2d_texture *texture, int x, int y, float rad, float center_x, float center_y);
400 
411 void sf2d_draw_texture_rotate_hotspot_blend(const sf2d_texture *texture, int x, int y, float rad, float center_x, float center_y, u32 color);
412 
413 
421 void sf2d_draw_texture_rotate(const sf2d_texture *texture, int x, int y, float rad);
422 
431 void sf2d_draw_texture_rotate_blend(const sf2d_texture *texture, int x, int y, float rad, u32 color);
432 
443 void sf2d_draw_texture_part(const sf2d_texture *texture, int x, int y, int tex_x, int tex_y, int tex_w, int tex_h);
444 
456 void sf2d_draw_texture_part_blend(const sf2d_texture *texture, int x, int y, int tex_x, int tex_y, int tex_w, int tex_h, u32 color);
457 
466 void sf2d_draw_texture_scale(const sf2d_texture *texture, int x, int y, float x_scale, float y_scale);
467 
477 void sf2d_draw_texture_scale_blend(const sf2d_texture *texture, int x, int y, float x_scale, float y_scale, u32 color);
478 
491 void sf2d_draw_texture_part_scale(const sf2d_texture *texture, float x, float y, float tex_x, float tex_y, float tex_w, float tex_h, float x_scale, float y_scale);
492 
506 void sf2d_draw_texture_part_scale_blend(const sf2d_texture *texture, float x, float y, float tex_x, float tex_y, float tex_w, float tex_h, float x_scale, float y_scale, u32 color);
507 
521 void sf2d_draw_texture_part_rotate_scale(const sf2d_texture *texture, int x, int y, float rad, int tex_x, int tex_y, int tex_w, int tex_h, float x_scale, float y_scale);
522 
537 void sf2d_draw_texture_part_rotate_scale_blend(const sf2d_texture *texture, int x, int y, float rad, int tex_x, int tex_y, int tex_w, int tex_h, float x_scale, float y_scale, u32 color);
538 
552 void sf2d_draw_texture_depth(const sf2d_texture *texture, int x, int y, signed short z);
553 
568 void sf2d_draw_texture_depth_blend(const sf2d_texture *texture, int x, int y, signed short z, u32 color);
569 
583 void sf2d_draw_quad_uv(const sf2d_texture *texture, float left, float top, float right, float bottom,
584  float u0, float v0, float u1, float v1, unsigned int params);
585 
593 void sf2d_set_pixel(sf2d_texture *texture, int x, int y, u32 new_color);
594 
602 u32 sf2d_get_pixel(sf2d_texture *texture, int x, int y);
603 
608 void sf2d_texture_tile32(sf2d_texture *texture);
609 
620 void sf2d_set_scissor_test(GPU_SCISSORMODE mode, u32 x, u32 y, u32 w, u32 h);
621 
626 gfxScreen_t sf2d_get_current_screen();
627 
632 gfx3dSide_t sf2d_get_current_side();
633 
634 #ifdef __cplusplus
635 }
636 #endif
637 
638 #endif
int width
Definition: sf2d.h:142
float v
Definition: sf2d.h:90
void sf2d_draw_quad_uv(const sf2d_texture *texture, float left, float top, float right, float bottom, float u0, float v0, float u1, float v1, unsigned int params)
Draws a texture using custom texture coordinates and parameters.
Definition: sf2d.h:79
void sf2d_bind_texture(const sf2d_texture *texture, GPU_TEXUNIT unit)
Binds a texture to a GPU texture unit.
void sf2d_bind_texture_color(const sf2d_texture *texture, GPU_TEXUNIT unit, u32 color)
Binds a texture to a GPU texture unit with a constant color.
sf2d_vector_2f texcoord
Definition: sf2d.h:130
void sf2d_draw_texture(const sf2d_texture *texture, int x, int y)
Draws a texture.
void sf2d_set_scissor_test(GPU_SCISSORMODE mode, u32 x, u32 y, u32 w, u32 h)
Sets the scissor test.
int sf2d_init_advanced(int gpucmd_size, int temppool_size)
Initializates the library (with advanced settings)
void sf2d_draw_texture_rotate_hotspot_blend(const sf2d_texture *texture, int x, int y, float rad, float center_x, float center_y, u32 color)
Draws a texture with rotation around a hotspot with a color.
unsigned char b
Definition: sf2d.h:110
void sf2d_set_clear_color(u32 color)
Sets the screen clear color.
unsigned char a
Definition: sf2d.h:111
sf2d_place place
Definition: sf2d.h:138
void sf2d_start_frame(gfxScreen_t screen, gfx3dSide_t side)
Starts a frame.
void sf2d_set_3D(int enable)
Enables or disables the 3D.
gfx3dSide_t sf2d_get_current_side()
Returns the current screen side (latest call to sf2d_start_frame)
void sf2d_draw_rectangle(int x, int y, int w, int h, u32 color)
Draws a rectangle.
void sf2d_end_frame()
Ends a frame, should be called on pair with sf2d_start_frame.
int sf2d_init()
Initializates the library.
void sf2d_draw_texture_part(const sf2d_texture *texture, int x, int y, int tex_x, int tex_y, int tex_w, int tex_h)
Draws a part of a texture.
Definition: sf2d.h:77
void sf2d_draw_texture_rotate_hotspot(const sf2d_texture *texture, int x, int y, float rad, float center_x, float center_y)
Draws a texture with rotation around a hotspot.
void sf2d_draw_texture_part_rotate_scale_blend(const sf2d_texture *texture, int x, int y, float rad, int tex_x, int tex_y, int tex_w, int tex_h, float x_scale, float y_scale, u32 color)
Draws a part of a texture, with rotation, scaling and color.
sf2d_vector_4uc color
Definition: sf2d.h:121
float sf2d_get_fps()
Returns the FPS (frames per second)
int sf2d_texture_get_params(const sf2d_texture *texture)
Returns the texture params.
int height
Definition: sf2d.h:143
int sf2d_fini()
Finishes the library.
sf2d_texfmt
Represents a texture format.
Definition: sf2d.h:54
Represents a two dimensional float vector.
Definition: sf2d.h:88
int tiled
Definition: sf2d.h:139
void * sf2d_pool_calloc(u32 nmemb, u32 size)
Allocates aligned memory for an array from a temporary pool. Works as sf2d_pool_malloc.
void sf2d_draw_fill_circle(int x, int y, int radius, u32 color)
Draws a filled circle.
sf2d_place
Data allocated on the RAM or VRAM.
Definition: sf2d.h:76
u32 sf2d_get_pixel(sf2d_texture *texture, int x, int y)
Gets a pixel of the texture.
void sf2d_draw_texture_rotate(const sf2d_texture *texture, int x, int y, float rad)
Draws a texture with rotation around its center.
Represents a four dimensional unsigned char vector.
Definition: sf2d.h:107
void sf2d_swapbuffers()
Swaps the framebuffers, should be called once after all the frames have been finished.
void sf2d_draw_line(int x0, int y0, int x1, int y1, u32 color)
Draws a line.
void * sf2d_pool_malloc(u32 size)
Allocates memory from a temporary pool. The pool will be emptied after a sf2d_swapbuffers call...
int pow2_h
Definition: sf2d.h:145
void sf2d_draw_texture_part_blend(const sf2d_texture *texture, int x, int y, int tex_x, int tex_y, int tex_w, int tex_h, u32 color)
Draws a part of a texture with color.
void sf2d_texture_set_params(sf2d_texture *texture, u32 params)
Changes the texture params (filters and wrapping)
void sf2d_bind_texture_parameters(const sf2d_texture *texture, GPU_TEXUNIT unit, unsigned int params)
Binds a texture to a GPU texture unit with custom parameters.
void sf2d_free_texture(sf2d_texture *texture)
Frees a texture.
void sf2d_draw_texture_part_scale_blend(const sf2d_texture *texture, float x, float y, float tex_x, float tex_y, float tex_w, float tex_h, float x_scale, float y_scale, u32 color)
Draws a part of a texture, with scaling, with color.
sf2d_texture * sf2d_create_texture_mem_RGBA8(const void *src_buffer, int src_w, int src_h, sf2d_texfmt pixel_format, sf2d_place place)
Creates a texture and fills it from a RGBA8 memory source. The returned texture is already tiled...
void * sf2d_pool_memalign(u32 size, u32 alignment)
Allocates aligned memory from a temporary pool. Works as sf2d_pool_malloc.
void sf2d_set_pixel(sf2d_texture *texture, int x, int y, u32 new_color)
Changes a pixel of the texture.
float z
Definition: sf2d.h:100
void sf2d_draw_texture_scale_blend(const sf2d_texture *texture, int x, int y, float x_scale, float y_scale, u32 color)
Draws a texture with scaling with color.
sf2d_vector_3f position
Definition: sf2d.h:129
Represents a vertex containing position (float) and color (unsigned char)
Definition: sf2d.h:119
sf2d_texfmt pixel_format
Definition: sf2d.h:140
void sf2d_set_vblank_wait(int enable)
Enables or disables the VBlank waiting.
int pow2_w
Definition: sf2d.h:144
float u
Definition: sf2d.h:89
gfxScreen_t sf2d_get_current_screen()
Returns the current screen (latest call to sf2d_start_frame)
unsigned char g
Definition: sf2d.h:109
void sf2d_draw_texture_scale(const sf2d_texture *texture, int x, int y, float x_scale, float y_scale)
Draws a texture with scaling.
unsigned char r
Definition: sf2d.h:108
int data_size
Definition: sf2d.h:146
Represents a texture.
Definition: sf2d.h:137
sf2d_vector_3f position
Definition: sf2d.h:120
Represents a vertex containing position and texture coordinates.
Definition: sf2d.h:128
void sf2d_pool_reset()
Empties the temporary pool.
void sf2d_draw_rectangle_rotate(int x, int y, int w, int h, u32 color, float rad)
Draws a rotated rectangle.
void sf2d_draw_texture_rotate_blend(const sf2d_texture *texture, int x, int y, float rad, u32 color)
Draws a texture with rotation around its center with color.
sf2d_texture * sf2d_create_texture(int width, int height, sf2d_texfmt pixel_format, sf2d_place place)
Creates an empty texture. The returned texture has the data allocated, this means that the raw pixel ...
u32 params
Definition: sf2d.h:141
Represents a three dimensional float vector.
Definition: sf2d.h:97
void sf2d_draw_texture_part_rotate_scale(const sf2d_texture *texture, int x, int y, float rad, int tex_x, int tex_y, int tex_w, int tex_h, float x_scale, float y_scale)
Draws a part of a texture, with rotation and scaling.
Definition: sf2d.h:78
void sf2d_draw_texture_part_scale(const sf2d_texture *texture, float x, float y, float tex_x, float tex_y, float tex_w, float tex_h, float x_scale, float y_scale)
Draws a part of a texture, with scaling.
void sf2d_draw_texture_blend(const sf2d_texture *texture, int x, int y, u32 color)
Draws a texture blended with a color.
float x
Definition: sf2d.h:98
void sf2d_draw_texture_depth_blend(const sf2d_texture *texture, int x, int y, signed short z, u32 color)
Draws a texture blended in a certain depth.
float y
Definition: sf2d.h:99
void sf2d_texture_tile32(sf2d_texture *texture)
Tiles a texture.
void sf2d_draw_texture_depth(const sf2d_texture *texture, int x, int y, signed short z)
Draws a texture blended in a certain depth.
void * data
Definition: sf2d.h:147
unsigned int sf2d_pool_space_free()
Returns the temporary pool's free space.
void sf2d_fill_texture_from_RGBA8(sf2d_texture *dst, const void *rgba8, int source_w, int source_h)
Fills an already allocated texture from a RGBA8 source.