Skip to content

Commit

Permalink
More commands
Browse files Browse the repository at this point in the history
Merge pull request #5 from haroldo-ok/more-commands
  • Loading branch information
haroldo-ok authored Sep 18, 2022
2 parents 712e1d7 + 291de30 commit d221334
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 30 deletions.
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# choice4genesis
A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis

This is a ChoiceScript clone that generates Sega Genesis ROMs. If can be used for visual novels or simple multimedia presentations.

It takes a bunch of scripts and images and, from that, it generates SGDK-compatible `.c` and `.res` files. Those are then compiled into a Sega Genesis compatible ROM, which can be run on an emulator or even on real hardware.

The syntax of the scripts is somewhat based on ChoiceScript, but it is not exactly the same.

*Please note that this is an early work and progress, and it is not as stable or user-friendly as it is planned to become.*

## Commands implemented so far

### `font`
Loads a `.png` file containing the 8x8 font. Note that the image must be paletized, with 16 colors. Future versions of this tool will probably convert the image *automagically*.

### `background`
Loads a `.png` file as a background image. Note that the image must be paletized, with 16 colors. Future versions of this tool will probably convert the image *automagically*.

### `choice`
Presents a menu to the user, allowing to choose between multiple options.

### `music`
Starts playing a `.vgm`/`.xgm` music in the background.

### `image`
Allows drawing a small image in `.png` format somewhere in the background. Note that the image must be paletized, with 16 colors. Future versions of this tool will probably convert the image *automagically*.

### `wait`
Waits for a few seconds.

## Planned commands

The tool accepts those commands, but, at the moment, they don't do anything.

### `create`
Will create a global variable.

### `temp`
Will create a local variable. `temp` variables are only visible inside the scene file that created them.

### `set`
Will change the current value of an existing variable.

### `if`/`elseif`/`else`
Will allow a certain block of code to only be executed on a given condition.

### `label`
Will allow to mark a place where the `goto` command can jump to.

### `goto`
Will jump to a given label from anywhere on the same scene.

### `goto_scene`
Will jump to a different scene.

### `sound`
Will play a digitized sound.

### `scene_list`
Will configure the default sequence in which the scenes will be played.

### `finish`
Will jump to the next scene in the game.

### `window`
Will allow to configure the region of the screen that will be used for the text popups and menus.

### `clear`
Will allow to clear regions of the screen.

### `video`
Will play a full screen video.
2 changes: 2 additions & 0 deletions examples/test/clean.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
del /s/q out\*
del /s/q res\*
Binary file added examples/test/project/Actraiser - Fillmore.vgm
Binary file not shown.
Binary file added examples/test/project/Smiley.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/test/project/ready.wav
Binary file not shown.
15 changes: 15 additions & 0 deletions examples/test/project/startup.choice
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
* font "damieng.com - Hourglass font.png"
* background "Blue Hedgehog.png"
* choice
# Play some music
* music "Actraiser - Fillmore.vgm"
OK, playing Fillmore, from Actraiser.
# Show a smiley
* image "Smiley.png", at(30, 3)
OK... showing a smiley!
# Third choice
You chose the third one
* choice
# Yet another choice
You chose this.
# One more choice
You chose that.
This is a test.
Second line.
Third line.
1 change: 1 addition & 0 deletions examples/test/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out\rom.bin
183 changes: 176 additions & 7 deletions examples/test/src/vn_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,119 @@
#include "vn_engine.h"

#define TEXT_BUFFER_LEN (8192)
#define CHOICE_MAX (8)

char textBuffer[TEXT_BUFFER_LEN];

struct {
u16 x, y, w, h;
} window;

struct {
u16 x, y;
u16 tileNumber;
} imageInfo;

struct {
bool up;
bool down;
bool next;
} input;

void VN_joyHandler(u16 joy, u16 changed, u16 state) {
if (joy != JOY_1) return;

input.up = !!(state & BUTTON_UP);
input.down = !!(state & BUTTON_DOWN);
input.next = !!(state & (BUTTON_A | BUTTON_B | BUTTON_C));
}

void VN_waitJoyRelease() {
do {
SYS_doVBlankProcess();
} while(input.up || input.down || input.next);
}

void VN_waitPressNext() {
do {
SYS_doVBlankProcess();
} while(!input.next);
VN_waitJoyRelease();
}

void VN_init() {
VDP_setTextPlane(BG_B);
JOY_init();
JOY_setEventHandler(&VN_joyHandler);

memset(textBuffer, 0, TEXT_BUFFER_LEN);

window.x = 1;
window.y = 20;
window.w = 38;
window.h = 4;
window.h = 6;

imageInfo.x = 0;
imageInfo.y = 0;
imageInfo.tileNumber = 256;

XGM_setLoopNumber(-1);
XGM_setForceDelayDMA(TRUE);

VDP_drawText("choice4genesis v0.0.1", 18, 27);
}

void VN_background(const Image *image) {
VDP_loadTileSet(image->tileset, 256, DMA);

void VN_showImage(const Image *image, u16 palNum, u16 x, u16 y) {
VDP_loadTileSet(image->tileset, imageInfo.tileNumber, DMA);
TileMap *tmap = unpackTileMap(image->tilemap, NULL);
VDP_setTileMapEx(BG_A, tmap, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, 256), 0, 0, 0, 0, 40, 28, CPU);
VDP_setPaletteColors(16, (u16*)image->palette->data, 32);
VDP_setTileMapEx(BG_B, tmap, TILE_ATTR_FULL(palNum, FALSE, FALSE, FALSE, imageInfo.tileNumber),
x, y, 0, 0, tmap->w, tmap->h, CPU);
VDP_setPalette(palNum, (u16*)image->palette->data);
imageInfo.tileNumber += image->tileset->numTile;
free(tmap);
}

void VN_background(const Image *image) {
imageInfo.tileNumber = 256;
VN_showImage(image, PAL1, 0, 0);
}

void VN_image(const Image *image) {
VN_showImage(image, PAL2, imageInfo.x, imageInfo.y);
}

void VN_imageAt(u16 x, u16 y) {
imageInfo.x = x;
imageInfo.y = y;
}

void VN_font(const Image *image) {
VDP_loadFont(image->tileset, DMA);
VDP_setPalette(PAL0, (u16*)image->palette->data);
}


void VN_music(const u8 *music) {
XGM_startPlay(music);
}


void VN_clearWindow() {
VDP_clearTextAreaEx(BG_A, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, 0x05A0), window.x, window.y, window.w, window.h, DMA);
}

void VN_text(char *text) {
if (textBuffer[0]) strcat(textBuffer, "\n");
strcat(textBuffer, text);
}

void VN_flushText() {
if (!textBuffer[0]) return;

VN_waitJoyRelease();

VN_clearWindow();

char lineBuffer[41];
char *o = textBuffer;
u16 y = window.y;
Expand All @@ -49,4 +130,92 @@ void VN_flushText() {
y++;
}
strclr(textBuffer);
}

VN_waitPressNext();
}

void VN_wait(u16 duration) {
VN_flushText();
for (u16 remainining = duration; remainining; remainining--) {
for (u16 i = 60; i; i--) SYS_doVBlankProcess();
}
}

void VN_option(u8 number, char *text) {
VN_text(text);

char *d = textBuffer + strlen(textBuffer);

*d = 1;
d++;
*d = number;
d++;
*d = 0;
}

u8 VN_choice() {
if (!textBuffer[0]) return 0;

VN_clearWindow();

u8 choiceCount = 0;
u16 cursorPositons[CHOICE_MAX];
u8 choiceValues[CHOICE_MAX];

char lineBuffer[41];
char *o = textBuffer;
u16 y = window.y;

while (*o) {
char *d = lineBuffer;
for (;*o && *o != '\n' && *o != 1; o++, d++) *d = *o;
*d = 0;

if (*o == 1) {
o++;
cursorPositons[choiceCount] = y;
choiceValues[choiceCount] = *o;
choiceCount++;
o++;
}

if (*o) o++;

VDP_drawText(lineBuffer, window.x + 1, y);
y++;
}
strclr(textBuffer);

VN_waitJoyRelease();

u8 choiceNumber = 0;
VDP_drawText(">", window.x, cursorPositons[0]);
while (!input.next) {
SYS_doVBlankProcess();
if (input.up || input.down) {
VDP_drawText(" ", window.x, cursorPositons[choiceNumber]);

// Previous choice?
if (input.up) {
if (choiceNumber) {
choiceNumber--;
} else {
choiceNumber = choiceCount - 1;
}
}

// Next choice?
if (input.down) {
choiceNumber++;
if (choiceNumber >= choiceCount) choiceNumber = 0;
}

VDP_drawText(">", window.x, cursorPositons[choiceNumber]);
VN_waitJoyRelease();
}
}

VN_waitJoyRelease();

return choiceValues[choiceNumber];
}
12 changes: 12 additions & 0 deletions examples/test/src/vn_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@

#include "genesis.h"
#include "gfx.h"
#include "music.h"

extern void VN_init();

extern void VN_background(const Image *image);
extern void VN_image(const Image *image);
extern void VN_imageAt(u16 x, u16 y);
extern void VN_font(const Image *image);

extern void VN_music(const u8 *music);

extern void VN_text(char *text);
extern void VN_flushText();
extern void VN_wait(u16 duration);

extern void VN_option(u8 number, char *text);
extern u8 VN_choice();

typedef void * (*scriptFunction)();

Expand Down
Loading

0 comments on commit d221334

Please sign in to comment.