| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "common.h" |
| 13 |
#include "definitions.h" |
| 14 |
#include "glk.h" |
| 15 |
#include "graphics.h" |
| 16 |
|
| 17 |
static uint8_t *screenmem = NULL; |
| 18 |
static uint8_t lobyte = 0, hibyte = 0; |
| 19 |
|
| 20 |
void ClearApple2ScreenMem(void) |
| 21 |
{ |
| 22 |
if (screenmem) |
| 23 |
memset(screenmem, 0, 0x2000); |
| 24 |
} |
| 25 |
|
| 26 |
static void AdvanceScreenByte(void) |
| 27 |
{ |
| 28 |
lobyte = ((y & 0xc0) >> 2 | (y & 0xc0)) >> 1 | (y & 8) << 4; |
| 29 |
hibyte = ((y & 7) << 1 | (uint8_t)(y << 2) >> 7) << 1 | (uint8_t)(y << 3) >> 7; |
| 30 |
} |
| 31 |
|
| 32 |
static void PutByte(uint8_t work, uint8_t work2) |
| 33 |
{ |
| 34 |
AdvanceScreenByte(); |
| 35 |
screenmem[hibyte * 0x100 + lobyte + x] = work; |
| 36 |
y++; |
| 37 |
AdvanceScreenByte(); |
| 38 |
screenmem[hibyte * 0x100 + lobyte + x] = work2; |
| 39 |
y++; |
| 40 |
if (y - 2 >= ylen) { |
| 41 |
if (x == xlen) { |
| 42 |
return; |
| 43 |
} |
| 44 |
x++; |
| 45 |
y = yoff; |
| 46 |
} |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
void DrawApple2ImageFromVideoMem(void); |
| 51 |
|
| 52 |
int DrawApple2ImageFromData(uint8_t *ptr, size_t datasize) |
| 53 |
{ |
| 54 |
int work, work2; |
| 55 |
int c; |
| 56 |
int i; |
| 57 |
size_t size; |
| 58 |
|
| 59 |
uint8_t *origptr = ptr; |
| 60 |
|
| 61 |
if (screenmem == NULL) { |
| 62 |
screenmem = MemAlloc(0x2000); |
| 63 |
ClearApple2ScreenMem(); |
| 64 |
} |
| 65 |
|
| 66 |
x = 0; |
| 67 |
y = 0; |
| 68 |
|
| 69 |
work = *ptr++; |
| 70 |
size = work + (*ptr++ * 256); |
| 71 |
|
| 72 |
|
| 73 |
xoff = *ptr++; |
| 74 |
yoff = *ptr++; |
| 75 |
x = xoff; |
| 76 |
y = yoff; |
| 77 |
|
| 78 |
|
| 79 |
xlen = *ptr++; |
| 80 |
ylen = *ptr++; |
| 81 |
|
| 82 |
glui32 curheight, curwidth; |
| 83 |
glk_window_get_size(Graphics, &curwidth, &curheight); |
| 84 |
|
| 85 |
if (yoff == 0 && (LastImgType == IMG_ROOM || LastImgType == IMG_SPECIAL)) { |
| 86 |
ImageHeight = ylen + 2; |
| 87 |
ImageWidth = xlen * 8 - 32; |
| 88 |
} |
| 89 |
|
| 90 |
int optimal_height = ImageHeight * pixel_size; |
| 91 |
if (curheight != optimal_height && ImageWidth * pixel_size <= curwidth) { |
| 92 |
x_offset = (curwidth - ImageWidth * pixel_size) / 2; |
| 93 |
right_margin = (ImageWidth * pixel_size) + x_offset; |
| 94 |
winid_t parent = glk_window_get_parent(Graphics); |
| 95 |
if (parent) { |
| 96 |
glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed, |
| 97 |
optimal_height, NULL); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
while (ptr - origptr < size) { |
| 102 |
|
| 103 |
c = *ptr++; |
| 104 |
|
| 105 |
if ((c & 0x80) == 0x80) { |
| 106 |
c &= 0x7f; |
| 107 |
work = *ptr++; |
| 108 |
work2 = *ptr++; |
| 109 |
for (i = 0; i < c + 1 && ptr - origptr < size; i++) { |
| 110 |
if (hibyte * 0x100 + lobyte + x > 0x1fff) |
| 111 |
return 0; |
| 112 |
PutByte(work, work2); |
| 113 |
if (x > xlen || y > ylen) { |
| 114 |
return 1; |
| 115 |
} |
| 116 |
} |
| 117 |
} else { |
| 118 |
|
| 119 |
|
| 120 |
for (i = 0; i < c + 1 && ptr - origptr < size; i++) { |
| 121 |
work = *ptr++; |
| 122 |
work2 = *ptr++; |
| 123 |
if (hibyte * 0x100 + lobyte + x > 0x1fff) |
| 124 |
return 0; |
| 125 |
PutByte(work, work2); |
| 126 |
if (x > xlen || y > ylen) { |
| 127 |
return 1; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
return 1; |
| 133 |
} |
| 134 |
|
| 135 |
static void PutApplePixel(glsi32 xpos, glsi32 ypos, glui32 color) |
| 136 |
{ |
| 137 |
xpos = xpos * pixel_size; |
| 138 |
|
| 139 |
if (upside_down) |
| 140 |
xpos = 280 * pixel_size - xpos; |
| 141 |
xpos += x_offset; |
| 142 |
|
| 143 |
if (xpos < x_offset || xpos >= right_margin) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
ypos = ypos * pixel_size; |
| 148 |
if (upside_down) |
| 149 |
ypos = 157 * pixel_size - ypos; |
| 150 |
ypos += y_offset; |
| 151 |
|
| 152 |
glk_window_fill_rect(Graphics, color, xpos, |
| 153 |
ypos, pixel_size, pixel_size); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
#define BLACK 0 |
| 158 |
#define PURPLE 0xd53ef9 |
| 159 |
#define BLUE 0x458ff7 |
| 160 |
#define ORANGE 0xd7762c |
| 161 |
#define GREEN 0x64d440 |
| 162 |
#define WHITE 0xffffff |
| 163 |
|
| 164 |
static const int32_t hires_artifact_color_table[] = |
| 165 |
{ |
| 166 |
BLACK, PURPLE, GREEN, WHITE, |
| 167 |
BLACK, BLUE, ORANGE, WHITE |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
static int32_t *m_hires_artifact_map = NULL; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
static void generate_artifact_map(void) |
| 176 |
{ |
| 177 |
|
| 178 |
int i, j; |
| 179 |
uint16_t c; |
| 180 |
|
| 181 |
|
| 182 |
m_hires_artifact_map = MemAlloc(sizeof(int32_t) * 8 * 2 * 2); |
| 183 |
|
| 184 |
|
| 185 |
for (i = 0; i < 8; i++) { |
| 186 |
for (j = 0; j < 2; j++) { |
| 187 |
if (i & 0x02) { |
| 188 |
if ((i & 0x05) != 0) |
| 189 |
c = 3; |
| 190 |
else |
| 191 |
c = j ? 2 : 1; |
| 192 |
} else { |
| 193 |
if ((i & 0x05) == 0x05) |
| 194 |
c = j ? 1 : 2; |
| 195 |
else |
| 196 |
c = 0; |
| 197 |
} |
| 198 |
m_hires_artifact_map[0 + j * 8 + i] = hires_artifact_color_table[(c + 0) % 8]; |
| 199 |
m_hires_artifact_map[16 + j * 8 + i] = hires_artifact_color_table[(c + 4) % 8]; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void DrawApple2ImageFromVideoMem(void) |
| 205 |
{ |
| 206 |
if (m_hires_artifact_map == NULL) |
| 207 |
generate_artifact_map(); |
| 208 |
|
| 209 |
int32_t *artifact_map_ptr; |
| 210 |
|
| 211 |
uint8_t const *const vram = screenmem; |
| 212 |
uint8_t vram_row[42]; |
| 213 |
vram_row[0] = 0; |
| 214 |
vram_row[41] = 0; |
| 215 |
|
| 216 |
for (int row = 0; row < 192; row++) { |
| 217 |
for (int col = 0; col < 40; col++) { |
| 218 |
int const offset = ((((row / 8) & 0x07) << 7) | (((row / 8) & 0x18) * 5 + col)) | ((row & 7) << 10); |
| 219 |
vram_row[1 + col] = vram[offset]; |
| 220 |
} |
| 221 |
|
| 222 |
int pixpos = 0; |
| 223 |
|
| 224 |
for (int col = 0; col < 40; col++) { |
| 225 |
uint32_t w = (((uint32_t)vram_row[col + 0] & 0x7f) << 0) |
| 226 |
| (((uint32_t)vram_row[col + 1] & 0x7f) << 7) |
| 227 |
| (((uint32_t)vram_row[col + 2] & 0x7f) << 14); |
| 228 |
|
| 229 |
artifact_map_ptr = &m_hires_artifact_map[((vram_row[col + 1] & 0x80) >> 7) * 16]; |
| 230 |
|
| 231 |
for (int b = 0; b < 7; b++) { |
| 232 |
int32_t const v = artifact_map_ptr[((w >> (b + 7 - 1)) & 0x07) | (((b ^ col) & 0x01) << 3)]; |
| 233 |
PutApplePixel(pixpos++, row, v); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |