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)
69 #define NEXT_TOKEN { \
70 while ((*p != ' ') && (*p != '\t')) p++; \
71 while ((*p == ' ') || (*p == '\t')) p++; }
104 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
108 SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
116 SDL_FreeSurface(surface);
122 pal[color] = strtoul(++p, NULL, 16) | 0xff000000;
133 pixels = (int *)&((char *)surface->pixels)[y++ * surface->pitch];
139 SDL_FreeSurface(surface);
142 r = (pal[*p] >> 16) & 0xff;
143 b = (pal[*p] & 0xff);
144 g = (pal[*p] >> 8) & 0xff;
145 a = (pal[*p] >> 24) & 0xff;
146 pixels[x] = SDL_MapRGBA(surface->format, r, g, b, a);
155 void *get_vgaptr(int page, int x, int y)
157 assert(drawing_enable==1);
159 return (unsigned char *)screen_buffer[page] + (y*screen_pitch)+(x*bytes_per_pixel);
163 void set_scaling(int scale)
171 screen_pitch=screen_width*bytes_per_pixel;
178 screen_pitch=screen_width*bytes_per_pixel;
182 void open_screen(void)
189 lval = SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO);
191 lval = SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_AUDIO);
194 fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
202 flags = SDL_SWSURFACE;
204 flags |= SDL_FULLSCREEN;
205 jnb_surface = SDL_SetVideoMode(screen_width, screen_height, bpp, flags);
208 fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
217 SDL_WM_SetCaption("Jump n Bump","");
219 icon=load_xpm_from_array(jumpnbump_xpm);
221 printf("Couldn't load icon\n");
223 SDL_WM_SetIcon(icon,NULL);
228 memset(current_pal, 0, sizeof(current_pal));
229 memset(dirty_blocks, 0, sizeof(dirty_blocks));
231 screen_buffer[0]=malloc(screen_width*screen_height*bytes_per_pixel);
232 screen_buffer[1]=malloc(screen_width*screen_height*bytes_per_pixel);
235 dirty_blocks[0]=malloc(sizeof(int)*25*16+1000);
236 dirty_blocks[1]=malloc(sizeof(int)*25*16+1000);
249 if (SDL_WM_ToggleFullScreen(jnb_surface))
254 void wait_vrt(int mix)
260 void clear_page(int page, int color)
264 assert(drawing_enable==1);
266 for (i=0; i<(25*16); i++)
267 dirty_blocks[page][i] = 1;
269 if (bytes_per_pixel==1) {
270 unsigned char *buf = get_vgaptr(page, 0, 0);
272 for (i=0; i<screen_height; i++)
273 for (j=0; j<screen_width; j++)
275 } else if (bytes_per_pixel==2) {
276 unsigned short *buf = get_vgaptr(page, 0, 0);
278 for (i=0; i<screen_height; i++)
279 for (j=0; j<screen_width; j++)
282 unsigned int *buf = get_vgaptr(page, 0, 0);
284 for (i=0; i<screen_height; i++)
285 for (j=0; j<screen_width; j++)
291 void clear_lines(int page, int y, int count, int color)
295 assert(drawing_enable==1);
302 if (bytes_per_pixel==1) {
303 for (i=0; i<count; i++) {
304 if ((i+y)<screen_height) {
305 unsigned char *buf = get_vgaptr(page, 0, i+y);
306 for (j=0; j<screen_width; j++)
310 } else if (bytes_per_pixel==2) {
311 for (i=0; i<count; i++) {
312 if ((i+y)<screen_height) {
313 unsigned short *buf = get_vgaptr(page, 0, i+y);
314 for (j=0; j<screen_width; j++)
319 for (i=0; i<count; i++) {
320 if ((i+y)<screen_height) {
321 unsigned int *buf = get_vgaptr(page, 0, i+y);
322 for (j=0; j<screen_width; j++)
327 count = ((y+count)>>dirty_block_shift) - (y>>dirty_block_shift) + 1;
328 y >>= dirty_block_shift;
329 for (i=0; i<count; i++)
331 dirty_blocks[page][(y+i)*25+j] = 1;
335 int get_color(int color, char pal[768])
339 return SDL_MapRGB(jnb_surface->format, (Uint8)(pal[color*3+0]<<2), (Uint8)(pal[color*3+1]<<2), (Uint8)(pal[color*3+2]<<2));
343 int get_pixel(int page, int x, int y)
345 assert(drawing_enable==1);
352 assert(x<screen_width);
353 assert(y<screen_height);
355 if (bytes_per_pixel==1)
356 return *(unsigned char *)get_vgaptr(page, x, y);
357 else if (bytes_per_pixel==2)
358 return *(unsigned short *)get_vgaptr(page, x, y);
360 return *(unsigned int *)get_vgaptr(page, x, y);
364 void set_pixel(int page, int x, int y, int color)
366 assert(drawing_enable==1);
373 assert(x<screen_width);
374 assert(y<screen_height);
376 dirty_blocks[page][(y>>dirty_block_shift)*25+(x>>dirty_block_shift)] = 1;
378 if (bytes_per_pixel==1)
379 *(unsigned char *)get_vgaptr(page, x, y) = color;
380 else if (bytes_per_pixel==2)
381 *(unsigned short *)get_vgaptr(page, x, y) = color;
383 *(unsigned int *)get_vgaptr(page, x, y) = color;
387 static unsigned int colorMask = 0xF7DEF7DE;
388 static unsigned int lowPixelMask = 0x08210821;
389 static unsigned int qcolorMask = 0xE79CE79C;
390 static unsigned int qlowpixelMask = 0x18631863;
391 static unsigned int redblueMask = 0xF81F;
392 static unsigned int greenMask = 0x7E0;
394 int Init_2xSaI (unsigned int BitFormat)
396 if (BitFormat == 565)
398 colorMask = SWAP32(0xF7DEF7DE);
399 lowPixelMask = SWAP32(0x08210821);
400 qcolorMask = SWAP32(0xE79CE79C);
401 qlowpixelMask = SWAP32(0x18631863);
402 redblueMask = SWAP16(0xF81F);
403 greenMask = SWAP16(0x7E0);
405 else if (BitFormat == 555)
407 colorMask = SWAP32(0x7BDE7BDE);
408 lowPixelMask = SWAP32(0x04210421);
409 qcolorMask = SWAP32(0x739C739C);
410 qlowpixelMask = SWAP32(0x0C630C63);
411 redblueMask = SWAP16(0x7C1F);
412 greenMask = SWAP16(0x3E0);
420 Init_2xSaIMMX (BitFormat);
427 void Scale2x (unsigned char *src, unsigned int src_pitch, int src_bytes_per_pixel,
428 unsigned char *dst, unsigned int dst_pitch, int dst_bytes_per_pixel,
429 int width, int height, int pal[256])
431 #define GET_COLOR(x) (pal[(x)])
434 unsigned char *src_line;
435 unsigned char *dst_line[2];
439 dst_line[1] = dst + dst_pitch;
440 for (y=0; y<height; y++) {
441 for (x=0; x<width; x++) {
444 if (src_bytes_per_pixel == 1) {
445 color = GET_COLOR(*(((unsigned char*)src_line) + x));
446 } else if (src_bytes_per_pixel == 2) {
447 color = *(((unsigned short*)src_line) + x);
449 color = *(((unsigned int*)src_line) + x);
452 if (dst_bytes_per_pixel == 2) {
453 *((unsigned long *) (&dst_line[0][x * 4])) = color | (color << 16);
454 *((unsigned long *) (&dst_line[1][x * 4])) = color | (color << 16);
456 *((unsigned long *) (&dst_line[0][x * 8])) = color;
457 *((unsigned long *) (&dst_line[0][x * 8 + 4])) = color;
458 *((unsigned long *) (&dst_line[1][x * 8])) = color;
459 *((unsigned long *) (&dst_line[1][x * 8 + 4])) = color;
463 src_line += src_pitch;
465 if (y < height - 1) {
466 dst_line[0] += dst_pitch * 2;
467 dst_line[1] += dst_pitch * 2;
473 void Super2xSaI (unsigned char *src, unsigned int src_pitch, int src_bytes_per_pixel,
474 unsigned char *dst, unsigned int dst_pitch, int dst_bytes_per_pixel,
475 int width, int height, int pal[256])
477 #define GET_RESULT(A, B, C, D) ((A != C || A != D) - (B != C || B != D))
479 #define INTERPOLATE(A, B) (((A & colorMask) >> 1) + ((B & colorMask) >> 1) + (A & B & lowPixelMask))
481 #define Q_INTERPOLATE(A, B, C, D) ((A & qcolorMask) >> 2) + ((B & qcolorMask) >> 2) + ((C & qcolorMask) >> 2) + ((D & qcolorMask) >> 2) \
482 + ((((A & qlowpixelMask) + (B & qlowpixelMask) + (C & qlowpixelMask) + (D & qlowpixelMask)) >> 2) & qlowpixelMask)
484 #define GET_COLOR(x) (pal[(x)])
486 unsigned char *src_line[4];
487 unsigned char *dst_line[2];
489 unsigned long color[16];
491 if ( (width<2) || (height<2) ) {
492 Scale2x(src, src_pitch, src_bytes_per_pixel, dst, dst_pitch, dst_bytes_per_pixel, width, height, pal);
496 /* Point to the first 3 lines. */
499 src_line[2] = src + src_pitch;
500 src_line[3] = src + (src_pitch * 2);
503 dst_line[1] = dst + dst_pitch;
507 if (src_bytes_per_pixel == 1) {
510 color[0] = GET_COLOR(*sbp); color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
511 color[4] = color[0]; color[5] = color[0]; color[6] = GET_COLOR(*(sbp + 1)); color[7] = GET_COLOR(*(sbp + 2));
513 color[8] = GET_COLOR(*sbp); color[9] = color[8]; color[10] = GET_COLOR(*(sbp + 1)); color[11] = GET_COLOR(*(sbp + 2));
515 color[12] = GET_COLOR(*sbp); color[13] = color[12]; color[14] = GET_COLOR(*(sbp + 1)); color[15] = GET_COLOR(*(sbp + 2));
516 } else if (src_bytes_per_pixel == 2) {
518 sbp = (unsigned short*)src_line[0];
519 color[0] = *sbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
520 color[4] = color[0]; color[5] = color[0]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
521 sbp = (unsigned short*)src_line[2];
522 color[8] = *sbp; color[9] = color[8]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
523 sbp = (unsigned short*)src_line[3];
524 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
527 lbp = (unsigned long*)src_line[0];
528 color[0] = *lbp; color[1] = color[0]; color[2] = color[0]; color[3] = color[0];
529 color[4] = color[0]; color[5] = color[0]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
530 lbp = (unsigned long*)src_line[2];
531 color[8] = *lbp; color[9] = color[8]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
532 lbp = (unsigned long*)src_line[3];
533 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
536 for (y = 0; y < height; y++) {
538 /* Todo: x = width - 2, x = width - 1 */
540 for (x = 0; x < width; x++) {
541 unsigned long product1a, product1b, product2a, product2b;
543 //--------------------------------------- B0 B1 B2 B3 0 1 2 3
544 // 4 5* 6 S2 -> 4 5* 6 7
545 // 1 2 3 S1 8 9 10 11
546 // A0 A1 A2 A3 12 13 14 15
547 //--------------------------------------
548 if (color[9] == color[6] && color[5] != color[10]) {
549 product2b = color[9];
550 product1b = product2b;
552 else if (color[5] == color[10] && color[9] != color[6]) {
553 product2b = color[5];
554 product1b = product2b;
556 else if (color[5] == color[10] && color[9] == color[6]) {
559 r += GET_RESULT(color[6], color[5], color[8], color[13]);
560 r += GET_RESULT(color[6], color[5], color[4], color[1]);
561 r += GET_RESULT(color[6], color[5], color[14], color[11]);
562 r += GET_RESULT(color[6], color[5], color[2], color[7]);
565 product1b = color[6];
567 product1b = color[5];
569 product1b = INTERPOLATE(color[5], color[6]);
571 product2b = product1b;
575 if (color[6] == color[10] && color[10] == color[13] && color[9] != color[14] && color[10] != color[12])
576 product2b = Q_INTERPOLATE(color[10], color[10], color[10], color[9]);
577 else if (color[5] == color[9] && color[9] == color[14] && color[13] != color[10] && color[9] != color[15])
578 product2b = Q_INTERPOLATE(color[9], color[9], color[9], color[10]);
580 product2b = INTERPOLATE(color[9], color[10]);
582 if (color[6] == color[10] && color[6] == color[1] && color[5] != color[2] && color[6] != color[0])
583 product1b = Q_INTERPOLATE(color[6], color[6], color[6], color[5]);
584 else if (color[5] == color[9] && color[5] == color[2] && color[1] != color[6] && color[5] != color[3])
585 product1b = Q_INTERPOLATE(color[6], color[5], color[5], color[5]);
587 product1b = INTERPOLATE(color[5], color[6]);
590 if (color[5] == color[10] && color[9] != color[6] && color[4] == color[5] && color[5] != color[14])
591 product2a = INTERPOLATE(color[9], color[5]);
592 else if (color[5] == color[8] && color[6] == color[5] && color[4] != color[9] && color[5] != color[12])
593 product2a = INTERPOLATE(color[9], color[5]);
595 product2a = color[9];
597 if (color[9] == color[6] && color[5] != color[10] && color[8] == color[9] && color[9] != color[2])
598 product1a = INTERPOLATE(color[9], color[5]);
599 else if (color[4] == color[9] && color[10] == color[9] && color[8] != color[5] && color[9] != color[0])
600 product1a = INTERPOLATE(color[9], color[5]);
602 product1a = color[5];
604 if (dst_bytes_per_pixel == 2) {
605 *((unsigned long *) (&dst_line[0][x * 4])) = product1a | (product1b << 16);
606 *((unsigned long *) (&dst_line[1][x * 4])) = product2a | (product2b << 16);
608 *((unsigned long *) (&dst_line[0][x * 8])) = product1a;
609 *((unsigned long *) (&dst_line[0][x * 8 + 4])) = product1b;
610 *((unsigned long *) (&dst_line[1][x * 8])) = product2a;
611 *((unsigned long *) (&dst_line[1][x * 8 + 4])) = product2b;
614 /* Move color matrix forward */
615 color[0] = color[1]; color[4] = color[5]; color[8] = color[9]; color[12] = color[13];
616 color[1] = color[2]; color[5] = color[6]; color[9] = color[10]; color[13] = color[14];
617 color[2] = color[3]; color[6] = color[7]; color[10] = color[11]; color[14] = color[15];
621 if (src_bytes_per_pixel == 1) {
622 color[3] = GET_COLOR(*(((unsigned char*)src_line[0]) + x));
623 color[7] = GET_COLOR(*(((unsigned char*)src_line[1]) + x));
624 color[11] = GET_COLOR(*(((unsigned char*)src_line[2]) + x));
625 color[15] = GET_COLOR(*(((unsigned char*)src_line[3]) + x));
626 } else if (src_bytes_per_pixel == 2) {
627 color[3] = *(((unsigned short*)src_line[0]) + x);
628 color[7] = *(((unsigned short*)src_line[1]) + x);
629 color[11] = *(((unsigned short*)src_line[2]) + x);
630 color[15] = *(((unsigned short*)src_line[3]) + x);
632 color[3] = *(((unsigned long*)src_line[0]) + x);
633 color[7] = *(((unsigned long*)src_line[1]) + x);
634 color[11] = *(((unsigned long*)src_line[2]) + x);
635 color[15] = *(((unsigned long*)src_line[3]) + x);
641 /* We're done with one line, so we shift the source lines up */
642 src_line[0] = src_line[1];
643 src_line[1] = src_line[2];
644 src_line[2] = src_line[3];
648 src_line[3] = src_line[2];
650 src_line[3] = src_line[2] + src_pitch;
652 /* Then shift the color matrix up */
653 if (src_bytes_per_pixel == 1) {
656 color[0] = GET_COLOR(*sbp); color[1] = color[0]; color[2] = GET_COLOR(*(sbp + 1)); color[3] = GET_COLOR(*(sbp + 2));
658 color[4] = GET_COLOR(*sbp); color[5] = color[4]; color[6] = GET_COLOR(*(sbp + 1)); color[7] = GET_COLOR(*(sbp + 2));
660 color[8] = GET_COLOR(*sbp); color[9] = color[8]; color[10] = GET_COLOR(*(sbp + 1)); color[11] = GET_COLOR(*(sbp + 2));
662 color[12] = GET_COLOR(*sbp); color[13] = color[12]; color[14] = GET_COLOR(*(sbp + 1)); color[15] = GET_COLOR(*(sbp + 2));
663 } else if (src_bytes_per_pixel == 2) {
665 sbp = (unsigned short*)src_line[0];
666 color[0] = *sbp; color[1] = color[0]; color[2] = *(sbp + 1); color[3] = *(sbp + 2);
667 sbp = (unsigned short*)src_line[1];
668 color[4] = *sbp; color[5] = color[4]; color[6] = *(sbp + 1); color[7] = *(sbp + 2);
669 sbp = (unsigned short*)src_line[2];
670 color[8] = *sbp; color[9] = color[9]; color[10] = *(sbp + 1); color[11] = *(sbp + 2);
671 sbp = (unsigned short*)src_line[3];
672 color[12] = *sbp; color[13] = color[12]; color[14] = *(sbp + 1); color[15] = *(sbp + 2);
675 lbp = (unsigned long*)src_line[0];
676 color[0] = *lbp; color[1] = color[0]; color[2] = *(lbp + 1); color[3] = *(lbp + 2);
677 lbp = (unsigned long*)src_line[1];
678 color[4] = *lbp; color[5] = color[4]; color[6] = *(lbp + 1); color[7] = *(lbp + 2);
679 lbp = (unsigned long*)src_line[2];
680 color[8] = *lbp; color[9] = color[9]; color[10] = *(lbp + 1); color[11] = *(lbp + 2);
681 lbp = (unsigned long*)src_line[3];
682 color[12] = *lbp; color[13] = color[12]; color[14] = *(lbp + 1); color[15] = *(lbp + 2);
685 if (y < height - 1) {
686 dst_line[0] += dst_pitch * 2;
687 dst_line[1] += dst_pitch * 2;
693 void flippage(int page)
699 assert(drawing_enable==0);
701 SDL_LockSurface(jnb_surface);
702 if (!jnb_surface->pixels) {
704 for (x=0; x<(25*16); x++) {
705 dirty_blocks[0][x] = 1;
706 dirty_blocks[1][x] = 1;
712 dest=(unsigned char *)jnb_surface->pixels;
713 src=screen_buffer[page];
714 Super2xSaI(src, JNB_WIDTH, 1, dest, jnb_surface->pitch, 2, JNB_WIDTH, JNB_HEIGHT, current_pal);
716 dest=(unsigned char *)jnb_surface->pixels;
717 src=screen_buffer[page];
718 for (y=0; y<screen_height; y++) {
719 //memset(&dest[y*jnb_surface->pitch],0,JNB_SURFACE_WIDTH*bytes_per_pixel);
720 for (x=0; x<25; x++) {
726 while ( (test_x<25) && (dirty_blocks[page][(y>>dirty_block_shift)*25+test_x]) ) {
731 memcpy( &dest[y*jnb_surface->pitch+(x<<dirty_block_shift)*bytes_per_pixel],
732 &src[y*screen_pitch+((x<<dirty_block_shift)*bytes_per_pixel)],
733 ((16<<dirty_block_shift)>>4)*bytes_per_pixel*count);
734 //*((pixel_t *)(&dest[(y>>dirty_block_shift)*jnb_surface->pitch+x*bytes_per_pixel]))=0xe0e0;
739 memset(&dirty_blocks[page], 0, sizeof(int)*25*16);
741 SDL_UnlockSurface(jnb_surface);
742 SDL_Flip(jnb_surface);
746 void draw_begin(void)
748 assert(drawing_enable==0);
751 if (background_drawn == 0) {
753 put_block(0, 0, 0, JNB_WIDTH, JNB_HEIGHT, background);
754 put_block(1, 0, 0, JNB_WIDTH, JNB_HEIGHT, background);
755 //put_block(0, 0, 0, rabbit_gobs.width[1], rabbit_gobs.height[1], rabbit_gobs.data[1]);
756 //put_block(1, 0, 0, rabbit_gobs.width[1], rabbit_gobs.height[1], rabbit_gobs.data[1]);
761 background_drawn = 1;
768 assert(drawing_enable==1);
774 void setpalette(int index, int count, char *palette)
776 SDL_Color colors[256];
779 assert(drawing_enable==0);
781 for (i = 0; i < count; i++) {
782 colors[i+index].r = palette[i * 3 + 0] << 2;
783 colors[i+index].g = palette[i * 3 + 1] << 2;
784 colors[i+index].b = palette[i * 3 + 2] << 2;
785 current_pal[i+index] = SDL_MapRGB(jnb_surface->format, colors[i+index].r, colors[i+index].g, colors[i+index].b);
788 SDL_SetColors(jnb_surface, &colors[index], index, count);
792 void fillpalette(int red, int green, int blue)
794 SDL_Color colors[256];
797 assert(drawing_enable==0);
799 for (i = 0; i < 256; i++) {
800 colors[i].r = red << 2;
801 colors[i].g = green << 2;
802 colors[i].b = blue << 2;
803 current_pal[i] = SDL_MapRGB(jnb_surface->format, colors[i].r, colors[i].g, colors[i].b);
806 SDL_SetColors(jnb_surface, colors, 0, 256);
810 void get_block(int page, int x, int y, int width, int height, void *buffer)
812 unsigned char *buffer_ptr, *vga_ptr;
815 assert(drawing_enable==1);
828 if (y + height >= screen_height)
829 height = screen_height - y;
830 if (x + width >= screen_width)
831 width = screen_width - x;
837 vga_ptr = get_vgaptr(page, x, y);
839 for (h = 0; h < height; h++) {
840 memcpy(buffer_ptr, vga_ptr, width * bytes_per_pixel);
841 vga_ptr += screen_pitch;
842 buffer_ptr += width * bytes_per_pixel;
848 void put_block(int page, int x, int y, int width, int height, void *buffer)
851 unsigned char *vga_ptr, *buffer_ptr;
853 assert(drawing_enable==1);
866 if (y + height >= screen_height)
867 height = screen_height - y;
868 if (x + width >= screen_width)
869 width = screen_width - x;
875 vga_ptr = get_vgaptr(page, x, y);
877 for (h = 0; h < height; h++) {
878 memcpy(vga_ptr, buffer_ptr, width * bytes_per_pixel);
879 vga_ptr += screen_pitch;
880 buffer_ptr += width * bytes_per_pixel;
882 width = ((x+width)>>dirty_block_shift) - (x>>dirty_block_shift) + 1;
883 height = ((y+height)>>dirty_block_shift) - (y>>dirty_block_shift) + 1;
884 x >>= dirty_block_shift;
885 y >>= dirty_block_shift;
887 for (h=0; h<height; h++)
888 dirty_blocks[page][(y+h)*25+(x+width)] = 1;
892 void put_text(int page, int x, int y, char *text, int align)
900 assert(drawing_enable==1);
902 if (text == NULL || strlen(text) == 0)
904 if (font_gobs.num_images == 0)
909 while (text[c1] != 0) {
916 if (t1 >= 33 && t1 <= 34)
919 else if (t1 >= 39 && t1 <= 41)
922 else if (t1 >= 44 && t1 <= 59)
925 else if (t1 >= 64 && t1 <= 90)
928 else if (t1 >= 97 && t1 <= 122)
954 width += pob_width(image, &font_gobs) + 1;
965 cur_x = x - width / 2;
968 cur_x = 0; /* this should cause error? -Chuck */
973 while (text[c1] != 0) {
980 if (t1 >= 33 && t1 <= 34)
983 else if (t1 >= 39 && t1 <= 41)
986 else if (t1 >= 44 && t1 <= 59)
989 else if (t1 >= 64 && t1 <= 90)
992 else if (t1 >= 97 && t1 <= 122)
1001 else if (t1 == 0x86)
1004 else if (t1 == 0x8e)
1007 else if (t1 == 0x8f)
1010 else if (t1 == 0x94)
1013 else if (t1 == 0x99)
1018 put_pob(page, cur_x, y, image, &font_gobs, 1, mask_pic);
1019 cur_x += pob_width(image, &font_gobs) + 1;
1024 void put_pob(int page, int x, int y, int image, gob_t *gob, int use_mask, void *mask_pic)
1029 int draw_width, draw_height;
1032 assert(drawing_enable==1);
1035 assert(image<gob->num_images);
1040 width = draw_width = gob->width[image]*2;
1041 height = draw_height = gob->height[image]*2;
1042 x -= gob->hs_x[image]*2;
1043 y -= gob->hs_y[image]*2;
1045 width = draw_width = gob->width[image];
1046 height = draw_height = gob->height[image];
1047 x -= gob->hs_x[image];
1048 y -= gob->hs_y[image];
1051 if ((x + width) <= 0 || x >= screen_width)
1053 if ((y + height) <= 0 || y >= screen_height)
1063 if ((x + width) > screen_width)
1064 draw_width -= x + width - screen_width;
1070 if ((y + height) > screen_height)
1071 draw_height -= y + height - screen_height;
1074 if (bytes_per_pixel==1) {
1075 unsigned char *vga_ptr;
1076 unsigned char *pob_ptr;
1077 unsigned char *mask_ptr;
1079 vga_ptr = get_vgaptr(page, x, y);
1080 pob_ptr = ((unsigned char *)gob->data[image]) + ((pob_y * width) + pob_x);
1081 mask_ptr = ((unsigned char *)mask) + ((y * screen_pitch) + (x*bytes_per_pixel));
1082 for (c1 = 0; c1 < draw_height; c1++) {
1083 for (c2 = 0; c2 < draw_width; c2++) {
1085 if (use_mask == 0 || (use_mask == 1 && colour == 0)) {
1095 pob_ptr += width - c2;
1096 vga_ptr += (screen_width - c2);
1097 mask_ptr += (screen_width - c2);
1099 } else if (bytes_per_pixel==2) {
1100 unsigned short *vga_ptr;
1101 unsigned short *pob_ptr;
1102 unsigned short *mask_ptr;
1104 vga_ptr = get_vgaptr(page, x, y);
1105 pob_ptr = (unsigned short *)(((unsigned char *)gob->data[image]) + ((pob_y * width) + pob_x));
1106 mask_ptr = (unsigned short *)(((unsigned char *)mask) + ((y * screen_pitch) + (x*bytes_per_pixel)));
1107 for (c1 = 0; c1 < draw_height; c1++) {
1108 for (c2 = 0; c2 < draw_width; c2++) {
1110 if (use_mask == 0 || (use_mask == 1 && colour == 0)) {
1120 pob_ptr += width - c2;
1121 vga_ptr += (screen_width - c2);
1122 mask_ptr += (screen_width - c2);
1125 unsigned int *vga_ptr;
1126 unsigned int *pob_ptr;
1127 unsigned int *mask_ptr;
1129 vga_ptr = get_vgaptr(page, x, y);
1130 pob_ptr = (unsigned int *)(((unsigned char *)gob->data[image]) + ((pob_y * width) + pob_x));
1131 mask_ptr = (unsigned int *)(((unsigned char *)mask) + ((y * screen_pitch) + (x*bytes_per_pixel)));
1132 for (c1 = 0; c1 < draw_height; c1++) {
1133 for (c2 = 0; c2 < draw_width; c2++) {
1135 if (use_mask == 0 || (use_mask == 1 && colour == 0)) {
1145 pob_ptr += width - c2;
1146 vga_ptr += (screen_width - c2);
1147 mask_ptr += (screen_width - c2);
1150 draw_width = ((x+draw_width)>>dirty_block_shift) - (x>>dirty_block_shift) + 1;
1151 draw_height = ((y+draw_height)>>dirty_block_shift) - (y>>dirty_block_shift) + 1;
1152 x >>= dirty_block_shift;
1153 y >>= dirty_block_shift;
1154 while (draw_width--)
1155 for (c1=0; c1<draw_height; c1++)
1156 dirty_blocks[page][(y+c1)*25+(x+draw_width)] = 1;
1160 int pob_width(int image, gob_t *gob)
1164 assert(image<gob->num_images);
1165 return gob->width[image];
1169 int pob_height(int image, gob_t *gob)
1173 assert(image<gob->num_images);
1174 return gob->height[image];
1178 int pob_hs_x(int image, gob_t *gob)
1182 assert(image<gob->num_images);
1183 return gob->hs_x[image];
1187 int pob_hs_y(int image, gob_t *gob)
1191 assert(image<gob->num_images);
1192 return gob->hs_y[image];
1196 int read_pcx(FILE * handle, void *buf, int buf_len, char *pal)
1198 unsigned char *buffer=buf;
1203 fseek(handle, 128, SEEK_CUR);
1205 while (ofs1 < buf_len) {
1207 if ((a & 0xc0) == 0xc0) {
1210 for (c1 = 0; c1 < a && ofs1 < buf_len; c1++)
1211 buffer[ofs1++] = (char) b;
1213 buffer[ofs1++] = (char) a;
1216 fseek(handle, 1, SEEK_CUR);
1217 for (c1 = 0; c1 < 768; c1++)
1218 pal[c1] = fgetc(handle) >> 2;
1225 void register_background(char *pixels, char pal[768])
1231 background_drawn = 0;
1239 for (i=0; i<256; i++)
1240 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));
1241 background = malloc(screen_pitch*screen_height);
1243 Super2xSaI(pixels, JNB_WIDTH, 1, (unsigned char *)background, screen_pitch, bytes_per_pixel, JNB_WIDTH, JNB_HEIGHT, int_pal);
1245 background = malloc(JNB_WIDTH*JNB_HEIGHT);
1247 memcpy(background, pixels, JNB_WIDTH*JNB_HEIGHT);
1251 int register_gob(FILE *handle, gob_t *gob, int len)
1253 unsigned char *gob_data;
1256 gob_data = malloc(len);
1257 fread(gob_data, 1, len, handle);
1259 gob->num_images = (short)((gob_data[0]) + (gob_data[1] << 8));
1261 gob->width = malloc(gob->num_images*sizeof(int));
1262 gob->height = malloc(gob->num_images*sizeof(int));
1263 gob->hs_x = malloc(gob->num_images*sizeof(int));
1264 gob->hs_y = malloc(gob->num_images*sizeof(int));
1265 gob->data = malloc(gob->num_images*sizeof(void *));
1266 gob->orig_data = malloc(gob->num_images*sizeof(void *));
1267 for (i=0; i<gob->num_images; i++) {
1271 offset = (gob_data[i*4+2]) + (gob_data[i*4+3] << 8) + (gob_data[i*4+4] << 16) + (gob_data[i*4+5] << 24);
1273 gob->width[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1274 gob->height[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1275 gob->hs_x[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1276 gob->hs_y[i] = (short)((gob_data[offset]) + (gob_data[offset+1] << 8)); offset += 2;
1278 image_size = gob->width[i] * gob->height[i];
1279 gob->orig_data[i] = malloc(image_size);
1280 memcpy(gob->orig_data[i], &gob_data[offset], image_size);
1282 image_size = gob->width[i] * gob->height[i] * 4 * bytes_per_pixel;
1283 gob->data[i] = malloc(image_size);
1285 gob->data[i] = (unsigned short *)gob->orig_data[i];
1293 void recalculate_gob(gob_t *gob, char pal[768])
1301 for (i=1; i<256; i++) {
1302 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));
1303 if (int_pal[i] == 0)
1304 int_pal[i] = SDL_MapRGB(jnb_surface->format, 8, 8, 8);
1308 for (i=0; i<gob->num_images; i++) {
1309 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);
1313 void register_mask(void *pixels)
1325 for (i=1; i<256; i++)
1326 int_pal[i] = 0xffffffff;
1327 mask = malloc(screen_pitch*screen_height);
1329 Scale2x(pixels, JNB_WIDTH, 1, (unsigned char *)mask, screen_pitch, bytes_per_pixel, JNB_WIDTH, JNB_HEIGHT, int_pal);
1331 mask = malloc(JNB_WIDTH*JNB_HEIGHT);
1333 memcpy(mask, pixels, JNB_WIDTH*JNB_HEIGHT);