Installing
gosu and activesupport will be loaded as dependencies
gem install gosu_wrapper
or in a gemfile: gem 'gosu_wrapper'
Usage
One public class is exposed, GosuWrapper
. It's essentially a wrapper over an
anonymous Gosu::Window
class that gets functionality added through metaprogramming.
In lieue of a "getting started" snippet, I'm going to delegate that to the gosu_wrapper_snake_example codebase and run through the API step-by-step here.
#initialize(width:, height:, attributes:)
-
This defines
#window
which is an instance ofGosu::Window
. -
width & height are num pixels (of the total window).
-
attributes is an array of symbols.
- It must at least include
:window_height
and:window_width
. - All of the keys in your app's state hash should be given here.
- Each of them get attr_accessors defined on
#window
and shorthand accessors usingmethod_missing
(see next section).
- It must at least include
There are four instance methods added using method_missing
-
#get_<attr>
delegates the getter towindow
-
#set_<attr>(val)
delegates the setter towindow
-
#change_<attr>(sym, arg)
a shorthand for some update operations on non-mutable objects. For examplechange_score(:+, 1)
-
#get_or_set_<attr>(&blk)
returns the val if it's truthy, and otherwise sets the val equal to the block result (returning it as well).
#define_method_on_window(name, &blk)
-
this should be a private method, but is used by
#add_hook/#add_helper
so it's worth explaining. -
It defines the method named
name
onwindow
withblk
as its body. -
Arguments are determined by the given block
-
The block is always invoked with the
GosuWrapper
instance as the value ofself
#scope(*args, **keywords, &blk)
- another method that should be private; this is what handles calling a proc
with the
GosuWrapper
instance as the value ofself
.
#config(&blk)
-
mostly the same thing as
#scope
but is meant to be used publically. -
the only functional difference is this always returns
self
(theGosuWrapper
instance) whereas#scope
returns the result of the block. -
Like
#scope
, this is a classic DSL-style method which exists for the sole purpose of saving keystrokes (usinginstance_exec
)
#add_hook(name, &blk)
-
a "hook" conceptually is a method internally defined by
Gosu::Window
that we're tapping into by overriding. Hooking into eventsupdate
,button_down
, anddraw
are how the app works fundamentally. -
the
button_down
proc is passed anid
which can be checked against the values in#buttons
to determine which key was pressed. -
update
anddraw
are run every tick. Conceptually,update
is used to change the state anddraw
to represent it.
#add_helper(name, &blk)
-
functionally speaking this is the exact same thing as
add_hook
- it defines a method onGosu::Window
. -
It exists to distinguish between built-in hooks like
update
from custom methods. -
These can be called with
#invoke
or its alias#call_helper
.
#show
- Starts the game. Needs to be called only once.
#draw_rect(start_x:, start_y:, end_x:, end_y:, color)
- Should be called from
draw
only. Fills the rectangle withcolor
- Needs to run every
draw
tick.
#colors
- a hash of name => color object pairs
- e.g.
colors[:red]
- it's also possible to make hexademical colors with Gosu
#buttons
- a hash of name => button id pairs
- e.g.
buttons[:right]
orbuttons[:escape]
- not all the gosu buttons are mapped here but I'd welcome a PR to add more.
#invoke/#call_helper/#dispatch/#call_hook
- all aliases for the same simple thing: calling a function on
window
(theGosu::Window instance
).
default helpers
There are a few predefined helpers.
They can be called with #call_helper(name_sym, *args, **keywords, &blk)
The required parameters differ between the helpers.
-
div_window_into(num_cols:, num_rows:, margin:)
- splits the window into a grid but doesn't draw it
- returns a matrix-like hash which can be passed to
draw_grid
as thesections
argument - uses
div_section_into
internally
-
div_setion_into(start_x: start_y:, end_x:, end_y:, num_cols:, num_rows:, margin:)
-
splits a rectangle into a grid but doesn't draw it
-
returns a matrix-like hash which can be passed to
draw_grid
-
margin
is an optional reduction of the size of each column. It's passed as1
indiv_window_into
and the columns won't be distinguishable (there will be no visible border) unless this is positive. -
specifically, the return value looks like this:
# many rows and columns can be stored in this structure { <row_idx> => { start_x: <num>, start_y: <num>, end_x: <num>, end_y: <num>, cols: { <col_idx> => { start_x: <num>, start_y: <num>, end_x: <num>, end_y: <num> } } } }
draw_grid(sections:, row_color:, col_color:)
- The result of
div_window_into
can be passed to this. - Internally it uses
GridHelpers#draw_rect