Class: Chingu::Window

Inherits:
Gosu::Window
  • Object
show all
Includes:
Helpers::GFX, Helpers::GameObject, Helpers::GameState, Helpers::InputClient, Helpers::InputDispatcher
Defined in:
lib/chingu/window.rb

Instance Attribute Summary collapse

Attributes included from Helpers::InputDispatcher

#input_clients

Instance Method Summary collapse

Methods included from Helpers::InputClient

#input, #input=

Methods included from Helpers::InputDispatcher

#add_input_client, #dispatch_action, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client

Methods included from Helpers::GameObject

#add_game_object, #game_objects_of_class, #remove_game_object

Methods included from Helpers::GameState

#clear_game_states, #current_game_state, #pop_game_state, #previous_game_state, #push_game_state, #switch_game_state, #transitional_game_state

Methods included from Helpers::GFX

#fill, #fill_gradient, #fill_rect

Constructor Details

#initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) ⇒ Window

See www.libgosu.org/rdoc/classes/Gosu/Window.html

On top of that we add:

  • Default widht / height, –fullscreen option from console

  • Global variable $window

  • Standard #update which updates all Chingu::GameObject’s

  • Standard #draw which goes through

  • Assethandling with Image and Sample



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chingu/window.rb', line 22

def initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666)
  fullscreen ||= ARGV.include?("--fullscreen")
  $window = super(width, height, fullscreen, update_interval)
			
  @root = File.dirname(File.expand_path($0))
  Gosu::Image.autoload_dirs = [".", File.join(@root, "gfx"), File.join(@root, "media")]
  Gosu::Sample.autoload_dirs = [".", File.join(@root, "sound"), File.join(@root, "media")]
  Gosu::Tile.autoload_dirs = [".", File.join(@root, "gfx"), File.join(@root, "media")]
  Gosu::Song.autoload_dirs = [".", File.join(@root, "sfx"), File.join(@root, "media")]
			
  @game_objects = GameObjectList.new
  @input_clients = Array.new
  
  @fps_counter = FPSCounter.new
  @game_state_manager = GameStateManager.new
  @milliseconds_since_last_tick = 0
end

Instance Attribute Details

#game_objectsObject (readonly)

Returns the value of attribute game_objects.



10
11
12
# File 'lib/chingu/window.rb', line 10

def game_objects
  @game_objects
end

#game_state_managerObject (readonly)

Returns the value of attribute game_state_manager.



10
11
12
# File 'lib/chingu/window.rb', line 10

def game_state_manager
  @game_state_manager
end

#milliseconds_since_last_tickObject (readonly)

Returns the value of attribute milliseconds_since_last_tick.



10
11
12
# File 'lib/chingu/window.rb', line 10

def milliseconds_since_last_tick
  @milliseconds_since_last_tick
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/chingu/window.rb', line 10

def root
  @root
end

Instance Method Details

#button_down(id) ⇒ Object

By default button_down sends the keyevent to the GameStateManager .. Which then is responsible to send it to the right GameState(s)



144
145
146
147
148
# File 'lib/chingu/window.rb', line 144

def button_down(id)
  dispatch_button_down(id, self)
  @input_clients.each { |object| dispatch_button_down(id, object) }
  @game_state_manager.button_down(id)
end

#button_up(id) ⇒ Object

By default button_up sends the keyevent to the GameStateManager .. Which then is responsible to send it to the right GameState(s)



134
135
136
137
138
# File 'lib/chingu/window.rb', line 134

def button_up(id)
  dispatch_button_up(id, self)
  @input_clients.each { |object| dispatch_button_up(id, object) }
  @game_state_manager.button_up(id)
end

#current_parentObject



40
41
42
# File 'lib/chingu/window.rb', line 40

def current_parent
  game_state_manager.current_game_state || self
end

#drawObject

Chingus main screen manupulation method. If you override this in your Chingu::Window class, make sure to call super. Gosu will call this each game-iteration just after #update



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chingu/window.rb', line 118

def draw
  #
  # Draw all game objects associated with the main window.      
  #
  @game_objects.draw
  
  #
  # Let the game state manager call draw on the active game state (if any)
  #
  @game_state_manager.draw
end

#dtObject

Mathematical short name for “milliseconds since last tick”



62
63
64
# File 'lib/chingu/window.rb', line 62

def dt
  @milliseconds_since_last_tick
end

#fpsObject Also known as: framerate

Frames per second, access with $window.fps or $window.framerate



47
48
49
# File 'lib/chingu/window.rb', line 47

def fps
  @fps_counter.fps
end

#intermediate_updateObject

“game logic” update that is safe to call even between Gosus update-calls



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chingu/window.rb', line 84

def intermediate_update
  #
  # Dispatch inputmap for main window
  #
  dispatch_input_for(self)
  
  #
  # Dispatch input for all input-clients handled by to main window (game objects with input created in main win)
  #
  @input_clients.each { |game_object| dispatch_input_for(game_object) }
  
  
  #
  # Call update() on all game objects belonging to the main window.
  #
  @game_objects.update
  
  #
  # Call update() on all game objects belonging to the current game state.
  #

  #
  # Call update() on our game_state_manger
  # -> call update on active states
  # -> call update on all game objects in that state
  #
  @game_state_manager.update
end

#ticksObject

Total amount of game iterations (ticks)



55
56
57
# File 'lib/chingu/window.rb', line 55

def ticks
  @fps_counter.ticks
end

#updateObject

Chingus core-logic / loop. Gosu will call this each game-iteration.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chingu/window.rb', line 69

def update
  #
  # Register a tick with our rather standard tick/framerate counter. 
  # Returns the amount of milliseconds since last tick. This number is used in all update()-calls.
  # Without this self.fps would return an incorrect value.
  # If you override this in your Chingu::Window class, make sure to call super.
  #
  @milliseconds_since_last_tick = @fps_counter.register_tick      
  
  intermediate_update
end