summaryrefslogtreecommitdiffstats
path: root/makefont/makefont.c
blob: 5480fb675bfe1d8b5a397d2de7deaca1abfb0443 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <math.h>
#include <fcntl.h>

#include <SDL.h>
#include <SDL/SDL_ttf.h>

#define XSIZE 1200
#define YSIZE 32

Uint32 gridcolor,xcolor,ocolor,white;

int mousex,mousey;

int bgr=0, bgg=00, bgb=0;
int fgr=255, fgg=255, fgb=255;


SDL_Surface *readppm(char *name)
{
char line[1024*3], *p;
int width=0, height=0;
int len;
SDL_Surface *s;
int x;
unsigned char *put;
FILE *f;
void *res;

	f=fopen(name, "r");
	if(!f)
	{
		fprintf(stderr, "Couldn't open %s\n", name);
		exit(-1);
	}
	p=fgets(line, sizeof(line), f); // P6
	if(!p || strncmp(p, "P6", 2))
	{
		fclose(f);
		fprintf(stderr, "Not ppm file %s\n", line);
		exit(-1);
	}
	res=fgets(line, sizeof(line), f); // 480 270
	sscanf(line, "%d %d", &width, &height);
	res=fgets(line, sizeof(line), f); // 255

	s = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
		0x0000ff, 0x00ff00, 0xff0000, 0x0);

	x=0;
	put = s->pixels;

	while((len = fread(line, 1, sizeof(line), f)) > 0)
	{
		p=line;
		while(len>0)
		{
			int r,g,b;
			r=*p++;
			g=*p++;
			b=*p++;
			put[x++] = r;
			put[x++] = g;
			put[x++] = b;
			if(x==width*3)
			{
				x = 0;
				put += s->pitch;
			}
			len-=3;
		}
	}
	fclose(f);
	return s;

}



SDL_Surface *thescreen;
void clear(SDL_Surface *s)
{
int i,c;
	c = SDL_MapRGB(s->format, bgr, bgg, bgb);
	for(i=0;i<s->w;++i)
		*(unsigned short *)(s->pixels+i*2) = c;
	for(i=1;i<s->h;++i)
		memcpy(s->pixels + i*s->pitch, s->pixels, s->w*2);

}
inline void colordot(SDL_Surface *s, unsigned x,unsigned y,int c)
{
	if(x<s->w && y<s->h)
		*((uint32_t *)s->pixels+y*thescreen->pitch/4+x)=c;
}


void scrlock(SDL_Surface *s)
{
	if(SDL_MUSTLOCK(s))
	{
		if ( SDL_LockSurface(s) < 0 )
		{
			fprintf(stderr, "Couldn't lock display surface: %s\n",
								SDL_GetError());
			return;
		}
	}
}
void scrunlock(SDL_Surface *s)
{
	if(SDL_MUSTLOCK(s))
		SDL_UnlockSurface(s);
}
void copyup(SDL_Surface *s)
{
	SDL_UpdateRect(s, 0, 0, 0, 0);
}

Uint32 maprgb(int r,int g,int b)
{
	return SDL_MapRGB(thescreen->format,r,g,b);
}


void circle(SDL_Surface *s, int cx,int cy,int radius,int c)
{
int x,y,e;

	x=0;
	y=radius;
	e=3-(radius<<1);
	while(x<=y)
	{
		colordot(s, cx+x,cy+y,c);
		colordot(s, cx-x,cy+y,c);
		colordot(s, cx+x,cy-y,c);
		colordot(s, cx-x,cy-y,c);
		colordot(s, cx+y,cy+x,c);
		colordot(s, cx-y,cy+x,c);
		colordot(s, cx+y,cy-x,c);
		colordot(s, cx-y,cy-x,c);
		if(e<0)
			e+=(x<<2)+6;
		else
		{
			e+=((x-y)<<2)+10;
			--y;
		}
		++x;
	}
}


#define JUST_CENTER 0
#define JUST_LEFT   1
#define JUST_RIGHT  2

void text_to(SDL_Surface *dest, TTF_Font *font, int x, int y, int just,
		char *fmt, ...)
{
SDL_Surface *s;
SDL_Rect r;
char text[256];
va_list ap;
SDL_Color tc = {fgr, fgg, fgb, 0};


	va_start(ap, fmt);
	vsnprintf(text, sizeof(text), fmt, ap);
	va_end(ap);

	s = TTF_RenderUTF8_Blended(font, text, tc);
	r.x = x;
	if(just == JUST_CENTER)
		r.x -= s->w/2;
	else if(just == JUST_RIGHT)
		r.x -= s->w;

	r.y = y - s->h/2;
	SDL_BlitSurface(s, 0, dest, &r);
	SDL_FreeSurface(s);
}

struct state {
	SDL_Surface *surface;
	SDL_Surface *pictures;
	TTF_Font *bigfont, *smallfont;
	int mousex, mousey;
};

int writeppm(SDL_Surface *thescreen, char *name)
{
int ofile;
unsigned char text[8192],*p;
char temp[128];

uint32_t *take;
int i,j;
int res;

	ofile=open(name,O_WRONLY|O_CREAT|O_TRUNC,0644);
	if(ofile<0) return -1;
	sprintf(temp,"P6\n");
	res=write(ofile,temp,strlen(temp));
	sprintf(temp,"%d %d\n", thescreen->w, thescreen->h);
	res=write(ofile,temp,strlen(temp));
	sprintf(temp,"255\n");
	res=write(ofile,temp,strlen(temp));
	take=(void *) thescreen->pixels;
	j=thescreen->h;
	while(j--)
	{
		p = text;
		for(i=0;i<thescreen->w;++i)
		{
			SDL_GetRGB(take[i],thescreen->format,p,p+1,p+2);
			p+=3;
		}
		res=write(ofile,text,p-text);
		take+=thescreen->pitch / sizeof(*take);
	}
//printf("%d\n", thescreen->format->BytesPerPixel);
	return 0;
}

void paint_all(struct state *st)
{
SDL_Surface *s = st->surface;
int x, y;
char *p;
SDL_Surface *ts;
SDL_Color tc = {255, 255, 255, 0};
SDL_Rect r;
int gap = maprgb(255,0,255);

	scrlock(s);
	clear(s);
	p="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.!:>^@() + ";
//	p="uvwxyz0123456789.!:>^@() + ";

//	text_to(s, st->bigfont, 0, 15, JUST_LEFT, "foo!");

	x = 0;
	while(*p)
	{
		char text[2];
		text[0] = *p++;
		text[1] = 0;
		ts = TTF_RenderUTF8_Blended(st->bigfont, text, tc);
		r.x = x+1;
		r.y = 15 - ts->h / 2;
		SDL_BlitSurface(ts, 0, s, &r);
		for(y=1;y<s->h-2;++y)
			colordot(s, x, y, gap);
		x += ts->w+1;
		SDL_FreeSurface(ts);
	}

	scrunlock(st->surface);
	copyup(st->surface);
	writeppm(s, "/tmp/font.ppm");
exit(0);

}




int main(int argc,char **argv)
{
int first;
int code;
SDL_Event event;
Uint32 videoflags;
struct state st;

	if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
	{
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		exit(1);
	}
	videoflags = 0;
	thescreen = SDL_SetVideoMode(XSIZE, YSIZE, 32, videoflags);
	if ( thescreen == NULL )
	{
		fprintf(stderr, "Couldn't set display mode: %s\n",
							SDL_GetError());
		exit(5);
	}

	TTF_Init();

	memset(&st, 0, sizeof(st));
	st.surface = thescreen;
//	st.pictures = readppm("animals.ppm");
	st.bigfont = TTF_OpenFont("hockey.ttf", 30);
	st.smallfont = TTF_OpenFont("hockey.ttf", XSIZE/16);


	first=0;

	for(;;)
	{
		usleep(50000);
		paint_all(&st);
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
			case SDL_MOUSEMOTION:
				mousex=event.motion.x;
				mousey=event.motion.y;
				break;
			case SDL_MOUSEBUTTONDOWN:
				break;
			case SDL_KEYDOWN:
				code=event.key.keysym.sym;
				if(code==SDLK_ESCAPE) exit(0);
				break;
			}
		}
	}
	return 0;
}