summaryrefslogtreecommitdiff
path: root/Source/Font.c
blob: c4b1491deb574658c9a903f1afa68f78ae934975 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* *************************************
 * 	Includes
 * *************************************/

#include "Font.h"

/* *************************************
 * 	Defines
 * *************************************/

#define FONT_INTERNAL_TEXT_BUFFER_MAX_SIZE 200

/* *************************************
 * 	Local Prototypes
 * *************************************/

/* *************************************
 * 	Local Variables
 * *************************************/

static char _internal_text[FONT_INTERNAL_TEXT_BUFFER_MAX_SIZE];
static unsigned char _blend_effect_lum;

bool FontLoadImage(char* strPath, TYPE_FONT * ptrFont)
{
	if(GfxSpriteFromFile(strPath, &ptrFont->spr) == false)
	{
		return false;
	}
	
	ptrFont->spr_w = ptrFont->spr.w;
	ptrFont->spr_h = ptrFont->spr.h;
	ptrFont->spr_u = ptrFont->spr.u;
	ptrFont->spr_v = ptrFont->spr.v;
	
	//Now set default values to font
	
	ptrFont->char_w = FONT_DEFAULT_CHAR_SIZE;
	ptrFont->char_h = FONT_DEFAULT_CHAR_SIZE;
	
	ptrFont->spr.attribute |= COLORMODE(COLORMODE_4BPP);
	ptrFont->spr.attribute &= COLORMODE(~(COLORMODE_8BPP | COLORMODE_16BPP | COLORMODE_24BPP));
	ptrFont->spr.r = NORMAL_LUMINANCE;
	ptrFont->spr.g = NORMAL_LUMINANCE;
	ptrFont->spr.b = NORMAL_LUMINANCE;
	
	//At this point, spr.w and spr.h = real w/h
	ptrFont->char_per_row = (uint8_t)(ptrFont->spr_w / ptrFont->char_w);
	ptrFont->max_ch_wrap = 0;
	
	ptrFont->spr.w = ptrFont->char_w;
	ptrFont->spr.h = ptrFont->char_h;
	
	ptrFont->flags = FONT_NOFLAGS;
	
	ptrFont->init_ch = FONT_DEFAULT_INIT_CHAR;
	
	dprintf("Sprite CX = %d, sprite CY = %d\n",ptrFont->spr.cx, ptrFont->spr.cy);
	
	return true;
}

void FontSetInitChar(TYPE_FONT * ptrFont, char c)
{
	ptrFont->init_ch = c;
}

void FontSetFlags(TYPE_FONT * ptrFont, FONT_FLAGS flags)
{
	ptrFont->flags = flags;
}

void FontSetSize(TYPE_FONT * ptrFont, short size, short bitshift)
{
	ptrFont->char_w = size;
	ptrFont->char_h = size;

    ptrFont->char_w_bitshift = bitshift;
	
	//At this point, spr.w and spr.h = real w/h
	ptrFont->char_per_row = (uint8_t)(ptrFont->spr_w / ptrFont->char_w);
	ptrFont->max_ch_wrap = 0;
	
	ptrFont->spr.w = ptrFont->char_w;
	ptrFont->spr.h = ptrFont->char_h;
}

void FontSetSpacing(TYPE_FONT* ptrFont, short spacing)
{
    ptrFont->char_spacing = spacing;
}

void FontCyclic(void)
{
	_blend_effect_lum -= 8;
}

void FontPrintText(TYPE_FONT * ptrFont, short x, short y, char* str, ...)
{
	uint16_t i;
	uint16_t line_count = 0;
	int result;
	short orig_x = x;
	
	va_list ap;
	
	va_start(ap, str);
	
	result = vsnprintf(	_internal_text,
						FONT_INTERNAL_TEXT_BUFFER_MAX_SIZE,
						str,
						ap	);

    if(ptrFont->flags & FONT_H_CENTERED)
    {
        x = (X_SCREEN_RESOLUTION >> 1) - ((result >> 1) << ptrFont->char_w_bitshift);
        orig_x = x;
    }
	
	for(i = 0; i < result ; i++)
	{
		char _ch = _internal_text[i];
		
		if(_ch == '\0')
		{
			// End of string
			break;
		}
		
		switch(_ch)
		{
			case ' ':
				x += ptrFont->char_w;
				continue;
			case '\n':
				x = orig_x;
				y += ptrFont->char_h;
			break;
			default:
				if(	(ptrFont->flags & FONT_WRAP_LINE) && (ptrFont->max_ch_wrap != 0) )
				{
					if(++line_count >= ptrFont->max_ch_wrap)
					{
						line_count = 0;
						x = orig_x;
						y += ptrFont->char_h;
					}
				}
	
				ptrFont->spr.x = x;
				ptrFont->spr.y = y;
				ptrFont->spr.w = ptrFont->char_w;
				ptrFont->spr.h = ptrFont->char_h;
				ptrFont->spr.u = (short)( (_ch - ptrFont->init_ch) % ptrFont->char_per_row) * ptrFont->char_w;
				ptrFont->spr.u += ptrFont->spr_u; // Add original offset for image
				ptrFont->spr.v = (short)( (_ch - ptrFont->init_ch) / ptrFont->char_per_row) * ptrFont->char_h;
				ptrFont->spr.v += ptrFont->spr_v; // Add original offset for image
				
				if(ptrFont->flags & FONT_BLEND_EFFECT)
				{
					ptrFont->spr.r += 8;
					ptrFont->spr.g += 8;
					ptrFont->spr.b += 8;
				}
				else
				{
					ptrFont->spr.r = NORMAL_LUMINANCE;
					ptrFont->spr.g = NORMAL_LUMINANCE;
					ptrFont->spr.b = NORMAL_LUMINANCE;
				}
				/*dprintf("char_w = %d, char_h = %d, char_per_row = %d, init_ch: %c\n",
						ptrFont->char_w,
						ptrFont->char_h,
						ptrFont->char_per_row,
						ptrFont->init_ch);
				dprintf("Char: %c, spr.u = %d, spr.v = %d\n",str[i],ptrFont->spr.u, ptrFont->spr.v);
				dprintf("Sprite CX = %d, sprite CY = %d\n",ptrFont->spr.cx, ptrFont->spr.cy);*/
				//dprintf("Sprite rgb={%d,%d,%d}\n",ptrFont->spr.r, ptrFont->spr.g, ptrFont->spr.b);
				
				GfxSortSprite(&ptrFont->spr);
				x += ptrFont->char_spacing;
			break;
		}
	}
	
	if(ptrFont->flags & FONT_BLEND_EFFECT)
	{
		ptrFont->spr.r = _blend_effect_lum;
		ptrFont->spr.g = _blend_effect_lum;
		ptrFont->spr.b = _blend_effect_lum;
	}

	va_end(ap);
}