-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfont.c
124 lines (101 loc) · 2.75 KB
/
font.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include "font.h"
static SDL_Surface *font_draw_surface;
static Font *font_current;
int font_init(SDL_Surface *surface) {
font_draw_surface = surface;
font_current = NULL;
return(0);
}
Font *font_load_font(char *filename, int charw, int charh, int fontw, int fonth) {
SDL_Surface *tempsurface, *fontsurface;
Font *font;
int numchars;
int x, y, c;
SDL_Rect src, dest;
numchars = fontw * fonth;
if ((fontsurface = SDL_LoadBMP(filename)) == NULL) {
printf("Unable to load %s: %s\n", filename, SDL_GetError());
return(NULL);
}
if (fontsurface->format->BytesPerPixel != 1) {
printf("%s can't be used as a font\n", filename);
SDL_FreeSurface(fontsurface);
return(NULL);
}
if (charw > fontsurface->w / fontw || charh > fontsurface->h / fonth) {
printf("Font size mismatch\n");
SDL_FreeSurface(fontsurface);
return(NULL);
}
dest.w = src.w = charw;
dest.h = src.h = charh;
if ((tempsurface = SDL_CreateRGBSurface(0, numchars * charw, charh, 8, 0, 0, 0, 0)) == NULL) {
printf("Surface allocation failed: %s\n", SDL_GetError());
SDL_FreeSurface(fontsurface);
return(NULL);
}
SDL_SetPalette(tempsurface, SDL_LOGPAL|SDL_PHYSPAL, fontsurface->format->palette->colors, 0, fontsurface->format->palette->ncolors);
SDL_SetColorKey(tempsurface, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0);
c = 0;
for (y = 0; y < fonth; ++y) {
for (x = 0; x < fontw; ++x) {
src.x = x * charw;
src.y = y * charh;
dest.x = c * charw;
dest.y = 0;
SDL_BlitSurface(fontsurface, &src, tempsurface, &dest);
++c;
}
}
SDL_FreeSurface(fontsurface);
if ((font = malloc(sizeof *font)) == NULL) {
return(NULL);
}
/*
font->surface = SDL_DisplayFormat(tempsurface);
SDL_FreeSurface(tempsurface);
if (font->surface == NULL) {
free(font);
return(NULL);
}
*/
font->surface = tempsurface;
font->width = charw;
font->height = charh;
font->numchars = numchars;
return(font);
}
void font_free(Font *font) {
SDL_FreeSurface(font->surface);
free(font);
}
Font *font_set_font(Font *font) {
Font *oldfont;
oldfont = font_current;
font_current = font;
return(oldfont);
}
SDL_Surface *font_set_draw_surface(SDL_Surface *surface) {
SDL_Surface *oldsurface;
oldsurface = font_draw_surface;
font_draw_surface = surface;
return(oldsurface);
}
void font_draw_string(int x, int y, char *text) {
SDL_Rect src, dest;
src.w = dest.w = font_current->width;
src.h = dest.h = font_current->height;
dest.x = x;
dest.y = y;
src.y = 0;
while (*text) {
src.x = (*text) * src.w;
SDL_BlitSurface(font_current->surface, &src, font_draw_surface, &dest);
++text;
dest.x += src.w;
}
}