aboutsummaryrefslogtreecommitdiff
path: root/saga/sdata.cpp
blob: c316208582a47d5e4fc498567b5a12fd5ac85af1 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */
// Type SDataWord_T must be unpadded
#include "saga/saga.h"

#include "saga/gfx.h"
#include "saga/script_mod.h"
#include "saga/script.h"
#include "saga/sdata.h"

namespace Saga {

SData::SData() {
	unsigned int i;
	void *alloc_ptr;

	debug(0, "Initializing script data buffers");
	for (i = 0; i < R_SCRIPT_DATABUF_NUM; i++) {
		alloc_ptr = malloc(sizeof *_vm->_script->dataBuffer(0));
		if (alloc_ptr == NULL) {
			error("Couldn't allocate memory for script data buffer %d", i);
		}

		_vm->_script->setBuffer(i, (R_SCRIPT_DATABUF *)alloc_ptr);
		alloc_ptr = calloc(R_SCRIPT_DATABUF_LEN, sizeof(SDataWord_T));

		if (alloc_ptr == NULL) {
			error("Couldn't allocate memory for script data buffer %d", i);
		}

		_vm->_script->dataBuffer(i)->len = R_SCRIPT_DATABUF_LEN;
		_vm->_script->dataBuffer(i)->data = (SDataWord_T *)alloc_ptr;
	}
}

SData::~SData() {
}

int SData::getWord(int n_buf, int n_word, SDataWord_T *data) {
	if ((n_buf < 0) || (n_buf >= R_SCRIPT_DATABUF_NUM)) {
		return R_FAILURE;
	}

	if ((n_word < 0) || (n_word >= _vm->_script->dataBuffer(n_buf)->len)) {
		return R_FAILURE;
	}

	if (data == NULL) {
		return R_FAILURE;
	}

	*data = _vm->_script->dataBuffer(n_buf)->data[n_word];

	return R_SUCCESS;
}

int SData::putWord(int n_buf, int n_word, SDataWord_T data) {
	if ((n_buf < 0) || (n_buf >= R_SCRIPT_DATABUF_NUM)) {
		return R_FAILURE;
	}

	if ((n_word < 0) || (n_word >= _vm->_script->dataBuffer(n_buf)->len)) {
		return R_FAILURE;
	}

	_vm->_script->dataBuffer(n_buf)->data[n_word] = data;

	return R_SUCCESS;
}

int SData::setBit(int n_buf, SDataWord_T n_bit, int bitstate) {
	int n_word;
	int n_bitpos;

	SDataWord_T bit_pattern = 0x01;

	if ((n_buf < 0) || (n_buf >= R_SCRIPT_DATABUF_NUM)) {
		return R_FAILURE;
	}

	if (n_bit >= (unsigned long)_vm->_script->dataBuffer(n_buf)->len * (sizeof(SDataWord_T) * CHAR_BIT)) {
		return R_FAILURE;
	}

	n_word = n_bit / (sizeof(SDataWord_T) * CHAR_BIT);
	n_bitpos = n_bit % (sizeof(SDataWord_T) * CHAR_BIT);

	bit_pattern <<= ((sizeof(SDataWord_T) * CHAR_BIT) - (n_bitpos + 1));

	if (bitstate) {
		_vm->_script->dataBuffer(n_buf)->data[n_word] |= bit_pattern;
	} else {
		_vm->_script->dataBuffer(n_buf)->data[n_word] &= ~bit_pattern;
	}

	return R_SUCCESS;
}

int SData::getBit(int n_buf, SDataWord_T n_bit, int *bitstate) {
	int n_word;
	int n_bitpos;

	SDataWord_T bit_pattern = 0x01;

	if ((n_buf < 0) || (n_buf >= R_SCRIPT_DATABUF_NUM)) {
		return R_FAILURE;
	}

	if (n_bit >= (SDataWord_T) _vm->_script->dataBuffer(n_buf)->len * (sizeof(SDataWord_T) * CHAR_BIT)) {
		return R_FAILURE;
	}

	n_word = n_bit / (sizeof(SDataWord_T) * CHAR_BIT);
	n_bitpos = n_bit % (sizeof(SDataWord_T) * CHAR_BIT);

	bit_pattern <<= ((sizeof(SDataWord_T) * CHAR_BIT) - (n_bitpos + 1));

	*bitstate = (_vm->_script->dataBuffer(n_buf)->data[n_word] & bit_pattern) ? 1 : 0;

	return R_SUCCESS;
}

int SData::readWordS(SDataWord_T word) {
	uint16 u_int = word;
	int s_int;

	if (u_int & 0x8000U) {
		s_int = (int)(u_int - 0x8000U) - 0x7FFF - 1;
	} else {
		s_int = u_int;
	}

	return s_int;
}

uint16 SData::readWordU(SDataWord_T word) {
	uint16 u_int = (uint16) word;

	return u_int;
}

} // End of namespace Saga