gba_input.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. (*
  2. gba_input.pas 01/09/2006 19.57.16
  3. ------------------------------------------------------------------------------
  4. This lib is a raw porting of libgba library for gba (you can find it at
  5. http://www.devkitpro.org).
  6. As this is a direct port from c, I'm pretty sure that something could not work
  7. as you expect. I am even more sure that this code could be written better, so
  8. if you think that I have made some mistakes or you have some better
  9. implemented functions, let me know [francky74 (at) gmail (dot) com]
  10. Enjoy!
  11. Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  12. (http://www.freepascal.org)
  13. Copyright (C) 2006 Francesco Lombardi
  14. This library is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU Lesser General Public
  16. License as published by the Free Software Foundation; either
  17. version 2.1 of the License, or (at your option) any later version.
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. Lesser General Public License for more details.
  22. You should have received a copy of the GNU Lesser General Public
  23. License along with this library; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. ------------------------------------------------------------------------------
  26. *)
  27. unit gba_input;
  28. {$i def.inc}
  29. interface
  30. uses
  31. gba_types, gba_regs;
  32. const
  33. KEY_A: TKeyPadBits = (1 shl 0);
  34. KEY_B: TKeyPadBits = (1 shl 1);
  35. KEY_SELECT: TKeyPadBits = (1 shl 2);
  36. KEY_START: TKeyPadBits = (1 shl 3);
  37. KEY_RIGHT: TKeyPadBits = (1 shl 4);
  38. KEY_LEFT: TKeyPadBits = (1 shl 5);
  39. KEY_UP: TKeyPadBits = (1 shl 6);
  40. KEY_DOWN: TKeyPadBits = (1 shl 7);
  41. KEY_R: TKeyPadBits = (1 shl 8);
  42. KEY_L: TKeyPadBits = (1 shl 9);
  43. KEYIRQ_ENABLE: TKeyPadBits = (1 shl 14);
  44. KEYIRQ_OR: TKeyPadBits = (0 shl 15);
  45. KEYIRQ_AND: TKeyPadBits = (1 shl 15);
  46. DPAD: TKeyPadBits = (1 shl 6) or (1 shl 7) or (1 shl 5) or (1 shl 4);
  47. type
  48. KeyInput = packed record
  49. Up: word;
  50. Down: word;
  51. Held: word;
  52. Last: word;
  53. DownRepeat: word;
  54. end;
  55. TKeyInput = KeyInput;
  56. //---------------------------------------------------------------------------------
  57. // Global variables
  58. //---------------------------------------------------------------------------------
  59. var
  60. Keys: TKeyInput;
  61. delay: byte = 60;
  62. rept: byte = 30;
  63. count: byte = 60;
  64. procedure ScanKeys();
  65. function KeysDown(): word;
  66. function KeysDownRepeat(): word;
  67. function KeysUp(): word;
  68. function KeysHeld(): word;
  69. procedure SetRepeat(SetDelay, SetRepeat: integer);
  70. implementation
  71. procedure SetRepeat(SetDelay, SetRepeat: integer);
  72. begin
  73. delay := SetDelay;
  74. rept := SetRepeat;
  75. end;
  76. //---------------------------------------------------------------------------------
  77. procedure ScanKeys();
  78. var
  79. pressed, released: word;
  80. begin
  81. Keys.Last := Keys.Held;
  82. Keys.Held := (REG_KEYINPUT^ and $03ff) xor $03ff; // upper 6 bits clear on hw not emulated
  83. pressed := Keys.Held and ( Keys.Last xor $03ff);
  84. Keys.DownRepeat := Keys.DownRepeat or pressed;
  85. Keys.Down := Keys.Down or pressed;
  86. released := ((Keys.Held xor $03ff) and Keys.Last);
  87. Keys.Up := Keys.Up or released;
  88. Keys.Down := Keys.Down and not released;
  89. Keys.DownRepeat := Keys.DownRepeat and not released;
  90. Keys.Up := Keys.Up and not pressed;
  91. if ( Keys.Last <> Keys.Held) then
  92. count := delay;
  93. if ( delay <> 0) then
  94. begin
  95. dec(count);
  96. if (count = 0) then
  97. begin
  98. count := rept;
  99. Keys.DownRepeat := Keys.DownRepeat or Keys.Held;
  100. end;
  101. end;
  102. end;
  103. function KeysDownRepeat(): word;
  104. var
  105. tmp: word;
  106. begin
  107. tmp := Keys.DownRepeat;
  108. Keys.DownRepeat := 0;
  109. KeysDownRepeat := tmp;
  110. end;
  111. function KeysDown(): word;
  112. var
  113. tmp: word;
  114. begin
  115. tmp := Keys.Down;
  116. Keys.Down := 0;
  117. KeysDown := tmp;
  118. end;
  119. function KeysUp(): word;
  120. var
  121. tmp: word;
  122. begin
  123. tmp := Keys.Up;
  124. Keys.Up := 0;
  125. KeysUp := tmp;
  126. end;
  127. function KeysHeld(): word;
  128. begin
  129. KeysHeld := Keys.Held;
  130. end;
  131. end.