am-kernels/kernels/typing-game/game.c

179 lines
4.5 KiB
C
Raw Normal View History

2020-08-12 15:04:27 +00:00
#include <am.h>
#include <klib.h>
#include <klib-macros.h>
2020-08-13 12:30:50 +00:00
#define FPS 30
#define CPS 5
#define CHAR_W 8
#define CHAR_H 16
#define NCHAR 128
2020-08-13 14:42:52 +00:00
#define COL_WHITE 0xeeeeee
#define COL_RED 0xff0033
#define COL_GREEN 0x00cc33
#define COL_PURPLE 0x2a0a29
2020-08-12 15:04:27 +00:00
2020-08-13 14:42:52 +00:00
enum { WHITE = 0, RED, GREEN, PURPLE };
2020-08-12 15:04:27 +00:00
struct character {
char ch;
2020-08-13 12:30:50 +00:00
int x, y, v, t;
} chars[NCHAR];
2020-08-12 15:04:27 +00:00
2020-08-13 12:30:50 +00:00
int screen_w, screen_h, hit, miss, wrong;
2020-08-13 14:42:52 +00:00
uint32_t texture[3][26][CHAR_W * CHAR_H], blank[CHAR_W * CHAR_H];
2020-08-13 12:30:50 +00:00
int min(int a, int b) {
return (a < b) ? a : b;
}
2020-08-12 15:04:27 +00:00
int randint(int l, int r) {
return l + (rand() & 0x7fffffff) % (r - l + 1);
}
void new_char() {
for (int i = 0; i < LENGTH(chars); i++) {
struct character *c = &chars[i];
2020-08-13 12:30:50 +00:00
if (!c->ch) {
2020-08-12 15:04:27 +00:00
c->ch = 'A' + randint(0, 25);
2020-08-13 12:30:50 +00:00
c->x = randint(0, screen_w - CHAR_W);
2020-08-12 15:04:27 +00:00
c->y = 0;
2020-08-13 12:30:50 +00:00
c->v = (screen_h - CHAR_H + 1) / randint(FPS * 3 / 2, FPS * 2);
c->t = 0;
2020-08-12 15:04:27 +00:00
return;
}
}
}
2020-08-13 12:30:50 +00:00
void game_logic_update(int frame) {
if (frame % (FPS / CPS) == 0) new_char();
2020-08-12 15:04:27 +00:00
for (int i = 0; i < LENGTH(chars); i++) {
struct character *c = &chars[i];
if (c->ch) {
2020-08-13 12:30:50 +00:00
if (c->t > 0) {
if (--c->t == 0) {
c->ch = '\0';
}
} else {
c->y += c->v;
if (c->y < 0) {
c->ch = '\0';
}
if (c->y + CHAR_H >= screen_h) {
miss++;
c->v = 0;
c->y = screen_h - CHAR_H;
c->t = FPS;
}
2020-08-12 15:04:27 +00:00
}
}
}
}
2020-08-13 12:30:50 +00:00
void render() {
static int x[NCHAR], y[NCHAR], n = 0;
for (int i = 0; i < n; i++) {
io_write(AM_GPU_FBDRAW, x[i], y[i], blank, CHAR_W, CHAR_H, false);
}
n = 0;
for (int i = 0; i < LENGTH(chars); i++) {
struct character *c = &chars[i];
if (c->ch) {
x[n] = c->x; y[n] = c->y; n++;
2020-08-13 14:42:52 +00:00
int col = (c->v > 0) ? WHITE : (c->v < 0 ? GREEN : RED);
io_write(AM_GPU_FBDRAW, c->x, c->y, texture[col][c->ch - 'A'], CHAR_W, CHAR_H, false);
2020-08-13 12:30:50 +00:00
}
}
io_write(AM_GPU_FBDRAW, 0, 0, NULL, 0, 0, true);
for (int i = 0; i < 40; i++) putch('\b');
printf("Hit: %d; Miss: %d; Wrong: %d", hit, miss, wrong);
}
2020-08-12 15:04:27 +00:00
void check_hit(char ch) {
int m = -1;
for (int i = 0; i < LENGTH(chars); i++) {
struct character *c = &chars[i];
2020-08-13 04:07:15 +00:00
if (ch == c->ch && c->v > 0 && (m < 0 || c->y > chars[m].y)) {
2020-08-12 15:04:27 +00:00
m = i;
}
}
2020-08-13 04:07:15 +00:00
if (m == -1) {
wrong++;
} else {
hit++;
2020-08-13 12:30:50 +00:00
chars[m].v = -(screen_h - CHAR_H + 1) / (FPS);
2020-08-13 04:07:15 +00:00
}
2020-08-12 15:04:27 +00:00
}
2020-08-13 12:30:50 +00:00
void video_init() {
screen_w = io_read(AM_GPU_CONFIG).width;
screen_h = io_read(AM_GPU_CONFIG).height;
2020-08-12 15:04:27 +00:00
2020-08-13 12:30:50 +00:00
extern char font[];
for (int i = 0; i < CHAR_W * CHAR_H; i++)
2020-08-13 14:42:52 +00:00
blank[i] = COL_PURPLE;
2020-08-12 15:04:27 +00:00
uint32_t blank_line[screen_w];
for (int i = 0; i < screen_w; i++)
blank_line[i] = COL_PURPLE;
for (int y = 0; y < screen_h; y ++)
io_write(AM_GPU_FBDRAW, 0, y, blank_line, screen_w, 1, false);
2020-08-13 12:30:50 +00:00
for (int ch = 0; ch < 26; ch++) {
char *c = &font[CHAR_H * ch];
for (int i = 0, y = 0; y < CHAR_H; y++)
2020-08-13 14:42:52 +00:00
for (int x = 0; x < CHAR_W; x++, i++) {
int t = (c[y] >> (CHAR_W - x - 1)) & 1;
texture[WHITE][ch][i] = t ? COL_WHITE : COL_PURPLE;
texture[GREEN][ch][i] = t ? COL_GREEN : COL_PURPLE;
texture[RED ][ch][i] = t ? COL_RED : COL_PURPLE;
}
2020-08-12 15:04:27 +00:00
}
}
2020-08-13 04:07:15 +00:00
char lut[256] = {
[AM_KEY_A] = 'A', [AM_KEY_B] = 'B', [AM_KEY_C] = 'C', [AM_KEY_D] = 'D',
[AM_KEY_E] = 'E', [AM_KEY_F] = 'F', [AM_KEY_G] = 'G', [AM_KEY_H] = 'H',
[AM_KEY_I] = 'I', [AM_KEY_J] = 'J', [AM_KEY_K] = 'K', [AM_KEY_L] = 'L',
[AM_KEY_M] = 'M', [AM_KEY_N] = 'N', [AM_KEY_O] = 'O', [AM_KEY_P] = 'P',
[AM_KEY_Q] = 'Q', [AM_KEY_R] = 'R', [AM_KEY_S] = 'S', [AM_KEY_T] = 'T',
[AM_KEY_U] = 'U', [AM_KEY_V] = 'V', [AM_KEY_W] = 'W', [AM_KEY_X] = 'X',
[AM_KEY_Y] = 'Y', [AM_KEY_Z] = 'Z',
};
2020-08-12 15:04:27 +00:00
int main() {
ioe_init();
2020-08-13 12:30:50 +00:00
video_init();
2020-08-12 15:04:27 +00:00
2020-08-13 12:30:50 +00:00
panic_on(!io_read(AM_TIMER_CONFIG).present, "requires timer");
panic_on(!io_read(AM_INPUT_CONFIG).present, "requires keyboard");
2020-08-12 15:04:27 +00:00
printf("Type 'ESC' to exit\n");
2020-08-12 15:04:27 +00:00
int current = 0, rendered = 0;
uint64_t t0 = io_read(AM_TIMER_UPTIME).us;
2020-08-12 15:04:27 +00:00
while (1) {
int frames = (io_read(AM_TIMER_UPTIME).us - t0) / (1000000 / FPS);
2020-08-12 15:04:27 +00:00
2020-08-13 12:30:50 +00:00
for (; current < frames; current++) {
game_logic_update(current);
2020-08-12 15:04:27 +00:00
}
2020-08-13 04:07:15 +00:00
while (1) {
AM_INPUT_KEYBRD_T ev = io_read(AM_INPUT_KEYBRD);
if (ev.keycode == AM_KEY_NONE) break;
if (ev.keydown && ev.keycode == AM_KEY_ESCAPE) halt(0);
2020-08-13 04:07:15 +00:00
if (ev.keydown && lut[ev.keycode]) {
check_hit(lut[ev.keycode]);
}
};
2020-08-12 15:04:27 +00:00
if (current > rendered) {
render();
rendered = current;
}
}
}