File: font.c

package info (click to toggle)
ale-clone 1.15pre16.3-1
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 2,432 kB
  • ctags: 3,217
  • sloc: ansic: 37,483; sh: 1,105; makefile: 343
file content (196 lines) | stat: -rw-r--r-- 4,634 bytes parent folder | download
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
/*
**	A clone of a famous game.
*/
/**@name font.c		-	The color fonts. */
/*
**	(c) Copyright 1998,1999 by Lutz Sammer
**
**	$Id: font.c,v 1.4 1999/12/09 16:27:08 root Exp $
*/

//@{

/*----------------------------------------------------------------------------
--	Includes
----------------------------------------------------------------------------*/

#include <stdio.h>

#include "clone.h"
#include "video.h"
#include "font.h"

/*----------------------------------------------------------------------------
--	Variables
----------------------------------------------------------------------------*/

/**
**	Fonts table
**		FIXME: human / orcish
*/
global ColorFont Fonts[] = {
    { "graphic/small font.png", 13,14 ,{
	 6, 2, 4, 8, 5, 7, 7, 3, 4, 4, 8, 6, 3, 5, 2, 6,
	 6, 3, 6, 6, 7, 6, 7, 6, 6, 6, 2, 3, 5, 4, 5, 6,
	11, 8, 8, 7, 8, 7, 7, 7, 8, 4, 7,10, 7,10, 8, 7,
	 8, 8, 9, 6, 8, 8, 8,12, 8,11, 7, 3, 5, 3, 6, 8,
	 3, 7,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, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
	 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
    },
};

// FIXME: move the next to video !!

local unsigned char CharColors[7]  = { 246, 200, 199, 197, 192, 0, 104 };
local unsigned char RCharColors[7] = { 0, 246, 246, 246, 104, 0, 0 };

local int ReverseText;
global int DefaultReverseText=1;

/*----------------------------------------------------------------------------
--	Functions
----------------------------------------------------------------------------*/

/**
**	Draw char.
*/
local void DrawChar(Sprite* sprite,int gx,int gy,int w,int h,int x,int y)
{
    int p;
    unsigned char* sp;
    unsigned char* lp;
    unsigned char* gp;
    int sa;
    VMemType* dp;
    int da;
    unsigned char* colors;

    colors=ReverseText ? RCharColors : CharColors;

    sp=sprite->Data+gx+gy*sprite->Width-1;
    gp=sp+sprite->Width*h;
    sa=sprite->Width-w;
    dp=VideoMemory+x+y*VideoWidth-1;
    da=VideoWidth-w;
    while( sp<gp ) {
	lp=sp+w;
	while( sp<lp ) {
	    ++dp;
	    ++sp;
	    p=*sp;
	    if( p==255 ) {
		continue;
	    }
	    *dp=Pixels[colors[p]];
	}
	sp+=sa;
	dp+=da;
    }
}

/**
**	Draw text with font at x,y.
*/
global void DrawText(int x,int y,int font,const unsigned char* text)
{
    int w;
    ColorFont* fp;

    fp=Fonts+font;
    for( ; *text; ++text ) {
	ReverseText=!DefaultReverseText;
	if( *text=='~' ) {
	    ReverseText=DefaultReverseText;
	    ++text;
	}
	w=fp->CharWidth[*text-32];
	DrawChar(&fp->Sprite,0,fp->Height*(*text-32),w,fp->Height,x,y);
	x+=w+1;
    }
}

/**
**	Returns the length of a text.
*/
global int TextLength(int font,const unsigned char* text)
{
    int width;
    const unsigned char* s;
    const ColorFont* fp;

    fp=Fonts+font;
    for( width=0,s=text; *s; ++s ) {
	if( *text=='~' ) {
	    continue;
	}
	width+=fp->CharWidth[*s-32]+1;
    }
    return width;
}

/**
**	Draw text with font at x,y centered.
*/
global void DrawTextCentered(int x,int y,int font,const unsigned char* text)
{
    int dx;
    const unsigned char* s;
    const ColorFont* fp;

    fp=Fonts+font;
    for( dx=0,s=text; *s; ++s ) {
	if( *text=='~' ) {
	    continue;
	}
	dx+=fp->CharWidth[*s-32]+1;
    }
    DrawText(x-dx/2,y,font,text);
}

/**
**	Draw number with font at x,y.
*/
global void DrawNumber(int x,int y,int font,int number)
{
    char buf[sizeof(int)*10+2];

    sprintf(buf,"%d",number);
    DrawText(x,y,font,buf);
}

/**
**	Load all fonts.
*/
global void LoadFonts(void)
{
    unsigned i;

    for( i=0; i<sizeof(Fonts)/sizeof(*Fonts); ++i ) {
	ShowLoadProgress("\tFonts %s\n",Fonts[i].File);
	LoadSprite(Fonts[i].File,&Fonts[i].Sprite);
    }
}

//@}