File: gr_buff.cpp

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 (229 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download | duplicates (2)
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
/* 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/>.
 *
 */

#include "m4/graphics/gr_buff.h"
#include "m4/graphics/gr_pal.h"
#include "m4/gui/gui_vmng_core.h"
#include "m4/core/errors.h"
#include "m4/mem/memman.h"
#include "m4/core/term.h"

namespace M4 {

GrBuff::GrBuff(int32 _w, int32 _h) {
	w = _w;
	h = _h;
	x_off = y_off = 0;
	pitch = _w;
	height = _h;
	alloc_pixmap();
}

GrBuff::GrBuff(int32 _w, int32 _h, int32 _x_off, int32 _y_off, int32 _pitch, int32 _height) {
	w = _w;
	h = _h;
	x_off = _x_off;
	y_off = _y_off;
	pitch = _pitch;
	height = _height;
	alloc_pixmap();
}

GrBuff::~GrBuff() {
	if (pixmap)
		DisposeHandle(pixmap);
}

void GrBuff::lock() {
	if (!pixmap)
		return;

	HLock(pixmap);
}

void GrBuff::release() {
	if (pixmap)
		HUnLock(pixmap);
}

void GrBuff::alloc_pixmap() {
	pixmap = NewHandle(pitch * height, "pixmap");
	if (!pixmap) {
		term_message("GrBuff::alloc_pixmap(): Trying to free up %d bytes", pitch * height);
		if (MakeMem(pitch * height, "pixmap")) {
			pixmap = NewHandle(pitch * height, "pixmap");
			if (!pixmap)
				error_show(FL, 15, "pixmap h:%d w:%d bytes:%d", height, pitch, pitch * height);
		} else
			error_show(FL, 1, "GrBuff::alloc_pixmap() x, y: %d %d", pitch, height);
	}
	HLock(pixmap);
	memset(*pixmap, __BLACK, pitch * height);
	HUnLock(pixmap);
}

uint8 *GrBuff::get_pixmap() {
	if (pixmap) {
		lock();
		return (uint8 *)*pixmap;
	}
	return nullptr;
}

Buffer *GrBuff::get_buffer() {
	if (pixmap) {
		lock();
		dummy.data = (uint8 *)*pixmap;
		dummy.w = w;
		dummy.h = h;
		dummy.encoding = 0;
		dummy.stride = pitch;

		return &dummy;
	}
	return nullptr;
}

void GrBuff::refresh_video(int32 scrnX, int32 scrnY, int32 x1, int32 y1, int32 x2, int32 y2) {
	vmng_refresh_video(scrnX, scrnY, x1, y1, x2, y2, get_buffer());
}

int32 gr_buffer_free(Buffer *buf) {
	buf->w = buf->h = buf->stride = 0;

	if (buf->data != nullptr) {
		mem_free((char *)buf->data);
		buf->data = nullptr;
		return true;
	}

	error_show(FL, 'BUF!');
	return false;
}

byte *gr_buffer_pointer(Buffer *buf, int32 x, int32 y) {
	if (!buf || !buf->data || y < 0 || x < 0) {
		error_show(FL, 'BUF!', "buffer_pointer x,y = %d,%d", x, y);
		return 0;
	}

	return (byte *)(buf->data + x + (y * buf->stride));
}

const byte *gr_buffer_pointer(const Buffer *buf, int32 x, int32 y) {
	if (!buf || !buf->data || y < 0 || x < 0) {
		error_show(FL, 'BUF!', "buffer_pointer x,y = %d,%d", x, y);
		return 0;
	}

	return (byte *)(buf->data + x + (y * buf->stride));
}

int32 gr_buffer_init(Buffer *buf, const char *name, int32 w, int32 h) {
	if (buf->data)
		error_show(FL, 'BUFR', "buffer_init %s", name);

	buf->w = w;
	buf->h = h;
	buf->stride = w;

	buf->data = (uint8 *)mem_alloc(buf->stride * h, name);
	if (buf->data == nullptr)
		error_show(FL, 'OOM!', "buffer: %s - w:%d h:%d bytes:%d", name, buf->stride, h, buf->stride * h);

	memset(buf->data, 0, buf->stride * h);

	return (true);
}

bool gr_buffer_rect_copy_2(const Buffer *from, Buffer *to, int32 sx, int32 sy,
                           int32 dx, int32 dy, int32 w, int32 h) {
	// stupid check for no data
	if (!from || !to || !from->data || !to->data)
		error_show(FL, 'BUF!', "buff_rect_copy2");

	// CKB: if height is greater than source height, truncate!
	if (h > from->h)
		h = from->h;

	// if source x,y or dest x,y won't touch dest or source buffers, we're done
	if ((sx > from->w) || (sy > from->h) || (dx > to->w) || (dy > to->h))
		return true;

	// if dest request intersects dest buffer, clip dest request
	if ((dx + w) > to->w)
		w = to->w - dx;
	if ((dy + h) > to->h)
		h = to->h - dy;

	// if our dest request is too small, we're done
	if ((w < 1) || (h < 1))
		return true;

	// initialize pointers
	const byte *src = gr_buffer_pointer(from, sx, sy);
	byte *dest = gr_buffer_pointer(to, dx, dy);

	// get stride
	int32 sIncr = from->stride;
	int32 dIncr = to->stride;

	// copy one to d'other
	for (int i = 0; i < h; i++, dest += dIncr, src += sIncr)
		memmove(dest, src, w);

	return true;
}

bool gr_buffer_rect_copy(Buffer *from, Buffer *to, int32 x, int32 y, int32 w, int32 h) {
	return (gr_buffer_rect_copy_2(from, to, x, y, x, y, w, h));
}

int32 gr_buffer_rect_fill(Buffer *target, int32 x1, int32 y1, int32 w, int32 h) {
	int32 i;
	uint8 *start;
	byte color = gr_color_get_current();

	// if no data, bad.
	if (!target || !target->data)
		error_show(FL, 'BUF!', "buffer_rect_fill");

	// if nothing to fill, we're done
	if ((w < 1) || (h < 1) || (x1 > target->w) || (y1 > target->h))
		return true;

	// clip if rectangles too big
	if ((x1 + w) > target->w)
		w = target->w - x1;
	if ((y1 + h) > target->h)
		h = target->h - y1;

	if ((w < 1) || (h < 1))
		return true;

	start = target->data + y1 * target->stride + x1;
	for (i = 0; i < h; i++, start += target->stride)
		memset(start, color, w);

	return true;
}

} // namespace M4