| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "picload.h" |
| 20 | |
| 21 | #include "file.h" |
| 22 | #include "opentyr.h" |
| 23 | #include "palette.h" |
| 24 | #include "pcxmast.h" |
| 25 | #include "video.h" |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | void JE_loadPic(SDL_Surface *screen, JE_byte PCXnumber, JE_boolean storepal) |
| 31 | { |
| 32 | PCXnumber--; |
| 33 | |
| 34 | FILE *f = dir_fopen_die(data_dir(), "tyrian.pic", "rb"); |
| 35 | |
| 36 | static bool first = true; |
| 37 | if (first) |
| 38 | { |
| 39 | first = false; |
| 40 | |
| 41 | Uint16 temp; |
| 42 | fread_u16_die(&temp, 1, f); |
| 43 | |
| 44 | fread_s32_die(pcxpos, PCX_NUM, f); |
| 45 | pcxpos[PCX_NUM] = ftell_eof(f); |
| 46 | } |
| 47 | |
| 48 | unsigned int size = pcxpos[PCXnumber + 1] - pcxpos[PCXnumber]; |
| 49 | Uint8 *buffer = malloc(size); |
| 50 | |
| 51 | fseek(f, pcxpos[PCXnumber], SEEK_SET); |
| 52 | fread_u8_die(buffer, size, f); |
| 53 | fclose(f); |
| 54 | |
| 55 | Uint8 *p = buffer; |
| 56 | Uint8 *s; /* screen pointer, 8-bit specific */ |
| 57 | |
| 58 | s = (Uint8 *)screen->pixels; |
| 59 | |
| 60 | for (int i = 0; i < 320 * 200; ) |
| 61 | { |
| 62 | if ((*p & 0xc0) == 0xc0) |
| 63 | { |
| 64 | i += (*p & 0x3f); |
| 65 | memset(s, *(p + 1), (*p & 0x3f)); |
| 66 | s += (*p & 0x3f); p += 2; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | i++; |
| 71 | *s = *p; |
| 72 | s++; p++; |
| 73 | } |
| 74 | if (i && (i % 320 == 0)) |
| 75 | { |
| 76 | s += screen->pitch - 320; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | free(buffer); |
| 81 | |
| 82 | memcpy(colors, palettes[pcxpal[PCXnumber]], sizeof(colors)); |
| 83 | |
| 84 | if (storepal) |
| 85 | set_palette(colors, 0, 255); |
| 86 | } |