Class: Chingu::Window

Inherits:
Gosu::Window
  • Object
show all
Includes:
Helpers::FPSCounter, 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

DEFAULT_VOLUME =

GLOBAL SOUND SETTINGS

1.0

Instance Attribute Summary collapse

Attributes included from Helpers::InputDispatcher

#input_clients

Instance Method Summary collapse

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

#game_objects_of_class, #load_game_objects, #save_game_objects

Methods included from Helpers::GFX

#draw_arc, #draw_circle, #draw_rect, #fill, #fill_arc, #fill_circle, #fill_gradient, #fill_rect

Constructor Details

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

Returns a new instance of Window



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chingu/window.rb', line 45

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.expand_path($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
  @times_muted = 0
  @volume = DEFAULT_VOLUME
  
  setup
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor



43
44
45
# File 'lib/chingu/window.rb', line 43

def cursor
  @cursor
end

#factorObject

Returns the value of attribute factor



43
44
45
# File 'lib/chingu/window.rb', line 43

def factor
  @factor
end

#game_objectsObject (readonly)

Returns the value of attribute game_objects



42
43
44
# File 'lib/chingu/window.rb', line 42

def game_objects
  @game_objects
end

#game_state_managerObject (readonly)

Returns the value of attribute game_state_manager



42
43
44
# File 'lib/chingu/window.rb', line 42

def game_state_manager
  @game_state_manager
end

#milliseconds_since_last_tickObject (readonly)

Returns the value of attribute milliseconds_since_last_tick



42
43
44
# File 'lib/chingu/window.rb', line 42

def milliseconds_since_last_tick
  @milliseconds_since_last_tick
end

#rootObject (readonly)

Returns the value of attribute root



42
43
44
# File 'lib/chingu/window.rb', line 42

def root
  @root
end

#volumeObject

Volume of all Samples and Songs, not affected by the Window being muted.



217
218
219
# File 'lib/chingu/window.rb', line 217

def volume
  @volume
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)



180
181
182
183
184
# File 'lib/chingu/window.rb', line 180

def button_down(id)
  dispatch_button_down(id, self)
  @input_clients.each { |object| dispatch_button_down(id, object) unless object.paused? }
  @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)



170
171
172
173
174
# File 'lib/chingu/window.rb', line 170

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

#closeObject

Close the window when it is no longer required. Ensure this is done before a new window is initialized.



189
190
191
192
193
194
195
196
197
198
# File 'lib/chingu/window.rb', line 189

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_scopeObject

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.



98
99
100
# File 'lib/chingu/window.rb', line 98

def current_scope
  game_state_manager.inside_state || 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



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/chingu/window.rb', line 154

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

#effective_volumeObject

Actual volume of all Samples and Songs, affected by the Window being muted.



220
221
222
# File 'lib/chingu/window.rb', line 220

def effective_volume
  muted? ? 0.0 : @volume
end

#intermediate_updateObject

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chingu/window.rb', line 120

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

#muteObject

Mute the window and all Samples and Songs played. Muting stacks, so sound will only be heard if the number of unmutes is the same as the number of mutes.



226
227
228
229
230
231
232
233
# File 'lib/chingu/window.rb', line 226

def mute
  unless muted?
    Song.send(:resources).each_value {|song| song.send :mute }
  end
  @times_muted += 1

  self
end

#muted?Boolean

Is the window currently muted?

Returns:

  • (Boolean)


248
249
250
# File 'lib/chingu/window.rb', line 248

def muted?
  @times_muted > 0
end

#needs_cursor?Boolean

If this returns true, GOSU will show a cursor Chingu solves this with the $window.cursor = [true|false] accessor

Returns:

  • (Boolean)


77
78
79
# File 'lib/chingu/window.rb', line 77

def needs_cursor?
  @cursor
end

#retrofyObject

Make all old and future images use hard borders. Hard borders + scaling = retro feel!



87
88
89
# File 'lib/chingu/window.rb', line 87

def retrofy
  Gosu::enable_undocumented_retrofication
end

#setupObject

Placeholder to be overwritten



82
# File 'lib/chingu/window.rb', line 82

def setup; end

#unmuteObject

Unmute the window and all Samples and Songs played. Muting stacks, so sound will only be heard if the number of unmutes is the same as the number of mutes.



237
238
239
240
241
242
243
244
245
# File 'lib/chingu/window.rb', line 237

def unmute
  raise "Can't unmute when not muted" unless muted?
  @times_muted -= 1
  unless muted?
    Song.send(:resources).each_value {|song| song.send :unmute }
  end

  self
end

#updateObject

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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chingu/window.rb', line 105

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