Class: Shattered::Game

Inherits:
Object show all
Includes:
Model, View
Defined in:
lib/shattered_game/game.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from View

append_features

Methods included from Model

append_features

Constructor Details

#initializeGame

Sets up the initial render window and initializes Ogre



16
17
18
19
20
21
22
23
# File 'lib/shattered_game/game.rb', line 16

def initialize
   @@game_instance = self
   setup_logger(Logger::DEBUG, STDOUT)
   throw Shattered::Error.new("User cancelled") unless setup_ogre
   setup_input unless running_specs?
   #Do this once on startup, since OSX wasn't getting the event.
   update_window_size
end

Instance Attribute Details

#cameraObject (readonly)

Returns the value of attribute camera.



6
7
8
# File 'lib/shattered_game/game.rb', line 6

def camera
  @camera
end

#keyboardObject

Returns the value of attribute keyboard.



7
8
9
# File 'lib/shattered_game/game.rb', line 7

def keyboard
  @keyboard
end

#keyboard_listenerObject

Returns the value of attribute keyboard_listener.



7
8
9
# File 'lib/shattered_game/game.rb', line 7

def keyboard_listener
  @keyboard_listener
end

#logObject (readonly)

Returns the value of attribute log.



6
7
8
# File 'lib/shattered_game/game.rb', line 6

def log
  @log
end

#mouseObject

Returns the value of attribute mouse.



7
8
9
# File 'lib/shattered_game/game.rb', line 7

def mouse
  @mouse
end

#render_windowObject (readonly)

Returns the value of attribute render_window.



6
7
8
# File 'lib/shattered_game/game.rb', line 6

def render_window
  @render_window
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/shattered_game/game.rb', line 6

def root
  @root
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/shattered_game/game.rb', line 8

def title
  @title
end

#viewportObject (readonly)

Returns the value of attribute viewport.



6
7
8
# File 'lib/shattered_game/game.rb', line 6

def viewport
  @viewport
end

Class Method Details

.instanceObject



232
233
234
# File 'lib/shattered_game/game.rb', line 232

def self.instance
  @@game_instance ||= self.new
end

.scene_managerObject

Game.scene_manager returns the same scene_manager as Game.instance.scene_manager



84
85
86
# File 'lib/shattered_game/game.rb', line 84

def self.scene_manager
  @@scene_manager
end

.scene_manager=(val) ⇒ Object

Set the scene_manager…



89
90
91
# File 'lib/shattered_game/game.rb', line 89

def self.scene_manager=(val)
  @@scene_manager = val
end

.title(title) ⇒ Object

Sets up the game’s title.



11
12
13
# File 'lib/shattered_game/game.rb', line 11

def self.title(title)
  before_init_call("title=", title)
end

Instance Method Details

#create_camera(options) ⇒ Object

Returns the camera used in the state. See ShatteredState::ClassMethods#camera



223
224
225
226
227
228
229
230
# File 'lib/shattered_game/game.rb', line 223

def create_camera(options)
  log.info "Creating camera"
  @camera = scene_manager.create_camera(options[:name] || "main")
 @camera.set_near_clip_distance(options[:near_clip] || 1)
 @camera.set_far_clip_distance(options[:far_clip] || 10000)
  @camera.position = options[:position] if options[:position]
  @camera.look_at(options[:look_at]) if options[:look_at]
end

#create_scene_manager(type) ⇒ Object

Creates a scene manager for Ogre to use.



216
217
218
219
220
# File 'lib/shattered_game/game.rb', line 216

def create_scene_manager(type)
   log.info "Creating scene manager #{type}"
 	root = Ogre::Root.instance
  @@scene_manager = root.create_scene_manager(ShatteredOgre.translate_to_scene_type(type), "#{root.to_s}.scene_manager(#{type})")
end

#create_viewport(options) ⇒ Object

Creates a viewport based off of the Camera.



209
210
211
212
213
# File 'lib/shattered_game/game.rb', line 209

def create_viewport(options)
  log.info "Creating viewport"
	@viewport = render_window.add_viewport(camera)
	@viewport.set_background_colour(options[:color] || rgb(0,0,0))
end

#each_frameObject

Main game loop



63
64
65
66
67
68
69
70
71
# File 'lib/shattered_game/game.rb', line 63

def each_frame
 	timer = Ogre::Timer.new
   while !quit? && Ogre::Root::instance.render_one_frame
  return false if @render_window.closed?
   	seconds = timer.get_microseconds / 1000000.0
    timer.reset
	  yield seconds
   end
end

#input_optionsObject

Overwrite this method to setup how you would like the window to act. DISCL_NONEXCLUSIVE means the mouse and keyboard are not bound to your program.



197
198
199
200
# File 'lib/shattered_game/game.rb', line 197

def input_options
  { "w32_mouse" => ["DISCL_FOREGROUND", "DISCL_NONEXCLUSIVE"],
    "w32_keyboard" => ["DISCL_FOREGROUND", "DISCL_NONEXCLUSIVE"] }
end

#load_resources(*paths) ⇒ Object

Sets all the resources in paths to be loaded as FileSystem General resources



26
27
28
29
30
31
# File 'lib/shattered_game/game.rb', line 26

def load_resources(*paths)
 	@media_paths = paths
 	# setup our resource paths specified in environment.rb
   Resources.instance.add_resource_paths(*paths)
   Resources.instance.setup
end

#playObject

Every time this exits, a game dies.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shattered_game/game.rb', line 34

def play
  begin
    each_frame do |time_elapsed|
      Ogre::WindowEventUtilities::message_pump
      unless running_specs?
  		  @keyboard.flush
  		  @ois_keyboard.capture
  		  @mouse.process_events @ois_mouse.mouse_state
  		  @ois_mouse.capture
      end
      Timer::Timer.update(time_elapsed)
    end
  rescue
    error = "Exception:\n\n#{$!}\n"
    
    # Let's hide the shit that's related to shattered unless SHATTERED_STACK_TRACE is true
    backtrace = $!.backtrace
    unless(ENV["SHATTERED_STACK_TRACE"])
      backtrace = backtrace.select { |bt| (bt =~ /shattered/).nil? }
    end
    backtrace.each do |details|
      error += "        from #{details}\n"
    end
    log.error(error)
  end
  shutdown
end

#quitObject

Quit the game from the game



79
80
81
# File 'lib/shattered_game/game.rb', line 79

def quit
  @quit = true
end

#quit?Boolean

Has the user quit? Call Game.quit! to exit the game

Returns:

  • (Boolean)


74
75
76
# File 'lib/shattered_game/game.rb', line 74

def quit?
  @quit ||= false
end

#running_specs?Boolean

Returns false if we are running the spec suite, true otherwise

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
# File 'lib/shattered_game/game.rb', line 151

def running_specs?
  begin
    # The specs shouldn't require user input but should still initialize the window
    return true if SHATTERED_SPEC == true
  rescue NameError
    # Specs are not running
    return false
  end

end

#scene_managerObject

Return the current scene_manager



94
95
96
# File 'lib/shattered_game/game.rb', line 94

def scene_manager
  @@scene_manager
end

#setup_inputObject

Set up the mouse and keyboard



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/shattered_game/game.rb', line 181

def setup_input
  log.info "Initializing mouse and keyboard"
  window_handle = render_window.get_custom_attribute_unsigned_long("WINDOW")
  window_options = {"WINDOW" => window_handle.to_s}
  
  @input_manager = OIS::InputManager.create_input_system(window_options.merge(input_options))
  @ois_mouse = @input_manager.create_input_object( OIS::OISMouse, buffered_mouse=false)
  @mouse = OIS::MouseManager.new(@ois_mouse)
  
  @ois_keyboard = @input_manager.create_input_object(OIS::OISKeyboard, true)
@keyboard = OIS::KeyManager.new(@ois_keyboard)
@keyboard_listener = Shattered::Input::Keyboard.new(@keyboard, @mouse) 
end

#setup_logger(level, where = "#{SHATTERED_ROOT}/log/game.log") ⇒ Object

Load a logger with a given level and log to a specific file.



99
100
101
102
103
104
105
106
107
108
# File 'lib/shattered_game/game.rb', line 99

def setup_logger(level, where="#{SHATTERED_ROOT}/log/game.log")
  if(where.is_a?(String))
    @log = Logger.new(open(where, File::WRONLY | File::CREAT))
  else
    @log = Logger.new(where)
  end
  @log.level = level

  @log.info("Game starting")
end

#setup_ogreObject

Load the root, setup the render window



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/shattered_game/game.rb', line 111

def setup_ogre
	plugins = SHATTERED_ROOT + "/config/ogre_plugins.#{Platform.short_name}.cfg"
	log.info("Ogre plugins: '#{plugins}'")
	config_save = SHATTERED_ROOT + "/config/ogrerb_defaults.save"
	log.info("Config saved: '#{config_save}'")
	ogre_log = SHATTERED_ROOT + "/log/ogrerb.log"
	log.info("Ogre's log  : '#{ogre_log}")
 
	log.debug("Creating Root")
	@root = ShatteredOgre.create_root(plugins, config_save, ogre_log)
	log.debug("Showing config dialog")

  # Allows us to avoid user input for the spec suite
  if running_specs?
    show_spec_window
  else
	  return false if !show_config_dialog
	end
	
	log.debug("Creating Render Window")
	@render_window = @root.initialise(true, @title || "No Title")

	log.debug("Adding WindowEvent Listener")
	@window_event = setup_window_events

	log.info("Ogre setup successfully.")
	return true
end

#setup_window_eventsObject

Sets up the window events resize and close



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/shattered_game/game.rb', line 163

def setup_window_events
  log.info "Registering window.resize and window.close listeners"
  event = Ogre::WindowEvents.new @render_window
  
	#Resize the window and let OIS know
  event.on_resize do
    update_window_size
  end
  
  #This is cleanup once the window is closed
  event.on_close do
    quit
  end
  
  return event
end

#show_config_dialogObject

Overwrite this to create your own config dialog. Return true to keep going, or false to cancel the startup.



204
205
206
# File 'lib/shattered_game/game.rb', line 204

def show_config_dialog
  return @root.show_config_dialog
end

#show_spec_windowObject

Create a blank window without user input



141
142
143
144
145
146
147
148
# File 'lib/shattered_game/game.rb', line 141

def show_spec_window
  unless Platform.mac?
    renderers = @root.get_available_renderers
	  @root.set_render_system(renderers[0])
 else
   show_config_dialog
 end
end