[jrme~main:83954028] Dump of initial and very partial gosu support

  • From: enebo@kenai.com
  • To: commits@jrme.kenai.com
  • Subject: [jrme~main:83954028] Dump of initial and very partial gosu support
  • Date: Sat, 22 Jan 2011 16:18:13 +0000

Project:    jrme
Repository: main
Revision:   83954028c16ff5861c28ba1c0c2d76d57315e2f9
Author:     enebo
Date:       2011-01-22 16:17:39 UTC
Link:       

Log Message:
------------
Another class for swarm demo
Dump of initial and very partial gosu support


Revisions:
----------
5a250b9e8aba55610318d080f9b751e94f4db651
83954028c16ff5861c28ba1c0c2d76d57315e2f9


Modified Paths:
---------------
lib/jme.rb


Added Paths:
------------
lib/gosu.rb
lib/gosu/button.rb
lib/gosu/font.rb
lib/gosu/gosu.rb
lib/gosu/image.rb
lib/gosu/window.rb


Diffs:
------
diff --git a/lib/jme.rb b/lib/jme.rb
index 5f3ad40..831846b 100644
--- a/lib/jme.rb
+++ b/lib/jme.rb
@@ -30,6 +30,7 @@ java_import com.jme.input.action.KeyNodeForwardAction
 java_import com.jme.input.action.KeyNodeRotateLeftAction
 java_import com.jme.input.action.KeyNodeRotateRightAction
 java_import com.jme.input.thirdperson.ThirdPersonMouseLook
+java_import com.jme.intersection.BoundingCollisionResults
 java_import com.jme.intersection.BoundingPickResults
 java_import com.jme.intersection.PickResults
 java_import com.jme.light.DirectionalLight
diff --git a/lib/gosu.rb b/lib/gosu.rb
new file mode 100644
index 0000000..ed2f363
--- /dev/null
+++ b/lib/gosu.rb
@@ -0,0 +1,4 @@
+require 'gosu/gosu'
+require 'gosu/window'
+require 'gosu/image'
+require 'gosu/button'
diff --git a/lib/gosu/button.rb b/lib/gosu/button.rb
new file mode 100644
index 0000000..419d68f
--- /dev/null
+++ b/lib/gosu/button.rb
@@ -0,0 +1,120 @@
+module Gosu
+  module Button
+    Kb0 = KeyInput::KEY_0
+    Kb1 = KeyInput::KEY_1
+    Kb2 = KeyInput::KEY_2
+    Kb3 = KeyInput::KEY_3
+    Kb4 = KeyInput::KEY_4
+    Kb5 = KeyInput::KEY_5
+    Kb6 = KeyInput::KEY_6
+    Kb7 = KeyInput::KEY_7
+    Kb8 = KeyInput::KEY_8
+    Kb9 = KeyInput::KEY_9
+    KbA = KeyInput::KEY_A
+    KbB = KeyInput::KEY_B
+    KbC = KeyInput::KEY_C
+    KbD = KeyInput::KEY_D
+    KbE = KeyInput::KEY_E
+    KbF = KeyInput::KEY_F
+    KbG = KeyInput::KEY_G
+    KbH = KeyInput::KEY_H
+    KbI = KeyInput::KEY_I
+    KbJ = KeyInput::KEY_J
+    KbK = KeyInput::KEY_K
+    KbL = KeyInput::KEY_L
+    KbM = KeyInput::KEY_M
+    KbN = KeyInput::KEY_N
+    KbO = KeyInput::KEY_O
+    KbP = KeyInput::KEY_P
+    KbQ = KeyInput::KEY_Q
+    KbR = KeyInput::KEY_R
+    KbS = KeyInput::KEY_S
+    KbT = KeyInput::KEY_T
+    KbU = KeyInput::KEY_U
+    KbV = KeyInput::KEY_V
+    KbW = KeyInput::KEY_W
+    KbX = KeyInput::KEY_X
+    KbY = KeyInput::KEY_Y
+    KbZ = KeyInput::KEY_Z
+    KbBackspace = KeyInput::KEY_BACK
+    KbDelete = KeyInput::KEY_DELETE
+    KbDown = KeyInput::KEY_DOWN
+    KbEnd = KeyInput::KEY_END
+    KbEnter = KeyInput::KEY_RETURN
+    KbEscape = KeyInput::KEY_ESCAPE
+    KbF1 = KeyInput::KEY_F1
+    KbF10 = KeyInput::KEY_F10
+    KbF11 = KeyInput::KEY_F11
+    KbF12 = KeyInput::KEY_F12
+    KbF2 = KeyInput::KEY_F2
+    KbF3 = KeyInput::KEY_F3
+    KbF4 = KeyInput::KEY_F4
+    KbF5 = KeyInput::KEY_F5
+    KbF6 = KeyInput::KEY_F6
+    KbF7 = KeyInput::KEY_F7
+    KbF8 = KeyInput::KEY_F8
+    KbF9 = KeyInput::KEY_F9
+    KbHome = KeyInput::KEY_HOME
+    KbInsert = KeyInput::KEY_INSERT
+    KbLeft = KeyInput::KEY_LEFT
+    KbLeftAlt = KeyInput::KEY_LMENU
+    KbLeftControl = KeyInput::KEY_LCONTROL
+    KbLeftShift = KeyInput::KEY_LSHIFT
+    KbNumpad0 = KeyInput::KEY_NUMPAD0
+    KbNumpad1 = KeyInput::KEY_NUMPAD1
+    KbNumpad2 = KeyInput::KEY_NUMPAD2
+    KbNumpad3 = KeyInput::KEY_NUMPAD3
+    KbNumpad4 = KeyInput::KEY_NUMPAD4
+    KbNumpad5 = KeyInput::KEY_NUMPAD5
+    KbNumpad6 = KeyInput::KEY_NUMPAD6
+    KbNumpad7 = KeyInput::KEY_NUMPAD7
+    KbNumpad8 = KeyInput::KEY_NUMPAD8
+    KbNumpad9 = KeyInput::KEY_NUMPAD9
+    KbNumpadAdd = KeyInput::KEY_ADD
+    KbNumpadDivide = KeyInput::KEY_DIVIDE
+    KbNumpadMultiply = KeyInput::KEY_MULTIPLY
+    KbNumpadSubtract = KeyInput::KEY_SUBTRACT
+    KbPageDown = KeyInput::KEY_PGDN
+    KbPageUp = KeyInput::KEY_PGUP
+    KbPause = KeyInput::KEY_PAUSE
+    KbReturn = KeyInput::KEY_RETURN
+    KbRight = KeyInput::KEY_RIGHT
+    KbRightAlt = KeyInput::KEY_RMENU
+    KbRightControl = KeyInput::KEY_RCONTROL
+    KbRightShift = KeyInput::KEY_RSHIFT
+    KbSpace = KeyInput::KEY_SPACE
+    KbTab = KeyInput::KEY_TAB
+    KbUp = KeyInput::KEY_UP
+    # TODO: Fix
+    MsLeft = KeyInput::KEY_A
+    MsMiddle = KeyInput::KEY_A
+    MsRight = KeyInput::KEY_A
+    MsWheelDown = KeyInput::KEY_A
+    MsWheelUp = KeyInput::KEY_A
+    GpButton0 = KeyInput::KEY_A
+    GpButton1 = KeyInput::KEY_A
+    GpButton10 = KeyInput::KEY_A
+    GpButton11 = KeyInput::KEY_A
+    GpButton12 = KeyInput::KEY_A
+    GpButton13 = KeyInput::KEY_A
+    GpButton14 = KeyInput::KEY_A
+    GpButton15 = KeyInput::KEY_A
+    GpButton2 = KeyInput::KEY_A
+    GpButton3 = KeyInput::KEY_A
+    GpButton4 = KeyInput::KEY_A
+    GpButton5 = KeyInput::KEY_A
+    GpButton6 = KeyInput::KEY_A
+    GpButton7 = KeyInput::KEY_A
+    GpButton8 = KeyInput::KEY_A
+    GpButton9 = KeyInput::KEY_A
+    GpDown = KeyInput::KEY_A
+    GpLeft = KeyInput::KEY_A
+    GpRight = KeyInput::KEY_A
+    GpUp = KeyInput::KEY_A
+
+    MAPPINGS = Gosu::Button.constants.inject({}) do |map, constant|
+      map[Gosu::Button.const_get(constant)] = constant
+      map
+    end
+  end
+end
diff --git a/lib/gosu/font.rb b/lib/gosu/font.rb
new file mode 100644
index 0000000..79554a6
--- /dev/null
+++ b/lib/gosu/font.rb
@@ -0,0 +1,45 @@
+module Gosu
+  class Font
+    attr_reader :height
+
+    # font_name can either be the name of a system font, or a filename 
+    # (must contain '/', does not work on Linux yet). height is the height 
+    # of the font, in pixels.
+    def initialize(window, font_name, height)
+      @height = height
+      @font_name = font_name
+      @font_size = 12
+      # Todo load font
+      @font = BitmapFontLoader.loadDefaultFont
+    end
+
+    # Returns the width in pixels the given text would span.
+    def text_width(text, factor_x=1)
+    end
+
+    def draw(text, x, y, z, factor_x=1, factor_y=1, color=0xffffffff, 
mode=:default)
+    end
+
+    # If relX is 0.0, the text will be to the right of x, if it is 1.0, the 
text
+    # will be to the left of x, if it is 0.5, it will be centered on x. Of
+    # course, all real numbers are possible values. The same applies to relY.
+    def draw_rel(text, x, y, z, rel_x, rel_y, factor_x=1, factor_y=1, 
color=0xffffffff, mode=:default)
+    end
+
+    # Same as draw but rotated at the top left corner. 
+    def draw_rot(text, x, y, z, angle, factor_x=1, factor_y=1, 
color=0xffffffff, mode=:default)
+    end
+
+    def make_bitmap_text(message, size, color, &block)
+      BitmapText.new(@font, false).tap do |font|
+        text.size = size
+        text.default_color = color.clone
+        text.box = Rectangle.new(10, -10, width - 20, height - 20)
+        block.arity == 1 ? block[text] : text.instance_eval(&block)
+        text.text = message
+        text.update
+      end
+    end
+    private :make_bitmap_text
+  end
+end
diff --git a/lib/gosu/gosu.rb b/lib/gosu/gosu.rb
new file mode 100644
index 0000000..022fee1
--- /dev/null
+++ b/lib/gosu/gosu.rb
@@ -0,0 +1,54 @@
+module Gosu
+  # Returns the horizontal distance between the origin and the point to 
which 
+  # you would get if you moved radius pixels in the direction specified by 
+  # angle.
+  def offset_x(angle, dist)
+    dist * Math::sin(degrees_to_radians(angle))
+  end
+  module_function :offset_x
+
+  # Returns the vertical distance between the origin and the point to which
+  # you would get if you moved radius pixels in the direction specified by 
+  # angle.
+  def offset_y(angle, dist)
+    dist * Math::cos(degrees_to_radians(angle))
+  end
+  module_function :offset_y
+
+  def degrees_to_radians(angle)
+    (angle / 180.0) * Math::PI
+  end
+  module_function :degrees_to_radians
+
+  # Returns the angle between two points in degrees, where 0.0 means upwards.
+  # Returns 0 if both points are equal. 
+  def angle(x1, y1, x2, y2)
+  end
+
+  # Returns the smallest angle that can be added to angle1 to get to angle2 
+  # (can be negative if counter-clockwise movement is shorter). 
+  def angle_diff(angle1, angle2)
+  end
+
+  # Returns the distance between two points. 
+  def distance(x1, y1, x2, y2)
+    Math.sqrt((y2 - y1)**2 + (x2 - x1)**2)
+  end
+
+  # Incrementing, possibly wrapping millisecond timer. 
+  def milliseconds()
+    java.lang.System.currentTimeMillis
+  end
+
+  # Returns a font name that will work on any system. 
+  def default_font_name()
+  end
+
+  # Return the dimensions of the system's primary screen. Can be used to 
+  # choose the size of your windowed resolution. 
+  def screen_width()
+  end
+
+  def screen_height()
+  end
+end
diff --git a/lib/gosu/image.rb b/lib/gosu/image.rb
new file mode 100644
index 0000000..9d25b73
--- /dev/null
+++ b/lib/gosu/image.rb
@@ -0,0 +1,93 @@
+# -*- coding: utf-8 -*-
+require 'jmephysics'
+
+module Gosu
+  class Image
+    def box_for_image(filename)
+      puts "WIDTHxHEIGHT #{width}x#{height}"
+      Box(filename, width, height, 1).tap do |box|
+        texture_state = 
DisplaySystem.display_system.renderer.createTextureState
+        texture = TextureManager.load_from_image(@image)
+#        texture.wrap = true if @tileable
+        texture_state.texture = texture
+        box.render_state texture_state
+      end
+    end
+
+    # [srcX, srcY, srcWidth, srcHeight]
+    def initialize(window, filename, tileable, *src)
+      @window, @filename, @tileable = window, filename, tileable
+      @image = ImageIcon.new(filename).image
+
+      # Variables for rotating the image
+      @rotation_vector = Vector3f.new
+      @quaternion = Quaternion.new
+      @rotation_vector.set Vector3f::UNIT_Z
+    end
+
+    def width
+      @image.width
+    end
+
+    def height
+      @image.height
+    end
+
+    def draw(x, y, z, factor_x=1, factor_y=1, color=0xffffffff, 
mode=:default)
+      setup_holder
+      @holder.local_translation.x = x
+      @holder.local_translation.y = y
+      @holder.local_translation.z = z
+
+      @window.update_scene_graph
+    end
+
+    # center_x Relative horizontal position of the rotation center on the 
+    # image. 0 is the left border, 1 is the right border, 0.5 is the center 
+    # (and default)—the same applies to center_y, respectively.
+    def draw_rot(x, y, z, angle, center_x=0.5, center_y=0.5, factor_x=1, 
factor_y=1, color=0xffffffff, mode=:default)
+      setup_holder
+      @holder.local_translation.x = x
+      @holder.local_translation.y = y
+      @holder.local_translation.z = z
+
+      @quaternion.fromAngleAxis Gosu.degrees_to_radians(angle), 
@rotation_vector
+      @holder.local_rotation.set @quaternion
+
+      @window.update_scene_graph
+    end
+
+    # Like Window#draw_quad, but with this texture instead of a solid color.
+    # Can be used to implement advanced, non-rectangular drawing techniques.
+    def draw_as_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z, 
mode=:default)
+    end
+
+    # Creates a line of text. font_name can either be the name of a system 
font, or a filename (must contain '/').
+    def self.from_text(window, text, font_name, font_height, *rest)
+    end
+
+    # Creates a block of text of width max_width. Each line will take 
+    # font_height + line_spacing pixels of vertical space.`: align must be
+    # one of :left, :right, :center oder :justify. : font_name can either be
+    # the name of a system font, or a filename (must contain '/', does not 
+    # work on Linux yet).
+    def self.from_text(window, text, font_name, font_height, line_spacing, 
max_width, align)
+    end
+
+    # tile_width can either be the width of one tile in pixels or the number
+    # of columns multiplied by -1. tile_height is its vertical equivalent
+    def self.load_tiles(window, filename_or_rmagick_image, tile_width, 
tile_height, tileable)
+    end
+
+    def gl_tex_info
+    end
+
+    def setup_holder
+      unless @holder # Setup late to know game has bootstrapped
+        @holder = box_for_image('file:' + @filename)
+        @window.scene_graph << @holder
+      end
+    end
+    private :setup_holder
+  end
+end
diff --git a/lib/gosu/window.rb b/lib/gosu/window.rb
new file mode 100644
index 0000000..0b14be6
--- /dev/null
+++ b/lib/gosu/window.rb
@@ -0,0 +1,220 @@
+require 'jmephysics'
+
+module Gosu
+  class MyHandler
+    include com.jme.input.action.InputActionInterface
+    
+    def initialize(gosu_window)
+      @gosu_window = gosu_window
+    end
+
+    def performAction(event)
+      id = Gosu::Button::MAPPINGS[event.trigger_index]
+
+      if event.trigger_pressed?
+        @gosu_window.button_down(id)
+      else
+        @gosu_window.button_up(id)
+      end
+    end
+  end
+
+  class KeyWatcher < com.jme.input.keyboard.KeyboardInputHandlerDevice
+    def initialize(jrme_game)
+      super()
+      @jrme_game = jrme_game
+      @callback = MyHandler.new(jrme_game.gosu_window)
+      @keys = []
+    end
+
+    def add(keycode)
+      @keys << keycode
+    end
+
+    def commit(input_handler)
+      @keys.each do |keycode|
+        createTriggers @callback, -1, keycode, false, input_handler
+      end
+    end
+  end
+
+  class JRMEGame < SimplePhysicsGame
+    ORIGIN = Vector3f.new 0, 0, 0
+    FAR_PLANE = 20000.0
+
+    attr_accessor :title, :gosu_window
+
+    def initialize(gosu_window, width, height, fullscreen, update_interval)
+      super()
+      @gosu_window = gosu_window 
+      @width, @height, @fullscreen = width, height, fullscreen
+      @update_interval = update_interval * 1_000_000 # nanos
+      @last_nanos = System.nano_time
+      @watcher = KeyWatcher.new(self)
+    end
+
+    def camera
+      cam.set_frustum_far(FAR_PLANE)
+#      cam.set_frustum_perspective(90.0, display.width.to_f / 
display.height, 8.0, FAR_PLANE)
+      cam.location = Vector3f.new(0, 0, 1400)
+      cam.lookAt ORIGIN, Vector3f::UNIT_Y
+      cam.update
+
+      
root_node.setRenderState(display.renderer.createCullState.set!(:cull_face => 
CullState::Face::Back))
+    end
+
+    def add_key(keycode)
+      @watcher.add keycode
+    end
+    
+    def getNewSettings
+      super().tap do |settings|
+        settings.width = @width
+        settings.height = @height
+        settings.fullscreen = @fullscreen
+      end
+    end
+
+    def simpleInitGame
+      KeyBindingManager.key_binding_manager.remove ["turnLeft", 
"strafeLeft", "turnRight", "forward", "strafeRight", "backward", "lookDown", 
"elevateUp", "lookUp", "elevateDown"]
+      @watcher.commit(input)
+      display.title = @title if @title
+      camera
+    end
+
+    def simpleUpdate
+      if time_to_update?
+        @gosu_window.update
+        @gosu_window.draw if @gosu_window.needs_redraw?
+      end
+    end
+
+    def time_to_update?
+      nanos = System.nano_time
+      return false if nanos - @last_nanos <= @update_interval
+      @last_nanos = nanos
+    end
+  end
+
+  class Window
+    attr_reader :update_interval
+
+    def initialize(width, height, fullscreen, update_interval = 16.666666)
+      @jrme_game = JRMEGame.new self, width, height, fullscreen, 
update_interval
+      @update_interval = update_interval
+      define_keybindings
+    end
+
+    def caption
+      @jrme_game.title
+    end
+
+    def caption=(caption)
+      @jrme_game.title = caption
+    end
+
+    def show
+      @jrme_game.start
+    end
+
+    def close
+      @jrme_game.quit
+    end
+
+    # Called by @jrme_game.simpleUpdate
+    def update
+    end
+
+    # Called by @jrme_game.simpleUpdate after update
+    def draw
+    end
+
+    def needs_redraw?
+      true
+    end
+
+    # Called when the user presses the button with the given id.
+    def button_down(id)
+    end
+
+    # Called when the user releases the button with the given id.
+    def button_up(id)
+    end
+
+    def draw_line(x1, y1, c1, x2, y2, c2, z=0, mode=:default)
+    end
+
+    def draw_triangle(x1, y1, c1, x2, y2, c2, x3, y3, c3, z=0, mode=:default)
+    end
+
+    def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z=0, 
mode=:default)
+    end
+
+    def mouse_x
+    end
+
+    def mouse_y
+    end
+
+    def mouse_x=(float)
+    end
+
+    def mouse_y=(float)
+    end
+
+    # To avoid intermediate position of calling mouse_x= followed by 
mouse_y=.
+    def set_mouse_position(x, y)
+    end
+
+    def button_down?(id)
+      @keys.valid? Gosu::Button::MAPPINGS[id]
+    end
+
+    # Returns the character a given id stands for, or nil.
+    def self.button_id_to_char(id)
+    end
+
+    # Returns the id usually used for a character, or nil.
+    def self.char_to_button_id(char)
+    end
+
+    def text_input
+    end
+
+    # Sets current text input object that builds an input string
+    def text_input=
+    end
+
+    def width
+      @jrme_game.settings.width
+    end
+
+    def height
+      @jrme_game.settings.height
+    end
+
+    def gl(&block)
+    end
+
+    def clip_to(x, y, w, h, &block)
+    end
+
+    # Not part of Gosu API, but useful for implementation of Gosu
+    def scene_graph
+      @jrme_game.root_node
+    end
+
+    def update_scene_graph
+      scene_graph.update_render_state
+    end
+
+    def define_keybindings
+      @keys = KeyBindingManager.key_binding_manager
+      Gosu::Button::MAPPINGS.each do |value, name|
+        @keys.set(name, value)
+        @jrme_game.add_key value
+      end
+    end
+  end
+end
+




[jrme~main:83954028] Dump of initial and very partial gosu support

enebo 01/22/2011
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close