Skip to content

Commit

Permalink
rework blink configuration on web/SDL
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Oct 27, 2024
1 parent 18d23de commit 18a3a96
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 43 deletions.
25 changes: 12 additions & 13 deletions src/sdl/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,6 @@ static void sdl_timer_init(void) {
SDL_AddTimerNS((Uint64) (SYS_TIMER_TIME * 1000000), sdl_timer_thread, (void*)NULL);
}

static bool sdl_is_blink_phase(long curr_time) {
int blink_duration_ms = zzt_get_active_blink_duration_ms();
if (blink_duration_ms <= 0) {
return false;
} else {
return ((curr_time % (blink_duration_ms*2)) >= blink_duration_ms);
}
}

// try to keep a budget of ~5ms per call

static int zzt_thread_func(void *ptr) {
Expand Down Expand Up @@ -347,10 +338,18 @@ static void sdl_resize_window(int delta, bool only_if_too_small, bool delta_is_s
}
}

static u8 video_blink = 1;
static int blink_duration_ms = 0;

static bool sdl_is_blink_phase(long curr_time) {
if (blink_duration_ms <= 0) {
return false;
} else {
return ((curr_time % (blink_duration_ms*2)) >= blink_duration_ms);
}
}

void zeta_update_blink(int blink) {
video_blink = (blink != 0) ? 1 : 0;
blink_duration_ms = blink;
}

void zeta_update_charset(int width, int height, u8* data) {
Expand Down Expand Up @@ -547,7 +546,7 @@ int main(int argc, char **argv) {
int swidth;
zzt_get_screen_size(&swidth, NULL);

if (!video_blink) sflags |= RENDER_BLINK_OFF;
if (blink_duration_ms < 0) sflags |= RENDER_BLINK_OFF;
else if (sdl_is_blink_phase(zeta_time_ms())) sflags |= RENDER_BLINK_PHASE;

#ifdef USE_LIBPNG
Expand Down Expand Up @@ -750,7 +749,7 @@ int main(int argc, char **argv) {
SDL_UnlockMutex(render_data_update_mutex);

long curr_time = zeta_time_ms();
int blink_mode = video_blink ? (sdl_is_blink_phase(curr_time) ? BLINK_MODE_2 : BLINK_MODE_1) : BLINK_MODE_NONE;
int blink_mode = blink_duration_ms >= 0 ? (sdl_is_blink_phase(curr_time) ? BLINK_MODE_2 : BLINK_MODE_1) : BLINK_MODE_NONE;
renderer->draw(zzt_vram_copy, blink_mode);
}

Expand Down
12 changes: 10 additions & 2 deletions src/zzt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ int zzt_load_palette(u32 *colors) {

int zzt_load_blink(int blink) {
zzt.blink = blink;
zeta_update_blink(zzt.blink);
zeta_update_blink(zzt_get_active_blink_duration_ms());
return 0;
}

Expand Down Expand Up @@ -1426,18 +1426,26 @@ bool zzt_get_blink_disable_user_override(void) {

void zzt_set_blink_disable_user_override(bool value) {
zzt.blink_disable_user_override = value;
zeta_update_blink(zzt_get_active_blink_duration_ms());
}

int zzt_get_blink_duration_ms(void) {
return zzt.blink_duration_ms;
}

int zzt_get_active_blink_duration_ms(void) {
return zzt.blink_disable_user_override ? -1 : zzt.blink_duration_ms;
// High colors?
if (!zzt.blink)
return -1;
// Blinking disabled by user?
if (zzt.blink_disable_user_override)
return 0;
return zzt.blink_duration_ms;
}

void zzt_set_blink_duration_ms(int value) {
zzt.blink_duration_ms = value;
zeta_update_blink(zzt_get_active_blink_duration_ms());
}

void zzt_init(int memory_kbs) {
Expand Down
33 changes: 17 additions & 16 deletions web/res/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,29 @@ The entrypoint is "ZetaLoad(options, callback);". The callback is optional, and
* max - maximum palette value in the array (f.e. 255),
* colors - an array of sixteen colors, either:
* 3-component arrays of range min - max (inclusive),
* "#012345" or "#012"-format strings.
* lock_palette: if true and a custom palette is provided, the palette cannot be changed by the emulated engine,
* memory_limit: the memory limit, in kilobytes (64-640)
* extended_memory_limit: the extended (EMS) memory limit, in kilobytes
* skip_kc: if true, press "K, C, ENTER" on start
* "#012345" or "#024"-format RGB hex strings.
* lock_palette: if true and a custom palette is provided, the palette cannot be changed by the emulated engine.
* memory_limit: the memory limit, in kilobytes (64-640).
* extended_memory_limit: the extended (EMS) memory limit, in kilobytes.
* skip_kc: if true, press "K, C, ENTER" on start.
* render:
* type: the engine to use for video rendering; can be "auto" (preferred) or "canvas"
* blink_cycle_duration: the length of a full blink cycle, in seconds; <= 0 to disable; the default is 0.466
* charset_override: the location of a PNG image file (16x16 chars) overriding the engine's character set, if present
* type: the engine to use for video rendering; can be "auto" (preferred) or "canvas".
* blink_cycle_duration: the length of a full blink cycle, in seconds. The default is 0.534 (534 ms).
* Set to 0 to disable blinking - force low colors (can be changed back in the F11 menu).
* Set to -1 to disable blinking - enable high colors (can be changed back by the running engine).
* charset_override: the location of a PNG image file (16x16 chars) overriding the engine's character set, if present.
* audio:
* type: the engine to use for audio rendering; can be "auto" (preferred), "buffer" or "oscillator" (pre-beta15; deprecated)
* bufferSize (buffer): the audio buffer size, in samples
* sampleRate (buffer): the audio sampling rate, in Hz
* noteDelay: the minimum note delay, in milliseconds; 1 is default
* volume: the volume of the outputted audio stream (range 0.0 - 1.0); 0.2 by default
* bufferSize (buffer): the audio buffer size, in samples.
* sampleRate (buffer): the audio sampling rate, in Hz.
* noteDelay: the minimum note delay, in milliseconds; 1 by default.
* volume: the volume of the outputted audio stream (range 0.0 - 1.0); 0.2 by default.

File entries are stored in the form of an object containing the following keys:

* type:
- array: local file to be directly added
- file: remote file to be directly added
- zip: remote .ZIP file to be unpacked into the VFS
- array: local file to be directly added,
- file: remote file to be directly added,
- zip: remote .ZIP file to be unpacked into the VFS.
* url: For remotely-acquired types, denotes the URL leading to the file.
* filename: For single-file types, denotes the target filename.
* data: For the "array" type, a byte array containing the file's data.
Expand Down
25 changes: 19 additions & 6 deletions web/src/emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ class Emulator {
render.setPalette(data);
}

window.zetag_update_blink = function(blink) {
// render.setBlinkEnabled(blink);
}
window.zetag_update_blink = function(blink) { }

window.speakerg_on = function(cycles, freq) {
if (!document.hasFocus()) {
Expand Down Expand Up @@ -442,9 +440,24 @@ export function createEmulator(render, audio, vfs, options) {
emu._zzt_set_max_extended_memory((options && options.engine && options.engine.extended_memory_limit) || -1);
emu._zzt_set_timer_offset(Date.now() % 86400000);

emuObj.render.setBlinkCycleDuration((options && options.render && options.render.blink_cycle_duration) || 0.534);
emuObj.render.setBlinkEnabled(emu._zzt_get_blink_duration_ms() > 0);

emuObj.render.setBlinkCycleDuration(0.534);
var blinkCycleDuration = (options && options.render && options.render.blink_cycle_duration);
if (blinkCycleDuration < 0) {
// high colors
emu._zzt_load_blink(0);
} else if (!blinkCycleDuration && blinkCycleDuration !== 0) {
// blinkCycleDuration not set
emu._zzt_load_blink(1);
} else {
// blinkCycleDuration set, but may be zero
emu._zzt_load_blink(1);
if (blinkCycleDuration !== 0) {
emuObj.render.setBlinkCycleDuration(blinkCycleDuration);
} else {
emu._zzt_set_blink_disable_user_override(1);
}
}

if (options && options.commands) {
const lastCommand = options.commands.length - 1;
for (var i = 0; i <= lastCommand; i++) {
Expand Down
8 changes: 2 additions & 6 deletions web/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export class CanvasBasedRenderer {
this.scale = Math.floor(Math.min(this.cw / this.pw, this.ch / this.ph));
if (this.scale < 1) this.scale = 1;

if (this.emu._zzt_get_blink()) {
let blink_duration = this.emu._zzt_get_active_blink_duration_ms();
let blink_duration = this.emu._zzt_get_active_blink_duration_ms();
if (blink_duration >= 0) {
if (blink_duration > 0 && (time % (blink_duration * 2)) >= blink_duration) {
this.blink_state = 2;
} else {
Expand Down Expand Up @@ -229,10 +229,6 @@ export class CanvasBasedRenderer {
}
}

setBlinkEnabled(blink) {
this.emu._zzt_load_blink(blink != 0);
}

setBlinkCycleDuration(duration) {
this.emu._zzt_set_blink_duration_ms(Math.round(duration * 500));
}
Expand Down

0 comments on commit 18a3a96

Please sign in to comment.