Skip to content

Commit

Permalink
0.0.9 TTF font perfect now!
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed May 21, 2012
1 parent bc784bb commit 6d19c9c
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 178 deletions.
15 changes: 15 additions & 0 deletions Makefile
Expand Up @@ -20,6 +20,7 @@ BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
PKGFILES := $(CURDIR)/pkgfiles

TITLE := NoRSX Example
APPID := NORSX0000
Expand Down Expand Up @@ -70,6 +71,8 @@ CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.bin)))
PNGFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.png)))
JPGFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.jpg)))
TTFFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.ttf)))
VCGFILES := $(foreach dir,$(SHADERS),$(notdir $(wildcard $(dir)/*.vcg)))
FCGFILES := $(foreach dir,$(SHADERS),$(notdir $(wildcard $(dir)/*.fcg)))
Expand All @@ -87,6 +90,8 @@ export OFILES := $(addsuffix .o,$(BINFILES)) \
$(addsuffix .o,$(TTFFILES)) \
$(addsuffix .o,$(VPOFILES)) \
$(addsuffix .o,$(FPOFILES)) \
$(addsuffix .o,$(PNGFILES)) \
$(addsuffix .o,$(JPGFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
$(sFILES:.s=.o) $(SFILES:.S=.o)

Expand Down Expand Up @@ -161,6 +166,16 @@ $(OUTPUT).elf: $(OFILES)
@echo $(notdir $<)
@$(bin2o)

#---------------------------------------------------------------------------------
%.jpg.o : %.jpg
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
%.png.o : %.png
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)

-include $(DEPENDS)

Expand Down
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -35,6 +35,9 @@ Changelog:
Added AlphaBlending (30 fps).
- 0.0.8 -
Added TTF support (be careful when using it! it's still sperimental).
- 0.0.9 -
Now TTF support is perfect!
Removed old font support.


Homebrews built with this lib:
Expand Down
Binary file removed data/Font_Test.bin
Binary file not shown.
Binary file removed data/NoRSX_Image.bin
Binary file not shown.
Binary file added data/NoRSX_Image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/Sans.ttf
Binary file not shown.
16 changes: 7 additions & 9 deletions include/Font.h
Expand Up @@ -12,30 +12,28 @@
#include FT_SYNTHESIS_H
#include FT_STROKER_H

#define CHROMAKEY 0x00ffffff
#define CHROMAKEY 0x000000ff
#define LINE_INDENT 40
#define LINE_SIZE 40

class Fonts{
class Font{
public:
Fonts(u32 Color, u32 Size,const void *MemFont, u32 MemFont_size, Minimum *min);
// Fonts(u32 BackGround, u32 Color, u32 Size, char *Font_Path, Min *min);
~Fonts();
u32 BackGroundColor;
Font(u32 Color, u32 Size, const void *MemFont, u32 MemFont_size, Minimum *min);
Font(u32 Color, u32 Size, const char *Font_Path, Minimum *min);
~Font();
u32 FontColor;
u32 FontSize;
// char* FontPath;
s32 width;
s32 height;
void Printf(u32 x, u32 y,const char *a, ...);
u32 color_depth;
u32 color_pitch;
u32 color_offset;
void DrawFont();
void Disable_Fonts();
protected:
bool Kerning;
FT_Byte *Pointer;
const void *RPointer;
s32 Lenght;
FT_Library library;
FT_Glyph glyph;
Expand All @@ -44,7 +42,7 @@ class Fonts{
FT_Bitmap *bitmap;
FT_Vector vec;

int disabled_font;
int font;
Minimum *m;

void FontDrawBitmap(FT_Bitmap *bitmap, s32 offset, s32 top);
Expand Down
38 changes: 0 additions & 38 deletions include/Font_Test.h

This file was deleted.

1 change: 0 additions & 1 deletion include/NoRSX.h
Expand Up @@ -25,7 +25,6 @@
#include "Image.h"
#include "Background.h"
#include "Objects.h"
#include "Font_Test.h"
#include "Font.h"
#include "Msg.h"

Expand Down
Binary file added pkgfiles/GOODTIME.ttf
Binary file not shown.
109 changes: 38 additions & 71 deletions source/Font.cpp
Expand Up @@ -12,11 +12,9 @@
#define B(argb) ( (argb) & 0xFF)


Fonts::Fonts(u32 Color, u32 Size,const void *MemFont, u32 MemFont_size, Minimum *min){
// BackGroundColor = BackGround;
Font::Font(u32 Color, u32 Size,const void *MemFont, u32 MemFont_size, Minimum *min){
FontColor = Color;
FontSize = Size;
// FontPath;
Pointer = (FT_Byte*)MemFont;
Lenght = MemFont_size;
m = min;
Expand All @@ -27,23 +25,42 @@ Fonts::Fonts(u32 Color, u32 Size,const void *MemFont, u32 MemFont_size, Minimum
Kerning = FT_HAS_KERNING(face);
FT_Set_Pixel_Sizes(face,0,FontSize);

rsxAddressToOffset(m->buffers[m->currentBuffer].ptr, &color_offset);
disabled_font=0;
font=0;
}

Fonts::~Fonts(){
if(disabled_font==0)
Font::Font(u32 Color, u32 Size, const char *Font_Path, Minimum *min){
FontColor = Color;
FontSize = Size;
Lenght = 0;
m = min;
FT_Init_FreeType(&library);
FT_New_Face(library,Font_Path,0,&face);
FT_Stroker_New(library,&stroker);
Kerning = FT_HAS_KERNING(face);
FT_Set_Pixel_Sizes(face,0,FontSize);
font=0;
}
Font::~Font(){
if(font==0)
Disable_Fonts();
}

void Fonts::Printf(u32 x, u32 y,const char *a, ...){
void Font::Disable_Fonts(){
FT_Done_Glyph(glyph);
FT_Stroker_Done(stroker);
FT_Done_Face(face);
FT_Done_FreeType(library);
font=1;
}

void Font::Printf(u32 x, u32 y,const char *a, ...){
char text[1024];
va_list va;
va_start(va, a);
vsnprintf(text, sizeof text, a, va);
size_t len = strlen(a);
if(len>0){
char text[len+1];
va_list va;
va_start(va, a);
vsnprintf(text, sizeof text, a, va);

len=strlen(text);
vec.x = 0;
vec.y = FontSize;
FT_GlyphSlot slot = face->glyph;
Expand All @@ -66,12 +83,11 @@ void Fonts::Printf(u32 x, u32 y,const char *a, ...){
vec.x += slot->advance.x >> 6;
vec.y += slot->advance.y >> 6;
}

}
}


void Fonts::FontDrawBitmap(FT_Bitmap *bitmap, s32 offset, s32 top){
void Font::FontDrawBitmap(FT_Bitmap *bitmap, s32 offset, s32 top){
static s32 color, a, r, g, b ;
FT_Int x, y, i, j ;
FT_Int x_max = offset + bitmap->width ;
Expand All @@ -90,64 +106,15 @@ void Fonts::FontDrawBitmap(FT_Bitmap *bitmap, s32 offset, s32 top){
for ( y = top, j = 0 ; y < y_max ; y++, j++ ){
if ( y >= (s32)M_height ) break ;
color = bitmap->buffer[bitmap->width * j + i] ;
a = ( ( fgA * color + 255 ) >> 8 ) ;
r = ( ( fgR * color + 255 ) >> 8 ) ;
g = ( ( fgG * color + 255 ) >> 8 ) ;
b = ( ( fgB * color + 255 ) >> 8 ) ;
*(m->buffers[m->currentBuffer].ptr + m->width * y + x ) = COLOR_TO_ARGB(a, r, g, b);
if(CHROMAKEY==color){
a = ( ( fgA * color + 255 ) >> 8 ) ;
r = ( ( fgR * color + 255 ) >> 8 ) ;
g = ( ( fgG * color + 255 ) >> 8 ) ;
b = ( ( fgB * color + 255 ) >> 8 ) ;
*(m->buffers[m->currentBuffer].ptr + m->width * y + x ) = COLOR_TO_ARGB(a, r, g, b);
}
}
}
}

void Fonts::DrawFont(){
color_depth = sizeof(u32) ;
color_pitch = width*color_depth;
gcmTransferScale xferscale ;
memset ( &xferscale, 0, sizeof ( xferscale ) ) ;

gcmTransferSurface xfersurface ;
memset ( &xfersurface, 0, sizeof ( xfersurface ) ) ;

/* configure transfer scale */
xferscale.conversion = GCM_TRANSFER_CONVERSION_TRUNCATE ;
xferscale.format = GCM_TRANSFER_SCALE_FORMAT_A8R8G8B8 ;
xferscale.operation = GCM_TRANSFER_OPERATION_SRCCOPY_AND ;
xferscale.clipX = 0 ;
xferscale.clipY = 0 ;
xferscale.clipW = m->width ;
xferscale.clipH = m->height ;
xferscale.outX = 0 ;
xferscale.outY = 0 ;
xferscale.outW = m->width;
xferscale.outH = m->height;
xferscale.inW = width;
xferscale.inH = height;

xferscale.ratioX = rsxGetFixedSint32 ( (float)xferscale.inW / (float)xferscale.outW ) ;
xferscale.ratioY = rsxGetFixedSint32 ( (float)xferscale.inH / (float)xferscale.outH ) ;

xferscale.pitch = color_pitch;
xferscale.origin = GCM_TRANSFER_ORIGIN_CORNER ;
xferscale.interp = GCM_TRANSFER_INTERPOLATOR_NEAREST ;
xferscale.offset = color_offset;

xferscale.inX = rsxGetFixedUint16 ( 1.0f ) ;
xferscale.inY = rsxGetFixedUint16 ( 1.0f ) ;

/* configure destination surface for transfer */
xfersurface.format = GCM_TRANSFER_SURFACE_FORMAT_A8R8G8B8 ;
xfersurface.pitch = color_pitch ;
xfersurface.offset = color_offset ;

/* blit font buffer */
rsxSetTransferScaleMode(m->context, GCM_TRANSFER_LOCAL_TO_LOCAL, GCM_TRANSFER_SURFACE);
rsxSetTransferScaleSurface(m->context, &xferscale, &xfersurface);
}

void Fonts::Disable_Fonts(){
FT_Done_FreeType(library);
FT_Stroker_Done(stroker);
FT_Done_Glyph(glyph);
free(Pointer);
disabled_font=1;
}
52 changes: 0 additions & 52 deletions source/Font_Test.cpp

This file was deleted.

17 changes: 10 additions & 7 deletions source/main.cpp
Expand Up @@ -17,7 +17,8 @@

#include <io/pad.h>
#include "NoRSX.h"
#include "NoRSX_Image_bin.h"
#include "NoRSX_Image_png.h"
#include "Sans_ttf.h"
#include <time.h>

static int exitapp, xmbopen;
Expand Down Expand Up @@ -54,10 +55,11 @@ s32 main(s32 argc, const char* argv[])
Image IMG(GFX);
Background BG(GFX);
Object OBJ(GFX);
Font F(GFX);
Font F1(COLOR_BLUE, 40,Sans_ttf,Sans_ttf_size ,GFX); //Loaded from Memory
Font F2(COLOR_RED, 40,"/dev_hdd0/game/NORSX0000/GOODTIME.ttf" ,GFX); //Loaded from File!


IMG.LoadPNG_Buf(NoRSX_Image_bin,NoRSX_Image_bin_size, &png);
IMG.LoadPNG_Buf(NoRSX_Image_png,NoRSX_Image_png_size, &png);
u32 imgX =(GFX->width/2)-(png.width/2), imgY = (GFX->height/2)-(png.height/2);
exitapp = 1;
int frame=0;
Expand All @@ -77,10 +79,11 @@ s32 main(s32 argc, const char* argv[])
OBJ.Circle(400,900,100,COLOR_YELLOW);
OBJ.Rectangle(500,400,200,500,COLOR_ORANGE);
OBJ.Line(400,400,900,100,COLOR_GREY);
IMG.AlphaDrawIMG(imgX,imgY,&png);
F.Printf(150,200,COLOR_BLACK,"SCREEN %d X %d",GFX->width,GFX->height);
F.Print(150,250,COLOR_YELLOW,"PRESS X TO EXIT");
F.Printf(150,100,COLOR_GREEN,"FPS %f", fps);
IMG.DrawIMG(imgX,imgY,&png);
F1.Printf(150,200,"Screen %d x %d",GFX->width,GFX->height);
F1.Printf(150,250,"Press X to exit!");
F1.Printf(150,100,"FPS %f", fps);
F2.Printf(150,300,"FreeType2 with TTF support :)");
GFX->Flip();
frame ++;
sysUtilCheckCallback();
Expand Down

0 comments on commit 6d19c9c

Please sign in to comment.