Skip to content

Commit

Permalink
Font API update and added SGRect
Browse files Browse the repository at this point in the history
  • Loading branch information
darkuranium committed Nov 13, 2014
1 parent 3a17669 commit 23eaf5d
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 104 deletions.
2 changes: 1 addition & 1 deletion include/siege/common.h
Expand Up @@ -102,7 +102,7 @@ extern "C"
/// @{
#define SG_VERSION_MAJOR 0
#define SG_VERSION_MINOR 8
#define SG_VERSION_PATCH 2
#define SG_VERSION_PATCH 3
/**
* \brief Version string
*
Expand Down
59 changes: 26 additions & 33 deletions include/siege/graphics/font.h
Expand Up @@ -24,6 +24,7 @@
#include "../util/stream.h"
#include "../util/conv.h"
#include "../util/map.h"
#include "../util/rect.h"
#include "texture.h"

#include <stdarg.h>
Expand Down Expand Up @@ -155,8 +156,6 @@ void SG_CALL _sgFontToLoad(SGFont* font, SGdchar* input, SGuint inlen, SGdchar*
SGbool SG_CALL _sgFontLoad(SGFont* font, SGdchar* chars, SGuint numchars, SGbool force);
SGubyte* SG_CALL _sgFontToRGBA(SGFont* font, SGubyte* data, SGuint datalen);

void SG_CALL _sgFontCenterOffsetU32(SGFont* font, float* x, float* y, const SGdchar* text);

SGdchar* SG_CALL _sgFontU16ToU32(SGFont* font, const SGwchar* text);
SGdchar* SG_CALL _sgFontU8ToU32(SGFont* font, const SGchar* text);
SGdchar* SG_CALL _sgFontWToU32(SGFont* font, const wchar_t* text);
Expand Down Expand Up @@ -230,37 +229,18 @@ void SG_CALL sgFontPrintAlignedU8(SGFont* font, float x, float y, SGenum align,
void SG_CALL sgFontPrintAlignedW(SGFont* font, float x, float y, SGenum align, const wchar_t* text);
void SG_CALL sgFontPrintAligned(SGFont* font, float x, float y, SGenum align, const char* text);

/**
* \name Get size of printed text
*
* \param font The font face to use for printing.
* \param[out] x Width
* \param[out] y Height
*
* These functions return the size of the text, had it been printed,
* in <em>x</em>,<em>y</em>, with the former being the width and the
* latter height.
*/
/// @{
void SG_CALL sgFontStrSizefW(SGFont* font, float* x, float* y, const wchar_t* format, ...);
void SG_CALL sgFontStrSizefvW(SGFont* font, float* x, float* y, const wchar_t* format, va_list args);
/**
* \brief printf-style text
*/
void SG_CALL SG_HINT_PRINTF(4, 5) sgFontStrSizef(SGFont* font, float* x, float* y, const char* format, ...);
/**
* \brief vprintf-style text
*/
void SG_CALL SG_HINT_PRINTF(4, 0) sgFontStrSizefv(SGFont* font, float* x, float* y, const char* format, va_list args);
/**
* \brief plain text
*/
void SG_CALL sgFontStrSizeU32(SGFont* font, float* x, float* y, const SGdchar* text);
void SG_CALL sgFontStrSizeU16(SGFont* font, float* x, float* y, const SGwchar* text);
void SG_CALL sgFontStrSizeU8(SGFont* font, float* x, float* y, const SGchar* text);
void SG_CALL sgFontStrSizeW(SGFont* font, float* x, float* y, const wchar_t* text);
void SG_CALL sgFontStrSize(SGFont* font, float* x, float* y, const char* text);
/// @}
/* get the bounding rectangle of a string */
SGRect SG_CALL sgFontStrRectfW(SGFont* font, const wchar_t* format, ...);
SGRect SG_CALL sgFontStrRectfvW(SGFont* font, const wchar_t* format, va_list args);

SGRect SG_CALL SG_HINT_PRINTF(2, 3) sgFontStrRectf(SGFont* font, const char* format, ...);
SGRect SG_CALL SG_HINT_PRINTF(2, 0) sgFontStrRectfv(SGFont* font, const char* format, va_list args);

SGRect SG_CALL sgFontStrRectU32(SGFont* font, const SGdchar* text);
SGRect SG_CALL sgFontStrRectU16(SGFont* font, const SGwchar* text);
SGRect SG_CALL sgFontStrRectU8(SGFont* font, const SGchar* text);
SGRect SG_CALL sgFontStrRectW(SGFont* font, const wchar_t* text);
SGRect SG_CALL sgFontStrRect(SGFont* font, const char* text);

// need a better name for FindIndex and GetPos...
size_t SG_CALL sgFontFindIndexfW(SGFont* font, float x, float y, const wchar_t* format, ...);
Expand All @@ -287,6 +267,19 @@ void SG_CALL sgFontGetPosU8(SGFont* font, float* x, float* y, size_t index, cons
void SG_CALL sgFontGetPosW(SGFont* font, float* x, float* y, size_t index, const wchar_t* text);
void SG_CALL sgFontGetPos(SGFont* font, float* x, float* y, size_t index, const char* text);

/* DEPRECATED: Use sgFontStrRect* instead */
void SG_CALL SG_HINT_DEPRECATED sgFontStrSizefW(SGFont* font, float* x, float* y, const wchar_t* format, ...);
void SG_CALL SG_HINT_DEPRECATED sgFontStrSizefvW(SGFont* font, float* x, float* y, const wchar_t* format, va_list args);

void SG_CALL SG_HINT_PRINTF(4, 5) SG_HINT_DEPRECATED sgFontStrSizef(SGFont* font, float* x, float* y, const char* format, ...);
void SG_CALL SG_HINT_PRINTF(4, 0) SG_HINT_DEPRECATED sgFontStrSizefv(SGFont* font, float* x, float* y, const char* format, va_list args);

void SG_CALL SG_HINT_DEPRECATED sgFontStrSizeU32(SGFont* font, float* x, float* y, const SGdchar* text);
void SG_CALL SG_HINT_DEPRECATED sgFontStrSizeU16(SGFont* font, float* x, float* y, const SGwchar* text);
void SG_CALL SG_HINT_DEPRECATED sgFontStrSizeU8(SGFont* font, float* x, float* y, const SGchar* text);
void SG_CALL SG_HINT_DEPRECATED sgFontStrSizeW(SGFont* font, float* x, float* y, const wchar_t* text);
void SG_CALL SG_HINT_DEPRECATED sgFontStrSize(SGFont* font, float* x, float* y, const char* text);

#ifdef __cplusplus
}
#endif // __cplusplus
Expand Down
1 change: 1 addition & 0 deletions include/siege/siege.h
Expand Up @@ -73,6 +73,7 @@
#include "util/map.h"
#include "util/matrix.h"
#include "util/rand.h"
#include "util/rect.h"
#include "util/set.h"
#include "util/smap.h"
#include "util/stream.h"
Expand Down
28 changes: 28 additions & 0 deletions include/siege/util/rect.h
@@ -0,0 +1,28 @@
#ifndef __SIEGE_UTIL_RECT_H__
#define __SIEGE_UTIL_RECT_H__

#include "../common.h"
#include "vector.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

typedef struct SGRect
{
SGVec2 a;
SGVec2 b;
} SGRect;

SGRect SG_CALL sgRect(float x1, float y1, float x2, float y2);
SGRect SG_CALL sgRectWH(float x, float y, float w, float h);

SGVec2 SG_CALL sgRectSize(SGRect rect);
SGVec2 SG_CALL sgRectCenter(SGRect rect);
SGRect SG_CALL sgRectOffset2f(SGRect rect, float x, float y);

#ifdef __cplusplus
}
#endif // __cplusplus

#endif /* __SIEGE_UTIL_RECT_H__ */
2 changes: 1 addition & 1 deletion src/examples/astar.c
Expand Up @@ -228,7 +228,7 @@ void drawInstructions(SGFont* headf, SGFont* textf, SGColor fill, SGColor line,

sgDrawColor4fv(&head.r);
sgFontPrint(headf, border.x + margin.x, border.y + margin.y, headt);
sgFontStrSize(headf, &csize.x, &csize.y, headt);
csize = sgRectSize(sgFontStrRect(headf, headt));

sgDrawColor4fv(&line.r);
sgDrawRectangle(border.x, border.y, WIDTH - border.x, HEIGHT - border.y, SG_FALSE);
Expand Down
25 changes: 12 additions & 13 deletions src/examples/text.c
Expand Up @@ -24,8 +24,7 @@ int main(void)
SGuint width = sgWindowGetWidth();
SGuint height = sgWindowGetHeight();

//float wx, wy;
//sgFontStrSize(font, &wx, &wy, "W");
SGVec2 size;

float dx, dy;
float y;
Expand All @@ -41,8 +40,8 @@ int main(void)
y = 0.0;
for(i = 0; i < numsizes; i++)
{
sgFontStrSize(sizes[i], &dx, &dy, text);
y += dy;
size = sgRectSize(sgFontStrRect(sizes[i], text));
y += size.y;
sgFontPrint(sizes[i], 0, floor(y), text);
}

Expand All @@ -59,7 +58,7 @@ int main(void)
sgFontPrintf(font, width / 4, height - height / 3, "Well, this is some more text...\nUseful for UI, consoles, etc...");

pos = sgFontFindIndexf(font, mx - (SGint)(width / 4), my - (SGint)(height - height / 3), "Well, this is some more text...\nUseful for UI, consoles, etc...");
sgFontGetPos(font, &dx, &dy, pos, "Well, this is some more text...\nUseful for UI, consoles, etc...");
sgFontGetPosf(font, &dx, &dy, pos, "Well, this is some more text...\nUseful for UI, consoles, etc...");
dx += width / 4;
dy += height - height / 3;
sgDrawColor4f(1.0, 0.0, 0.0, 1.0);
Expand All @@ -71,22 +70,22 @@ int main(void)
sgFontPrintW(font, 2, 256, L"Testing: ABC abc 012\nčšž\nSome cyrillic: Съешь ещё этих мягких французских булок да выпей же чаю\nAnd now for greek: Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός");
sgFontPrintfW(font, 2, 328, L"sgFontPrintfW: %ls", L"čšž");

sgFontStrSizef(font, &dx, &dy, "Some test text");
size = sgRectSize(sgFontStrRectf(font, "Some test text"));
sgDrawColor4f(1.0, 1.0, 0.0, 1.0);
sgDrawLine(640 - 160, 480 - 120, 640 - 160 + dx, 480 - 120);
sgDrawRectangle(640 / 2 - dx / 2, 480 - 120 - dy / 2,
640 / 2 + dx / 2, 480 - 120 + dy / 2,
sgDrawLine(640 - 160, 480 - 120, 640 - 160 + size.x, 480 - 120);
sgDrawRectangle(640 / 2 - size.x / 2, 480 - 120 - size.y / 2,
640 / 2 + size.x / 2, 480 - 120 + size.y / 2,
SG_TRUE);
sgDrawColor4f(1.0, 1.0, 1.0, 1.0);
sgFontPrint(font, 640 - 160, 480 - 120, "Some test text");

sgDrawColor4f(0.0, 0.0, 1.0, 1.0);
sgDrawRectangle(640 / 2 - dx / 2, 480 - 120 - dy / 2,
640 / 2 + dx / 2, 480 - 120 + dy / 2,
sgDrawRectangle(640 / 2 - size.x / 2, 480 - 120 - size.y / 2,
640 / 2 + size.x / 2, 480 - 120 + size.y / 2,
SG_TRUE);
sgDrawColor4f(1.0, 1.0, 0.0, 1.0);
sgDrawRectangle(640 / 2 - dx / 2, 480 - 120 - dy / 2,
640 / 2 + dx / 2, 480 - 120 + dy / 2,
sgDrawRectangle(640 / 2 - size.x / 2, 480 - 120 - size.y / 2,
640 / 2 + size.x / 2, 480 - 120 + size.y / 2,
SG_FALSE);
sgDrawColor4f(1.0, 1.0, 1.0, 1.0);
sgFontPrintAligned(font, 640 / 2, 480 - 120, SG_ALIGN_CENTER, "Some test text");
Expand Down

0 comments on commit 23eaf5d

Please sign in to comment.