Add bitmap fonts

This commit is contained in:
Rob Loach 2017-07-14 18:00:22 -04:00
parent 77a4262a69
commit c4e5e6678c
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
12 changed files with 120 additions and 22 deletions

3
.gitmodules vendored
View File

@ -16,3 +16,6 @@
[submodule "vendor/ChaiScript_Extras"]
path = vendor/ChaiScript_Extras
url = https://github.com/ChaiScript/ChaiScript_Extras.git
[submodule "vendor/SDL_tty"]
path = vendor/SDL_tty
url = https://github.com/Grumbel/SDL_tty.git

View File

@ -83,7 +83,9 @@ OBJECTS := libretro.o Application.o \
chaigame/src/Point.o \
chaigame/src/Joystick.o \
chaigame/joystick.o \
vendor/physfs/extras/physfsrwops.o
vendor/physfs/extras/physfsrwops.o \
vendor/SDL_tty/src/SDL_fnt.o \
vendor/SDL_tty/src/SDL_tty.o
all: vendor/physfs/libphysfs.a vendor/libretro-common/include/libretro.h $(TARGET)
@ -108,6 +110,7 @@ FLAGS += -I. \
-Ivendor/sdl-libretro/include \
-Ivendor/libretro-common/include \
-Ivendor/chaiscript/include \
-Ivendor/SDL_tty/include \
-Ivendor/sdl-libretro/tests/SDL_ttf-2.0.11/VisualC/external/include \
-Ivendor/ChaiScript_Extras/include \
-Ivendor/physfs/src

View File

@ -147,6 +147,10 @@ namespace chaigame {
}
}
Font* graphics::newFont(const std::string& filename, int glyphWidth, int glyphHeight, const std::string& letters) {
return new Font(filename, glyphWidth, glyphHeight, letters);
}
Font* graphics::newFont(const std::string& filename, int size) {
return new Font(filename, size);
}

View File

@ -32,6 +32,7 @@ namespace chaigame {
Font* newFont();
Font* newFont(const std::string& filename);
Font* newFont(const std::string& filename, int size);
Font* newFont(const std::string& filename, int glyphWidth, int glyphHeight, const std::string& letters);
void setFont(Font* font);
void setFont();
Font* getFont();

View File

@ -52,6 +52,7 @@ namespace chaigame {
chai.add(user_type<Font>(), "Font");
chai.add(fun(&Font::loaded), "loaded");
chai.add(fun(&Font::getHeight), "getHeight");
chai.add(fun(&Font::getWidth), "getWidth");
// Add the Config.
chai.add(user_type<windowConfig>(), "windowConfig");
@ -86,6 +87,7 @@ namespace chaigame {
chai.add(fun(&graphics::newQuad), "newQuad");
chai.add(fun<Font*, graphics, const std::string&, int>(&graphics::newFont), "newFont");
chai.add(fun<Font*, graphics, const std::string&>(&graphics::newFont), "newFont");
chai.add(fun<Font*, graphics, const std::string&, int, int, const std::string&>(&graphics::newFont), "newFont");
chai.add(fun<Font*, graphics>(&graphics::newFont), "newFont");
chai.add(fun<void, graphics, Font*>(&graphics::setFont), "setFont");
chai.add(fun<void, graphics>(&graphics::setFont), "setFont");

View File

@ -3,19 +3,44 @@
#include <string>
#include <SDL_ttf.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_fnt.h>
#include "../../Application.h"
#include <SDL_image.h>
namespace chaigame {
Font::Font() {
Font::Font() {}
Font::Font(const std::string& filename, int glyphWidth, int glyphHeight, const std::string& letters) {
SDL_RWops* rwops = Application::getInstance()->filesystem.openRW(filename);
if (rwops) {
SDL_Surface* surface = IMG_Load_RW(rwops, 1);
if (surface == NULL) {
printf("Font::Font: %s\n", IMG_GetError());
return;
}
TTY_Font* newFont = FNT_Create(surface, glyphWidth, glyphHeight, letters.c_str());
if (newFont == NULL) {
printf("Error creating FNT_Create()\n");
return;
}
ttyFont = newFont;
ttyFontWidth = glyphWidth;
ttyFontHeight = glyphHeight;
}
}
Font::Font(const std::string& filename, int ptsize) {
SDL_RWops* rwops = Application::getInstance()->filesystem.openRW(filename);
if (rwops) {
font = TTF_OpenFontRW(rwops, 1, ptsize);
if(!font) {
TTF_Font* newFont = TTF_OpenFontRW(rwops, 1, ptsize);
if (!newFont) {
printf("TTF_OpenFontRW: %s\n", TTF_GetError());
return;
}
ttfFont = newFont;
}
}
@ -24,27 +49,54 @@ namespace chaigame {
}
bool Font::loaded() {
return font != NULL;
return ttfFont != NULL || ttyFont != NULL;
}
bool Font::destroy() {
if (loaded()) {
TTF_CloseFont(font);
font = NULL;
if (ttfFont != NULL) {
TTF_CloseFont(ttfFont);
ttfFont = NULL;
}
if (ttyFont != NULL) {
FNT_Free(ttyFont);
ttyFont = NULL;
}
}
int Font::getHeight() {
if (loaded()) {
return TTF_FontHeight(font);
if (ttfFont != NULL) {
return TTF_FontHeight(ttfFont);
}
if (ttyFont != NULL) {
return ttyFontHeight;
}
return 12;
}
int Font::getWidth(const std::string& text) {
if (ttfFont != NULL) {
int w;
if (TTF_SizeText(ttfFont, text.c_str(), &w, NULL) == 0) {
return w;
}
}
if (ttyFont != NULL) {
return ttyFontWidth * text.length();
}
return 0;
}
void Font::print(const std::string& text, int x, int y, int r, int g, int b, int a) {
if (loaded()) {
SDL_Surface* screen = Application::getInstance()->screen;
// Attempt to render the TTF Font.
if (ttfFont != NULL) {
SDL_Color color = {(Uint8)r, (Uint8)g, (Uint8)b};
SDL_Surface* surface = TTF_RenderText_Blended(font, text.c_str(), color);
SDL_Surface* surface = TTF_RenderText_Blended(ttfFont, text.c_str(), color);
if (!surface) {
printf("Font::print - %s\n", TTF_GetError());
return;
@ -53,13 +105,20 @@ namespace chaigame {
SDL_Rect* dstrect = new SDL_Rect();
dstrect->x = x;
dstrect->y = y;
SDL_BlitSurface(surface, NULL, Application::getInstance()->screen, dstrect);
SDL_BlitSurface(surface, NULL, screen, dstrect);
// TODO: Allow re-use of text renderings.
SDL_FreeSurface(surface);
// TODO: Allow re-use of text renderings.
SDL_FreeSurface(surface);
return;
}
else {
stringRGBA(Application::getInstance()->screen, x, y, text.c_str(), r, g, b, a);
// Use TTY to print the bitmap font?
if (ttyFont != NULL) {
FNT_Print(ttyFont, screen, x, y, FNT_ALIGN_LEFT, text.c_str());
return;
}
// Fall back to SDL_gfx primitives.
stringRGBA(screen, x, y, text.c_str(), r, g, b, a);
}
}

View File

@ -4,18 +4,23 @@
#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
#include <SDL_fnt.h>
namespace chaigame {
class Font {
public:
Font();
Font(const std::string& filename, int glyphWidth, int glyphHeight, const std::string& letters);
Font(const std::string& filename, int ptsize);
~Font();
bool loaded();
bool destroy();
void print(const std::string& text, int x, int y, int r, int g, int b, int a);
TTF_Font *font = NULL;
TTF_Font* ttfFont = NULL;
TTY_Font* ttyFont = NULL;
int ttyFontWidth, ttyFontHeight;
int getHeight();
int getWidth(const std::string& text);
};
}

View File

@ -11,7 +11,8 @@ void Test::conf(chaigame::Config& t) {
bool Test::load() {
Application* app = Application::getInstance();
thefont = app->graphics.newFont("test/examples/assets/Raleway-Regular.ttf", 60);
secondfont = app->graphics.newFont("test/examples/assets/Raleway-Regular.ttf", 60);
thefont = app->graphics.newFont("test/examples/assets/c64_16x16.png", 16, 16, "\x7f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");
}
void Test::update(float delta) {}
@ -20,11 +21,21 @@ void Test::draw() {
Application* app = Application::getInstance();
app->graphics.setColor(77, 182, 172);
app->graphics.ellipse("fill", 300, 400, 200, 180);
app->graphics.ellipse("fill", 0, 768, 200, 180);
int width = thefont->getWidth("Bitmap Commodore Font!");
int height = app->graphics.getHeight() / 2.0f - thefont->getHeight() / 2.0f;
app->graphics.setColor(100, 181, 246);
app->graphics.setFont(thefont);
app->graphics.print("Hello World!", 300, 100);
app->graphics.print("Bitmap Commodore Font!", app->graphics.getWidth() / 2.0f - width / 2.0f, 200);
app->graphics.setColor(255, 181, 0);
app->graphics.setFont(secondfont);
width = secondfont->getWidth("TTF Font here!");
height = app->graphics.getHeight() / 2.0f - secondfont->getHeight() / 2.0f;
app->graphics.print("TTF Font here!", app->graphics.getWidth() / 2.0f - width / 2.0f, height);
}
#endif

View File

@ -13,6 +13,7 @@ public:
void draw();
int tester = 0;
chaigame::Font* thefont;
chaigame::Font* secondfont;
};
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -1,6 +1,7 @@
class graphics_print_font {
var name;
var thefont;
var secondfont;
def graphics_print_font() {
this.name = "graphics.print(font)";
@ -8,12 +9,19 @@ class graphics_print_font {
def load() {
this.thefont = graphics.newFont("assets/Raleway-Regular.ttf", 40)
this.secondfont = graphics.newFont("assets/c64_16x16.png", 16, 16, "\x7f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
}
def draw() {
var text = "Hello World!"
graphics.setFont(this.thefont)
graphics.setColor(255, 200, 0)
graphics.print("Hello World!", 100, 100);
graphics.print(text, 100, 100)
graphics.setFont(this.secondfont)
var width = this.secondfont.getWidth(text)
var height = this.secondfont.getHeight()
graphics.print(text, graphics.getWidth() / 2.0f - width / 2.0f, graphics.getHeight() - height)
}
def update(delta) {}

1
vendor/SDL_tty vendored Submodule

@ -0,0 +1 @@
Subproject commit 1e84486c90f20dbafbce748cf81128ceee517c79