-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSDLFont.cpp
More file actions
166 lines (139 loc) · 3.6 KB
/
SDLFont.cpp
File metadata and controls
166 lines (139 loc) · 3.6 KB
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
// Copyright (C) 2003 Jeremy Stanley
// This is free software licensed under the GNU GPL; see COPYING for details
#include "SDLFont.h"
#include <string.h>
SDLFont::SDLFont(SDL_Surface *pSDL)
{
pDisplay = pSDL;
pFont = NULL;
SetColor(255, 255, 255);
dpitch = pSDL->pitch;
Bpp = pSDL->format->BytesPerPixel;
}
SDLFont::~SDLFont()
{
Clear();
}
bool SDLFont::LoadFont(const char *bitmap)
{
Clear();
if(!(pFont=SDL_LoadBMP(bitmap)))
return false;
if(pFont->format->BytesPerPixel != 1)
{
Clear();
return false;
}
InitCommon();
return true;
}
bool SDLFont::InitFont(void *pixels, int width, int height)
{
Clear();
if ( !(pFont = SDL_CreateRGBSurfaceFrom(pixels, width, height, 8, width,
0, 0, 0, 0)) )
return false;
InitCommon();
return true;
}
void SDLFont::InitCommon()
{
cw = pFont->w / 96;
ch = pFont->h;
bgcolor = *(Uint8 *)pFont->pixels;
cwidth = Bpp * cw;
cpitch = pFont->pitch;
dinc = dpitch - cwidth;
cinc = cpitch - cw;
}
void SDLFont::Clear()
{
if(pFont != NULL) {
SDL_FreeSurface(pFont);
pFont=NULL;
}
}
void SDLFont::SetColor(int r, int g, int b)
{
pixel = SDL_MapRGB(pDisplay->format, r, g, b);
}
void SDLFont::WriteText(int x, int y, const char *text)
{
if(pFont==NULL)
return; // not initialized
if(y + ch > pDisplay->h)
return; // off the bottom of the screen
Uint8 *pixloc = (Uint8 *)pDisplay->pixels + dpitch * y + Bpp * x;
Uint8 *pChar, *pScreen;
char c;
int i, j;
for(const char *p = text; *p != '\0'; p++)
{
if(x + cw > pDisplay->w)
return; // off the edge of the screen
c = *p - 32; if(c < 0 || c > 95) c = 0;
pScreen = pixloc;
pChar = (Uint8 *)pFont->pixels + c * cw;
switch(Bpp) {
case 1:
for(i = 0; i < ch; i++) {
for(j = 0; j < cw; j++) {
if(*pChar != bgcolor)
*pScreen = (Uint8)pixel;
pChar++;
pScreen += Bpp;
}
pChar += cinc;
pScreen += dinc;
}
break;
case 2:
for(i = 0; i < ch; i++) {
for(j = 0; j < cw; j++) {
if(*pChar != bgcolor)
*(Uint16 *)pScreen = (Uint16)pixel;
pChar++;
pScreen += Bpp;
}
pChar += cinc;
pScreen += dinc;
}
break;
case 4:
for(i = 0; i < ch; i++) {
for(j = 0; j < cw; j++) {
if(*pChar != bgcolor)
*(Uint32 *)pScreen = pixel;
pChar++;
pScreen += Bpp;
}
pChar += cinc;
pScreen += dinc;
}
break;
case 3:
for(i = 0; i < ch; i++) {
for(j = 0; j < cw; j++) {
if(*pChar != bgcolor) {
*(pScreen+2) = pixel >> 16;
*(pScreen+1) = pixel >> 8;
*(pScreen) = pixel;
}
pScreen += 3;
pChar++;
}
pChar += cinc;
pScreen += dinc;
}
break;
}
x += cw;
pixloc += cwidth;
}
}
void SDLFont::CenterText(int x0, int y, const char *text)
{
int x = x0 - (strlen(text) * cw) / 2;
if(x < 0) x = 0;
WriteText(x, y, text);
}