Hi there, lately I've been coding a GPU accelerated 2D library for the Nintendo 3DS, on top of ctrulib. I've got to the point where it starts to be usable, so I think it's time to release it.
This will benefit you if you want to code a 2D game/app/emulator, as you obviously will need to draw textures/images. Maybe software rendering is enough, but if you have a GPU out there, why don't you use it ?
DOCUMENTATION: http://xerpi.github.io/sf2dlib/html/sf2d_8h.html#func-members
DOWNLOAD: https://github.com/xerpi/sf2dlib
Precompiled portlibs: https://github.com/xerpi/3ds_portlibs/releases/tag/1
If you need to load images, take a look at this lib: https://github.com/xerpi/sfillib
Here there are the functions SF2D provides:
The 2D coordinate system goes like this:
And the skeleton of a program using this lib:
A more complex sample: https://github.com/xerpi/sf2dlib/blob/master/sample/source/main.c
Screenshot of Citra emulator running a sample using my lib:
This will benefit you if you want to code a 2D game/app/emulator, as you obviously will need to draw textures/images. Maybe software rendering is enough, but if you have a GPU out there, why don't you use it ?
DOCUMENTATION: http://xerpi.github.io/sf2dlib/html/sf2d_8h.html#func-members
DOWNLOAD: https://github.com/xerpi/sf2dlib
Precompiled portlibs: https://github.com/xerpi/3ds_portlibs/releases/tag/1
If you need to load images, take a look at this lib: https://github.com/xerpi/sfillib
Here there are the functions SF2D provides:
Code:
int sf2d_init();
int sf2d_init_advanced(int gpucmd_size, int temppool_size);
int sf2d_fini();
void sf2d_set_3D(int enable);
void sf2d_start_frame(gfxScreen_t screen, gfx3dSide_t side);
void sf2d_end_frame();
void sf2d_swapbuffers();
void sf2d_set_vblank_wait(int enable);
float sf2d_get_fps();
void *sf2d_pool_malloc(u32 size);
void *sf2d_pool_memalign(u32 size, u32 alignment);
unsigned int sf2d_pool_space_free();
void sf2d_pool_reset();
void sf2d_set_clear_color(u32 color);
void sf2d_draw_line(int x0, int y0, int x1, int y1, u32 color);
void sf2d_draw_rectangle(int x, int y, int w, int h, u32 color);
void sf2d_draw_rectangle_rotate(int x, int y, int w, int h, u32 color, float rad);
sf2d_texture *sf2d_create_texture(int width, int height, GPU_TEXCOLOR pixel_format, sf2d_place place);
void sf2d_free_texture(sf2d_texture *texture);
void sf2d_fill_texture_from_RGBA8(sf2d_texture *dst, const void *rgba8, int source_w, int source_h);
sf2d_texture *sf2d_create_texture_mem_RGBA8(const void *src_buffer, int src_w, int src_h, GPU_TEXCOLOR pixel_format, sf2d_place place);
void sf2d_bind_texture(const sf2d_texture *texture, GPU_TEXUNIT unit);
void sf2d_bind_texture_color(const sf2d_texture *texture, GPU_TEXUNIT unit, u32 color);
void sf2d_draw_texture(const sf2d_texture *texture, int x, int y);
void sf2d_draw_texture_rotate(const sf2d_texture *texture, int x, int y, float rad);
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);
void sf2d_draw_texture_scale(const sf2d_texture *texture, int x, int y, float x_scale, float y_scale);
void sf2d_draw_texture_rotate_cut_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);
void sf2d_draw_texture_blend(const sf2d_texture *texture, int x, int y, u32 color);
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);
void sf2d_draw_texture_depth(const sf2d_texture *texture, int x, int y, signed short z);
void sf2d_texture_tile32(sf2d_texture *texture);
void sf2d_set_scissor_test(GPU_SCISSORMODE mode, u32 x, u32 y, u32 w, u32 h);
gfxScreen_t sf2d_get_current_screen();
gfx3dSide_t sf2d_get_current_side();
The 2D coordinate system goes like this:
Code:
Top screen:
X
---------->
| (0,0)
| ______________________
Y | | |
| | |
v | |
| |
| |
|______________________|
(400, 240)
Bottom screen:
X
---------->
| (0,0)
| ________________
Y | | |
| | |
v | |
| |
| |
|________________|
(320, 240)
Code:
int main()
{
sf2d_init();
sf2d_set_clear_color(RGBA8(0x00, 0x00, 0x00, 0x00));
while (aptMainLoop()) {
hidScanInput();
if (hidKeysDown() & KEY_START) break;
sf2d_start_frame(GFX_TOP, GFX_LEFT);
//Draws a 100x100 yellow rectangle (255, 255, 00, 255) at (150, 70)
sf2d_draw_rectangle(150, 70, 100, 100, RGBA8(0xFF, 0xFF, 0x00, 0xFF));
sf2d_end_frame();
sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
//Draws a 70x100 blue rectangle (0, 0, 00, 255) at (120, 30)
sf2d_draw_rectangle(120, 30, 70, 100, RGBA8(0x00, 0x00, 0xFF, 0xFF));
sf2d_end_frame();
sf2d_swapbuffers();
}
sf2d_fini();
return 0;
}
A more complex sample: https://github.com/xerpi/sf2dlib/blob/master/sample/source/main.c
Screenshot of Citra emulator running a sample using my lib:
Last edited by xerpi,