gfx.c 10.2 KB
Newer Older
1 2 3
#include "globals.h"

#define JNB_BPP 8
4
static SDL_Surface *jnb_surface;
5 6
static int fullscreen = 0;
static int vinited = 0;
7
static unsigned char screen_buffer[JNB_WIDTH*JNB_HEIGHT*2];
8

9

10 11 12 13 14 15 16 17 18
char *get_vgaptr(int page, int x, int y)
{
	if (page == 1)
		return &screen_buffer[JNB_WIDTH*JNB_HEIGHT+((y * JNB_WIDTH) + x)];
	else
		return &screen_buffer[(y * JNB_WIDTH) + x];
}


19 20 21 22 23 24 25 26 27 28
void open_screen(void)
{
	int lval = 0;

	lval = SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_AUDIO);
	if (lval < 0) {
		fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
		exit(EXIT_FAILURE);
	}

29 30 31 32
	if (fullscreen)
		jnb_surface = SDL_SetVideoMode(JNB_WIDTH, JNB_HEIGHT, JNB_BPP, SDL_SWSURFACE | SDL_FULLSCREEN);
	else
		jnb_surface = SDL_SetVideoMode(JNB_WIDTH, JNB_HEIGHT, JNB_BPP, SDL_SWSURFACE);
33 34 35 36 37 38 39 40 41 42 43 44
	if (!jnb_surface) {
		fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
		exit(EXIT_FAILURE);
	}

	SDL_ShowCursor(0);

	vinited = 1;

	return;
}

45

46 47 48 49 50 51 52 53 54 55
void fs_toggle()
{
	if (!vinited) {
		fullscreen ^= 1;
		return;
	}
	if (SDL_WM_ToggleFullScreen(jnb_surface))
		fullscreen ^= 1;
}

56 57

void wait_vrt(int mix)
58 59 60 61
{
	return;
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

void clear_page(int page, int color)
{
	memset((void *) get_vgaptr(page, 0, 0), color, JNB_WIDTH * JNB_HEIGHT);
}


void clear_lines(int page, int y, int count, int color)
{
	int i;

	for (i=0; i<count; i++)
		if ((i+y)<JNB_HEIGHT)
			memset((void *) get_vgaptr(page, 0, i+y), color, JNB_WIDTH);
}


79 80 81 82 83 84 85 86 87 88 89 90
int get_pixel(int page, int x, int y)
{
	return *(char *) get_vgaptr(page, x, y);
}


void set_pixel(int page, int x, int y, int color)
{
	*(char *) get_vgaptr(page, x, y) = color;
}


91
void flippage(int page)
92
{
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	int h;
	int w;
	char *src;
	char *dest;

	SDL_LockSurface(jnb_surface);
        dest=(char *)jnb_surface->pixels;
	if (page == 1)
		src=&screen_buffer[JNB_WIDTH*JNB_HEIGHT];
	else
		src=&screen_buffer[0];
        w=(jnb_surface->clip_rect.w>JNB_WIDTH)?(JNB_WIDTH):(jnb_surface->clip_rect.w);
        h=(jnb_surface->clip_rect.h>JNB_HEIGHT)?(JNB_HEIGHT):(jnb_surface->clip_rect.h);
        for (; h>0; h--)
        {
		memcpy(dest,src,w);
		dest+=jnb_surface->pitch;
		src+=JNB_WIDTH;
        }
        SDL_UnlockSurface(jnb_surface);
	SDL_Flip(jnb_surface);
114 115
}

116

117 118 119 120 121 122 123 124 125 126 127 128 129
void setpalette(int index, int count, char *palette)
{
	SDL_Color colors[256];
	int i;

	for (i = 0; i < count; i++) {
		colors[i].r = palette[i * 3 + 0] << 2;
		colors[i].g = palette[i * 3 + 1] << 2;
		colors[i].b = palette[i * 3 + 2] << 2;
	}
	SDL_SetColors(jnb_surface, colors, index, count);
}

130

131 132 133 134 135 136 137 138 139 140 141 142 143
void fillpalette(int red, int green, int blue)
{
	SDL_Color colors[256];
	int i;

	for (i = 0; i < 256; i++) {
		colors[i].r = red << 2;
		colors[i].g = green << 2;
		colors[i].b = blue << 2;
	}
	SDL_SetColors(jnb_surface, colors, 0, 256);
}

144

145
void get_block(int page, int x, int y, int width, int height, char *buffer)
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
{
	short w, h;
	char *buffer_ptr, *vga_ptr;

	if (x < 0)
		x = 0;
	if (y < 0)
		y = 0;
	if (y + height >= JNB_HEIGHT)
		height = JNB_HEIGHT - y;
	if (x + width >= JNB_WIDTH)
		width = JNB_WIDTH - x;

	for (h = 0; h < height; h++) {
		buffer_ptr = &buffer[h * width];

		vga_ptr = get_vgaptr(page, x, h + y);

		for (w = 0; w < width; w++) {
			unsigned char ch;
			ch = *vga_ptr;
			*buffer_ptr = ch;
			buffer_ptr++;
			vga_ptr++;
		}
	}

}

175

176
void put_block(int page, int x, int y, int width, int height, char *buffer)
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
{
	short w, h;
	char *vga_ptr, *buffer_ptr;

	if (x < 0)
		x = 0;
	if (y < 0)
		y = 0;
	if (y + height >= JNB_HEIGHT)
		height = JNB_HEIGHT - y;
	if (x + width >= JNB_WIDTH)
		width = JNB_WIDTH - x;

	for (h = 0; h < height; h++) {
		vga_ptr = get_vgaptr(page, x, y + h);

		buffer_ptr = &buffer[h * width];
		for (w = 0; w < width; w++) {
			*vga_ptr = *buffer_ptr;
			vga_ptr++;
			buffer_ptr++;
		}
	}
}

202

203
void put_text(int page, int x, int y, char *text, int align)
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
{
	int c1;
	int t1;
	int width;
	int cur_x;
	int image;

	if (text == NULL || strlen(text) == 0)
		return;
	if (font_gobs == NULL)
		return;

	width = 0;
	c1 = 0;
	while (text[c1] != 0) {
		t1 = text[c1];
		c1++;
		if (t1 == ' ') {
			width += 5;
			continue;
		}
		if (t1 >= 33 && t1 <= 34)
			image = t1 - 33;

		else if (t1 >= 39 && t1 <= 41)
			image = t1 - 37;

		else if (t1 >= 44 && t1 <= 59)
			image = t1 - 39;

		else if (t1 >= 64 && t1 <= 90)
			image = t1 - 43;

		else if (t1 >= 97 && t1 <= 122)
			image = t1 - 49;

		else if (t1 == '~')
			image = 74;

		else if (t1 == 0x84)
			image = 75;

		else if (t1 == 0x86)
			image = 76;

		else if (t1 == 0x8e)
			image = 77;

		else if (t1 == 0x8f)
			image = 78;

		else if (t1 == 0x94)
			image = 79;

		else if (t1 == 0x99)
			image = 80;
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		else
			continue;
		width += pob_width(image, font_gobs) + 1;
	}

	switch (align) {
	case 0:
		cur_x = x;
		break;
	case 1:
		cur_x = x - width;
		break;
	case 2:
		cur_x = x - width / 2;
		break;
	default:
		cur_x = 0;	/* this should cause error? -Chuck */
		break;
	}
	c1 = 0;

	while (text[c1] != 0) {
		t1 = text[c1];
		c1++;
		if (t1 == ' ') {
			cur_x += 5;
			continue;
		}
		if (t1 >= 33 && t1 <= 34)
			image = t1 - 33;

		else if (t1 >= 39 && t1 <= 41)
			image = t1 - 37;

		else if (t1 >= 44 && t1 <= 59)
			image = t1 - 39;

		else if (t1 >= 64 && t1 <= 90)
			image = t1 - 43;

		else if (t1 >= 97 && t1 <= 122)
			image = t1 - 49;

		else if (t1 == '~')
			image = 74;

307
		else if (t1 == 0x84)
308 309
			image = 75;

310
		else if (t1 == 0x86)
311 312
			image = 76;

313
		else if (t1 == 0x8e)
314 315
			image = 77;

316
		else if (t1 == 0x8f)
317 318
			image = 78;

319
		else if (t1 == 0x94)
320 321
			image = 79;

322
		else if (t1 == 0x99)
323 324 325 326 327 328 329 330 331
			image = 80;

		else
			continue;
		put_pob(page, cur_x, y, image, font_gobs, 1, mask_pic);
		cur_x += pob_width(image, font_gobs) + 1;
	}
}

332

333
void put_pob(int page, int x, int y, int image, char *pob_data, int mask, char *mask_pic)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
{
	long c1, c2;
	long pob_offset;
	char *pob_ptr, *vga_ptr, *mask_ptr;
	long width, height;
	long draw_width, draw_height;
	char colour;

	if (image < 0 || image >= *(short *) (pob_data))
		return;

	vga_ptr = get_vgaptr(page, 0, 0);
	pob_offset = *(unsigned long *) (pob_data + (image * 4) + 2);
	width = draw_width = *(short *) (pob_data + pob_offset);
	height = draw_height = *(short *) (pob_data + pob_offset + 2);
	x -= *(short *) (pob_data + pob_offset + 4);
	y -= *(short *) (pob_data + pob_offset + 6);
	pob_offset += 8;
	if ((x + width) <= 0 || x >= 400)
		return;
	if ((y + height) <= 0 || y >= 256)
		return;
	if (x < 0) {
		pob_offset -= x;
		draw_width += x;
		x = 0;
	}
	if ((x + width) > 400)
		draw_width -= x + width - 400;
	if (y < 0) {
		pob_offset += -y * width;
		draw_height -= -y;
		y = 0;
	}
	if ((y + height) > 256)
		draw_height -= y + height - 256;

	pob_ptr = &pob_data[pob_offset];


	vga_ptr = get_vgaptr(page, x, y);
	mask_ptr = (char *) (mask_pic + (y * 400) + x);
	for (c1 = 0; c1 < draw_height; c1++) {
		for (c2 = 0; c2 < draw_width; c2++) {
			colour = *mask_ptr;
			if (mask == 0 || (mask == 1 && colour == 0)) {
				colour = *pob_ptr;
				if (colour != 0)
					*vga_ptr = colour;
			}
			pob_ptr++;
			vga_ptr++;
			mask_ptr++;
		}
		pob_ptr += width - c2;
		vga_ptr += (400 - c2);
		mask_ptr += (400 - c2);
	}
}

394

395
int pob_col(int x1, int y1, int image1, char *pob_data1, int x2, int y2, int image2, char *pob_data2)
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
{
	short c1, c2;
	long pob_offset1, pob_offset2;
	short width1, width2;
	short height1, height2;
	short check_width, check_height;
	char *pob_ptr1, *pob_ptr2;

	pob_offset1 = *(long *) (pob_data1 + image1 * 4 + 2);
	width1 = *(short *) (pob_data1 + pob_offset1);
	height1 = *(short *) (pob_data1 + pob_offset1 + 2);
	x1 -= *(short *) (pob_data1 + pob_offset1 + 4);
	y1 -= *(short *) (pob_data1 + pob_offset1 + 6);
	pob_offset1 += 8;
	pob_offset2 = *(long *) (pob_data2 + image2 * 4 + 2);
	width2 = *(short *) (pob_data2 + pob_offset2);
	height2 = *(short *) (pob_data2 + pob_offset2 + 2);
	x2 -= *(short *) (pob_data2 + pob_offset2 + 4);
	y2 -= *(short *) (pob_data2 + pob_offset2 + 6);
	pob_offset2 += 8;

	if (x1 < x2) {
		if ((x1 + width1) <= x2)
			return 0;

		else if ((x1 + width1) <= (x2 + width2)) {
			pob_offset1 += x2 - x1;
			check_width = x1 + width1 - x2;
		}

		else {
			pob_offset1 += x2 - x1;
			check_width = width2;
		}
	}

	else {
		if ((x2 + width2) <= x1)
			return 0;

		else if ((x2 + width2) <= (x1 + width1)) {
			pob_offset2 += x1 - x2;
			check_width = x2 + width2 - x1;
		}

		else {
			pob_offset2 += x1 - x2;
			check_width = width1;
		}
	}
	if (y1 < y2) {
		if ((y1 + height1) <= y2)
			return 0;

		else if ((y1 + height1) <= (y2 + height2)) {
			pob_offset1 += (y2 - y1) * width1;
			check_height = y1 + height1 - y2;
		}

		else {
			pob_offset1 += (y2 - y1) * width1;
			check_height = height2;
		}
	}

	else {
		if ((y2 + height2) <= y1)
			return 0;

		else if ((y2 + height2) <= (y1 + height1)) {
			pob_offset2 += (y1 - y2) * width2;
			check_height = y2 + height2 - y1;
		}

		else {
			pob_offset2 += (y1 - y2) * width2;
			check_height = height1;
		}
	}
	pob_ptr1 = (char *) (pob_data1 + pob_offset1);
	pob_ptr2 = (char *) (pob_data2 + pob_offset2);
	for (c1 = 0; c1 < check_height; c1++) {
		for (c2 = 0; c2 < check_width; c2++) {
			if (*pob_ptr1 != 0 && *pob_ptr2 != 0)
				return 1;
			pob_ptr1++;
			pob_ptr2++;
		}
		pob_ptr1 += width1 - check_width;
		pob_ptr2 += width2 - check_width;
	}
	return 0;
}

490

491
int pob_width(int image, char *pob_data)
492 493 494 495
{
	return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2));
}

496

497
int pob_height(int image, char *pob_data)
498 499 500 501
{
	return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2) + 2);
}

502

503
int pob_hs_x(int image, char *pob_data)
504 505 506 507
{
	return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2) + 4);
}

508

509
int pob_hs_y(int image, char *pob_data)
510 511 512 513
{
	return *(short *) (pob_data + *(long *) (pob_data + image * 4 + 2) + 6);
}

514

515
int read_pcx(FILE * handle, char *buffer, int buf_len, char *pal)
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	short c1;
	short a, b;
	long ofs1;
	if (buffer != 0) {
		fseek(handle, 128, SEEK_CUR);
		ofs1 = 0;
		while (ofs1 < buf_len) {
			a = fgetc(handle);
			if ((a & 0xc0) == 0xc0) {
				b = fgetc(handle);
				a &= 0x3f;
				for (c1 = 0; c1 < a && ofs1 < buf_len; c1++)
					buffer[ofs1++] = (char) b;
			} else
				buffer[ofs1++] = (char) a;
		}
		if (pal != 0) {
			fseek(handle, 1, SEEK_CUR);
			for (c1 = 0; c1 < 768; c1++)
				pal[c1] = fgetc(handle) >> 2;
		}
	}
	return 0;
}

542

543
#ifndef _MSC_VER
544
int filelength(int handle)
545 546 547 548 549 550 551 552 553 554 555
{
	struct stat buf;

	if (fstat(handle, &buf) == -1) {
		perror("filelength");
		exit(EXIT_FAILURE);
	}

	return buf.st_size;
}
#endif