Class: Chingu::Window
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- Chingu::Window
- Includes:
- Helpers::GFX, Helpers::GameObject, Helpers::GameState, Helpers::InputClient, Helpers::InputDispatcher
- Defined in:
- lib/chingu/window.rb
Overview
See www.libgosu.org/rdoc/classes/Gosu/Window.html
On top of that we add:
-
Default widht / height, –fullscreen option from console
-
Sets a global variable $window = self, which is then used throughout Chingu
-
Defaultd #update which updates all game_objects which are not pasued
-
Default #draw which draws all game_objects which are visible
-
Default Asset-directories media/, sfx/, gfx/ etc.
-
Tracking of button_up/button_down etc to enable Chingus pretty inputhandling
Constant Summary
Constants included from Helpers::GFX
Instance Attribute Summary collapse
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#factor ⇒ Object
Returns the value of attribute factor.
-
#game_objects ⇒ Object
readonly
Returns the value of attribute game_objects.
-
#game_state_manager ⇒ Object
readonly
Returns the value of attribute game_state_manager.
-
#milliseconds_since_last_tick ⇒ Object
readonly
Returns the value of attribute milliseconds_since_last_tick.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Attributes included from Helpers::InputDispatcher
Instance Method Summary collapse
-
#button_down(id) ⇒ Object
By default button_down sends the keyevent to the GameStateManager ..
-
#button_up(id) ⇒ Object
By default button_up sends the keyevent to the GameStateManager ..
-
#close ⇒ Object
Close the window when it is no longer required.
-
#current_scope ⇒ Object
Returns self inside GameState.initialize (a game state is not ‘active’ inside initialize()) Or returns current active game state (as in a switched to or pushed game state) …
-
#draw ⇒ Object
Chingus main screen manupulation method.
-
#dt ⇒ Object
Mathematical short name for “milliseconds since last tick”.
-
#fps ⇒ Object
(also: #framerate)
Frames per second, access with $window.fps or $window.framerate.
-
#initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) ⇒ Window
constructor
A new instance of Window.
-
#intermediate_update ⇒ Object
“game logic” update that is safe to call even between Gosus update-calls.
-
#needs_cursor? ⇒ Boolean
If this returns true, GOSU will show a cursor Chingu solves this with the $window.cursor = [true|false] accessor.
-
#retrofy ⇒ Object
Make all old and future images use hard borders.
-
#setup ⇒ Object
Placeholder to be overwritten.
-
#ticks ⇒ Object
Total amount of game iterations (ticks).
-
#update ⇒ Object
Chingus core-logic / loop.
Methods included from Helpers::InputClient
#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input
Methods included from Helpers::InputDispatcher
#add_input_client, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client
Methods included from Helpers::GameObject
#add_game_object, #game_objects_of_class, #hide_game_object, #load_game_objects, #pause_game_object, #remove_game_object, #save_game_objects, #show_game_object, #unpause_game_object
Methods included from Helpers::GameState
#clear_game_states, #current_game_state, #game_states, #pop_game_state, #push_game_state, #switch_game_state, #transitional_game_state
Methods included from Helpers::GFX
#draw_circle, #draw_rect, #fill, #fill_gradient, #fill_rect
Constructor Details
#initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) ⇒ Window
Returns a new instance of Window.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/chingu/window.rb', line 44 def initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666) raise "Cannot create a new #{self.class} before the old one has been closed" if $window fullscreen ||= ARGV.include?("--fullscreen") $window = super(width, height, fullscreen, update_interval) @root = File.dirname(File.($0)) Chingu::Asset.autoload_dirs += [".", File.join(@root, "assets"), File.join(@root, "media")] Gosu::Image.autoload_dirs += [".", File.join(@root, "images"), File.join(@root, "gfx"), File.join(@root, "media")] Gosu::Sample.autoload_dirs += [".", File.join(@root, "sounds"), File.join(@root, "sfx"), File.join(@root, "media")] Gosu::Song.autoload_dirs += [".", File.join(@root, "songs"), File.join(@root, "sounds"), File.join(@root, "sfx"), File.join(@root, "media")] Gosu::Font.autoload_dirs += [".", File.join(@root, "fonts"), 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 @factor = 1 @cursor = false setup end |
Instance Attribute Details
#cursor ⇒ Object
Returns the value of attribute cursor.
42 43 44 |
# File 'lib/chingu/window.rb', line 42 def cursor @cursor end |
#factor ⇒ Object
Returns the value of attribute factor.
42 43 44 |
# File 'lib/chingu/window.rb', line 42 def factor @factor end |
#game_objects ⇒ Object (readonly)
Returns the value of attribute game_objects.
41 42 43 |
# File 'lib/chingu/window.rb', line 41 def game_objects @game_objects end |
#game_state_manager ⇒ Object (readonly)
Returns the value of attribute game_state_manager.
41 42 43 |
# File 'lib/chingu/window.rb', line 41 def game_state_manager @game_state_manager end |
#milliseconds_since_last_tick ⇒ Object (readonly)
Returns the value of attribute milliseconds_since_last_tick.
41 42 43 |
# File 'lib/chingu/window.rb', line 41 def milliseconds_since_last_tick @milliseconds_since_last_tick end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
41 42 43 |
# File 'lib/chingu/window.rb', line 41 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)
199 200 201 202 203 |
# File 'lib/chingu/window.rb', line 199 def (id) (id, self) @input_clients.each { |object| (id, object) unless object.paused? } @game_state_manager.(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)
189 190 191 192 193 |
# File 'lib/chingu/window.rb', line 189 def (id) (id, self) @input_clients.each { |object| (id, object) unless object.paused? } @game_state_manager.(id) end |
#close ⇒ Object
Close the window when it is no longer required. Ensure this is done before a new window is initialized.
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/chingu/window.rb', line 208 def close super # Clear out all assets, tied to this $window, so that a new instance can create more. [Gosu::Image, Gosu::Song, Gosu::Font, Gosu::Sample].each do |asset| asset.clear end $window = nil end |
#current_scope ⇒ Object
Returns self inside GameState.initialize (a game state is not ‘active’ inside initialize()) Or returns current active game state (as in a switched to or pushed game state) … Falls back to returning $window
current_scope is used to make GameObject.all and friends work everywhere.
95 96 97 |
# File 'lib/chingu/window.rb', line 95 def current_scope game_state_manager.inside_state || game_state_manager.current_game_state || self end |
#draw ⇒ Object
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
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/chingu/window.rb', line 173 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 |
#dt ⇒ Object
Mathematical short name for “milliseconds since last tick”
117 118 119 |
# File 'lib/chingu/window.rb', line 117 def dt @milliseconds_since_last_tick end |
#fps ⇒ Object Also known as: framerate
Frames per second, access with $window.fps or $window.framerate
102 103 104 |
# File 'lib/chingu/window.rb', line 102 def fps @fps_counter.fps end |
#intermediate_update ⇒ Object
“game logic” update that is safe to call even between Gosus update-calls
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/chingu/window.rb', line 139 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) unless game_object.paused? } # # 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 |
#needs_cursor? ⇒ Boolean
If this returns true, GOSU will show a cursor Chingu solves this with the $window.cursor = [true|false] accessor
74 75 76 |
# File 'lib/chingu/window.rb', line 74 def needs_cursor? @cursor end |
#retrofy ⇒ Object
Make all old and future images use hard borders. Hard borders + scaling = retro feel!
84 85 86 |
# File 'lib/chingu/window.rb', line 84 def retrofy Gosu::enable_undocumented_retrofication end |
#setup ⇒ Object
Placeholder to be overwritten
79 |
# File 'lib/chingu/window.rb', line 79 def setup; end |
#ticks ⇒ Object
Total amount of game iterations (ticks)
110 111 112 |
# File 'lib/chingu/window.rb', line 110 def ticks @fps_counter.ticks end |
#update ⇒ Object
Chingus core-logic / loop. Gosu will call this each game-iteration.
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/chingu/window.rb', line 124 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 |