Skip to content

Commit

Permalink
Implement cursor command.
Browse files Browse the repository at this point in the history
Merge pull request #63 from haroldo-ok/custom-cursor
This allows to customize the sprite that will be used for the cursor.
This fixes #58 and #59
  • Loading branch information
haroldo-ok committed Oct 9, 2022
2 parents 7b1103d + a9c7a06 commit 444ed83
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Jumps to a different scene. The scene files are located on the script directory,
### `window`
Allows to configure the region of the screen that will be used for the text popups and menus.

### `cursor`
Allows to configure the blinking text cursor.

### `flush`
Immediately shows the contents of the current text buffer on the text window; if passed the flag `nowait`, does not wait for a button press.

Expand Down
Binary file added examples/test/project/Cursor sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/test/project/damieng.com - Hourglass font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/test/project/startup.choice
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* author "John Doe"

* font "damieng.com - Hourglass font.png"
* cursor "Cursor sprite.png", 1, 1, 3
* background "Blue Hedgehog.png"

* create intVar, 1
Expand Down
41 changes: 35 additions & 6 deletions examples/test/src/vn_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ char textBuffer[TEXT_BUFFER_LEN];

struct {
u16 x, y, w, h;
Sprite *cursor;
} window;

struct {
Expand All @@ -32,15 +33,20 @@ void VN_joyHandler(u16 joy, u16 changed, u16 state) {
input.next = !!(state & (BUTTON_A | BUTTON_B | BUTTON_C));
}

void VN_doVBlank() {
SPR_update();
SYS_doVBlankProcess();
}

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

void VN_waitPressNext() {
do {
SYS_doVBlankProcess();
VN_doVBlank();
} while(!input.next);
VN_waitJoyRelease();
}
Expand All @@ -52,6 +58,9 @@ void VN_init() {
memset(textBuffer, 0, TEXT_BUFFER_LEN);

VN_windowDefault();
window.cursor = NULL;

SPR_init(0, 0, 0);

imageInfo.x = 0;
imageInfo.y = 0;
Expand All @@ -60,7 +69,7 @@ void VN_init() {
XGM_setLoopNumber(-1);
XGM_setForceDelayDMA(TRUE);

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


Expand Down Expand Up @@ -162,7 +171,14 @@ void VN_flush(const u8 flags) {
}
strclr(textBuffer);

if (shouldWait) VN_waitPressNext();
if (shouldWait) {
if (window.cursor) {
SPR_setPosition (window.cursor, (window.x + window.w - 1) * 8, (window.y + window.h - 1) * 8);
SPR_setVisibility(window.cursor, VISIBLE);
}
VN_waitPressNext();
if (window.cursor) SPR_setVisibility(window.cursor, HIDDEN);
}
}

void VN_clear(const u8 flags) {
Expand All @@ -174,7 +190,7 @@ void VN_clear(const u8 flags) {
void VN_wait(u16 duration) {
VN_flushText();
for (u16 remainining = duration; remainining; remainining--) {
for (u16 i = 60; i; i--) SYS_doVBlankProcess();
for (u16 i = 60; i; i--) VN_doVBlank();
}
}

Expand Down Expand Up @@ -228,7 +244,7 @@ u8 VN_choice() {
u8 choiceNumber = 0;
VDP_drawText(">", window.x, cursorPositons[0]);
while (!input.next) {
SYS_doVBlankProcess();
VN_doVBlank();
if (input.up || input.down) {
VDP_drawText(" ", window.x, cursorPositons[choiceNumber]);

Expand Down Expand Up @@ -277,3 +293,16 @@ void VN_windowSize(u16 w, u16 h) {
window.w = w;
window.h = h;
}

void VN_cursor(const SpriteDefinition *sprite) {
if (window.cursor) {
SPR_releaseSprite(window.cursor);
window.cursor = NULL;
}

if (!sprite) return;

window.cursor = SPR_addSprite(sprite, 0, 0, TILE_ATTR(PAL0, 1, FALSE, FALSE));
SPR_setVisibility(window.cursor, HIDDEN);
VDP_setPalette(PAL0, (u16*) sprite->palette->data);
}
2 changes: 2 additions & 0 deletions examples/test/src/vn_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

#define STOP_MUSIC (1)
Expand Down Expand Up @@ -38,6 +39,7 @@ extern void VN_windowDefault();
extern void VN_windowFrom(u16 x, u16 y);
extern void VN_windowTo(u16 x, u16 y);
extern void VN_windowSize(u16 w, u16 h);
extern void VN_cursor(const SpriteDefinition *sprite);

extern void VN_option(u8 number, char *text);
extern u8 VN_choice();
Expand Down
14 changes: 13 additions & 1 deletion generator/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ const COMMAND_GENERATORS = {
return generated.join('\n');
},

'cursor': (entity, context) => {
const imageFileName = getStringConstant(entity, entity.params.positional.fileName, context, 'Image filename');
const width = getNumber(entity, entity.params.positional.width, context, 'Sprite width in tiles');
const height = getNumber(entity, entity.params.positional.height, context, 'Sprite height in tiles');
const frameDelay = getNumber(entity, entity.params.positional.frameDelay, context, 'Sprite animation delay');

const imageVariable = addResource(context.res.sprite, imageFileName, imageVariable =>
`SPRITE ${imageVariable} "../project/${imageFileName}" ${width} ${height} NONE ${frameDelay}`);

return `VN_cursor(&${imageVariable});`;
},

'flush': (entity, context) => {
const generatedFlags = generateFlags(entity, 'FLUSH');
return `VN_flush(${generatedFlags || 0});`;
Expand Down Expand Up @@ -458,7 +470,7 @@ const generate = fileSystem => {
scenesToProcess: [],
generatedScripts: [],
errors: [],
res: { gfx: {}, music: {}, sound: {} },
res: { gfx: {}, sprite: {}, music: {}, sound: {} },
choices: [],
globals: createNamespace(),
locals: null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "choice4genesis",
"version": "0.7.0",
"version": "0.8.0",
"description": "A ChoiceScript clone that generates SGDK-compatible C source for the Sega Genesis ",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions parser/syntax-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const COMMANDS = {
'stop': { flags: ['music', 'sound'] },

'window': { named: { 'from': ['x', 'y'], 'to': ['x', 'y'], 'size': ['w', 'h'] }, flags: ['borderless', 'withborder', 'default'] },
'cursor': { positional: ['fileName', 'width', 'height', 'frameDelay'] },
'clear': { flags: ['background', 'foreground', 'window'] },
'flush': { flags: ['nowait'] },

Expand Down

0 comments on commit 444ed83

Please sign in to comment.