Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[PRInput] New Faster Input Module

RTH

Member

http://img209.imageshack.us/img209/875/ ... putzg4.png[/img]

http://img152.imageshack.us/img152/2110 ... iontu8.png[/img]


This script is faster then the old ones because it handles the KeyState calls inside the dll.
Extra credits to poccil, thanks to him, we found out how to convert RGSS Object -> C variable


http://img71.imageshack.us/img71/8927/howtousepp8.png[/img]

Only copy the DLL RGSSInput.dll to your game folder and copy the script:

http://img293.imageshack.us/img293/9623/scriptdm3.png[/img]

Code:
#===============================================================================
# Input module
#-------------------------------------------------------------------------------
# Créditos: PRCoders
#              poccil  - Thanks to him we discovered how to convert RGSS Objects into C variables
#===============================================================================

module Input
  
  #--------------------------------------------------------------------------
  # * Váriáveis
  #-------------------------------------------------------------------------- 
  
  @keys = Array.new(256)
  @pressed = Array.new(256)
  @repeated = Array.new(256)
  @released = Array.new(256)
  @dir = Array.new(2)
  
  #--------------------------------------------------------------------------
  # * Constantes
  #-------------------------------------------------------------------------- 
  
  Mouse_Left = 1
  Mouse_Right = 2
  Mouse_Middle = 4
  Back = 8
  Tab = 9
  Enter = 13
  SHIFT = Shift = 16
  CTRL = Ctrl = 17
  ALT = Alt = 18
  Pause = 0x13
  CAPS = 0x14
  Esc = 0x1B
  LEFT = 0x25
  UP = 0x26  
  RIGHT = 0x27 
  DOWN = 0x28
  Space = 32
  PageUp = 0x21
  PageDowm = 0x22
  Home = 0x23
  End = 0x24
  Numberkeys = {}
  Numberkeys[0] = 48        # => 0
  Numberkeys[1] = 49        # => 1
  Numberkeys[2] = 50        # => 2
  Numberkeys[3] = 51        # => 3
  Numberkeys[4] = 52        # => 4
  Numberkeys[5] = 53        # => 5
  Numberkeys[6] = 54        # => 6
  Numberkeys[7] = 55        # => 7
  Numberkeys[8] = 56        # => 8
  Numberkeys[9] = 57        # => 9
  Numberpad = {}
  Numberpad[0] = 96
  Numberpad[1] = 97
  Numberpad[2] = 98
  Numberpad[3] = 99
  Numberpad[4] = 100
  Numberpad[5] = 101
  Numberpad[6] = 102
  Numberpad[7] = 103
  Numberpad[8] = 104
  Numberpad[9] = 105
  Letters = {}
  Letters["A"] = 65
  Letters["B"] = 66
  Letters["C"] = 67
  Letters["D"] = 68
  Letters["E"] = 69
  Letters["F"] = 70
  Letters["G"] = 71
  Letters["H"] = 72
  Letters["I"] = 73
  Letters["J"] = 74
  Letters["K"] = 75
  Letters["L"] = 76
  Letters["M"] = 77
  Letters["N"] = 78
  Letters["O"] = 79
  Letters["P"] = 80
  Letters["Q"] = 81
  Letters["R"] = 82
  Letters["S"] = 83
  Letters["T"] = 84
  Letters["U"] = 85
  Letters["V"] = 86
  Letters["W"] = 87
  Letters["X"] = 88
  Letters["Y"] = 89
  Letters["Z"] = 90
  Fkeys = {}
  F1 = Fkeys[1] = 112
  F2 = Fkeys[2] = 113
  F3 = Fkeys[3] = 114
  F4 = Fkeys[4] = 115
  F5 = Fkeys[5] = 116
  F6 = Fkeys[6] = 117
  F7 = Fkeys[7] = 118
  F8 = Fkeys[8] = 119
  F9 = Fkeys[9] = 120
  F10 = Fkeys[10] = 121
  F11 = Fkeys[11] = 122
  F12 = Fkeys[12] = 123
  Collon = 186        # => \ |
  Equal = 187         # => = +
  Comma = 188         # => , <
  Underscore = 189    # => - _
  Dot = 190           # => . >
  Backslash = 191     # => / ?
  Lb = 219
  Rb = 221
  Quote = 222         # => '"
  
  #--------------------------------------------------------------------------
  # * Constantes originais
  #-------------------------------------------------------------------------- 
  
  A = Letters["C"]
  B = [Letters["X"], Esc]
  C = [Letters["Z"], Space, Enter]
  X = Letters["A"]
  Y = Letters["S"]
  Z = Letters["D"]
  L = Letters["Q"]
  R = Letters["W"]
  
  #--------------------------------------------------------------------------
  # * Métodos da DLL
  #-------------------------------------------------------------------------- 
  
  UPDATE = Win32API.new("PRABS.dll", "UpdateInputArray", "lllll", "")
  ADDKEY = Win32API.new("PRABS.dll", "InputAddUsedKey", "i", "")
  
  #--------------------------------------------------------------------------
  # * Adiciona uma tecla para ser atualizada
  #-------------------------------------------------------------------------- 
  
  def self.add_key(key)
    if (key.is_a?(Array))
      for k in key
        self.add_key(k)
      end
      return
    end
    ADDKEY.call(key)
  end
  
  #--------------------------------------------------------------------------
  # * Reseta as teclas atualizadas
  #-------------------------------------------------------------------------- 
  
  def self.clear
    Win32API.new("PRABS.dll", "ClearUsedKeys", "", "").call()
  end
  
  #--------------------------------------------------------------------------
  # * Reseta as teclas atualizadas
  #-------------------------------------------------------------------------- 
  
  def self.setup_direction_keys(up, down, left, right)
    self.add_key(up)
    self.add_key(down)
    self.add_key(left)
    self.add_key(right)
    Win32API.new("PRABS.dll", "SetupDirectionKeys", "iiii", "").call(up, down, left, right)
  end
  
  #--------------------------------------------------------------------------
  # * Atualização
  #-------------------------------------------------------------------------- 
  
  def self.update
    UPDATE.call(@keys.__id__, @pressed.__id__, @repeated.__id__, @released.__id__, @dir.__id__)
  end
  
  #--------------------------------------------------------------------------
  # * Trigger?
  #-------------------------------------------------------------------------- 
  
  def self.trigger?(key)
    if (key.is_a?(Array))
      return key.any? { |k| self.trigger?(k) }
    end
    return @keys[key]
  end
  
  #--------------------------------------------------------------------------
  # * Press?
  #-------------------------------------------------------------------------- 
  
  def self.press?(key)
    if (key.is_a?(Array))
      return key.any? { |k| self.press?(k) }
    end
    return @pressed[key]
  end
  
  #--------------------------------------------------------------------------
  # * Repeat?
  #-------------------------------------------------------------------------- 
  
  def self.repeat?(key)
    if (key.is_a?(Array))
      return key.any? { |k| self.repeat?(k) }
    end
    return @repeated[key]
  end
  
  #--------------------------------------------------------------------------
  # * Release?
  #-------------------------------------------------------------------------- 
  
  def self.release?(key)
    if (key.is_a?(Array))
      return key.any? { |k| self.release?(k) }
    end
    return @released[key]
  end
  
  #--------------------------------------------------------------------------
  # * Dir4
  #-------------------------------------------------------------------------- 
  
  def self.dir4
    return @dir[0]
  end

  #--------------------------------------------------------------------------
  # * Dir8
  #-------------------------------------------------------------------------- 
  
  def self.dir8
    return @dir[1]
  end

  
  self.clear
  
end

# Adicionando as teclas mais utilizadas ao sistema

Input.add_key(Input::DOWN)
Input.add_key(Input::UP)
Input.add_key(Input::LEFT)
Input.add_key(Input::RIGHT)

Input.add_key(Input::A)
Input.add_key(Input::B)
Input.add_key(Input::C)
Input.add_key(Input::X)
Input.add_key(Input::Y)
Input.add_key(Input::Z)
Input.add_key(Input::L)
Input.add_key(Input::R)

Input.add_key(Input::CTRL)
Input.add_key(Input::SHIFT)
Input.add_key(Input::ALT)

Input.setup_direction_keys(Input::UP, Input::DOWN, Input::LEFT, Input::RIGHT)

Methods available:
Input.press?(key)
Input.trigger?(key)
Input.repeat?(key)
Input.release?(key)

You MUST add every additional key using Input.add_key(key), otherwise, the DLL will not read it.
To clear the added keys, use Input.clear.

You can now setup the 4 directions keys using:
Input.setup_direction_keys(key_for_up, down, left, right)

http://img361.imageshack.us/img361/3016/botaumdownload02zl4.jpg[/img]


http://img227.imageshack.us/img227/2402/prlogoqp5.png[/img]
NightWalker
PHCDO
RTH


http://i.creativecommons.org/l/by-nc-sa/2.5/br/88x31.png[/img]

PR Input DLL by PR Coders is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 2.5 Brazill License.
 
It's an input module. It enables you to make keys work when you push them. If you have to ask, chances are you have no use for it.
 

ikos

Member

Hevendor":30wa6ngx said:
It's an input module. It enables you to make keys work when you push them. If you have to ask, chances are you have no use for it.

A mild case of ignorance is no reason not to have the potential idea to.
Me not knowing what it is doesn't mean I wouldn't have my uses for it.
 
OooOOoooO, this is nice, since i have a lot of extra stuff in my game to use with alot of buttons.

I would also like to know if you could use this with a joypad too.
 
How would one go about adding the Keys:  Page Up & Page Down  ..?

After several attempts, I have not succeeded.  :huh:
 
ikos":2y2x77zy said:
What exactly does this do? I'm a little confused.
This script, and all input module scripts allow you to use other keys on your keyboard other than the ones that RMXP comes provided with. for example, rmxp allows you to use A, S, C, X, Z... and some others, but with this you can use almost any key in your game.
 
just a quick note... ive checked the speed comparison to my own personal script (which is literally a cut down version of that one but using GetKeyboardAsyncState) and there really isnt that much difference... it good though... especially as its small and simple unlike many other OTT input modules
 
Here's my result:
========Default Module========

Avg: 0.328

========New Module========

Avg: 0.969000000000001

I did this the test like this:

---Default scripts
---Testing speed of input module (10000 calls of Input.press?(Input::A))
---New Input Module
---Testing speed of input module (10000 calls of Input.press?(Input::A))
 
no way can that be... coz my results from MY input module are:
t = Time.now
10000.times do
   Input.press?(Input::A)
end
print Time.now - t

and the resturns 0.063

no offence but either you got it wrong or have executed the code in a strange manner / place or these input modules just plain suck...

*EDIT*

sorry i was using ruby for this test - but still the VX version - which is exactly the same - returns 0.094 on the same test so SOMETHING is seriously wrong here...

*EDIT AGAIN*

and the default returns 0.015... what the hell did you actually test???
i think you should recheck what you did and try again as currently, for 10,000 presses, its:
default - 0.015
mine - 0.094 / 0.078 (switchs between them randomly... but usually 94)
this one - not sure... havnt downloaded
 

RTH

Member

It's weird very weird, mine results are this.

Using default module:
Code:
t = Time.now
for i in 0...10000
Input.update
Input.press?(Input::A)
end
p (Time.now - t) # 0.153

Using PRInput:
Code:
t = Time.now
for i in 0...10000
Input.update
Input.press?(Input::A)
end
p (Time.now - t) # 0.139

See, mine is a little bit faster (when only using the default keys) then the default Input module, besides: mine have the Input.released?(key) method.

I know it also depends on the PC, but i don't know if these results are correct.

I updated the script and now, the Input.dir4 and dir8 are also on the Dll.
 
haha... input.released method XD

i use ruby with gosu extension library so my input module has a draw freeform line function with the mouse, draw rect with the mouse and draw standard 2 point line with the mouse... its taken a while but i think im gonna stick with my production for the moment XD
 
its released... but its for ruby due to X11 windows and blitting graphics on screen... i can make the drawing reach about 400 lines without lag... on rmxp i reckon that would be 50 as they are created on gltextures...

i would convert it...  but its not possible...  well it is... but its hard and i cant be bothered.

if you want it then tell me but you need Ruby and Imagemagick, my system uses Gosu (supplied) and Rmagick (supplied but requires imagemagick) XD.

i'll even screenshot if you dont believe it actuall exists... because IT DOES... MWAHAHAHAHAHAAAAA
 
or have executed the code in a strange manner / place
That must be it, sorry ^_^' I'll do it again properly.

I have a suggestion/question. Wouldn't it be faster if you just compile everything in the DLL? After all, that's how RGSS dll works.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top