aboutsummaryrefslogtreecommitdiff
path: root/devtools/create_glk/create_glk.cpp
blob: 82ace274c0680d4014c0bf63cb9b6bb1fdb3943a (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

 // Disable symbol overrides so that we can use system headers.
#define FORBIDDEN_SYMBOL_ALLOW_ALL

// HACK to allow building with the SDL backend on MinGW
// see bug #1800764 "TOOLS: MinGW tools building broken"
#ifdef main
#undef main
#endif // main

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graphics/surface.h"
#include "common/algorithm.h"
#include "common/endian.h"

const byte FONT[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00,
	0xA0, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x50, 0xF8, 0x50, 0xF8, 0x50, 0x00, 0x00, 0x00,
	0x20, 0xF8, 0xA0, 0xF8, 0x28, 0xF8, 0x20, 0x00,
	0x90, 0x10, 0x20, 0x40, 0x80, 0x90, 0x00, 0x00,
	0x40, 0xA0, 0x40, 0xA8, 0x90, 0x68, 0x00, 0x00,
	0x40, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x20, 0x40, 0x80, 0x80, 0x80, 0x40, 0x20, 0x00,
	0x80, 0x40, 0x20, 0x20, 0x20, 0x40, 0x80, 0x00,
	0x90, 0x60, 0xF0, 0x60, 0x90, 0x00, 0x00, 0x00,
	0x00, 0x20, 0x20, 0xF8, 0x20, 0x20, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x80, 0x00,
	0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
	0x10, 0x10, 0x20, 0x40, 0x80, 0x80, 0x00, 0x00,
	0x60, 0x90, 0x90, 0x90, 0x90, 0x60, 0x00, 0x00,
	0x40, 0xC0, 0x40, 0x40, 0x40, 0xE0, 0x00, 0x00,
	0x60, 0x90, 0x20, 0x40, 0x80, 0xF0, 0x00, 0x00,
	0x60, 0x90, 0x20, 0x10, 0x90, 0x60, 0x00, 0x00,
	0xA0, 0xA0, 0xF0, 0x20, 0x20, 0x20, 0x00, 0x00,
	0xF0, 0x80, 0xE0, 0x10, 0x10, 0xE0, 0x00, 0x00,
	0x70, 0x80, 0xE0, 0x90, 0x90, 0x60, 0x00, 0x00,
	0xF0, 0x10, 0x20, 0x40, 0x40, 0x40, 0x00, 0x00,
	0x60, 0x90, 0x60, 0x90, 0x90, 0x60, 0x00, 0x00,
	0x60, 0x90, 0x90, 0x70, 0x10, 0xE0, 0x00, 0x00,
	0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x80, 0x00,
	0x00, 0x20, 0x40, 0x80, 0x40, 0x20, 0x00, 0x00,
	0x00, 0x00, 0xF0, 0x00, 0xF0, 0x00, 0x00, 0x00,
	0x00, 0x80, 0x40, 0x20, 0x40, 0x80, 0x00, 0x00,
	0x70, 0x88, 0x10, 0x20, 0x00, 0x20, 0x00, 0x00,
	0x60, 0x90, 0xB0, 0xB0, 0x80, 0x70, 0x00, 0x00,
	0x60, 0x90, 0x90, 0xF0, 0x90, 0x90, 0x00, 0x00,
	0xE0, 0x90, 0xE0, 0x90, 0x90, 0xE0, 0x00, 0x00,
	0x70, 0x80, 0x80, 0x80, 0x80, 0x70, 0x00, 0x00,
	0xE0, 0x90, 0x90, 0x90, 0x90, 0xE0, 0x00, 0x00,
	0xF0, 0x80, 0xE0, 0x80, 0x80, 0xF0, 0x00, 0x00,
	0xF0, 0x80, 0xE0, 0x80, 0x80, 0x80, 0x00, 0x00,
	0x70, 0x80, 0x80, 0xB0, 0x90, 0x70, 0x00, 0x00,
	0x90, 0x90, 0x90, 0xF0, 0x90, 0x90, 0x00, 0x00,
	0xE0, 0x40, 0x40, 0x40, 0x40, 0xE0, 0x00, 0x00,
	0x10, 0x10, 0x10, 0x10, 0x90, 0x60, 0x00, 0x00,
	0x90, 0xA0, 0xC0, 0xA0, 0x90, 0x90, 0x00, 0x00,
	0x80, 0x80, 0x80, 0x80, 0x80, 0xF0, 0x00, 0x00,
	0x88, 0xD8, 0xA8, 0x88, 0x88, 0x88, 0x00, 0x00,
	0x90, 0xD0, 0xB0, 0x90, 0x90, 0x90, 0x00, 0x00,
	0x60, 0x90, 0x90, 0x90, 0x90, 0x60, 0x00, 0x00,
	0xE0, 0x90, 0x90, 0xE0, 0x80, 0x80, 0x00, 0x00,
	0x60, 0x90, 0x90, 0x90, 0xB0, 0x70, 0x18, 0x00,
	0xE0, 0x90, 0x90, 0xE0, 0xC0, 0xB0, 0x00, 0x00,
	0x70, 0x80, 0x60, 0x10, 0x90, 0x60, 0x00, 0x00,
	0xF8, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x60, 0x00, 0x00,
	0x88, 0x88, 0x88, 0x50, 0x50, 0x20, 0x00, 0x00,
	0x88, 0x88, 0x88, 0xA8, 0xD8, 0x88, 0x00, 0x00,
	0x88, 0x50, 0x20, 0x20, 0x50, 0x88, 0x00, 0x00,
	0x88, 0x88, 0x50, 0x20, 0x20, 0x20, 0x00, 0x00,
	0xF0, 0x10, 0x20, 0x40, 0x80, 0xF0, 0x00, 0x00,
	0xC0, 0x80, 0x80, 0x80, 0x80, 0xC0, 0x00, 0x00,
	0x80, 0x80, 0x40, 0x20, 0x10, 0x10, 0x00, 0x00,
	0xC0, 0x40, 0x40, 0x40, 0x40, 0xC0, 0x00, 0x00,
	0x20, 0x50, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00,
	0x80, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0xE0, 0x20, 0xE0, 0xA0, 0xE0, 0x00, 0x00,
	0x80, 0xE0, 0xA0, 0xA0, 0xA0, 0xE0, 0x00, 0x00,
	0x00, 0xE0, 0x80, 0x80, 0x80, 0xE0, 0x00, 0x00,
	0x20, 0xE0, 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 0x40, 0x80, 0x40, 0x40, 0x20, 0x00,
	0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00,
	0x80, 0x40, 0x40, 0x20, 0x40, 0x40, 0x80, 0x00,
	0x00, 0x00, 0x40, 0xA8, 0x10, 0x00, 0x00, 0x00
};


#define X_COUNT 32
#define CHAR_COUNT sizeof(FONT) / 8
#define Y_COUNT 3
#define CHAR_WIDTH 6
#define CHAR_HEIGHT 8


/**
 * Stream reader
 */
class MemoryReadStream {
private:
	const byte *_ptr;
public:
	MemoryReadStream(const byte *ptr) : _ptr(ptr) {}
	
	byte readByte() {
		return *_ptr++;
	}
};

/**
 * Simple surface structure
 */
struct Surface {
	int _w;
	int _h;
	byte *_pixels;

	Surface(int w, int h) : _w(w), _h(h) {
		_pixels = new byte[w * h];
		memset(_pixels, 0xff, w * h);
	}

	~Surface() {
		delete[] _pixels;
	}

	Surface &operator=(const Surface &src) {
		assert(src._w == _w && src._h == _h);
		memcpy(_pixels, src._pixels, _w * _h);
		return *this;
	}

	byte *getBasePtr(int x, int y) {
		assert(y < _h);
		return _pixels + (y * _w) + x;
	}

	void decodeFont(MemoryReadStream &src);

	/**
	 * Save to a BMP file
	 */
	void saveToFile(const char *filename);
};

/**
 * File writing class
 */
class File {
private:
	FILE *_f;
public:
	File(const char *filename) {
		_f = fopen(filename, "wb");
	}
	~File() {
		fclose(_f);
	}
	void writeByte(byte v) {
		fwrite(&v, 1, 1, _f);
	}
	void writeUint16LE(uint16 v) {
		writeByte(v & 0xff);
		writeByte(v >> 8);
	}
	void writeUint32LE(uint32 v) {
		writeUint16LE(v & 0xffff);
		writeUint16LE(v >> 16);
	}
	void write(byte val, size_t count) {
		while (count-- > 0)
			writeByte(val);
	}
	void write(const byte *buf, size_t count) {
		fwrite(buf, 1, count, _f);
	}
};


int main(int argc, char *argv[]) {
	MemoryReadStream src(FONT);

	Surface norm(X_COUNT * CHAR_WIDTH, Y_COUNT * CHAR_HEIGHT), surf(X_COUNT * CHAR_WIDTH, Y_COUNT * CHAR_HEIGHT);

	// Decode the normal font and write it out
	norm.decodeFont(src);
	norm.saveToFile("infocom6x8.bmp");

	return 0;
}

void Surface::decodeFont(MemoryReadStream &src) {
	for (int charNum = 0; charNum < CHAR_COUNT; ++charNum) {
		int xs = (charNum % X_COUNT) * CHAR_WIDTH;
		int ys = (charNum / X_COUNT) * CHAR_HEIGHT;

		for (int y = 0; y < CHAR_HEIGHT; ++y) {
			byte *pDest = getBasePtr(xs, ys + y);
			byte bits = src.readByte();

			for (int x = 0; x < CHAR_WIDTH; ++x, ++pDest, bits <<= 1) {
				if (bits & 0x80) {
					*pDest = 0;
					assert(x < CHAR_WIDTH);
				}
			}
		}
	}
}

void Surface::saveToFile(const char *filename) {
	File f(filename);
	f.writeByte('B');
	f.writeByte('M');
	f.writeUint32LE(0x436 + _w * _h + 2);	// File size
	f.writeUint16LE(0);			// Custom 1
	f.writeUint16LE(0);			// Custom 2
	f.writeUint32LE(0x436);		// Pixels offset

	f.writeUint32LE(40);		// Info size
	f.writeUint32LE(_w);		// Width
	f.writeUint32LE(_h);		// Height
	f.writeUint16LE(1);			// # Planes
	f.writeUint16LE(8);			// Bits per pixel
	f.writeUint32LE(0);			// Compression
	f.writeUint32LE(_w * _h);	// Image size
	f.writeUint32LE(3790);		// Pixels per meter X
	f.writeUint32LE(3800);		// Pixels per meter Y
	f.writeUint32LE(0);			// color count
	f.writeUint32LE(0);			// important colors

	// Palette
	for (int idx = 0; idx < 256; ++idx) {
		f.write(idx, 3);
		f.writeByte(0);
	}

	// Write out each line from the bottom up
	for (int y = _h - 1; y >= 0; --y) {
		byte *lineP = getBasePtr(0, y);
		f.write(lineP, _w);
	}
}