forked from DusteDdk/Wizznic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.c
161 lines (143 loc) · 4.06 KB
/
text.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
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
/************************************************************************
* This file is part of Wizznic. *
* Copyright 2009-2011 Jimmy Christensen <[email protected]> *
* Wizznic is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Wizznic is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Foobar. If not, see <http://www.gnu.org/licenses/>. *
************************************************************************/
#include <math.h>
#include "text.h"
#include "sprite.h"
#include "pack.h"
#ifndef DATADIR
#define DATADIR ""
#endif
static SDL_Surface* txtSurf[NUMFONTS];
static spriteType* txtSprites[NUMFONTS][91];
static int txtSize[NUMFONTS][2];
//Small is 9x12
//Large is 18x24
void loadFont(const char* imgFileName, int fontNum,int w, int h )
{
txtSize[fontNum][0] = w;
txtSize[fontNum][1] = h;
txtSurf[fontNum] = loadImg( imgFileName );
if(txtSurf[fontNum]!=NULL)
{
int i;
int xpos=0;
int ypos=0;
int linlen=0;
for(i=0; i < 91; i++)
{
txtSprites[fontNum][i] = cutSprite(txtSurf[fontNum],xpos,ypos,txtSize[fontNum][0],txtSize[fontNum][1]);
xpos+=txtSize[fontNum][0];
linlen++;
if(linlen==16)
{
linlen=0;
ypos += txtSize[fontNum][1];
xpos=0;
}
}
} else {
printf("fontLoad(); Can't load '%s'\n", imgFileName);
}
}
void txtLoadGameCharSet(const char* font)
{
char* buf = malloc( sizeof(char)*128 );
sprintf( buf, "%s0.png", font );
loadFont( packGetFile( "themes/chars/",buf),GAMEFONTSMALL,9,12);
sprintf( buf, "%s1.png", font );
loadFont( packGetFile( "themes/chars/",buf),GAMEFONTMEDIUM,18,24);
free( buf );
}
void txtFreeGameCharSet()
{
int i;
for(i=0; i < 91;i++)
{
free( txtSprites[GAMEFONTSMALL][i] );
free( txtSprites[GAMEFONTMEDIUM][i] );
}
SDL_FreeSurface(txtSurf[GAMEFONTSMALL]);
SDL_FreeSurface(txtSurf[GAMEFONTMEDIUM]);
}
void txtInit()
{
loadFont( DATADIR"data/menu/charmap0.png", FONTSMALL,9,12);
loadFont( DATADIR"data/menu/charmap1.png", FONTMEDIUM,18,24);
}
void txtWrite( SDL_Surface* scr,int fontNum, const char* txt, int x, int y)
{
int px=x;
int py=y;
int pos=0;
signed char c;
c = txt[pos];
while( c != '\0')
{
pos++;
if(c=='\n')
{
py+=txtSize[fontNum][1];
px=x-txtSize[fontNum][0];
}
else if(c!=' ' && c>-1 && c<123) //Don't draw spaces or unicode chars. (like danish)
{
drawSprite(scr, txtSprites[fontNum][(int)c-32],px,py);
}
c = txt[pos];
px+=txtSize[fontNum][0];
}
}
void txtWriteCenter( SDL_Surface* scr,int fontNum, const char* txt, int x, int y)
{
int len = txtSize[fontNum][0]*strlen(txt);
x -= len/2;
txtWrite(scr,fontNum,txt,x,y);
}
void txtWave( SDL_Surface* scr, int fontNum, const char* txt, int x, int y, float* rot)
{
//dec rot
*rot -= (float)getTicks()/200.0;
//For each char:
float amp=6;
int px=x;
int py=y;
int pos=0;
int len=strlen(txt);
float chInc = 6.28318531/len;
px = x - (txtSize[fontNum][0]*len)/2;
char c;
c = txt[pos];
while( c != '\0')
{
pos++;
if(c=='\n')
{
py+=txtSize[fontNum][1];
px=x-txtSize[fontNum][0];
}
else if(c!=' ')
{
drawSprite(scr, txtSprites[fontNum][(int)c-32],px,py+(sin(*rot+(pos*chInc))*amp));
}
c = txt[pos];
px+=txtSize[fontNum][0];
}
}
int* getCharSize(int font)
{
return(txtSize[font]);
}