class Gosu::Window
Attributes
Public Class Methods
update_interval
-
Interval in milliseconds between two calls
to the update member function. The default means the game will run at 60 FPS, which is ideal on standard 60 Hz TFT screens.
# File lib/gosu_android/main-window.rb, line 75 def initialize(width, height, fullscreen, update_interval=16.666666) android_initializer = AndroidInitializer.instance @fullscreen = fullscreen @showing = false @activity = android_initializer.activity @display = @activity.getWindowManager.getDefaultDisplay @width = width @height= height @internal_update_interval = update_interval/1000.0 @update_interval = update_interval #@surface_view = GosuSurfaceView.new(@activity) @surface_view = android_initializer.surface_view @input = Input.new(@display, self) @surface_view.atributes(self, @input) #@graphics = Graphics.new(@width, @height, @fullscreen, self) @graphics = android_initializer.graphics @graphics.initialize_window(@width, @height, @fullscreen, self) #@surface_view.renderer = @graphics @surface_view.set_render_mode(JavaImports::GLSurfaceView::RENDERMODE_WHEN_DIRTY) @fonts_manager = FontsManager.new self @media_player = nil add_key_event_listener @activity.input = @input @showing_keyboard = false end
Public Instance Methods
TODO At least check if the methods were already defined, if so called them after/before this methods
# File lib/gosu_android/main-window.rb, line 107 def add_key_event_listener # Get the class of the object. @activity.class.class_eval do attr_accessor :input def on_destroy super #Release audio resources Song.release_resources end def onKeyDown(keyCode, event) if @input.feed_key_event_down(keyCode) return true else return super keyCode, event end end def onKeyUp(keyCode, event) if @input.feed_key_event_up(keyCode) return true else return super keyCode, event end end #TODO It does never get called, it does not matter how long #the press was def onKeyLongPress(keyCode, event) if @input.feed_key_event_up(keyCode) return true else return super keyCode, event end end end end
# File lib/gosu_android/main-window.rb, line 307 def caption= value @caption = value if @showing and not @fullscreen p = Proc.new do @activity.setTitle @caption end @activity.runOnUiThread(p) end end
Limits the drawing area to a given rectangle while evaluating the code inside of the block.
# File lib/gosu_android/main-window.rb, line 269 def clip_to(x, y, w, h, &rendering_code); end
Tells the window to end the current show loop as soon as possible.
# File lib/gosu_android/main-window.rb, line 184 def close #Self trigger window lost focus, since we are going to home focus_changed false, @screen_width, @screen_height @showing = false @activity.moveTaskToBack(true) end
# File lib/gosu_android/main-window.rb, line 373 def create_image(source, src_x, src_y, src_width, src_height, tileable) @graphics.create_image(source, src_x, src_y, src_width, src_height, tileable) end
# File lib/gosu_android/main-window.rb, line 173 def do_show @start_time = Time.now do_tick #TODO gosu dark side @end_time = Time.now if (@start_time <= @end_time and (@end_time - @start_time) < @internal_update_interval) sleep(@internal_update_interval - (@end_time - @start_time)) end end
# File lib/gosu_android/main-window.rb, line 317 def do_tick @input.update self.update @input.clear @graphics.begin(Color::BLACK) self.draw @graphics.end @surface_view.request_render end
Called after every update and when the OS wants the window to repaint itself. Your application’s rendering code goes here.
# File lib/gosu_android/main-window.rb, line 197 def draw; end
Draws a line from one point to another (last pixel exclusive). Note: OpenGL lines are not reliable at all and may have a missing pixel at the start or end point. Please only use this for debugging purposes. Otherwise, use a quad or image to simulate lines, or contribute a better draw_line
to Gosu
.
# File lib/gosu_android/main-window.rb, line 237 def draw_line(x1, y1, c1, x2, y2, c2, z=0, mode=:default) @graphics.draw_line(x1, y1, c1, x2, y2, c2, z, AM_MODES[mode]) end
Draws a rectangle (two triangles) with given corners and corresponding colors. The points can be in clockwise order, or in a Z shape.
# File lib/gosu_android/main-window.rb, line 248 def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z=0, mode=:default) @graphics.draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, AM_MODES[mode]) end
# File lib/gosu_android/main-window.rb, line 241 def draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z=0, mode=:default) @graphics.draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z, AM_MODES[mode]) end
Flushes all drawing operations to OpenGL so that Z-ordering can start anew. This is useful when drawing several parts of code on top of each other that use conflicting z positions.
# File lib/gosu_android/main-window.rb, line 255 def flush; end
TODO On screen rotation the app breaks down
# File lib/gosu_android/main-window.rb, line 328 def focus_changed has_focus, width, height @screen_width = width @screen_height = height if has_focus if @showing and @media_player != nil @media_player.start end #TODO Keyboard does not appears again if @showing_keyboard show_soft_keyboard end else #Hide keyboard but mark it so it will be shown later if @showing_keyboard hide_soft_keyboard @showing_keyboard = true end if @showing and @media_player != nil @media_player.pause end end return true end
For custom OpenGL calls. Executes the given block in a clean OpenGL environment. Use the ruby-opengl gem to access OpenGL function (if you manage to get it to work). IF no z position is given, it will execute the given block immediately, otherwise, the code will be scheduled to be called between Gosu
drawing operations.
Note: You cannot call Gosu
rendering functions within this block, and you can only call the gl function in the call tree of Window#draw
.
See examples/OpenGLIntegration.rb for an example.
# File lib/gosu_android/main-window.rb, line 266 def gl(z=nil, &custom_gl_code); end
# File lib/gosu_android/main-window.rb, line 366 def hide_soft_keyboard context = @activity.getApplicationContext imm = context.getSystemService(Context::INPUT_METHOD_SERVICE) imm.toggleSoftInput(JavaImports::InputMethodManager::HIDE_IMPLICIT_ONLY,0) @showing_keyboard = false end
Can be overriden to show the system cursor when necessary, e.g. in level editors or other situations where introducing a custom cursor is not desired.
# File lib/gosu_android/main-window.rb, line 210 def needs_cursor?; end
Can be overriden to give the game a chance to say no to being redrawn. This is not a definitive answer. The operating system can still cause redraws for one reason or another.
By default, the window is redrawn all the time (i.e. Window#needs_redraw?
always returns true.)
# File lib/gosu_android/main-window.rb, line 205 def needs_redraw?; end
Called when and object collides with another object
# File lib/gosu_android/main-window.rb, line 231 def object_collided(x, y, object); end
# File lib/gosu_android/main-window.rb, line 119 def onKeyDown(keyCode, event) if @input.feed_key_event_down(keyCode) return true else return super keyCode, event end end
TODO It does never get called, it does not matter how long the press was
# File lib/gosu_android/main-window.rb, line 137 def onKeyLongPress(keyCode, event) if @input.feed_key_event_up(keyCode) return true else return super keyCode, event end end
# File lib/gosu_android/main-window.rb, line 127 def onKeyUp(keyCode, event) if @input.feed_key_event_up(keyCode) return true else return super keyCode, event end end
# File lib/gosu_android/main-window.rb, line 113 def on_destroy super #Release audio resources Song.release_resources end
Returns a Gosu::Image
that containes everything rendered within the given block. It can be used to optimize rendering of many static images, e.g. the map. There are still several restrictions that you will be informed about via exceptions.
The returned Gosu::Image
will have the width and height you pass as arguments, regardless of how the area you draw on. It is important to pass accurate values if you plan on using Gosu::Image#draw_as_quad or Gosu::Image#draw_rot
with the result later.
@return [Gosu::Image]
# File lib/gosu_android/main-window.rb, line 280 def record(width, height, &rendering_code); end
Rotates everything drawn in the block around (around_x, around_y).
# File lib/gosu_android/main-window.rb, line 283 def rotate(angle, around_x=0, around_y=0, &rendering_code); end
Scales everything drawn in the block by a factor.
# File lib/gosu_android/main-window.rb, line 286 def scale(factor_x, factor_y=factor_x, &rendering_code); end
@deprecated Use Window#mouse_x=
and Window#mouse_y=
instead.
# File lib/gosu_android/main-window.rb, line 305 def set_mouse_position(x, y); end
Enters a modal loop where the Window
is visible on screen and receives calls to draw, update etc.
# File lib/gosu_android/main-window.rb, line 149 def show @showing = true if @fullscreen #Use full java path for Window, since there is a Gosu::Window @activity.request_window_feature(JavaImports::Window::FEATURE_NO_TITLE) @activity.get_window.set_flags(JavaImports::WindowManager::LayoutParams::FLAG_FULLSCREEN, JavaImports::WindowManager::LayoutParams::FLAG_FULLSCREEN) #@activity.content_view = @surface_view @window = @activity.getWindow #Replace position and size with gosu metrics else @window = @activity.getWindow #Only the thread that created the view can change it, so setLayout #and setTitle cannot be executed here p = Proc.new do @window.setLayout(@width, @height) @activity.setTitle @caption end @activity.runOnUiThread(p) end @screen_width = @surface_view.get_width @screen_height = @surface_view.get_height end
TODO It would be nice that the keyboard was transparent
# File lib/gosu_android/main-window.rb, line 359 def show_soft_keyboard context = @activity.getApplicationContext imm = context.getSystemService(Context::INPUT_METHOD_SERVICE) imm.toggleSoftInput(JavaImports::InputMethodManager::SHOW_FORCED,0) @showing_keyboard = true end
Called when the user started a touch on the screen
# File lib/gosu_android/main-window.rb, line 224 def touch_began(touch); end
Called when the user finished a touch on the screen
# File lib/gosu_android/main-window.rb, line 228 def touch_ended(touch); end
Called when the user continue a touch on the screen
# File lib/gosu_android/main-window.rb, line 226 def touch_moved(touch); end
Applies a free-form matrix rotation to everything drawn in the block.
# File lib/gosu_android/main-window.rb, line 295 def transform(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, &rendering_code); end
Moves everything drawn in the block by an offset in each dimension.
# File lib/gosu_android/main-window.rb, line 292 def translate(x, y, &rendering_code); end
Called every update_interval
milliseconds while the window is being shown. Your application’s main game logic goes here.
# File lib/gosu_android/main-window.rb, line 193 def update; end