Skip to content

Commit

Permalink
fix: event bus for native and ginga
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Oct 5, 2024
1 parent f17df30 commit 4b78b88
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 83 deletions.
18 changes: 10 additions & 8 deletions src/engine/core/ginga/draw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local math = require('math')

--! @cond
local canvas = nil
local application = nil
local game = nil
local std = nil
--! @endcond
Expand Down Expand Up @@ -49,8 +50,13 @@ local function image(src, x, y)
canvas:compose(x, y, image)
end

local function install(lstd, lgame, application, ginga)
local function event_bus()
std.bus.listen_safe('draw', application.callbacks.draw)
end

local function install(lstd, lgame, lapplication, ginga)
canvas = ginga.canvas
application = lapplication
game = lgame
std = lstd
std = std or {}
Expand All @@ -63,18 +69,14 @@ local function install(lstd, lgame, application, ginga)
std.draw.font=font
std.draw.line=line

local event_draw = function()
application.callbacks.draw(std, game)
end

return {
event={draw=event_draw},
std={draw=std.draw}
draw=std.draw
}
end

local P = {
install=install
event_bus = event_bus,
install = install
}

return P
35 changes: 15 additions & 20 deletions src/engine/core/ginga/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ local key_bindings={
CURSOR_DOWN='down',
CURSOR_LEFT='left',
CURSOR_RIGHT='right',
RED='red',
GREEN='green',
YELLOW='yellow',
BLUE='blue',
F6='red',
z='red',
x='green',
c='yellow',
v='blue',
ENTER='enter'
RED='a',
GREEN='b',
YELLOW='c',
BLUE='d',
z='a',
x='b',
c='c',
v='d',
ENTER='a'
}

--! @li https://github.com/TeleMidia/ginga/issues/190
Expand All @@ -25,22 +24,18 @@ local function event_ginga(std, game, application, evt)
fixture190 = evt.type
end

if fixture190 == evt.type then
std.key.press[key_bindings[evt.key]] = 1
else
std.key.press[key_bindings[evt.key]] = 0
end
std.bus.spawn('rkey', key_bindings[evt.key], fixture190 == evt.type)
end

local function install(std, game, application)
application.callbacks.loop = application.callbacks.loop or function () end
local function event_bus(std)
std.bus.listen_std('ginga', event_ginga)
end

return {
event={ginga=event_ginga}
}
local function install(std, game, application)
end

local P = {
event_bus = event_bus,
install=install
}

Expand Down
17 changes: 0 additions & 17 deletions src/engine/core/ginga/loop.lua

This file was deleted.

22 changes: 13 additions & 9 deletions src/engine/core/ginga/main.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
local zeebo_module = require('src/lib/engine/module')
local engine_encoder = require('src/lib/engine/encoder')
local engine_bus = require('src/lib/engine/bus')
local engine_fps = require('src/lib/engine/fps')
local engine_math = require('src/lib/engine/math')
local engine_game = require('src/lib/engine/game')
local engine_http = require('src/lib/engine/http')
local engine_i18n = require('src/lib/engine/i18n')
local engine_keys2 = require('src/lib/engine/key')
local engine_memory = require('src/lib/engine/memory')
local engine_color = require('src/lib/object/color')
local engine_keys = require('src/engine/core/ginga/keys')
local engine_loop = require('src/engine/core/ginga/loop')
local engine_keys1 = require('src/engine/core/ginga/keys')
local engine_draw = require('src/engine/core/ginga/draw')
local engine_draw_fps = require('src/lib/draw/fps')
local engine_draw_poly = require('src/lib/draw/poly')
Expand All @@ -31,15 +32,17 @@ local event = event
--! @brief GINGA?
_ENV = nil

local function register_event_loop(listener)
event.register(listener('ginga'))
local function register_event_loop()
event.register(std.bus.trigger('ginga'))
end

local function register_fixed_loop(listener)
local function register_fixed_loop()
local tick = nil
local loop = listener('loop')
local draw = listener('draw')
local loop = std.bus.trigger('loop')
local draw = std.bus.trigger('draw')

std.bus.listen_safe('loop', application.callbacks.loop)

tick = function()
local delay = application.internal.fps_controler(event.uptime())
loop()
Expand Down Expand Up @@ -83,12 +86,13 @@ local function install(evt, gamefile)
game.fps_show = application.config and application.config.fps_show or 0

zeebo_module.require(std, game, application)
:package('@bus', engine_bus)
:package('@fps', engine_fps, config_fps)
:package('@math', engine_math)
:package('@game', engine_game)
:package('@color', engine_color)
:package('@keys', engine_keys)
:package('@loop', engine_loop)
:package('@keys1', engine_keys1)
:package('@keys2', engine_keys2)
:package('@draw', engine_draw, ginga)
:package('@draw.fps', engine_draw_fps)
:package('@draw.poly', engine_draw_poly, polygons)
Expand Down
23 changes: 12 additions & 11 deletions src/engine/core/html5/driver-wasmoon.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ gly.wasmoon = async (game_file) => {
}

const keys = [
[403, 'red'],
[404, 'green'],
[405, 'yellow'],
[406, 'blue'],
['KeyZ', 'red'],
['KeyX', 'green'],
['KeyC', 'yellow'],
['KeyV', 'blue'],
['Enter', 'enter'],
[403, 'a'],
[404, 'b'],
[405, 'c'],
[406, 'd'],
['KeyZ', 'a'],
['KeyX', 'b'],
['KeyC', 'c'],
['KeyV', 'd'],
['Enter', 'a'],
['ArrowUp', 'up'],
['ArrowDown', 'down'],
['ArrowLeft', 'left'],
Expand All @@ -64,9 +64,10 @@ gly.wasmoon = async (game_file) => {
}

function updateKey(ev) {
const key = keys.find(key => key[0] == ev.code)
const key = keys.find(key => [ev.code, ev.keyCode].includes(key[0]))
if (key) {
gly.input(key[1], Number(ev.type === 'keydown'))
ev.preventDefault()
gly.input(key[1], ev.type === 'keydown')
}
}

Expand Down
22 changes: 7 additions & 15 deletions src/engine/core/native/main.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
local zeebo_module = require('src/lib/engine/module')
local engine_bus = require('src/lib/engine/bus')
local engine_game = require('src/lib/engine/game')
local engine_math = require('src/lib/engine/math')
local engine_color = require('src/lib/object/color')
local engine_http = require('src/lib/engine/http')
local engine_key = require('src/lib/engine/key')
local engine_encoder = require('src/lib/engine/encoder')
local engine_draw_fps = require('src/lib/draw/fps')
local engine_draw_poly = require('src/lib/draw/poly')
Expand All @@ -12,23 +14,17 @@ local library_csv = require('src/third_party/csv/rodrigodornelles')
local game = require('src/lib/object/game')
local std = require('src/lib/object/std')
local application = nil
local extraevents = {
loop = function(dt) end,
draw = function() end,
keydown = function(key, value) end
}

function native_callback_loop(milis)
game.milis = milis
application.callbacks.loop(std, game)
extraevents.loop(milis)
return game.dt
std.bus.spawn('loop', game.dt)
end

function native_callback_draw()
native_draw_start()
application.callbacks.draw(std, game)
extraevents.draw()
std.bus.spawn('draw')
native_draw_flush()
end

Expand All @@ -38,8 +34,7 @@ function native_callback_resize(width, height)
end

function native_callback_keyboard(key, value)
std.key.press[key] = value
extraevents.keydown(key, value)
std.bus.spawn('rkey', key, value)
end

function native_callback_init(width, height, game_lua)
Expand All @@ -54,9 +49,11 @@ function native_callback_init(width, height, game_lua)
std.draw.image=native_draw_image

zeebo_module.require(std, game, application)
:package('@bus', engine_bus)
:package('@game', engine_game)
:package('@math', engine_math)
:package('@color', engine_color)
:package('@key', engine_key)
:package('@draw.fps', engine_draw_fps)
:package('@draw.poly', engine_draw_poly, native_dict_poly)
:package('@memory', engine_memory)
Expand All @@ -68,11 +65,6 @@ function native_callback_init(width, height, game_lua)
:package('xml', engine_encoder, native_dict_xml)
:package('csv', engine_encoder, library_csv)
:package('i18n', engine_i18n, native_get_system_lang)
:register(function(listener)
extraevents.loop = listener('loop')
extraevents.draw = listener('draw')
extraevents.keydown = listener('keydown')
end)
:run()

game.width = width
Expand Down
10 changes: 8 additions & 2 deletions src/lib/engine/i18n.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,19 @@ local function decorator_draw_text(func)
end
end

local function event_bus(std, game, application)
std.bus.listen('i18n', function(texts)
update_languages(texts)
end)
std.bus.spawn('i18n', application.callbacks.i18n(std, game))
end

local function install(std, game, application, system_language)
if not (std and std.draw and std.draw.text) then
error('missing draw text')
end

local old_draw_text = std.draw.text
local texts = application.callbacks.i18n(std, game)
update_languages(texts)

if system_language then
set_language(system_language())
Expand All @@ -177,6 +182,7 @@ local function install(std, game, application, system_language)
end

local P = {
event_bus=event_bus,
install=install
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/engine/key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end

local function install(std, game, application, key_bindings)
application = application or {}
application.internal = application.iternal or {}
application.internal = application.internal or {}
application.internal.key_bindings = key_bindings or {}
end

Expand Down

0 comments on commit 4b78b88

Please sign in to comment.