| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "sagagraphics.h" |
| 13 |
#include "scott.h" |
| 14 |
|
| 15 |
#include "apple2draw.h" |
| 16 |
|
| 17 |
extern int x, y, xlen, ylen, xoff, yoff, size; |
| 18 |
|
| 19 |
uint8_t *descrambletable = NULL; |
| 20 |
|
| 21 |
static uint8_t *screenmem = NULL; |
| 22 |
static uint8_t lobyte = 0, hibyte = 0; |
| 23 |
|
| 24 |
void ClearApple2ScreenMem(void) |
| 25 |
{ |
| 26 |
if (!screenmem) |
| 27 |
return; |
| 28 |
memset(screenmem, 0, 0x2000); |
| 29 |
} |
| 30 |
|
| 31 |
static void AdvanceScreenByte(void) |
| 32 |
{ |
| 33 |
lobyte = ((y & 0xc0) >> 2 | (y & 0xc0)) >> 1 | (y & 8) << 4; |
| 34 |
hibyte = ((y & 7) << 1 | (uint8_t)(y << 2) >> 7) << 1 | (uint8_t)(y << 3) >> 7; |
| 35 |
} |
| 36 |
|
| 37 |
static int PutByte(uint8_t work, uint8_t work2) |
| 38 |
{ |
| 39 |
AdvanceScreenByte(); |
| 40 |
if (hibyte * 0x100 + lobyte + x > 0x1fff) |
| 41 |
return 0; |
| 42 |
screenmem[hibyte * 0x100 + lobyte + x] = work; |
| 43 |
y++; |
| 44 |
AdvanceScreenByte(); |
| 45 |
if (hibyte * 0x100 + lobyte + x > 0x1fff) |
| 46 |
return 0; |
| 47 |
screenmem[hibyte * 0x100 + lobyte + x] = work2; |
| 48 |
y++; |
| 49 |
if (y - 2 >= ylen) { |
| 50 |
if (x == xlen) { |
| 51 |
return 1; |
| 52 |
} |
| 53 |
x++; |
| 54 |
y = yoff; |
| 55 |
} |
| 56 |
return 1; |
| 57 |
} |
| 58 |
|
| 59 |
uint16_t CalcScreenAddress(uint8_t ypos) |
| 60 |
{ |
| 61 |
if (0xc0 + ypos >= 0x182) |
| 62 |
return 0; |
| 63 |
uint16_t result = descrambletable[ypos] + descrambletable[0xc0 + ypos] * 0x100 - 0x2000; |
| 64 |
if (result > 0x1fff) |
| 65 |
return 0; |
| 66 |
return result; |
| 67 |
} |
| 68 |
|
| 69 |
int DrawScrambledApple2Image(uint8_t *ptr, size_t datasize) |
| 70 |
{ |
| 71 |
|
| 72 |
if (screenmem == NULL) { |
| 73 |
screenmem = MemAlloc(0x2000); |
| 74 |
ClearApple2ScreenMem(); |
| 75 |
} |
| 76 |
|
| 77 |
uint8_t *origptr = ptr; |
| 78 |
|
| 79 |
uint8_t *end_of_data = ptr + datasize; |
| 80 |
|
| 81 |
uint8_t xoff = *ptr++; |
| 82 |
uint8_t yoff = *ptr++; |
| 83 |
uint8_t width = *ptr++; |
| 84 |
uint8_t height = *ptr++; |
| 85 |
|
| 86 |
uint8_t xpos = xoff; |
| 87 |
uint8_t ypos = yoff; |
| 88 |
|
| 89 |
if (ypos > 0xc0) { |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
while (1) { |
| 94 |
uint8_t repeats = 1; |
| 95 |
|
| 96 |
if (ptr >= end_of_data - 1) { |
| 97 |
return ptr - origptr; |
| 98 |
} |
| 99 |
|
| 100 |
uint8_t work1 = *ptr++; |
| 101 |
if (work1 == 0) { |
| 102 |
if (ptr > end_of_data - 3) { |
| 103 |
return ptr - origptr; |
| 104 |
} |
| 105 |
repeats = *ptr++; |
| 106 |
work1 = *ptr++; |
| 107 |
} |
| 108 |
|
| 109 |
uint8_t work2 = *ptr++; |
| 110 |
|
| 111 |
if (repeats == 0) { |
| 112 |
repeats = 1; |
| 113 |
} |
| 114 |
|
| 115 |
while (repeats--) { |
| 116 |
screenmem[CalcScreenAddress(ypos) + xpos] = work1; |
| 117 |
ypos++; |
| 118 |
screenmem[CalcScreenAddress(ypos) + xpos] = work2; |
| 119 |
ypos++; |
| 120 |
if (height <= ypos) { |
| 121 |
ypos = yoff; |
| 122 |
xpos++; |
| 123 |
if (width <= xpos) { |
| 124 |
return ptr - origptr; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
int DrawApple2ImageFromData(uint8_t *ptr, size_t datasize) |
| 132 |
{ |
| 133 |
if (ptr == NULL) |
| 134 |
return 0; |
| 135 |
|
| 136 |
if (screenmem == NULL) { |
| 137 |
screenmem = MemAlloc(0x2000); |
| 138 |
ClearApple2ScreenMem(); |
| 139 |
} |
| 140 |
|
| 141 |
if (descrambletable) { |
| 142 |
int result = DrawScrambledApple2Image(ptr, datasize); |
| 143 |
debug_print("Stopped drawing at offset %x\n", result); |
| 144 |
return (result > 0); |
| 145 |
} |
| 146 |
|
| 147 |
int work, work2; |
| 148 |
int c; |
| 149 |
int i; |
| 150 |
|
| 151 |
uint8_t *origptr = ptr; |
| 152 |
|
| 153 |
int countflag = (CurrentGame == COUNT_US); |
| 154 |
|
| 155 |
x = 0; |
| 156 |
y = 0; |
| 157 |
|
| 158 |
if (!countflag) { |
| 159 |
|
| 160 |
xoff = *ptr++; |
| 161 |
yoff = *ptr++; |
| 162 |
x = xoff; |
| 163 |
y = yoff; |
| 164 |
|
| 165 |
|
| 166 |
xlen = *ptr++; |
| 167 |
ylen = *ptr++; |
| 168 |
} else { |
| 169 |
xoff = 0; |
| 170 |
yoff = 0; |
| 171 |
xlen = 0; |
| 172 |
ylen = 0; |
| 173 |
while (xlen == 0 && ylen == 0) { |
| 174 |
xlen = *ptr++; |
| 175 |
ylen = *ptr++; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
while (xlen == 0 && ylen == 0) { |
| 180 |
xlen = *ptr++; |
| 181 |
ylen = *ptr++; |
| 182 |
} |
| 183 |
|
| 184 |
debug_print("xlen: %d ylen: %d\n", xlen, ylen); |
| 185 |
|
| 186 |
while (ptr - origptr < datasize - 2) { |
| 187 |
|
| 188 |
c = *ptr++; |
| 189 |
|
| 190 |
if ((c & 0x80) == 0x80 || countflag) { |
| 191 |
if (countflag) { |
| 192 |
c -= 1; |
| 193 |
} else { |
| 194 |
c &= 0x7f; |
| 195 |
} |
| 196 |
work = *ptr++; |
| 197 |
work2 = *ptr++; |
| 198 |
for (i = 0; i < c + 1 && ptr - origptr < datasize; i++) { |
| 199 |
if (PutByte(work, work2) == 0) { |
| 200 |
debug_print("Reached end of screen memory at offset %lx\n", ptr - origptr); |
| 201 |
return 1; |
| 202 |
} |
| 203 |
if (x > xlen || y > ylen) { |
| 204 |
debug_print("Reached end of image dimensions at offset %lx\n", ptr - origptr); |
| 205 |
return 1; |
| 206 |
} |
| 207 |
} |
| 208 |
} else { |
| 209 |
|
| 210 |
|
| 211 |
for (i = 0; i < c + 1 && ptr - origptr < datasize - 1; i++) { |
| 212 |
work = *ptr++; |
| 213 |
work2 = *ptr++; |
| 214 |
if (PutByte(work, work2) == 0) { |
| 215 |
debug_print("Reached end of screen memory at offset %lx\n", ptr - origptr); |
| 216 |
return 1; |
| 217 |
} |
| 218 |
if (x > xlen || y > ylen) { |
| 219 |
debug_print("Reached end of image dimensions at offset %lx\n", ptr - origptr); |
| 220 |
return 1; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
int DrawApple2Image(USImage *image) |
| 229 |
{ |
| 230 |
if (image->usage == IMG_ROOM) |
| 231 |
ClearApple2ScreenMem(); |
| 232 |
DrawApple2ImageFromData(image->imagedata, image->datasize); |
| 233 |
debug_print("Drawing image with index %d, usage %d\n", image->index, image->usage); |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
static void PutApplePixel(glsi32 xpos, glsi32 ypos, glui32 color) |
| 238 |
{ |
| 239 |
xpos = xpos * pixel_size; |
| 240 |
xpos += x_offset; |
| 241 |
|
| 242 |
ypos = ypos * pixel_size; |
| 243 |
ypos += y_offset; |
| 244 |
|
| 245 |
glk_window_fill_rect(Graphics, color, xpos, |
| 246 |
ypos, pixel_size, pixel_size); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
#define BLACK 0 |
| 252 |
#define PURPLE 0xD53EF9 |
| 253 |
#define BLUE 0x458ff7 |
| 254 |
#define ORANGE 0xd7762c |
| 255 |
#define GREEN 0x64d440 |
| 256 |
#define WHITE 0xffffff |
| 257 |
|
| 258 |
static const int32_t hires_artifact_color_table[] = { |
| 259 |
BLACK, PURPLE, GREEN, WHITE, |
| 260 |
BLACK, BLUE, ORANGE, WHITE |
| 261 |
}; |
| 262 |
|
| 263 |
static int32_t *m_hires_artifact_map = NULL; |
| 264 |
|
| 265 |
static void generate_artifact_map(void) |
| 266 |
{ |
| 267 |
|
| 268 |
int i, j; |
| 269 |
uint16_t c; |
| 270 |
|
| 271 |
|
| 272 |
m_hires_artifact_map = MemAlloc(sizeof(int32_t) * 8 * 2 * 2); |
| 273 |
|
| 274 |
|
| 275 |
for (i = 0; i < 8; i++) { |
| 276 |
for (j = 0; j < 2; j++) { |
| 277 |
if (i & 0x02) { |
| 278 |
if ((i & 0x05) != 0) |
| 279 |
c = 3; |
| 280 |
else |
| 281 |
c = j ? 2 : 1; |
| 282 |
} else { |
| 283 |
if ((i & 0x05) == 0x05) |
| 284 |
c = j ? 1 : 2; |
| 285 |
else |
| 286 |
c = 0; |
| 287 |
} |
| 288 |
m_hires_artifact_map[0 + j * 8 + i] = hires_artifact_color_table[(c + 0) % 8]; |
| 289 |
m_hires_artifact_map[16 + j * 8 + i] = hires_artifact_color_table[(c + 4) % 8]; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
void DrawApple2ImageFromVideoMem(void) |
| 295 |
{ |
| 296 |
if (m_hires_artifact_map == NULL) |
| 297 |
generate_artifact_map(); |
| 298 |
|
| 299 |
int32_t *artifact_map_ptr; |
| 300 |
|
| 301 |
uint8_t const *const vram = screenmem; |
| 302 |
uint8_t vram_row[42]; |
| 303 |
vram_row[0] = 0; |
| 304 |
vram_row[41] = 0; |
| 305 |
|
| 306 |
for (int row = 0; row < ImageHeight; row++) { |
| 307 |
for (int col = 0; col < 40; col++) { |
| 308 |
int const offset = ((((row / 8) & 0x07) << 7) | (((row / 8) & 0x18) * 5 + col)) | ((row & 7) << 10); |
| 309 |
vram_row[1 + col] = vram[offset]; |
| 310 |
} |
| 311 |
|
| 312 |
int pixpos = 0; |
| 313 |
|
| 314 |
for (int col = 0; col < 40; col++) { |
| 315 |
uint32_t w = (((uint32_t)vram_row[col + 0] & 0x7f) << 0) |
| 316 |
| (((uint32_t)vram_row[col + 1] & 0x7f) << 7) |
| 317 |
| (((uint32_t)vram_row[col + 2] & 0x7f) << 14); |
| 318 |
|
| 319 |
artifact_map_ptr = &m_hires_artifact_map[((vram_row[col + 1] & 0x80) >> 7) * 16]; |
| 320 |
|
| 321 |
for (int b = 0; b < 7; b++) { |
| 322 |
int32_t const v = artifact_map_ptr[((w >> (b + 7 - 1)) & 0x07) | (((b ^ col) & 0x01) << 3)]; |
| 323 |
PutApplePixel(pixpos++, row, v); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |