joeylib2/examples/keys/keys.c

162 lines
4.7 KiB
C

// Visual keyboard demo: one square per JoeyKeyE, filled bright when
// the key is held and dim otherwise. Press ESC to quit.
//
// The first paint fills every cell and does one full-surface present.
// Thereafter the loop compares each cell against its previous drawn
// state and only redraws + presents the cells that changed. Full-
// surface chunky-to-planar conversion is expensive on 68000-class
// hardware (hundreds of milliseconds for 320x200x4bpp on Amiga / ST);
// per-cell rect presents keep the response tight regardless of how
// fast the host can convert a full frame.
#include <stdio.h>
#include <joey/joey.h>
#define GRID_COLS 10
#define GRID_ROWS 6
#define CELL_W 28
#define CELL_H 28
#define GAP 4
#define MARGIN_X 2
#define MARGIN_Y 6
#define COLOR_BACKGROUND 0
#define COLOR_UNLIT 1
#define COLOR_LIT 2
static void buildPalette(SurfaceT *screen);
static void drawCell(SurfaceT *screen, int16_t col, int16_t row, bool lit);
static void drawAllCells(SurfaceT *screen);
static void presentChangedCells(SurfaceT *screen);
// Keys laid out row-by-row. KEY_NONE cells stay blank. Shape roughly
// resembles a real keyboard (top number row, then QWERTY rows, then a
// cluster of modifiers / arrows / function keys).
static const JoeyKeyE gKeyGrid[GRID_ROWS][GRID_COLS] = {
{ KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0 },
{ KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P },
{ KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_BACKSPACE },
{ KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_LSHIFT, KEY_RSHIFT, KEY_TAB },
{ KEY_SPACE, KEY_ESCAPE, KEY_RETURN, KEY_LCTRL, KEY_LALT, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_NONE },
{ KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10 }
};
static bool gCellLit[GRID_ROWS][GRID_COLS];
static void buildPalette(SurfaceT *screen) {
uint16_t colors[SURFACE_COLORS_PER_PALETTE];
uint16_t i;
for (i = 0; i < SURFACE_COLORS_PER_PALETTE; i++) {
colors[i] = 0x0000;
}
colors[COLOR_BACKGROUND] = 0x0000; // black
colors[COLOR_UNLIT] = 0x0333; // dark gray
colors[COLOR_LIT] = 0x00F0; // bright green
paletteSet(screen, 0, colors);
}
static void drawCell(SurfaceT *screen, int16_t col, int16_t row, bool lit) {
int16_t x;
int16_t y;
uint8_t color;
x = (int16_t)(MARGIN_X + col * (CELL_W + GAP));
y = (int16_t)(MARGIN_Y + row * (CELL_H + GAP));
color = lit ? COLOR_LIT : COLOR_UNLIT;
fillRect(screen, x, y, CELL_W, CELL_H, color);
}
static void drawAllCells(SurfaceT *screen) {
int16_t col;
int16_t row;
JoeyKeyE key;
bool lit;
for (row = 0; row < GRID_ROWS; row++) {
for (col = 0; col < GRID_COLS; col++) {
key = gKeyGrid[row][col];
if (key == KEY_NONE) {
continue;
}
lit = joeyKeyDown(key);
drawCell(screen, col, row, lit);
gCellLit[row][col] = lit;
}
}
}
static void presentChangedCells(SurfaceT *screen) {
int16_t col;
int16_t row;
JoeyKeyE key;
bool lit;
int16_t x;
int16_t y;
for (row = 0; row < GRID_ROWS; row++) {
for (col = 0; col < GRID_COLS; col++) {
key = gKeyGrid[row][col];
if (key == KEY_NONE) {
continue;
}
lit = joeyKeyDown(key);
if (lit == gCellLit[row][col]) {
continue;
}
drawCell(screen, col, row, lit);
x = (int16_t)(MARGIN_X + col * (CELL_W + GAP));
y = (int16_t)(MARGIN_Y + row * (CELL_H + GAP));
surfacePresentRect(screen, x, y, CELL_W, CELL_H);
gCellLit[row][col] = lit;
}
}
}
int main(void) {
JoeyConfigT config;
SurfaceT *screen;
config.hostMode = HOST_MODE_TAKEOVER;
config.codegenBytes = 32 * 1024;
config.maxSurfaces = 4;
config.audioBytes = 64 * 1024;
config.assetBytes = 128 * 1024;
if (!joeyInit(&config)) {
fprintf(stderr, "joeyInit failed: %s\n", joeyLastError());
return 1;
}
screen = surfaceGetScreen();
if (screen == NULL) {
fprintf(stderr, "surfaceGetScreen returned NULL\n");
joeyShutdown();
return 1;
}
buildPalette(screen);
scbSetRange(screen, 0, SURFACE_HEIGHT - 1, 0);
surfaceClear(screen, COLOR_BACKGROUND);
joeyInputPoll();
drawAllCells(screen);
surfacePresent(screen);
for (;;) {
joeyInputPoll();
if (joeyKeyPressed(KEY_ESCAPE)) {
break;
}
presentChangedCells(screen);
}
joeyShutdown();
return 0;
}