File: dfont.cpp

package info (click to toggle)
openssn 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,340 kB
  • sloc: cpp: 10,973; makefile: 80
file content (157 lines) | stat: -rw-r--r-- 3,611 bytes parent folder | download | duplicates (3)
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
// $Id: dfont.cpp,v 1.3 2003/04/14 05:51:03 mbridak Exp $
#include "dfont.h"
#include "files.h"

DFont::DFont (char * IFile, char * DFile)
{
  loadImage(IFile);

  ifstream inCredit(DFile, ios::in);
  if(!inCredit)
  //  if (!fstream(DFile, ios::in)) //does the file exist?
    initMap(DFile); //if the font file doesn't exist then create one
  else
    loadMap(DFile);
  
  loadMap(DFile);
  gap = fonts['!'].w;
}

DFont::~DFont ()
{
  SDL_FreeSurface(fontSurface);
}

void DFont::loadImage (char *name)
{
	SDL_Surface *temp;
	temp = Load_Image(name);
	fontSurface = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
  if (fontSurface == NULL) {
    cerr << "Error loading font!\n";
    abort();
  }
 // SDL_SetColorKey(fontSurface, SDL_SRCCOLORKEY,
	//	  GetPixel(fontSurface, 0, 1));
}


Uint32 DFont::GetPixel (SDL_Surface *screen, Sint32 x, Sint32 y)
{
  switch (screen->format->BytesPerPixel) {
  case 1: { /* Assuming 8-bpp */
    return *((Uint8 *)screen->pixels + y*screen->pitch + x);
  }
  break;

  case 2: { /* Probably 15-bpp or 16-bpp */
    return *((Uint16 *)screen->pixels + y*screen->pitch/2 + x);
  }
  break;

  case 3: { /* Slow 24-bpp mode, usually not used */
				//Fixme
    return 0;
  }
  break;

  case 4: { /* Probably 32-bpp */
    return *((Uint32 *)screen->pixels + y*screen->pitch/4 + x);
  }
  break;
  }
  return 0xffffffff;
}

void DFont::loadMap (char * name)
{
  ifstream file (name);
  char c='!';  //all fonts must start with ! and then increment through the
               //ASCII table!!
  while (file >> fonts[c++]) ;
}

void DFont::PutString (SDL_Surface * screen, Sint16 x, Sint16 y, char *
str)
{
  SDL_Rect dest;
  for (; *str != '\0'; ++str)
    {
      if (*str == ' ')
        x+=gap;
      else
    {
      dest.w = fonts[*str].w;
      dest.h = fonts[*str].h;
      dest.x = x;
      dest.y = y;
      SDL_BlitSurface (fontSurface, &fonts[*str], screen, &dest);
      SDL_UpdateRects(screen, 1, &dest);
      x+=dest.w;
    }
    }
}

void DFont::PutChar (SDL_Surface * screen, Sint16 x, Sint16 y, char c)
{
  SDL_Rect dest;
  dest.w = fonts[c].w;
  dest.h = fonts[c].h;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface (fontSurface, &fonts[c], screen, &dest);
	SDL_UpdateRects(screen, 1, &dest);
}


/*
This function used to save our newly created fonts to a file.
This is a bad idea as we do not know where the file is being
created. It usually ends up in ./data/ and that is not
nice behaviour. Commenting out the file creation and
writes to the file to avoid this. We ship with the proper
font files in the tarball, that should be enough.
-- Jesse
*/
void DFont::initMap (char * name)
{
  // ofstream file(name);
  //Sentry value
  Uint32 sentry = SDL_MapRGB(fontSurface->format, 255, 0, 255);
  char index = '!';  //Start of the fonts in ASCII
  //Uint16 w;
  Sint16 x(0);
//Read the file from left to right detecting the font break markers along the way
  while ( x < fontSurface->w )
    {
      if (GetPixel(fontSurface, x, 0) != sentry) {
          fonts[index].x = x;
          fonts[index].y = 1;
          fonts[index].h = fontSurface->h;
          for (; GetPixel(fontSurface, x, 0) != sentry && x < fontSurface->w; ++x)
            ;
          fonts[index].w = (x - fonts[index].x);
        // file << fonts[index];
          ++index;
        }
      else
        ++x;
    }
}


//Good Ol operator overloading Ahhhhh
ostream& operator << (ostream & os, SDL_Rect const & r)
{
    return os << r.x << ' ' << r.y << ' '<< r.w << ' ' << r.h << ' ';
}

istream& operator >> (istream & is, SDL_Rect & r)
{
    return is >> r.x >> r.y >> r.w >> r.h;
}