File: text.c

package info (click to toggle)
wizznic 0.9.2-preview2%2Bdfsg-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 49,456 kB
  • ctags: 808
  • sloc: ansic: 6,842; sh: 139; makefile: 65; xml: 7
file content (144 lines) | stat: -rw-r--r-- 3,062 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
#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;
  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]);
}