mirror of
https://github.com/ctruLua/ctruLua.git
synced 2025-10-28 00:39:30 +00:00
Updated sftdlib and sf2dlib
This commit is contained in:
parent
3fd2efb77e
commit
49ae4454d3
11 changed files with 373 additions and 170 deletions
|
|
@ -15,6 +15,7 @@ typedef struct atlas_htab_entry {
|
|||
int bitmap_top;
|
||||
int advance_x;
|
||||
int advance_y;
|
||||
int glyph_size;
|
||||
} atlas_htab_entry;
|
||||
|
||||
typedef struct texture_atlas {
|
||||
|
|
@ -25,9 +26,9 @@ typedef struct texture_atlas {
|
|||
|
||||
texture_atlas *texture_atlas_create(int width, int height, sf2d_texfmt format, sf2d_place place);
|
||||
void texture_atlas_free(texture_atlas *atlas);
|
||||
int texture_atlas_insert(texture_atlas *atlas, unsigned int character, const void *image, int width, int height, int bitmap_left, int bitmap_top, int advance_x, int advance_y);
|
||||
int texture_atlas_insert(texture_atlas *atlas, unsigned int character, const void *image, int width, int height, int bitmap_left, int bitmap_top, int advance_x, int advance_y, int glyph_size);
|
||||
int texture_atlas_exists(texture_atlas *atlas, unsigned int character);
|
||||
void texture_atlas_get(texture_atlas *atlas, unsigned int character, bp2d_rectangle *rect, int *bitmap_left, int *bitmap_top, int *advance_x, int *advance_y);
|
||||
void texture_atlas_get(texture_atlas *atlas, unsigned int character, bp2d_rectangle *rect, int *bitmap_left, int *bitmap_top, int *advance_x, int *advance_y, int *glyph_size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ void sftd_free_font(sftd_font *font)
|
|||
}
|
||||
}
|
||||
|
||||
static int atlas_add_glyph(texture_atlas *atlas, unsigned int glyph_index, const FT_BitmapGlyph bitmap_glyph)
|
||||
static int atlas_add_glyph(texture_atlas *atlas, unsigned int glyph_index, const FT_BitmapGlyph bitmap_glyph, int glyph_size)
|
||||
{
|
||||
const FT_Bitmap *bitmap = &bitmap_glyph->bitmap;
|
||||
|
||||
|
|
@ -171,7 +171,8 @@ static int atlas_add_glyph(texture_atlas *atlas, unsigned int glyph_index, const
|
|||
int ret = texture_atlas_insert(atlas, glyph_index, buffer,
|
||||
bitmap->width, bitmap->rows,
|
||||
bitmap_glyph->left, bitmap_glyph->top,
|
||||
bitmap_glyph->root.advance.x, bitmap_glyph->root.advance.y);
|
||||
bitmap_glyph->root.advance.x, bitmap_glyph->root.advance.y,
|
||||
glyph_size);
|
||||
|
||||
free(buffer);
|
||||
|
||||
|
|
@ -213,7 +214,7 @@ void sftd_draw_text(sftd_font *font, int x, int y, unsigned int color, unsigned
|
|||
if (!texture_atlas_exists(font->tex_atlas, glyph_index)) {
|
||||
FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL);
|
||||
|
||||
if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph)) {
|
||||
if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph, size)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
@ -221,25 +222,29 @@ void sftd_draw_text(sftd_font *font, int x, int y, unsigned int color, unsigned
|
|||
bp2d_rectangle rect;
|
||||
int bitmap_left, bitmap_top;
|
||||
int advance_x, advance_y;
|
||||
int glyph_size;
|
||||
|
||||
texture_atlas_get(font->tex_atlas, glyph_index,
|
||||
&rect, &bitmap_left, &bitmap_top,
|
||||
&advance_x, &advance_y);
|
||||
&advance_x, &advance_y, &glyph_size);
|
||||
|
||||
sf2d_draw_texture_part_blend(font->tex_atlas->tex,
|
||||
pen_x + bitmap_left,
|
||||
pen_y - bitmap_top,
|
||||
const float draw_scale = glyph_size/(float)size;
|
||||
|
||||
sf2d_draw_texture_part_scale_blend(font->tex_atlas->tex,
|
||||
pen_x + bitmap_left * draw_scale,
|
||||
pen_y - bitmap_top * draw_scale,
|
||||
rect.x, rect.y, rect.w, rect.h,
|
||||
draw_scale,
|
||||
draw_scale,
|
||||
color);
|
||||
|
||||
pen_x += advance_x >> 16;
|
||||
pen_y += advance_y >> 16;
|
||||
pen_x += (advance_x >> 16) * draw_scale;
|
||||
pen_y += (advance_y >> 16) * draw_scale;
|
||||
|
||||
previous = glyph_index;
|
||||
text++;
|
||||
}
|
||||
}
|
||||
|
||||
void sftd_draw_textf(sftd_font *font, int x, int y, unsigned int color, unsigned int size, const char *text, ...)
|
||||
{
|
||||
char buffer[256];
|
||||
|
|
@ -285,7 +290,7 @@ void sftd_draw_wtext(sftd_font *font, int x, int y, unsigned int color, unsigned
|
|||
if (!texture_atlas_exists(font->tex_atlas, glyph_index)) {
|
||||
FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL);
|
||||
|
||||
if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph)) {
|
||||
if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph, size)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
@ -293,19 +298,24 @@ void sftd_draw_wtext(sftd_font *font, int x, int y, unsigned int color, unsigned
|
|||
bp2d_rectangle rect;
|
||||
int bitmap_left, bitmap_top;
|
||||
int advance_x, advance_y;
|
||||
int glyph_size;
|
||||
|
||||
texture_atlas_get(font->tex_atlas, glyph_index,
|
||||
&rect, &bitmap_left, &bitmap_top,
|
||||
&advance_x, &advance_y);
|
||||
&advance_x, &advance_y, &glyph_size);
|
||||
|
||||
sf2d_draw_texture_part_blend(font->tex_atlas->tex,
|
||||
pen_x + bitmap_left,
|
||||
pen_y - bitmap_top,
|
||||
const float draw_scale = (float)size/glyph_size;
|
||||
|
||||
sf2d_draw_texture_part_scale_blend(font->tex_atlas->tex,
|
||||
pen_x + bitmap_left * draw_scale,
|
||||
pen_y - bitmap_top * draw_scale,
|
||||
rect.x, rect.y, rect.w, rect.h,
|
||||
draw_scale,
|
||||
draw_scale,
|
||||
color);
|
||||
|
||||
pen_x += advance_x >> 16;
|
||||
pen_y += advance_y >> 16;
|
||||
pen_x += (advance_x >> 16) * draw_scale;
|
||||
pen_y += (advance_y >> 16) * draw_scale;
|
||||
|
||||
previous = glyph_index;
|
||||
text++;
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ void texture_atlas_free(texture_atlas *atlas)
|
|||
free(atlas);
|
||||
}
|
||||
|
||||
int texture_atlas_insert(texture_atlas *atlas, unsigned int character, const void *image, int width, int height, int bitmap_left, int bitmap_top, int advance_x, int advance_y)
|
||||
int texture_atlas_insert(texture_atlas *atlas, unsigned int character, const void *image, int width, int height, int bitmap_left, int bitmap_top, int advance_x, int advance_y, int glyph_size)
|
||||
{
|
||||
bp2d_size size;
|
||||
size.w = width;
|
||||
|
|
@ -51,6 +51,7 @@ int texture_atlas_insert(texture_atlas *atlas, unsigned int character, const voi
|
|||
entry->bitmap_top = bitmap_top;
|
||||
entry->advance_x = advance_x;
|
||||
entry->advance_y = advance_y;
|
||||
entry->glyph_size = glyph_size;
|
||||
|
||||
int_htab_insert(atlas->htab, character, entry);
|
||||
|
||||
|
|
@ -71,7 +72,7 @@ int texture_atlas_exists(texture_atlas *atlas, unsigned int character)
|
|||
return int_htab_find(atlas->htab, character) != NULL;
|
||||
}
|
||||
|
||||
void texture_atlas_get(texture_atlas *atlas, unsigned int character, bp2d_rectangle *rect, int *bitmap_left, int *bitmap_top, int *advance_x, int *advance_y)
|
||||
void texture_atlas_get(texture_atlas *atlas, unsigned int character, bp2d_rectangle *rect, int *bitmap_left, int *bitmap_top, int *advance_x, int *advance_y, int *glyph_size)
|
||||
{
|
||||
atlas_htab_entry *entry = int_htab_find(atlas->htab, character);
|
||||
|
||||
|
|
@ -83,4 +84,5 @@ void texture_atlas_get(texture_atlas *atlas, unsigned int character, bp2d_rectan
|
|||
*bitmap_top = entry->bitmap_top;
|
||||
*advance_x = entry->advance_x;
|
||||
*advance_y = entry->advance_y;
|
||||
*glyph_size = entry->glyph_size;
|
||||
}
|
||||
|
|
|
|||
BIN
libs/sftdlib/sample/data/FreeSans.ttf
Normal file
BIN
libs/sftdlib/sample/data/FreeSans.ttf
Normal file
Binary file not shown.
Binary file not shown.
|
|
@ -4,19 +4,17 @@
|
|||
#include <3ds.h>
|
||||
#include <sf2d.h>
|
||||
#include <sftd.h>
|
||||
#include "airstrike_ttf.h"
|
||||
#include "FreeSans_ttf.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
sf2d_init();
|
||||
sf2d_set_clear_color(RGBA8(0x40, 0x40, 0x40, 0xFF));
|
||||
|
||||
consoleInit(GFX_BOTTOM, NULL);
|
||||
printf("sftd sample\n");
|
||||
sf2d_set_vblank_wait(0);
|
||||
|
||||
// Font loading
|
||||
sftd_init();
|
||||
sftd_font *font = sftd_load_font_mem(airstrike_ttf, airstrike_ttf_size);
|
||||
sftd_font *font = sftd_load_font_mem(FreeSans_ttf, FreeSans_ttf_size);
|
||||
|
||||
while (aptMainLoop()) {
|
||||
|
||||
|
|
@ -25,8 +23,32 @@ int main()
|
|||
|
||||
sf2d_start_frame(GFX_TOP, GFX_LEFT);
|
||||
|
||||
sftd_draw_text(font, 10, 10, RGBA8(255, 0, 0, 255), 20, "Font drawing on the top screen!");
|
||||
sftd_draw_textf(font, 10, 40, RGBA8(0, 255, 0, 255), 20, "FPS %f", sf2d_get_fps());
|
||||
sftd_draw_textf(font, 10, 10, RGBA8(0, 255, 0, 255), 20, "FPS %f", sf2d_get_fps());
|
||||
|
||||
sftd_draw_text(font, 10, 30, RGBA8(255, 0, 0, 255), 20, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 50, RGBA8(0, 255, 0, 255), 15, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 68, RGBA8(0, 0, 255, 255), 25, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 90, RGBA8(255, 255, 0, 255), 10, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 105, RGBA8(255, 0, 255, 255), 8, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 120, RGBA8(0, 255, 255, 255), 30, "Font drawing on the top screen!");
|
||||
|
||||
sftd_draw_text(font, 10, 155, RGBA8(255, 0, 0, 255), 20, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 170, RGBA8(0, 255, 0, 255), 2, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 180, RGBA8(0, 0, 255, 255), 10, "Font drawing on the top screen!");
|
||||
sftd_draw_text(font, 10, 205, RGBA8(255, 255, 0, 255), 170, "Font drawing on the top screen!");
|
||||
|
||||
sf2d_end_frame();
|
||||
|
||||
sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
|
||||
|
||||
sftd_draw_text(font, 10, 10, RGBA8(0, 255, 0, 255), 20, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 30, RGBA8(255, 0, 0, 255), 10, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 50, RGBA8(0, 255, 0, 255), 15, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 70, RGBA8(0, 0, 255, 255), 5, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 90, RGBA8(255, 255, 0, 255), 25, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 120, RGBA8(255, 0, 255, 255), 20, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 140, RGBA8(0, 255, 255, 255), 12, "Font drawing on the bot. screen!");
|
||||
sftd_draw_text(font, 10, 150, RGBA8(255, 0, 0, 255), 28, "Font drawing on the bot. screen!");
|
||||
|
||||
sf2d_end_frame();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue