File: SFont.c

package info (click to toggle)
rockdodger 0.9.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,220 kB
  • ctags: 756
  • sloc: ansic: 5,374; makefile: 159; sh: 21
file content (284 lines) | stat: -rw-r--r-- 7,584 bytes parent folder | download | duplicates (2)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <SDL/SDL.h>
#include "SFont.h"
#include "string.h"

SFont_FontInfo InternalFont;
int alpha = 127;
int font_height;
SDL_Surface *Back = NULL;
char tmp[1024];

Uint32 GetPixel(SDL_Surface * Surface, Sint32 X, Sint32 Y) {

  Uint8 *bits;
  Uint32 Bpp;

  if(X < 0)
    puts
      ("SFONT ERROR: x too small in GetPixel. Report this to <karlb@gmx.net>");
  if(X >= Surface->w)
    puts
      ("SFONT ERROR: x too big in GetPixel. Report this to <karlb@gmx.net>");

  Bpp = Surface->format->BytesPerPixel;

  bits = ((Uint8 *) Surface->pixels) + Y * Surface->pitch + X * Bpp;

  // Paint the walls with the fresh blood of your enemies

  // Get the pixel
  switch (Bpp) {
  case 1:
    return *((Uint8 *) Surface->pixels + Y * Surface->pitch + X);
    break;
  case 2:
    return *((Uint16 *) Surface->pixels + Y * Surface->pitch / 2 + X);
    break;
  case 3:{			// Format/endian independent 
      Uint8 r, g, b;
      r = *((bits) + Surface->format->Rshift / 8);
      g = *((bits) + Surface->format->Gshift / 8);
      b = *((bits) + Surface->format->Bshift / 8);
      return SDL_MapRGB(Surface->format, r, g, b);
    }
    break;
  case 4:
    return *((Uint32 *) Surface->pixels + Y * Surface->pitch / 4 + X);
    break;
  }

  return -1;
}

void InitFont2(SFont_FontInfo * Font) {
  int x = 0, i = 0;

  if(Font->Surface == NULL) {
    printf("The font has not been loaded!\n");
    exit(1);
  }

  while(x < Font->Surface->w) {
    if(GetPixel(Font->Surface, x, 0) ==
       SDL_MapRGB(Font->Surface->format, 255, 0, 255)) {
      Font->CharPos[i++] = x;
      while((x < Font->Surface->w - 1)
	    && (GetPixel(Font->Surface, x, 0) ==
		SDL_MapRGB(Font->Surface->format, 255, 0, 255)))
	x++;
      Font->CharPos[i++] = x;
    }
    x++;
  }

  Font->h = Font->Surface->h;
  font_height = Font->h;
  SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY,
		  GetPixel(Font->Surface, 0, Font->Surface->h - 1));
}

void InitFont(SDL_Surface * Font) {
  InternalFont.Surface = Font;
  InitFont2(&InternalFont);
}

int SFont_wide(const char *text) {
  int i = 0, xwide = 0;
  int ofs;
  SFont_FontInfo *Font = &InternalFont;

  while(text[i]) {
    if(text[i] == ' ') {
      xwide += (int) (Font->CharPos[2] - Font->CharPos[1]);
    } else {
      ofs = (text[i] - 33) * 2 + 1;
      xwide += (int) (Font->CharPos[ofs + 1] - Font->CharPos[ofs]);
    }
    i++;
  }
  return xwide;
}

int SFont_height() {
  return InternalFont.Surface->h - 1;
}

void PutString2(SDL_Surface * Surface, SFont_FontInfo * Font, int x, int y,
		const char *text) {
  int ofs;
  int i = 0;
  SDL_Rect srcrect, dstrect;

  while(text[i]) {
    if(text[i] == ' ') {
      x += Font->CharPos[2] - Font->CharPos[1];
    } else {
//          printf("-%c- %c - %u\n",228,text[i],text[i]);
      ofs = (text[i] - 33) * 2 + 1;
      //printf("printing %c %d\n",text[i],ofs);
      srcrect.w = dstrect.w =
	(Font->CharPos[ofs + 2] + Font->CharPos[ofs + 1]) / 2 -
	(Font->CharPos[ofs] + Font->CharPos[ofs - 1]) / 2;
      srcrect.h = dstrect.h = Font->Surface->h - 1;
      srcrect.x = (Font->CharPos[ofs] + Font->CharPos[ofs - 1]) / 2;
      srcrect.y = 1;
      dstrect.x =
	x - (float) (Font->CharPos[ofs] - Font->CharPos[ofs - 1]) / 2;
      dstrect.y = y;

      //SDL_SetAlpha ( Font->Surface, SDL_SRCALPHA, 127);
      SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect);

      x += Font->CharPos[ofs + 1] - Font->CharPos[ofs];
    }
    i++;
  }
}

// Return a new surface, with the text on it.
// This surface is new, fresh, and must eventually be freed.
// Create the new surface with the same colour system as a parent surface.
SDL_Surface *new_Surface_PutString(SDL_Surface * parent, const char *text) {

  Uint32 rmask = parent->format->Rmask;
  Uint32 gmask = parent->format->Gmask;
  Uint32 bmask = parent->format->Bmask;
  Uint32 amask = parent->format->Amask;
  Uint32 bytesperpixel = parent->format->BytesPerPixel;

  return SDL_CreateRGBSurface(SDL_SWSURFACE,
			      SFont_wide(text),
			      SFont_height(),
			      bytesperpixel, rmask, gmask, bmask, amask);
}

void PutString(SDL_Surface * Surface, int x, int y, const char *text) {
  PutString2(Surface, &InternalFont, x, y, text);
}

int TextWidth2(SFont_FontInfo * Font, char *text) {
  int x = 0, i = 0;
  unsigned char ofs = 0;
  while(text[i] != '\0') {
    if(text[i] == ' ') {
      x += Font->CharPos[2] - Font->CharPos[1];
      i++;
    } else {
      ofs = (text[i] - 33) * 2 + 1;
      x += Font->CharPos[ofs + 1] - Font->CharPos[ofs];
      i++;
    }
  }
  return x + Font->CharPos[ofs + 2] - Font->CharPos[ofs + 1];
}

int TextWidth(char *text) {
  return TextWidth2(&InternalFont, text);
}

void TextAlpha(int a) {
  alpha = a;
}

void XCenteredString2(SDL_Surface * Surface, SFont_FontInfo * Font, int y,
		      char *text) {
  PutString2(Surface, &InternalFont, Surface->w / 2 - TextWidth(text) / 2, y,
	     text);
}

void XCenteredString(SDL_Surface * Surface, int y, char *text) {
  XCenteredString2(Surface, &InternalFont, y, text);
}

void clearBuffer() {
  SDL_Event event;

  // Delete the event buffer
  while(SDL_PollEvent(&event));
  // start input
  SDL_EnableUNICODE(1);
}

int SFont_Input2(SDL_Surface * Dest, SFont_FontInfo * Font, int x, int y,
		 int PixelWidth, char *text) {
  SDL_Event event;
  SDL_Rect rect;
  int ofs = (text[0] - 33) * 2 + 1;
  int leftshift;
  int ch = 0; //Fixes a warning: may be used unintialised.

  if(ofs < 0) {
    leftshift = 0;
  } else {
    leftshift = (Font->CharPos[ofs] - Font->CharPos[ofs - 1]) / 2;
  }

  rect.x = x - leftshift;
  rect.y = y;
  rect.w = PixelWidth;
  rect.h = Font->Surface->h;

  //SDL_SetAlpha (Dest, SDL_SRCALPHA, 127);

  SDL_BlitSurface(Dest, &rect, Back, NULL);
  sprintf(tmp, "%s_", text);
  PutString2(Dest, Font, x, y, tmp);
  SDL_UpdateRect(Dest, x - leftshift, y, PixelWidth, Font->h);

  while(SDL_PollEvent(&event) && event.type == SDL_KEYDOWN) {

    // Find the character pressed
    ch = event.key.keysym.unicode;

    // If the character is illegal, we PASS IT THROUGH TO APPEND TO THE
    // STRING ANYWAY! Boy are we dumb sometimes.  This character should
    // first be checked to see if it's in the list of characters supported
    // by this font.
    // TODO

    // If backspace and the length of the text > 0, reduce the string by 1 character
    if(ch == '\b') {
      if(strlen(text) > 0) {
	text[strlen(text) - 1] = '\0';
      }
    } else {
      sprintf(text, "%s%c", text, ch);
    }

    // If the new character would exceed the allowed width
    if(TextWidth2(Font, text) > PixelWidth) {
      text[strlen(text) - 1] = '\0';
    }
    //SDL_SetAlpha (Back, SDL_SRCALPHA, 127);
    SDL_BlitSurface(Back, NULL, Dest, &rect);
    PutString2(Dest, Font, x, y, text);
    if(ofs > 0) {
      SDL_UpdateRect(Dest,
		     x - (Font->CharPos[ofs] - Font->CharPos[ofs - 1]) / 2, y,
		     PixelWidth, Font->Surface->h);
    }

  }
  if(ch == SDLK_RETURN) {
    SDL_FreeSurface(Back);
    Back = NULL;
    text[strlen(text)-1]='\0'; //Remove the CR character as this produces weird effects.
    return 1;
  } else
    return 0;
}

int SFont_Input(SDL_Surface * Dest, int x, int y, int PixelWidth, char *text) {
  if(Back == NULL) {
    Back = SDL_AllocSurface(Dest->flags,
			    PixelWidth,
			    InternalFont.h,
			    Dest->format->BitsPerPixel,
			    Dest->format->Rmask,
			    Dest->format->Gmask, Dest->format->Bmask, 0);
  }
  return SFont_Input2(Dest, &InternalFont, x, y, PixelWidth, text);
  // ph:
  // Returns 1 when the return key is pressed
  // Returns 0 when nothing exceptional happened
}