Files
VCCPlusOSS/examples/cmoc/MissionRecorder/flight.c
T

183 lines
6.5 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include <coco.h>
#include <font5x8.h>
#include <vcc/bank.h>
#include <vcc/gime.h>
#include <vcc/input.h>
#include <vcc/orch90.h>
#include <vcc/ssc.h>
#include "mission.h"
enum { SKY, WHITE, ICE, GOLD, BASALT, RIDGE, TEAL };
#define NO_MARK 255
/* The steady beacon plays from resident, read-only data during the flight. */
static const struct orch90_frame beacon[] = {
{128,128}, {142,128}, {148,128}, {142,128}, {128,128}, {114,128}, {108,128}, {114,128},
{128,128}, {142,128}, {148,128}, {142,128}, {128,128}, {128,128}, {128,128}, {128,128},
{128,128}, {128,128}, {128,142}, {128,142}, {128,148}, {128,148}, {128,142}, {128,142},
{128,128}, {128,128}, {128,114}, {128,114}, {128,128}, {128,128}, {128,128}, {128,128}
};
static byte previousShip[2], previousMark[2], hidden;
static void label(unsigned x, byte y, const char *text, byte color)
{
while (*text) {
const byte *glyph = font5x8 + ((unsigned)*text++ - ' ') * 8;
byte row, col;
for (row = 0; row < 8; ++row)
for (col = 0; col < 5; ++col)
if (!(glyph[row] & (0x80 >> col)))
gime_plot(x + col, y + row, color);
x += 6;
}
}
/* Paint the permanent landscape once on each page before switching on sound. */
static void landscape(void)
{
unsigned i;
gime_clear(SKY);
label(8, 7, "MISSION RECORDER", ICE);
label(240, 7, "SURVEY / 01", WHITE);
gime_fill_rect(8, 20, 304, 1, TEAL);
for (i = 0; i < 16; ++i) {
unsigned x = i * 20, y = 106 + ((i * 7) % 19);
gime_plot(x + 4, 28 + ((i * 11) % 17), WHITE);
gime_fill_rect(x, y, 20, 141 - y, BASALT);
gime_fill_rect(x, y, 20, 2, RIDGE);
gime_fill_rect(x + 9, 139, 2, 4, ICE);
}
label(8, 149, "SECTOR 07 MARK --", WHITE);
label(8, 164, "A/D OR RIGHT STICK: STEER SPACE/FIRE: MARK", WHITE);
label(8, 178, "S: SAVE AND PRINT Q: DISCARD", ICE);
}
static void digits(unsigned x, byte value)
{
char pair[3];
pair[0] = '0' + value / 10;
pair[1] = '0' + value % 10;
pair[2] = 0;
gime_fill_rect(x, 149, 11, 8, SKY);
label(x, 149, pair, WHITE);
}
/* Each hidden page remembers where its own ship was painted. Erase just
that footprint. A mark stays put for the rest of this flight. */
static byte draw(byte sector, byte mark)
{
byte old = previousShip[hidden];
unsigned x = (unsigned)sector * 20;
if (old != sector) {
if (old != NO_MARK)
gime_fill_rect((unsigned)old * 20 + 2, 53, 16, 12, SKY);
gime_fill_rect(x + 7, 53, 6, 3, ICE);
gime_fill_rect(x + 4, 56, 12, 3, WHITE);
gime_fill_rect(x + 2, 59, 16, 3, TEAL);
gime_fill_rect(x + 5, 62, 2, 3, GOLD);
gime_fill_rect(x + 13, 62, 2, 3, GOLD);
digits(50, sector);
previousShip[hidden] = sector;
}
if (mark != NO_MARK && previousMark[hidden] != mark) {
gime_fill_rect((unsigned)mark * 20 + 8, 68, 4, 4, GOLD);
digits(110, mark);
previousMark[hidden] = mark;
}
hidden ^= 1;
return gime_present();
}
static byte flightLoop(byte speech, byte *mark, byte *speechError)
{
VccInput stick;
byte sector = 7, recorded = NO_MARK;
ssc_phrase words;
byte exit = 0;
unsigned started = 0, lastSample = getTimer();
words.remaining = 0;
input_init(&stick, INPUT_RIGHT);
for (;;) {
byte key = inkey(), fire = 0, previous = sector;
unsigned now = getTimer();
if (key >= 'a' && key <= 'z') key -= 'a' - 'A';
if (key == 'Q') exit = DISCARDED;
if (key == 'S' && recorded != NO_MARK) exit = RECORDED;
/* Service one cartridge byte per turn. Remember Save or Quit even
when there is no ready signal. Two seconds bounds the whole phrase. */
if (words.remaining) {
if ((unsigned)(now - started) < 120) ssc_service(&words);
else { words.remaining = 0; *speechError = SSC_TIMEOUT; }
}
if (exit) {
if (words.remaining) continue;
if (exit == RECORDED && !bank_read(0, mark, 1)) return FLIGHT_ERROR;
return exit;
}
if ((unsigned)(now - lastSample) >= 5) {
lastSample = now;
input_sample(&stick);
fire = stick.pressed & INPUT_PRIMARY;
if (stick.dx < 0 && sector > 0) --sector;
if (stick.dx > 0 && sector < 15) ++sector;
}
if (key == 'A' && sector > 0) --sector;
if (key == 'D' && sector < 15) ++sector;
if ((key == ' ' || fire) && recorded == NO_MARK) {
if (!bank_write(0, &sector, 1)) return FLIGHT_ERROR;
recorded = sector;
if (speech) {
started = now;
ssc_begin_phrase(&words, "MARK RECORDED");
}
} else if (previous == sector) {
continue;
}
if (!draw(sector, recorded)) {
if (words.remaining) *speechError = SSC_TIMEOUT;
return FLIGHT_ERROR;
}
}
}
unsigned char fly(unsigned char audio, unsigned char *mark)
{
static const byte colors[] = {0x01, 0x3f, 0x1f, 0x36, 0x20, 0x34, 0x0f};
byte i, result = FLIGHT_ERROR, stereo = 0, speechError = SSC_OK;
unsigned route = 0;
if (!bank_open()) {
printf("USE STANDARD COCO 3 BASIC WITH\n512K OR MORE\n");
return FLIGHT_ERROR;
}
if (!gime_open()) {
bank_close();
printf("GRAPHICS UNAVAILABLE\n");
return FLIGHT_ERROR;
}
for (i = 0; i < sizeof(colors); ++i) gime_set_palette(i, colors[i]);
previousShip[0] = previousShip[1] = NO_MARK;
previousMark[0] = previousMark[1] = NO_MARK;
hidden = 0;
landscape();
if (gime_present()) {
/* Show the ship only when the second page and controls are ready. */
landscape();
if (audio & AUDIO_SPEECH) route = ssc_route_audio();
if (audio & AUDIO_STEREO)
stereo = orch90_start(beacon, sizeof(beacon) / sizeof(beacon[0]), 1, ORCH90_CLOCK_NORMAL);
if (draw(7, NO_MARK))
result = flightLoop(audio & AUDIO_SPEECH, mark, &speechError);
if (stereo) orch90_stop();
if (audio & AUDIO_SPEECH) {
if (speechError == SSC_TIMEOUT) ssc_reset();
ssc_restore_audio(route);
}
}
gime_close();
bank_close();
if (speechError != SSC_OK) printf("SPEECH UNAVAILABLE\n");
if ((audio & AUDIO_STEREO) && !stereo) printf("STEREO PLAYER UNAVAILABLE\n");
if (result == FLIGHT_ERROR) printf("FLIGHT TRANSFER OR DISPLAY ERROR\n");
return result;
}