File: file.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (172 lines) | stat: -rw-r--r-- 3,333 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef FILE_H
#define FILE_H

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cassert>

typedef unsigned char byte;
typedef unsigned int uint32;

#include "create_ultima.h"

class File {
private:
	FILE *_file;
public:
	File(const char *filename) {
		_file = fopen(filename, "rb");
		if (!_file) {
			char buf[255];
			sprintf(buf, "Could not open file %s", filename);
			error(buf);
		}
	}
	~File() {
		fclose(_file);
	}

	int seek(int offset, int origin = SEEK_SET) {
		return fseek(_file, offset, origin);
	}

	byte readByte() {
		int b;
		fread(&b, 1, 1, _file);
		return b;
	}

	int readWord() {
		byte b1, b2;
		fread(&b1, 1, 1, _file);
		fread(&b2, 1, 1, _file);
		return b1 | (b2 << 8);
	}

	void read(void *buf, int size) {
		fread(buf, 1, size, _file);
	}

	bool eof() const {
		return feof(_file);
	}

	int pos() const {
		return ftell(_file);
	}

	uint32 computeMD5();
};

class WriteFile {
private:
	FILE *_file;
public:
	WriteFile(const char *filename) {
		_file = fopen(filename, "wb");
		assert(_file);
	}
	~WriteFile() {
		fclose(_file);
	}

	void writeByte(byte val) {
		fwrite(&val, 1, 1, _file);
	}

	void writeWord(int val) {
		int b1 = val & 0xff, b2 = (val >> 8) & 0xff;
		fwrite(&b1, 1, 1, _file);
		fwrite(&b2, 1, 1, _file);
	}

	void writeLong(long val) {
		writeWord(val & 0xffff);
		writeWord((val >> 16) & 0xffff);
	}

	int write(const void *buf, int size) {
		return (int)fwrite(buf, 1, size, _file);
	}

	void write(File &src, int size) {
		byte *data = new byte[size];
		src.read(data, size);
		write(data, size);
		delete[] data;
	}

	void writeRepeating(byte val, size_t count) {
		while (count-- > 0)
			writeByte(val);
	}
};

/**
 * Simple surface structure
 */
struct Surface {
	int _w;
	int _h;
	byte *_pixels;
	byte _palette[768];

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

		// Set a default palette
		for (int idx = 0; idx < 256; ++idx) {
			memset(_palette + idx * 3, idx, 3);
		}

	}

	~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 setPaletteEntry(byte index, byte r, byte g, byte b);

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

#endif