2019-01-05 21:16:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dos.h>
|
|
|
|
|
2019-01-25 02:36:42 +00:00
|
|
|
#include "video.h"
|
|
|
|
#include "kbd.h"
|
|
|
|
#include "mouse.h"
|
|
|
|
#include "tiff.h"
|
|
|
|
#include "tiles.h"
|
2019-01-30 00:35:48 +00:00
|
|
|
#include "serial.h"
|
2019-02-09 16:48:40 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "jorth.h"
|
2019-01-19 04:02:50 +00:00
|
|
|
|
|
|
|
/*** S C R A T C H ***/
|
2019-01-25 02:36:42 +00:00
|
|
|
|
2019-01-19 04:03:02 +00:00
|
|
|
#define NUM_TILES 128
|
|
|
|
#define NUM_SPRITES 64
|
2019-01-20 16:44:42 +00:00
|
|
|
#define OFF_TILES 0x5000
|
2019-01-19 20:50:50 +00:00
|
|
|
#define TILE_STRIDE 64
|
|
|
|
#define SPRITE_STRIDE 80
|
|
|
|
unsigned int tiles[NUM_TILES * TILE_STRIDE];
|
|
|
|
unsigned int sprites[NUM_SPRITES * SPRITE_STRIDE];
|
2019-01-19 04:03:02 +00:00
|
|
|
|
2019-01-19 04:02:56 +00:00
|
|
|
unsigned char map[10000];
|
2019-01-19 04:02:50 +00:00
|
|
|
|
2019-01-19 04:02:56 +00:00
|
|
|
void fillMap() {
|
2019-01-19 04:02:50 +00:00
|
|
|
unsigned int x, y, z;
|
|
|
|
z = 0;
|
|
|
|
|
|
|
|
for (y = 0; y < 100; y ++) {
|
|
|
|
for (x = 0; x < 100; x ++) {
|
2019-01-19 20:50:50 +00:00
|
|
|
map[x + (y * 100)] = ((x + y + z) >> 2) % 4;
|
2019-01-19 04:02:50 +00:00
|
|
|
}
|
2019-01-05 21:16:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 16:44:42 +00:00
|
|
|
#define DIR_N 0
|
|
|
|
#define DIR_E 1
|
|
|
|
#define DIR_W 2
|
|
|
|
#define DIR_S 3
|
|
|
|
|
|
|
|
#define STATE_MAP 0
|
|
|
|
#define STATE_DIALOG 1
|
|
|
|
|
|
|
|
struct entity;
|
|
|
|
typedef struct entity Entity_t;
|
|
|
|
|
|
|
|
struct entity {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int dir;
|
|
|
|
int sprites[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
void drawEntity(Entity_t *entity) {
|
|
|
|
int isprite = entity->sprites[entity->dir];
|
|
|
|
drawSprite(&sprites[isprite * SPRITE_STRIDE], entity->x, entity->y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void playerThink(Entity_t *self) {
|
2019-01-25 02:36:42 +00:00
|
|
|
if (keyIsDown(K_LEFT)) { self->x -= 3; self->dir = DIR_W; }
|
|
|
|
if (keyIsDown(K_RIGHT)) { self->x += 3; self->dir = DIR_E; }
|
|
|
|
if (keyIsDown(K_UP)) { self->y -= 3; self->dir = DIR_N; }
|
|
|
|
if (keyIsDown(K_DOWN)) { self->y += 3; self->dir = DIR_S; }
|
2019-01-20 16:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int dy;
|
|
|
|
int y;
|
|
|
|
} Footer_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Entity_t player;
|
|
|
|
Footer_t footer;
|
|
|
|
int state;
|
|
|
|
} Game_t;
|
|
|
|
|
|
|
|
Game_t game;
|
|
|
|
|
2019-01-19 04:02:56 +00:00
|
|
|
void game_init() {
|
2019-01-05 21:16:08 +00:00
|
|
|
FILE *f;
|
|
|
|
TifImageMeta_t meta;
|
|
|
|
|
2019-01-20 16:44:42 +00:00
|
|
|
game.player.x = 100;
|
|
|
|
game.player.y = 100;
|
|
|
|
game.player.dir = DIR_E;
|
|
|
|
game.player.sprites[DIR_E] = 0;
|
|
|
|
game.player.sprites[DIR_N] = 1;
|
|
|
|
game.player.sprites[DIR_W] = 2;
|
|
|
|
game.player.sprites[DIR_S] = 3;
|
|
|
|
game.footer.y = 0;
|
|
|
|
game.state = STATE_MAP;
|
|
|
|
|
2019-01-05 21:16:08 +00:00
|
|
|
setEGAMode();
|
2019-01-07 02:29:20 +00:00
|
|
|
atexit(vid_cleanup);
|
|
|
|
|
2019-01-25 02:36:42 +00:00
|
|
|
kbd_init();
|
2019-02-09 16:48:40 +00:00
|
|
|
timer_init(TIMER_30HZ);
|
2019-01-25 02:36:42 +00:00
|
|
|
|
2019-01-19 04:02:56 +00:00
|
|
|
tile_init();
|
|
|
|
fillMap();
|
|
|
|
|
2019-01-07 02:29:20 +00:00
|
|
|
f = fopen("FOOTER.TIF", "rb");
|
|
|
|
meta = tifLoadMeta(f);
|
2019-01-19 04:02:50 +00:00
|
|
|
tifLoadEGA(f, meta, 0, 24, 336);
|
2019-01-07 02:29:20 +00:00
|
|
|
fclose(f);
|
2019-01-05 21:16:08 +00:00
|
|
|
|
|
|
|
f = fopen("TILES.TIF", "rb");
|
|
|
|
meta = tifLoadMeta(f);
|
2019-01-19 04:03:02 +00:00
|
|
|
tifLoad(f, meta, tiles, NUM_TILES * 16, 16, 4);
|
2019-01-19 20:50:50 +00:00
|
|
|
tifLoadEGA(f, meta, OFF_TILES, NUM_TILES * 16, 16);
|
2019-01-19 04:03:02 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
f = fopen("SPRITE.TIF", "rb");
|
|
|
|
meta = tifLoadMeta(f);
|
|
|
|
tifLoad(f, meta, sprites, NUM_SPRITES * 16, 16, 5);
|
2019-01-05 21:16:08 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2019-01-19 04:02:56 +00:00
|
|
|
loadTiles(OFF_TILES, tiles);
|
|
|
|
loadMap(map, 100, 100);
|
|
|
|
scroll(0, 0);
|
|
|
|
}
|
|
|
|
|
2019-01-30 00:35:48 +00:00
|
|
|
void showDialog() {
|
|
|
|
game.state = STATE_DIALOG;
|
|
|
|
game.footer.dy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dialogThink() {
|
|
|
|
if (keyWasPressed(K_SPACE)) {
|
|
|
|
game.footer.dy = -1;
|
|
|
|
}
|
|
|
|
game.footer.y += game.footer.dy;
|
|
|
|
if (game.footer.dy > 0 && game.footer.y > 24) {
|
|
|
|
game.footer.y = 24;
|
|
|
|
game.footer.dy = 0;
|
|
|
|
} else if (game.footer.dy < 0 && game.footer.y < 0) {
|
|
|
|
game.footer.y = 0;
|
|
|
|
game.footer.dy = 0;
|
|
|
|
game.state = STATE_MAP;
|
|
|
|
}
|
|
|
|
setSplitScreen(399 - (game.footer.y << 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void overworldThink() {
|
|
|
|
if (keyWasPressed(K_SPACE)) {
|
|
|
|
showDialog();
|
|
|
|
}
|
|
|
|
playerThink(&game.player);
|
|
|
|
scroll(game.player.x - 152, game.player.y - 92);
|
|
|
|
}
|
|
|
|
|
2019-02-06 02:07:30 +00:00
|
|
|
void f_seremit() {
|
|
|
|
ser_write_byte(TOP().i);
|
|
|
|
if (TOP().i == '\n') {
|
|
|
|
ser_write_byte('\r');
|
|
|
|
}
|
|
|
|
DROP(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void f_keyWasPressed() {
|
|
|
|
TOP().i = keyWasPressed(TOP().i);
|
|
|
|
}
|
|
|
|
void f_keyIsDown() {
|
|
|
|
TOP().i = keyIsDown(TOP().i);
|
|
|
|
}
|
|
|
|
void f_drawSprite() { // ( x y sprite -- )
|
|
|
|
drawSprite(&sprites[TOP().i * SPRITE_STRIDE], ST2().i, ST1().i);
|
|
|
|
DROP(3);
|
|
|
|
}
|
|
|
|
void f_scroll() { // ( x y -- )
|
|
|
|
scroll(ST1().i, TOP().i);
|
|
|
|
}
|
2019-02-09 16:48:40 +00:00
|
|
|
void f_ticks() {
|
|
|
|
PUSHU(timer_counter);
|
|
|
|
}
|
2019-02-06 02:07:30 +00:00
|
|
|
|
|
|
|
void game_f_init() {
|
|
|
|
f_init();
|
|
|
|
CDEF("seremit", f_seremit);
|
|
|
|
CDEF("key-pressed", f_keyWasPressed);
|
|
|
|
CDEF("key-down", f_keyIsDown);
|
|
|
|
CDEF("sprite", f_drawSprite);
|
|
|
|
CDEF("scroll", f_scroll);
|
|
|
|
CDEF("draw", drawScreen);
|
2019-02-09 16:48:40 +00:00
|
|
|
CDEF("ticks", f_ticks);
|
2019-02-06 02:07:30 +00:00
|
|
|
|
2019-02-09 16:48:40 +00:00
|
|
|
f_loadfile("game.jor");
|
2019-02-04 02:04:02 +00:00
|
|
|
}
|
2019-02-06 02:07:30 +00:00
|
|
|
|
|
|
|
void f_poll() {
|
|
|
|
static char line[256] = { 0 };
|
|
|
|
|
|
|
|
int i = strlen(line);
|
|
|
|
int value;
|
|
|
|
for (value = ser_poll(); value != SER_NODATA; value = ser_poll()) {
|
|
|
|
if (value == '\b' || value == 127) {
|
|
|
|
i --;
|
|
|
|
} else {
|
|
|
|
line[i] = value;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
line[i] = '\0';
|
|
|
|
ser_write_byte(value); // echo
|
|
|
|
|
|
|
|
if (value == '\r') {
|
|
|
|
line[i - 1] = '\n';
|
|
|
|
ser_write_byte('\n');
|
|
|
|
PUSHS(line);
|
|
|
|
f_runstring("REPL send");
|
|
|
|
f_taskloop();
|
|
|
|
i = 0;
|
|
|
|
line[i] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 16:48:40 +00:00
|
|
|
f_taskloop();
|
2019-02-06 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2019-02-09 16:48:40 +00:00
|
|
|
ser_init(SER_COM2, BAUD_19200, SER_8N1);
|
2019-01-19 04:02:56 +00:00
|
|
|
game_init();
|
2019-02-06 02:07:30 +00:00
|
|
|
game_f_init();
|
|
|
|
|
2019-01-25 02:36:42 +00:00
|
|
|
while (!keyIsDown(K_ESC)) {
|
|
|
|
kbd_debounce();
|
2019-02-06 02:07:30 +00:00
|
|
|
f_poll();
|
|
|
|
|
2019-01-20 16:44:42 +00:00
|
|
|
if (game.state == STATE_MAP) {
|
2019-01-30 00:35:48 +00:00
|
|
|
overworldThink();
|
2019-01-20 16:44:42 +00:00
|
|
|
} else if (game.state == STATE_DIALOG) {
|
2019-01-30 00:35:48 +00:00
|
|
|
dialogThink();
|
2019-01-20 16:44:42 +00:00
|
|
|
}
|
|
|
|
drawEntity(&game.player);
|
2019-01-19 04:02:56 +00:00
|
|
|
drawScreen();
|
2019-01-05 21:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|