Split game code and tests into focused modules
This commit is contained in:
20 files changed
+2611
-2431
No files matched your search
+206
@@ -0,0 +1,206 @@
|
||||
#include "game.h"
|
||||
|
||||
#ifndef GAME_TEST
|
||||
/* Never erase a character on the visible screen. Finish drawing in RAM. */
|
||||
u16 frame_buffer[WIDTH * HEIGHT];
|
||||
#endif
|
||||
|
||||
u16 dirty_first[HEIGHT];
|
||||
u16 dirty_end[HEIGHT];
|
||||
|
||||
void reset_dirty_rows(void)
|
||||
{
|
||||
for (int y = 0; y < HEIGHT; ++y) {
|
||||
dirty_first[y] = WIDTH;
|
||||
dirty_end[y] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void mark_dirty(int x, int y, int w, int h)
|
||||
{
|
||||
int left = x < 0 ? 0 : x;
|
||||
int right = x + w > WIDTH ? WIDTH : x + w;
|
||||
int top = y < 0 ? 0 : y;
|
||||
int bottom = y + h > HEIGHT ? HEIGHT : y + h;
|
||||
if (left >= right || top >= bottom) return;
|
||||
for (int row = top; row < bottom; ++row) {
|
||||
if (left < dirty_first[row]) dirty_first[row] = left;
|
||||
if (right > dirty_end[row]) dirty_end[row] = right;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef GAME_TEST
|
||||
unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
|
||||
#endif
|
||||
|
||||
void rectangle(int x, int y, int w, int h, u16 color)
|
||||
{
|
||||
#ifdef GAME_TEST
|
||||
++rectangle_calls;
|
||||
#endif
|
||||
mark_dirty(x, y, w, h);
|
||||
for (int row = y; row < y + h; ++row) {
|
||||
for (int col = x; col < x + w; ++col) {
|
||||
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
|
||||
SCREEN[row * WIDTH + col] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Tiny original 3-by-5 alphabet: one row of bits per letter. */
|
||||
static const unsigned char alphabet[26][5] = {
|
||||
{2,5,7,5,5}, {6,5,6,5,6}, {3,4,4,4,3}, {6,5,5,5,6},
|
||||
{7,4,6,4,7}, {7,4,6,4,4}, {3,4,5,5,3}, {5,5,7,5,5},
|
||||
{7,2,2,2,7}, {1,1,1,5,2}, {5,5,6,5,5}, {4,4,4,4,7},
|
||||
{5,7,7,5,5}, {5,7,7,7,5}, {2,5,5,5,2}, {6,5,6,4,4},
|
||||
{2,5,5,3,1}, {6,5,6,5,5}, {3,4,2,1,6}, {7,2,2,2,2},
|
||||
{5,5,5,5,7}, {5,5,5,5,2}, {5,5,7,7,5}, {5,5,2,5,5},
|
||||
{5,5,2,2,2}, {7,1,2,4,7}
|
||||
};
|
||||
static const unsigned char digit_font[10][5] = {
|
||||
{7,5,5,5,7}, {2,6,2,2,7}, {6,1,2,4,7}, {6,1,2,1,6}, {5,5,7,1,1},
|
||||
{7,4,6,1,6}, {3,4,7,5,7}, {7,1,2,2,2}, {7,5,7,5,7}, {7,5,7,1,6}
|
||||
};
|
||||
|
||||
void text(int x, int y, const char *message, u16 color)
|
||||
{
|
||||
for (; *message; ++message, x += 4) {
|
||||
const unsigned char *letter;
|
||||
if (*message >= 'A' && *message <= 'Z') letter = alphabet[*message - 'A'];
|
||||
else if (*message >= '0' && *message <= '9') letter = digit_font[*message - '0'];
|
||||
else continue;
|
||||
for (int row = 0; row < 5; ++row)
|
||||
for (int col = 0; col < 3; ++col)
|
||||
if (letter[row] & (4 >> col))
|
||||
rectangle(x + col, y + row, 1, 1, color);
|
||||
}
|
||||
}
|
||||
|
||||
/* Larger, easy-to-read letters for the RPG conversation window. */
|
||||
TEST_INTERNAL const unsigned char large_alphabet[26][7] = {
|
||||
{14,17,17,31,17,17,17}, {30,17,17,30,17,17,30},
|
||||
{14,17,16,16,16,17,14}, {30,17,17,17,17,17,30},
|
||||
{31,16,16,30,16,16,31}, {31,16,16,30,16,16,16},
|
||||
{14,17,16,23,17,17,14}, {17,17,17,31,17,17,17},
|
||||
{14,4,4,4,4,4,14}, {7,2,2,2,18,18,12},
|
||||
{17,18,20,24,20,18,17}, {16,16,16,16,16,16,31},
|
||||
{17,27,21,21,17,17,17}, {17,25,21,19,17,17,17},
|
||||
{14,17,17,17,17,17,14}, {30,17,17,30,16,16,16},
|
||||
{14,17,17,17,21,18,13}, {30,17,17,30,20,18,17},
|
||||
{15,16,16,14,1,1,30}, {31,4,4,4,4,4,4},
|
||||
{17,17,17,17,17,17,14}, {17,17,17,17,17,10,4},
|
||||
{17,17,17,21,21,21,10}, {17,17,10,4,10,17,17},
|
||||
{17,17,10,4,4,4,4}, {31,1,2,4,8,16,31}
|
||||
};
|
||||
|
||||
void large_text(int x, int y, const char *message, u16 color)
|
||||
{
|
||||
for (; *message; ++message, x += 6) {
|
||||
if (*message < 'A' || *message > 'Z') continue;
|
||||
const unsigned char *letter = large_alphabet[*message - 'A'];
|
||||
for (int row = 0; row < 7; ++row)
|
||||
for (int col = 0; col < 5; ++col)
|
||||
if (letter[row] & (16 >> col))
|
||||
rectangle(x + col, y + row, 1, 1, color);
|
||||
}
|
||||
}
|
||||
|
||||
enum { SPRITE_SIZE = 24, SPRITE_CACHE_SIZE = 7 };
|
||||
struct DecodedSprite {
|
||||
const char *const *art;
|
||||
unsigned char width, rows;
|
||||
unsigned char first[SPRITE_SIZE], end[SPRITE_SIZE];
|
||||
unsigned char pixels[SPRITE_SIZE * SPRITE_SIZE];
|
||||
};
|
||||
static struct DecodedSprite sprite_cache[SPRITE_CACHE_SIZE];
|
||||
static unsigned sprite_cache_next;
|
||||
|
||||
static const struct DecodedSprite *decode_sprite(const char *const *art, int width, int rows)
|
||||
{
|
||||
for (int i = 0; i < SPRITE_CACHE_SIZE; ++i)
|
||||
if (sprite_cache[i].art == art && sprite_cache[i].width == width &&
|
||||
sprite_cache[i].rows == rows) return &sprite_cache[i];
|
||||
struct DecodedSprite *decoded = &sprite_cache[sprite_cache_next];
|
||||
sprite_cache_next = (sprite_cache_next + 1) % SPRITE_CACHE_SIZE;
|
||||
decoded->art = art;
|
||||
decoded->width = (unsigned char)width;
|
||||
decoded->rows = (unsigned char)rows;
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
decoded->first[row] = (unsigned char)width;
|
||||
decoded->end[row] = 0;
|
||||
for (int col = 0; col < width; ++col) {
|
||||
unsigned char encoded = 0;
|
||||
if (art[row][col] != '.') {
|
||||
char pixel = art[row][col];
|
||||
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
|
||||
/* Zero means transparent; palette entry zero is still usable. */
|
||||
encoded = (unsigned char)(index + 1);
|
||||
if (decoded->first[row] == width) decoded->first[row] = (unsigned char)col;
|
||||
decoded->end[row] = (unsigned char)(col + 1);
|
||||
}
|
||||
decoded->pixels[row * SPRITE_SIZE + col] = encoded;
|
||||
}
|
||||
}
|
||||
#ifdef GAME_TEST
|
||||
++sprite_decodes;
|
||||
#endif
|
||||
return decoded;
|
||||
}
|
||||
|
||||
/* Decode once, then write clipped rows directly into the RAM frame buffer. */
|
||||
void blit_sprite(int x, int y, const char *const *art, int width, int rows,
|
||||
const u16 *palette, int scale, int mirrored)
|
||||
{
|
||||
if (width <= 0 || width > SPRITE_SIZE || rows <= 0 || rows > SPRITE_SIZE || scale <= 0)
|
||||
return;
|
||||
if (x >= WIDTH || y >= HEIGHT || x + width * scale <= 0 || y + rows * scale <= 0) return;
|
||||
const struct DecodedSprite *decoded = decode_sprite(art, width, rows);
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
int first = decoded->first[row], end = decoded->end[row];
|
||||
if (first >= end) continue;
|
||||
int top = y + row * scale, bottom = top + scale;
|
||||
if (bottom <= 0 || top >= HEIGHT) continue;
|
||||
int left = x + (mirrored ? width - end : first) * scale;
|
||||
int right = x + (mirrored ? width - first : end) * scale;
|
||||
if (left < 0) left = 0;
|
||||
if (right > WIDTH) right = WIDTH;
|
||||
if (top < 0) top = 0;
|
||||
if (bottom > HEIGHT) bottom = HEIGHT;
|
||||
if (left >= right) continue;
|
||||
mark_dirty(left, top, right - left, bottom - top);
|
||||
#ifdef GAME_TEST
|
||||
++sprite_row_marks;
|
||||
#endif
|
||||
const unsigned char *source = &decoded->pixels[row * SPRITE_SIZE];
|
||||
if (scale == 1) {
|
||||
u16 *destination = &SCREEN[top * WIDTH + left];
|
||||
int col = mirrored ? width - 1 - (left - x) : left - x;
|
||||
int advance = mirrored ? -1 : 1;
|
||||
for (int offset = 0; offset < right - left; ++offset, col += advance) {
|
||||
unsigned char pixel = source[col];
|
||||
if (pixel) destination[offset] = palette[pixel - 1];
|
||||
}
|
||||
} else {
|
||||
for (int col = first; col < end; ++col) {
|
||||
unsigned char pixel = source[col];
|
||||
if (!pixel) continue;
|
||||
u16 color = palette[pixel - 1];
|
||||
int start = x + (mirrored ? width - 1 - col : col) * scale;
|
||||
int finish = start + scale;
|
||||
if (start < left) start = left;
|
||||
if (finish > right) finish = right;
|
||||
for (int screen_y = top; screen_y < bottom; ++screen_y) {
|
||||
u16 *destination = &SCREEN[screen_y * WIDTH];
|
||||
for (int screen_x = start; screen_x < finish; ++screen_x)
|
||||
destination[screen_x] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
|
||||
int mirrored)
|
||||
{
|
||||
blit_sprite(x, y, art, 16, rows, palette, 1, mirrored);
|
||||
}
|
||||
Reference in new issue
Block a user