Sha256: 56d1af83f8fe80593dd5450bf687c065e30bcb986c063f01e50763f119b9fd06

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module Lotu
  class Window < Gosu::Window
    extend HasBehavior

    # delta time
    attr_reader :dt
    attr_accessor :update_queue, :draw_queue, :input_listeners, :font

    def initialize(params={})
      super(640, 480, false)

      # Handy global window variable
      $window = self

      # Systems setup
      @update_queue = []
      @draw_queue = []
      @input_register = Hash.new{|hash,key| hash[key] = []}

      @fps_counter = FpsCounter.new
      @last_time = Gosu::milliseconds
      @font = Gosu::Font.new(self, Gosu::default_font_name, 14)

      # Initialize the behaviors included in subclasses
      init_behavior
    end

    def update
      new_time = Gosu::milliseconds
      @dt = (new_time - @last_time)/1000.0
      @last_time = new_time
      @fps_counter.update(@dt)

      @update_queue.each do |item|
        item.update
      end
    end

    def draw
      @draw_queue.each do |item|
        item.draw
      end
    end

    def button_down(id)
      @input_register[id].each do |item|
        item.button_down(id)
      end
    end

    def button_up(id)
      @input_register[id].each do |item|
        item.button_up(id)
      end
    end

    # Register controller
    def register_for_input(controller)
      controller.keys.each_key do |key|
        @input_register[key] << controller
      end
      @update_queue << controller
    end

    def register_for_draw(object)
      @draw_queue << object
    end

    # Meant to be overriden by behaviors
    def init_behavior;end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lotu-0.1.1 lib/lotu/window.rb
lotu-0.1.0 lib/lotu/window.rb