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
#windowwhich 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_heightand:window_width. - All of the keys in your app's state hash should be given here.
- Each of them get attr_accessors defined on
#windowand 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_helperso it's worth explaining.It defines the method named
nameonwindowwithblkas its body.Arguments are determined by the given block
The block is always invoked with the
GosuWrapperinstance as the value ofself
#scope(*args, **keywords, &blk)
- another method that should be private; this is what handles calling a proc
with the
GosuWrapperinstance as the value ofself.
#config(&blk)
mostly the same thing as
#scopebut is meant to be used publically.the only functional difference is this always returns
self(theGosuWrapperinstance) whereas#scopereturns 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::Windowthat we're tapping into by overriding. Hooking into eventsupdate,button_down, anddraware how the app works fundamentally.the
button_downproc is passed anidwhich can be checked against the values in#buttonsto determine which key was pressed.updateanddraware run every tick. Conceptually,updateis used to change the state anddrawto 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
updatefrom custom methods.These can be called with
#invokeor 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
drawonly. Fills the rectangle withcolor - Needs to run every
drawtick.
#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_gridas thesectionsargument - uses
div_section_intointernally
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 marginis an optional reduction of the size of each column. It's passed as1indiv_window_intoand 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 {
=> { start_x: , start_y: , end_x: , end_y: , cols: { => { start_x: , start_y: , end_x: , end_y: } } } }
draw_grid(sections:, row_color:, col_color:)- The result of
div_window_intocan be passed to this. - Internally it uses
GridHelpers#draw_rect
- The result of