File: JUtil.cpp

package info (click to toggle)
holotz-castle 1.3.14-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,252 kB
  • sloc: cpp: 21,636; makefile: 154
file content (262 lines) | stat: -rw-r--r-- 6,127 bytes parent folder | download | duplicates (5)
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
/*
 *  JLib - Jacob's Library.
 *  Copyright (C) 2003, 2004  Juan Carlos Seijo Prez
 * 
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 * 
 *  This library 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
 *  Library General Public License for more details.
 * 
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  Juan Carlos Seijo Prez
 *  jacob@mainreactor.net
 */

/** Macros y funciones de utilidad diversa.
 * @file    JUtil.cpp.
 * @author  Juan Carlos Seijo Prez
 * @date    16/10/2003
 * @version 0.0.1 - 16/10/2003 - Primera versin.
 */

#include <JLib/Util/JUtil.h>

// Carga una fuente de un fichero Targa, con caracteres de 8x8 pxels,
// 16 filas y 16 columnas (256). 
void JDumpTGAFontBits(const JString &TGAFileName, const JString &outFileName)
{
  JImage img;
  if (!img.Load(TGAFileName))
    return;

  u8 *chars[256];
  u8 *ptr;
  u32 rowSize;
  s32 ind, imgInd;

  rowSize = (u32)(img.Line(1) - img.Line(0));
  
  for (s32 j = 0; j < 16; ++j)
  {
    // Avanza al primer caracter de la siguiente fila
    ptr = img.Line(j * 8);

    for (s32 i = 0; i < 16; ++i)
    {
      ind = ((15 - j) * 16) + i; // = (row * nCharsRow) + col

      // Reservamos 8 bytes para cada caracter
      chars[ind] = new u8[8];
      memset(chars[ind], 0, 8 * sizeof(s8));

      for (s32 y = 0; y < 8; ++y)
      {
        imgInd = (y * rowSize);

        for (s32 x = 0; x < 8; ++x)
        {
          if (0xFF & *(ptr + imgInd))
          {
						// 7 - y empieza a rellenar por abajo, ya que en memoria est invertido
            chars[ind][y] |= 0x80 >> x;
          }

          imgInd += 4;
        }
      }

      // Avanza al siguiente caracter de la fila
      ptr += 32;
    }
  }

  // Vuelca en formato de array los datos de la fuente al fichero dado
  JTextFile outFile(outFileName);
  if (outFile.Open())
	{
		outFile.Printf("const u8 font[256][] = {\n");
		for (s32 row = 0; row < 16; ++row)
		{
			for (s32 col = 0; col < 16; ++col)
			{
				outFile.Printf("{");
				for (s32 idx = 0; idx < 8; ++idx)
				{
					outFile.Printf("0x%02x,", chars[(row * 16) + col][idx]);
				}
				outFile.Printf("},\n");
			}
		}
		
		outFile.Printf("};\n");
	}
	else
	{
		printf("DumpTGAFontBits: Error al abrir el archivo.\n");
	}

  for (s32 i = 0; i < 256; ++i)
    delete[] chars[i];
}

JVideoMode * JListVideoModes(s32 *count, s32 depth)
{
	bool uninit = false;
	s32 bpps[] = {32, 24, 16, 8};

	s32 *pBpps = bpps, nBpps = 4;
	
	if (depth != 0)
	{
		pBpps = &depth;
		nBpps = 1;
	}
	
	if (SDL_WasInit(SDL_INIT_VIDEO) == 0)
	{
		// Video no inicializado, lo inicializa
		if (-1 == SDL_Init(SDL_INIT_VIDEO))
		{
			// No se puede iniciar el video
			return 0;
		}

		// Deinicializar SDL al final
		uninit = true;
	}
	
	std::vector<JVideoMode*> modes;
	SDL_Rect **modeList = 0;
	JVideoMode *mode;
	
	// Busca los modos de vdeo sin aceleracin para la profundidad actual y comprueba para el resto de profundidades
	modeList = SDL_ListModes(0, SDL_FULLSCREEN);
	if ((long)modeList == 0 || (long)modeList == -1)
	{
		return 0;
	}

	s32 rec;

	for (s32 k = 0; k < nBpps; ++k)
	{
		for (s32 j = 0; modeList[j] != 0; ++j)
		{
			if (0 != (rec = SDL_VideoModeOK(modeList[j]->w, modeList[j]->h, pBpps[k], SDL_HWSURFACE | SDL_FULLSCREEN))
					&& (j == 0 || (modeList[j]->w != modeList[j - 1]->w || modeList[j]->h != modeList[j - 1]->h)))
			{
				mode = new JVideoMode;
				mode->w = modeList[j]->w;
				mode->h = modeList[j]->h;
				mode->bpp = pBpps[k];
				mode->hw = true;
				modes.push_back(mode);
			}
		}
	}

	JVideoMode *result = 0;

	if (modes.size() == 0)
	{
		result = 0;
	}
	else
	{
		if (count)
			*count = modes.size();

		result = new JVideoMode [modes.size() + 1];
		for (u32 i = 0; i < modes.size(); ++i)
		{
			memcpy(&result[i], modes[i], sizeof(JVideoMode));
			delete modes[i];
		}
		
		// ltimo elemento
		result[modes.size()].w = result[modes.size()].h = -1;
		modes.clear();
	}
	
	if (uninit)
	{
		SDL_QuitSubSystem(SDL_INIT_VIDEO);
	}
	
	return result;
}

void JDumpImage(JImage *img, s32 n)
{
	SDL_Surface *s = img->Surface();
	SDL_PixelFormat *fmt = s->format;

	if (0 != SDL_LockSurface(s))
	{
		return;
	}

	fprintf(stderr, 
					"%dx%d@%d CKEY: %08x ALPHA: %d SRCALPHA: %s SRCCOLORKEY: %s RLE: %s\n", 
					s->w, s->h, fmt->BitsPerPixel, fmt->colorkey, fmt->alpha, 
					s->flags & SDL_SRCALPHA ? "yes" : "no", 
					s->flags & SDL_SRCCOLORKEY ? "yes" : "no",
					s->flags & SDL_RLEACCEL ? "yes" : "no");
	fprintf(stderr, 
					"RGBAmask: R: 0x%08x G: 0x%08x B: 0x%08x A: 0x%08x\n", 
					fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);
	fprintf(stderr, 
					"RGBAshift: R: 0x%08x G: 0x%08x B: 0x%08x A: 0x%08x\n", 
					fmt->Rshift, fmt->Gshift, fmt->Bshift, fmt->Ashift);
	fprintf(stderr, 
					"RGBAloss: R: 0x%08x G: 0x%08x B: 0x%08x A: 0x%08x\n", 
					fmt->Rloss, fmt->Gloss, fmt->Bloss, fmt->Aloss);

	// Muestra el valor de algunos pxeles
	if (n != 0)
	{
		fprintf(stderr, "First %d pixels:\n", n);
		for (s32 j = 0, count = 1; count <= n && j < s->h; ++j)
		{
			for (s32 i = 0; count <= n && i < s->w; ++i)
			{
				fprintf(stderr, "%08x ", img->GetPixel(i, j));

				if (count % 10 == 0)
					fprintf(stderr, "\n");

				++count;
			}
		}

		fprintf(stderr, "First %d pixels != CKEY:\n", n);
		for (s32 j = 0, count = 1; count <= n && j < s->h; ++j)
		{
			for (s32 i = 0; count <= n && i < s->w; ++i)
			{
				if (img->GetPixel(i, j) != fmt->colorkey)
				{
					fprintf(stderr, "%08x ", img->GetPixel(i, j));

					if (count % 10 == 0)
						fprintf(stderr, "\n");

					++count;
				}
			}
		}
		
		fprintf(stderr, "\n");
	}
	
	SDL_UnlockSurface(s);
}