3 * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
5 * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
7 * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
9 * Portions of this code are from the MPEG software simulation group
10 * idct implementation. This code will be replaced with a new
11 * implementation soon.
13 * This file is part of Jump'n'Bump.
15 * Jump'n'Bump is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * Jump'n'Bump is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <SDL_endian.h>
32 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
36 #define SWAP16(X) SDL_Swap16(X)
37 #define SWAP32(X) SDL_Swap32(X)
41 #include "jumpnbump32.xpm"
43 #include "jumpnbump128.xpm"
45 #include "jumpnbump64.xpm"
50 int screen_height=256;
53 int bytes_per_pixel=1;
54 int dirty_block_shift=4;
56 static int current_pal[256];
57 static SDL_Surface *jnb_surface;
58 static int fullscreen = 0;
59 static int vinited = 0;
60 static void *screen_buffer[2];
61 static int drawing_enable = 0;
62 static void *background = NULL;
63 static int background_drawn;
64 static void *mask = NULL;
65 static int dirty_blocks[2][25*16*2];
67 static SDL_Surface *load_xpm_from_array(char *xpm[])
79 void *get_vgaptr(int page, int x, int y)
81 assert(drawing_enable==1);
83 return (unsigned char *)screen_buffer[page] + (y*screen_pitch)+(x*bytes_per_pixel);
87 void set_scaling(int scale)
95 screen_pitch=screen_width*bytes_per_pixel;
102 screen_pitch=screen_width*bytes_per_pixel;
106 void open_screen(void)
113 lval = SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO);
115 lval = SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_AUDIO);
118 fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
126 flags = SDL_SWSURFACE;
128 flags |= SDL_FULLSCREEN;
129 jnb_surface = SDL_SetVideoMode(screen_width, screen_height, bpp, flags);
132 fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
141 SDL_WM_SetCaption("Jump n Bump","");
143 icon=load_xpm_from_array(jumpnbump_xpm);
145 printf("Couldn't load icon\n");
147 SDL_WM_SetIcon(icon,NULL);
152 memset(current_pal, 0, sizeof(current_pal));
153 memset(dirty_blocks, 0, sizeof(dirty_blocks));
155 screen_buffer[0]=malloc(screen_width*screen_height*bytes_per_pixel);
156 screen_buffer[1]=malloc(screen_width*screen_height*bytes_per_pixel);
159 dirty_blocks[0]=malloc(sizeof(int)*25*16+1000);
160 dirty_blocks[1]=malloc(sizeof(int)*25*16+1000);
173 if (SDL_WM_ToggleFullScreen(jnb_surface))
178 void wait_vrt(int mix)
184 void clear_page(int page, int color)
188 assert(drawing_enable==1);
190 for (i=0; i<(25*16); i++)
191 dirty_blocks[page][i] = 1;
193 if (bytes_per_pixel==1) {
194 unsigned char *buf = get_vgaptr(page, 0, 0);
196 for (i=0; i<screen_height; i++)
197 for (j=0; j<screen_width; j++)
199 } else if (bytes_per_pixel==2) {
200 unsigned short *buf = get_vgaptr(page, 0, 0);
202 for (i=0; i<screen_height; i++)
203 for (j=0; j<screen_width; j++)
206 unsigned int *buf = get_vgaptr(page, 0, 0);
208 for (i=0; i<screen_height; i++)
209 for (j=0; j<screen_width; j++)
215 void clear_lines(int page, int y, int count, int color)
219 assert(drawing_enable==1);
226 if (bytes_per_pixel==1) {
227 for (i=0; i<count; i++) {
228 if ((i+y)<screen_height) {
229 unsigned char *buf = get_vgaptr(page, 0, i+y);
230 for (j=0; j<screen_width; j++)
234 } else if (bytes_per_pixel==2) {
235 for (i=0; i<count; i++) {
236 if ((i+y)<screen_height) {
237 unsigned short *buf = get_vgaptr(page, 0, i+y);
238 for (j=0; j<screen_width; j++)
243 for (i=0; i<count; i++) {
244 if ((i+y)<screen_height) {
245 unsigned int *buf = get_vgaptr(page, 0, i+y);
246 for (j=0; j<screen_width; j++)
251 count = ((y+count)>>dirty_block_shift) - (y>>dirty_block_shift) + 1;
252 y >>= dirty_block_shift;
253 for (i=0; i<count; i++)
255 dirty_blocks[page][(y+i)*25+j] = 1;
259 int get_color(int color, char pal[768])
263 return SDL_MapRGB(jnb_surface->format, (Uint8)(pal[color*3+0]<<2), (Uint8)(pal[color*3+1]<<2), (Uint8)(pal[color*3+2]<<2));
267 int get_pixel(int page, int x, int y)
269 assert(drawing_enable==1);
276 assert(x<screen_width);
277 assert(y<screen_height);
279 if (bytes_per_pixel==1)
280 return *(unsigned char *)get_vgaptr(page, x, y);
281 else if (bytes_per_pixel==2)
282 return *(unsigned short *)get_vgaptr(page, x, y);
284 return *(unsigned int *)get_vgaptr(page, x, y);
288 void set_pixel(int page, int x, int y, int color)
290 assert(drawing_enable==1);
297 assert(x<screen_width);
298 assert(y<screen_height);
300 dirty_blocks[page][(y>>dirty_block_shift)*25+(x>>dirty_block_shift)] = 1;
302 if (bytes_per_pixel==1)
303 *(unsigned char *)get_vgaptr(page, x, y) = color;
304 else if (bytes_per_pixel==2)
305 *(unsigned short *)get_vgaptr(page, x, y) = color;
307 *(unsigned int *)get_vgaptr(page, x, y) = color;
311 static unsigned int colorMask = 0xF7DEF7DE;
312 static unsigned int lowPixelMask = 0x08210821;
313 static unsigned int qcolorMask = 0xE79CE79C;
314 static unsigned int qlowpixelMask = 0x18631863;
315 static unsigned int redblueMask = 0xF81F;
316 static unsigned int greenMask = 0x7E0;
318 int Init_2xSaI (unsigned int BitFormat)
320 if (BitFormat == 565)
322 colorMask = SWAP32(0xF7DEF7DE);
323 lowPixelMask = SWAP32(0x08210821);
324 qcolorMask = SWAP32(0xE79CE79C);
325 qlowpixelMask = SWAP32(0x18631863);
326 redblueMask = SWAP16(0xF81F);
327 greenMask = SWAP16(0x7E0);
329 else if (BitFormat == 555)
331 colorMask = SWAP32(0x7BDE7BDE);
332 lowPixelMask = SWAP32(0x04210421);
333 qcolorMask = SWAP32(0x739C739C);
334 qlowpixelMask = SWAP32(0x0C630C63);
335 redblueMask = SWAP16(0x7C1F);
336 greenMask = SWAP16(0x3E0);
344 Init_2xSaIMMX (BitFormat);
351 void Scale2x (unsigned char *src, unsigned int src_pitch, int src_bytes_per_pixel,
352 unsigned char *dst, unsigned int dst_pitch, int dst_bytes_per_pixel,
353 int width, int height, int pal[256])
355 #define GET_COLOR(x) (pal[(x)])
358 unsigned char *src_line;
359 unsigned char *dst_line[2];
363 dst_line[1] = dst + dst_pitch;
364 for (y=0; y<height; y++) {
365 for (x=0; x<width; x++) {
368 if (src_bytes_per_pixel == 1) {
369 color = GET_COLOR(*(((unsigned char*)src_line) + x));
370 } else if (src_bytes_per_pixel == 2) {
371 color = *(((unsigned short*)src_line) + x);
373 color = *(((unsigned int*)src_line) + x);
376 if (dst_bytes_per_pixel == 2) {
377 *((unsigned long *) (&dst_line[0][x * 4])) = color | (color << 16);
378 *((unsigned long *) (&dst_line[1][x * 4])) = color | (color << 16);
380 *((unsigned long *) (&dst_line[0][x * 8])) = color;
381 *((unsigned long *) (&dst_line[0][x * 8 + 4])) = color;
382 *((unsigned long *) (&dst_line[1][x * 8])) = color;
383 *((unsigned long *) (&dst_line[1][x * 8 + 4])) = color;
387 src_line += src_pitch;
389 if (y < height - 1) {
390 dst_line[0] += dst_pitch * 2;
391 dst_line[1] += dst_pitch * 2;
397 void Super2xSaI (unsigned char *src, unsigned int src_pitch, int src_bytes_per_pixel,
398 unsigned char *dst, unsigned int dst_pitch, int dst_bytes_per_pixel,
399 int width, int height, int pal[256])
401 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
403 #define INTERPOLATE(A, B) (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + (A & B & lowPixelMask))
405 #define Q_INTERPOLATE(A, B, C, D) ((A & qcolorMask) >> 2) + ((B & qcolorMask) >> 2) + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2) \
406 + ((((A & qlowpixelMask) + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask)) >> 2) & qlowpixelMask)
408 #define GET_COLOR(x) (pal[(x)])
410 unsigned char *src_line[4];
411 unsigned char *dst_line[2];
413 unsigned long color[16];
415 if ( (width<2) || (height<2) ) {
416 Scale2x(src, src_pitch, src_bytes_per_pixel, dst, dst_pitch, dst_bytes_per_pixel, width, height, pal);
420 /* Point to the first 3 lines. */
423 src_line[2] = src + src_pitch;
424 src_line[3] = src + (src_pitch * 2);
427 dst_line[1] = dst + dst_pitch;
431 if (src_bytes_per_pixel == 1) {
434 color[0] = GET_COLOR(*sbp); color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
435 color[4] = color[0]; color[5] = color[0]; color[6] = GET_COLOR(*(sbp + 1)); color[7] = GET_COLOR(*(sbp + 2));
437 color[8] = GET_COLOR(*sbp); color[9] = color[8]; color[10] = GET_COLOR(*(sbp + 1)); color[11] = GET_COLOR(*(sbp + 2));
439 color[12] = GET_COLOR(*sbp); color[13] = color[12]; color[14] = GET_COLOR(*(sbp + 1)); color[15] = GET_COLOR(*(sbp + 2));
440 } else if (src_bytes_per_pixel == 2) {
442 sbp = (unsigned short*)src_line[0];
443 color[0] = *sbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
444 color[4] = color[0]; color[5] = color[0]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
445 sbp = (unsigned short*)src_line[2];
446 color[8] = *sbp; color[9] = color[8]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
447 sbp = (unsigned short*)src_line[3];
448 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
451 lbp = (unsigned long*)src_line[0];
452 color[0] = *lbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
453 color[4] = color[0]; color[5] = color[0]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
454 lbp = (unsigned long*)src_line[2];
455 color[8] = *lbp; color[9] = color[8]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
456 lbp = (unsigned long*)src_line[3];
457 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
460 for (y = 0; y < height; y++) {
462 /* Todo: x = width - 2, x = width - 1 */
464 for (x = 0; x < width; x++) {
465 unsigned long product1a, product1b, product2a, product2b;
467 //--------------------------------------- B0 B1 B2 B3 0 1 2 3
468 // 4 5* 6 S2 -> 4 5* 6 7
469 // 1 2 3 S1 8 9 10 11
470 // A0 A1 A2 A3 12 13 14 15
471 //--------------------------------------
472 if (color[9] == color[6] && color[5] != color[10]) {
473 product2b = color[9];
474 product1b = product2b;
476 else if (color[5] == color[10] && color[9] != color[6]) {
477 product2b = color[5];
478 product1b = product2b;
480 else if (color[5] == color[10] && color[9] == color[6]) {
483 r += GET_RESULT(color[6], color[5], color[8], color[13]);
484 r += GET_RESULT(color[6], color[5], color[4], color[1]);
485 r += GET_RESULT(color[6], color[5], color[14], color[11]);
486 r += GET_RESULT(color[6], color[5], color[2], color[7]);
489 product1b = color[6];
491 product1b = color[5];
493 product1b = INTERPOLATE(color[5], color[6]);
495 product2b = product1b;
499 if (color[6] == color[10] && color[10] == color[13] && color[9] != color[14] && color[10] != color[12])
500 product2b = Q_INTERPOLATE(color[10], color[10], color[10], color[9]);
501 else if (color[5] == color[9] && color[9] == color[14] && color[13] != color[10] && color[9] != color[15])
502 product2b = Q_INTERPOLATE(color[9], color[9], color[9], color[10]);
504 product2b = INTERPOLATE(color[9], color[10]);
506 if (color[6] == color[10] && color[6] == color[1] && color[5] != color[2] && color[6] != color[0])
507 product1b = Q_INTERPOLATE(color[6], color[6], color[6], color[5]);
508 else if (color[5] == color[9] && color[5] == color[2] && color[1] != color[6] && color[5] != color[3])
509 product1b = Q_INTERPOLATE(color[6], color[5], color[5], color[5]);
511 product1b = INTERPOLATE(color[5], color[6]);
514 if (color[5] == color[10] && color[9] != color[6] && color[4] == color[5] && color[5] != color[14])
515 product2a = INTERPOLATE(color[9], color[5]);
516 else if (color[5] == color[8] && color[6] == color[5] && color[4] != color[9] && color[5] != color[12])
517 product2a = INTERPOLATE(color[9], color[5]);
519 product2a = color[9];
521 if (color[9] == color[6] && color[5] != color[10] && color[8] == color[9] && color[9] != color[2])
522 product1a = INTERPOLATE(color[9], color[5]);
523 else if (color[4] == color[9] && color[10] == color[9] && color[8] != color[5] && color[9] != color[0])
524 product1a = INTERPOLATE(color[9], color[5]);
526 product1a = color[5];
528 if (dst_bytes_per_pixel == 2) {
529 *((unsigned long *) (&dst_line[0][x * 4])) = product1a | (product1b << 16);
530 *((unsigned long *) (&dst_line[1][x * 4])) = product2a | (product2b << 16);
532 *((unsigned long *) (&dst_line[0][x * 8])) = product1a;
533 *((unsigned long *) (&dst_line[0][x * 8 + 4])) = product1b;
534 *((unsigned long *) (&dst_line[1][x * 8])) = product2a;
535 *((unsigned long *) (&dst_line[1][x * 8 + 4])) = product2b;
538 /* Move color matrix forward */
539 color[0] = color[1]; color[4] = color[5]; color[8] = color[9]; color[12] = color[13];
540 color[1] = color[2]; color[5] = color[6]; color[9] = color[10]; color[13] = color[14];
541 color[2] = color[3]; color[6] = color[7]; color[10] = color[11]; color[14] = color[15];
545 if (src_bytes_per_pixel == 1) {
546 color[3] = GET_COLOR(*(((unsigned char*)src_line[0]) + x));
547 color[7] = GET_COLOR(*(((unsigned char*)src_line[1]) + x));
548 color[11] = GET_COLOR(*(((unsigned char*)src_line[2]) + x));
549 color[15] = GET_COLOR(*(((unsigned char*)src_line[3]) + x));
550 } else if (src_bytes_per_pixel == 2) {
551 color[3] = *(((unsigned short*)src_line[0]) + x);
552 color[7] = *(((unsigned short*)src_line[1]) + x);
553 color[11] = *(((unsigned short*)src_line[2]) + x);
554 color[15] = *(((unsigned short*)src_line[3]) + x);
556 color[3] = *(((unsigned long*)src_line[0]) + x);
557 color[7] = *(((unsigned long*)src_line[1]) + x);
558 color[11] = *(((unsigned long*)src_line[2]) + x);
559 color[15] = *(((unsigned long*)src_line[3]) + x);
565 /* We're done with one line, so we shift the source lines up */
566 src_line[0] = src_line[1];
567 src_line[1] = src_line[2];
568 src_line[2] = src_line[3];
572 src_line[3] = src_line[2];
574 src_line[3] = src_line[2] + src_pitch;
576 /* Then shift the color matrix up */
577 if (src_bytes_per_pixel == 1) {
580 color[0] = GET_COLOR(*sbp); color[1] = color[0]; color[2] = GET_COLOR(*(sbp + 1)); color[3] = GET_COLOR(*(sbp + 2));
582 color[4] = GET_COLOR(*sbp); color[5] = color[4]; color[6] = GET_COLOR(*(sbp + 1)); color[7] = GET_COLOR(*(sbp + 2));
584 color[8] = GET_COLOR(*sbp); color[9] = color[8]; color[10] = GET_COLOR(*(sbp + 1)); color[11] = GET_COLOR(*(sbp + 2));
586 color[12] = GET_COLOR(*sbp); color[13] = color[12]; color[14] = GET_COLOR(*(sbp + 1)); color[15] = GET_COLOR(*(sbp + 2));
587 } else if (src_bytes_per_pixel == 2) {
589 sbp = (unsigned short*)src_line[0];
590 color[0] = *sbp; color[1] = color[0]; color[2] = *(sbp + 1); color[3] = *(sbp + 2);
591 sbp = (unsigned short*)src_line[1];
592 color[4] = *sbp; color[5] = color[4]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
593 sbp = (unsigned short*)src_line[2];
594 color[8] = *sbp; color[9] = color[9]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
595 sbp = (unsigned short*)src_line[3];
596 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
599 lbp = (unsigned long*)src_line[0];
600 color[0] = *lbp; color[1] = color[0]; color[2] = *(lbp + 1); color[3] = *(lbp + 2);
601 lbp = (unsigned long*)src_line[1];
602 color[4] = *lbp; color[5] = color[4]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
603 lbp = (unsigned long*)src_line[2];
604 color[8] = *lbp; color[9] = color[9]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
605 lbp = (unsigned long*)src_line[3];
606 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
609 if (y < height - 1) {
610 dst_line[0] += dst_pitch * 2;
611 dst_line[1] += dst_pitch * 2;
617 void flippage(int page)
623 assert(drawing_enable==0);
625 SDL_LockSurface(jnb_surface);
626 if (!jnb_surface->pixels) {
628 for (x=0; x<(25*16); x++) {
629 dirty_blocks[0][x] = 1;
630 dirty_blocks[1][x] = 1;
636 dest=(unsigned char *)jnb_surface->pixels;
637 src=screen_buffer[page];
638 Super2xSaI(src, JNB_WIDTH, 1, dest, jnb_surface->pitch, 2, JNB_WIDTH, JNB_HEIGHT, current_pal);
640 dest=(unsigned char *)jnb_surface->pixels;
641 src=screen_buffer[page];
642 for (y=0; y<screen_height; y++) {
643 //memset(&dest[y*jnb_surface->pitch],0,JNB_SURFACE_WIDTH*bytes_per_pixel);
644 for (x=0; x<25; x++) {
650 while ( (test_x<25) && (dirty_blocks[page][(y>>dirty_block_shift)*25+test_x]) ) {
655 memcpy( &dest[y*jnb_surface->pitch+(x<<dirty_block_shift)*bytes_per_pixel],
656 &src[y*screen_pitch+((x<<dirty_block_shift)*bytes_per_pixel)],
657 ((16<<dirty_block_shift)>>4)*bytes_per_pixel*count);
658 //*((pixel_t *)(&dest[(y>>dirty_block_shift)*jnb_surface->pitch+x*bytes_per_pixel]))=0xe0e0;
663 memset(&dirty_blocks[page], 0, sizeof(int)*25*16);
665 SDL_UnlockSurface(jnb_surface);
666 SDL_Flip(jnb_surface);
670 void draw_begin(void)
672 assert(drawing_enable==0);
675 if (background_drawn == 0) {
677 put_block(0, 0, 0, JNB_WIDTH, JNB_HEIGHT, background);
678 put_block(1, 0, 0, JNB_WIDTH, JNB_HEIGHT, background);
679 //put_block(0, 0, 0, rabbit_gobs.width[1], rabbit_gobs.height[1], rabbit_gobs.data[1]);
680 //put_block(1, 0, 0, rabbit_gobs.width[1], rabbit_gobs.height[1], rabbit_gobs.data[1]);
685 background_drawn = 1;
692 assert(drawing_enable==1);
698 void setpalette(int index, int count, char *palette)
700 SDL_Color colors[256];
703 assert(drawing_enable==0);
705 for (i = 0; i < count; i++) {
706 colors[i+index].r = palette[i * 3 + 0] << 2;
707 colors[i+index].g = palette[i * 3 + 1] << 2;
708 colors[i+index].b = palette[i * 3 + 2] << 2;
709 current_pal[i+index] = SDL_MapRGB(jnb_surface->format, colors[i+index].r, colors[i+index].g, colors[i+index].b);
712 SDL_SetColors(jnb_surface, &colors[index], index, count);
716 void fillpalette(int red, int green, int blue)
718 SDL_Color colors[256];
721 assert(drawing_enable==0);
723 for (i = 0; i < 256; i++) {
724 colors[i].r = red << 2;
725 colors[i].g = green << 2;
726 colors[i].b = blue << 2;
727 current_pal[i] = SDL_MapRGB(jnb_surface->format, colors[i].r, colors[i].g, colors[i].b);
730 SDL_SetColors(jnb_surface, colors, 0, 256);
734 void get_block(int page, int x, int y, int width, int height, void *buffer)
736 unsigned char *buffer_ptr, *vga_ptr;
739 assert(drawing_enable==1);
752 if (y + height >= screen_height)
753 height = screen_height - y;
754 if (x + width >= screen_width)
755 width = screen_width - x;
761 vga_ptr = get_vgaptr(page, x, y);
763 for (h = 0; h < height; h++) {
764 memcpy(buffer_ptr, vga_ptr, width * bytes_per_pixel);
765 vga_ptr += screen_pitch;
766 buffer_ptr += width * bytes_per_pixel;
772 void put_block(int page, int x, int y, int width, int height, void *buffer)
775 unsigned char *vga_ptr, *buffer_ptr;
777 assert(drawing_enable==1);
790 if (y + height >= screen_height)
791 height = screen_height - y;
792 if (x + width >= screen_width)
793 width = screen_width - x;
799 vga_ptr = get_vgaptr(page, x, y);
801 for (h = 0; h < height; h++) {
802 memcpy(vga_ptr, buffer_ptr, width * bytes_per_pixel);
803 vga_ptr += screen_pitch;
804 buffer_ptr += width * bytes_per_pixel;
806 width = ((x+width)>>dirty_block_shift) - (x>>dirty_block_shift) + 1;
807 height = ((y+height)>>dirty_block_shift) - (y>>dirty_block_shift) + 1;
808 x >>= dirty_block_shift;
809 y >>= dirty_block_shift;
811 for (h=0; h<height; h++)
812 dirty_blocks[page][(y+h)*25+(x+width)] = 1;
816 void put_text(int page, int x, int y, char *text, int align)
824 assert(drawing_enable==1);
826 if (text == NULL || strlen(text) == 0)
828 if (font_gobs.num_images == 0)
833 while (text[c1] != 0) {
840 if (t1 >= 33 && t1 <= 34)
843 else if (t1 >= 39 && t1 <= 41)
846 else if (t1 >= 44 && t1 <= 59)
849 else if (t1 >= 64 && t1 <= 90)
852 else if (t1 >= 97 && t1 <= 122)
878 width += pob_width(image, &font_gobs) + 1;
889 cur_x = x - width / 2;
892 cur_x = 0; /* this should cause error? -Chuck */
897 while (text[c1] != 0) {
904 if (t1 >= 33 && t1 <= 34)
907 else if (t1 >= 39 && t1 <= 41)
910 else if (t1 >= 44 && t1 <= 59)
913 else if (t1 >= 64 && t1 <= 90)
916 else if (t1 >= 97 && t1 <= 122)
942 put_pob(page, cur_x, y, image, &font_gobs, 1, mask_pic);
943 cur_x += pob_width(image, &font_gobs) + 1;
948 void put_pob(int page, int x, int y, int image, gob_t *gob, int use_mask, void *mask_pic)
953 int draw_width, draw_height;
956 assert(drawing_enable==1);
959 assert(image<gob->num_images);
964 width = draw_width = gob->width[image]*2;
965 height = draw_height = gob->height[image]*2;
966 x -= gob->hs_x[image]*2;
967 y -= gob->hs_y[image]*2;
969 width = draw_width = gob->width[image];
970 height = draw_height = gob->height[image];
971 x -= gob->hs_x[image];
972 y -= gob->hs_y[image];
975 if ((x + width) <= 0 || x >= screen_width)
977 if ((y + height) <= 0 || y >= screen_height)
987 if ((x + width) > screen_width)
988 draw_width -= x + width - screen_width;
994 if ((y + height) > screen_height)
995 draw_height -= y + height - screen_height;
998 if (bytes_per_pixel==1) {
999 unsigned char *vga_ptr;
1000 unsigned char *pob_ptr;
1001 unsigned char *mask_ptr;
1003 vga_ptr = get_vgaptr(page, x, y);
1004 pob_ptr = ((unsigned char *)gob->data[image]) + ((pob_y * width) + pob_x);
1005 mask_ptr = ((unsigned char *)mask) + ((y * screen_pitch) + (x*bytes_per_pixel));
1006 for (c1 = 0; c1 < draw_height; c1++) {
1007 for (c2 = 0; c2 < draw_width; c2++) {
1009 if (use_mask == 0 || (use_mask == 1 && colour == 0)) {
1019 pob_ptr += width - c2;
1020 vga_ptr += (screen_width - c2);
1021 mask_ptr += (screen_width - c2);
1023 } else if (bytes_per_pixel==2) {
1024 unsigned short *vga_ptr;
1025 unsigned short *pob_ptr;
1026 unsigned short *mask_ptr;
1028 vga_ptr = get_vgaptr(page, x, y);
1029 pob_ptr = (unsigned short *)(((unsigned char *)gob->data[image]) + ((pob_y * width) + pob_x));
1030 mask_ptr = (unsigned short *)(((unsigned char *)mask) + ((y * screen_pitch) + (x*bytes_per_pixel)));
1031 for (c1 = 0; c1 < draw_height; c1++) {
1032 for (c2 = 0; c2 < draw_width; c2++) {
1034 if (use_mask == 0 || (use_mask == 1 && colour == 0)) {
1044 pob_ptr += width - c2;
1045 vga_ptr += (screen_width - c2);
1046 mask_ptr += (screen_width - c2);
1049 unsigned int *vga_ptr;
1050 unsigned int *pob_ptr;
1051 unsigned int *mask_ptr;
1053 vga_ptr = get_vgaptr(page, x, y);
1054 pob_ptr = (unsigned int *)(((unsigned char *)gob->data[image]) + ((pob_y * width) + pob_x));
1055 mask_ptr = (unsigned int *)(((unsigned char *)mask) + ((y * screen_pitch) + (x*bytes_per_pixel)));
1056 for (c1 = 0; c1 < draw_height; c1++) {
1057 for (c2 = 0; c2 < draw_width; c2++) {
1059 if (use_mask == 0 || (use_mask == 1 && colour == 0)) {
1069 pob_ptr += width - c2;
1070 vga_ptr += (screen_width - c2);
1071 mask_ptr += (screen_width - c2);
1074 draw_width = ((x+draw_width)>>dirty_block_shift) - (x>>dirty_block_shift) + 1;
1075 draw_height = ((y+draw_height)>>dirty_block_shift) - (y>>dirty_block_shift) + 1;
1076 x >>= dirty_block_shift;
1077 y >>= dirty_block_shift;
1078 while (draw_width--)
1079 for (c1=0; c1<draw_height; c1++)
1080 dirty_blocks[page][(y+c1)*25+(x+draw_width)] = 1;
1084 int pob_width(int image, gob_t *gob)
1088 assert(image<gob->num_images);
1089 return gob->width[image];
1093 int pob_height(int image, gob_t *gob)
1097 assert(image<gob->num_images);
1098 return gob->height[image];
1102 int pob_hs_x(int image, gob_t *gob)
1106 assert(image<gob->num_images);
1107 return gob->hs_x[image];
1111 int pob_hs_y(int image, gob_t *gob)
1115 assert(image<gob->num_images);
1116 return gob->hs_y[image];
1120 int read_pcx(FILE * handle, void *buf, int buf_len, char *pal)
1122 unsigned char *buffer=buf;
1127 fseek(handle, 128, SEEK_CUR);
1129 while (ofs1 < buf_len) {
1131 if ((a & 0xc0) == 0xc0) {
1134 for (c1 = 0; c1 < a && ofs1 < buf_len; c1++)
1135 buffer[ofs1++] = (char) b;
1137 buffer[ofs1++] = (char) a;
1140 fseek(handle, 1, SEEK_CUR);
1141 for (c1 = 0; c1 < 768; c1++)
1142 pal[c1] = fgetc(handle) >> 2;
1149 void register_background(char *pixels, char pal[768])
1155 background_drawn = 0;
1163 for (i=0; i<256; i++)
1164 int_pal[i] = SDL_MapRGB(jnb_surface->format, (Uint8)(pal[i*3+0]<<2), (Uint8)(pal[i*3+1]<<2), (Uint8)(pal[i*3+2]<<2));
1165 background = malloc(screen_pitch*screen_height);
1167 Super2xSaI(pixels, JNB_WIDTH, 1, (unsigned char *)background, screen_pitch, bytes_per_pixel, JNB_WIDTH, JNB_HEIGHT, int_pal);
1169 background = malloc(JNB_WIDTH*JNB_HEIGHT);
1171 memcpy(background, pixels, JNB_WIDTH*JNB_HEIGHT);
1175 int register_gob(FILE *handle, gob_t *gob, int len)
1177 unsigned char *gob_data;
1180 gob_data = malloc(len);
1181 fread(gob_data, 1, len, handle);
1183 gob->num_images = (short)((gob_data[0]) + (gob_data[1] << 8));
1185 gob->width = malloc(gob->num_images*sizeof(int));
1186 gob->height = malloc(gob->num_images*sizeof(int));
1187 gob->hs_x = malloc(gob->num_images*sizeof(int));
1188 gob->hs_y = malloc(gob->num_images*sizeof(int));
1189 gob->data = malloc(gob->num_images*sizeof(void *));
1190 gob->orig_data = malloc(gob->num_images*sizeof(void *));
1191 for (i=0; i<gob->num_images; i++) {
1195 offset = (gob_data[i*4+2]) + (gob_data[i*4+3] << 8) + (gob_data[i*4+4] << 16) + (gob_data[i*4+5] << 24);
1197 gob->width[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1198 gob->height[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1199 gob->hs_x[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1200 gob->hs_y[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1202 image_size = gob->width[i] * gob->height[i];
1203 gob->orig_data[i] = malloc(image_size);
1204 memcpy(gob->orig_data[i], &gob_data[offset], image_size);
1206 image_size = gob->width[i] * gob->height[i] * 4 * bytes_per_pixel;
1207 gob->data[i] = malloc(image_size);
1209 gob->data[i] = (unsigned short *)gob->orig_data[i];
1217 void recalculate_gob(gob_t *gob, char pal[768])
1225 for (i=1; i<256; i++) {
1226 int_pal[i] = SDL_MapRGB(jnb_surface->format, (Uint8)(pal[i*3+0]<<2), (Uint8)(pal[i*3+1]<<2), (Uint8)(pal[i*3+2]<<2));
1227 if (int_pal[i] == 0)
1228 int_pal[i] = SDL_MapRGB(jnb_surface->format, 8, 8, 8);
1232 for (i=0; i<gob->num_images; i++) {
1233 Super2xSaI(gob->orig_data[i], gob->width[i], 1, (unsigned char *)gob->data[i], gob->width[i]*2*bytes_per_pixel, bytes_per_pixel, gob->width[i], gob->height[i], int_pal);
1237 void register_mask(void *pixels)
1249 for (i=1; i<256; i++)
1250 int_pal[i] = 0xffffffff;
1251 mask = malloc(screen_pitch*screen_height);
1253 Scale2x(pixels, JNB_WIDTH, 1, (unsigned char *)mask, screen_pitch, bytes_per_pixel, JNB_WIDTH, JNB_HEIGHT, int_pal);
1255 mask = malloc(JNB_WIDTH*JNB_HEIGHT);
1257 memcpy(mask, pixels, JNB_WIDTH*JNB_HEIGHT);