sdlgameinterface.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. unit sdlgameinterface;
  2. {
  3. $Id: sdlgameinterface.pas,v 1.4 2005/08/03 18:57:31 savage Exp $
  4. }
  5. {******************************************************************************}
  6. { }
  7. { JEDI-SDL : Pascal units for SDL - Simple DirectMedia Layer }
  8. { Game Interface Base class }
  9. { }
  10. { The initial developer of this Pascal code was : }
  11. { Dominqiue Louis <[email protected]> }
  12. { }
  13. { Portions created by Dominqiue Louis are }
  14. { Copyright (C) 2000 - 2001 Dominqiue Louis. }
  15. { }
  16. { }
  17. { Contributor(s) }
  18. { -------------- }
  19. { }
  20. { }
  21. { Obtained through: }
  22. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  23. { }
  24. { You may retrieve the latest version of this file at the Project }
  25. { JEDI home page, located at http://delphi-jedi.org }
  26. { }
  27. { The contents of this file are used with permission, subject to }
  28. { the Mozilla Public License Version 1.1 (the "License"); you may }
  29. { not use this file except in compliance with the License. You may }
  30. { obtain a copy of the License at }
  31. { http://www.mozilla.org/MPL/MPL-1.1.html }
  32. { }
  33. { Software distributed under the License is distributed on an }
  34. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  35. { implied. See the License for the specific language governing }
  36. { rights and limitations under the License. }
  37. { }
  38. { Description }
  39. { ----------- }
  40. { }
  41. { }
  42. { }
  43. { }
  44. { }
  45. { }
  46. { }
  47. { Requires }
  48. { -------- }
  49. { The SDL Runtime libraris on Win32 : SDL.dll on Linux : libSDL.so }
  50. { They are available from... }
  51. { http://www.libsdl.org . }
  52. { }
  53. { Programming Notes }
  54. { ----------------- }
  55. { }
  56. { }
  57. { }
  58. { }
  59. { Revision History }
  60. { ---------------- }
  61. { September 23 2004 - DL : Initial Creation }
  62. {
  63. $Log: sdlgameinterface.pas,v $
  64. Revision 1.4 2005/08/03 18:57:31 savage
  65. Various updates and additions. Mainly to handle OpenGL 3D Window support and better cursor support for the mouse class
  66. Revision 1.3 2004/10/17 18:41:49 savage
  67. Slight Change to allow Reseting of Input Event handlers
  68. Revision 1.2 2004/09/30 22:35:47 savage
  69. Changes, enhancements and additions as required to get SoAoS working.
  70. }
  71. {******************************************************************************}
  72. interface
  73. uses
  74. sdl,
  75. sdlwindow;
  76. type
  77. TGameInterfaceClass = class of TGameInterface;
  78. TGameInterface = class( TObject )
  79. private
  80. FNextGameInterface : TGameInterfaceClass;
  81. protected
  82. Dragging : Boolean;
  83. Loaded : Boolean;
  84. procedure FreeSurfaces; virtual;
  85. procedure Render; virtual; abstract;
  86. procedure Close; virtual;
  87. procedure Update( aElapsedTime : single ); virtual;
  88. procedure MouseDown( Button : Integer; Shift: TSDLMod; MousePos : TPoint ); virtual;
  89. procedure MouseMove( Shift: TSDLMod; CurrentPos : TPoint; RelativePos : TPoint ); virtual;
  90. procedure MouseUp( Button : Integer; Shift: TSDLMod; MousePos : TPoint ); virtual;
  91. procedure MouseWheelScroll( WheelDelta : Integer; Shift: TSDLMod; MousePos : TPoint ); virtual;
  92. procedure KeyDown( var Key: TSDLKey; Shift: TSDLMod; unicode : UInt16 ); virtual;
  93. public
  94. MainWindow : TSDLCustomWindow;
  95. procedure ResetInputManager;
  96. procedure LoadSurfaces; virtual;
  97. function PointIsInRect( Point : TPoint; x, y, x1, y1 : integer ) : Boolean;
  98. constructor Create( const aMainWindow : TSDLCustomWindow );
  99. destructor Destroy; override;
  100. property NextGameInterface : TGameInterfaceClass read FNextGameInterface write FNextGameInterface;
  101. end;
  102. implementation
  103. { TGameInterface }
  104. procedure TGameInterface.Close;
  105. begin
  106. FNextGameInterface := nil;
  107. end;
  108. constructor TGameInterface.Create( const aMainWindow : TSDLCustomWindow );
  109. begin
  110. inherited Create;
  111. MainWindow := aMainWindow;
  112. FNextGameInterface := TGameInterface;
  113. ResetInputManager;
  114. end;
  115. destructor TGameInterface.Destroy;
  116. begin
  117. if Loaded then
  118. FreeSurfaces;
  119. inherited;
  120. end;
  121. procedure TGameInterface.FreeSurfaces;
  122. begin
  123. Loaded := False;
  124. end;
  125. procedure TGameInterface.KeyDown(var Key: TSDLKey; Shift: TSDLMod; unicode: UInt16);
  126. begin
  127. end;
  128. procedure TGameInterface.LoadSurfaces;
  129. begin
  130. Loaded := True;
  131. end;
  132. procedure TGameInterface.MouseDown(Button: Integer; Shift: TSDLMod; MousePos: TPoint);
  133. begin
  134. Dragging := True;
  135. end;
  136. procedure TGameInterface.MouseMove(Shift: TSDLMod; CurrentPos, RelativePos: TPoint);
  137. begin
  138. end;
  139. procedure TGameInterface.MouseUp(Button: Integer; Shift: TSDLMod; MousePos: TPoint);
  140. begin
  141. Dragging := True;
  142. end;
  143. procedure TGameInterface.MouseWheelScroll(WheelDelta: Integer; Shift: TSDLMod; MousePos: TPoint);
  144. begin
  145. end;
  146. function TGameInterface.PointIsInRect( Point : TPoint; x, y, x1, y1: integer ): Boolean;
  147. begin
  148. if ( Point.x >= x )
  149. and ( Point.y >= y )
  150. and ( Point.x <= x1 )
  151. and ( Point.y <= y1 ) then
  152. result := true
  153. else
  154. result := false;
  155. end;
  156. procedure TGameInterface.ResetInputManager;
  157. var
  158. temp : TSDLNotifyEvent;
  159. begin
  160. MainWindow.InputManager.Mouse.OnMouseDown := MouseDown;
  161. MainWindow.InputManager.Mouse.OnMouseMove := MouseMove;
  162. MainWindow.InputManager.Mouse.OnMouseUp := MouseUp;
  163. MainWindow.InputManager.Mouse.OnMouseWheel := MouseWheelScroll;
  164. MainWindow.InputManager.KeyBoard.OnKeyDown := KeyDown;
  165. temp := Render;
  166. MainWindow.OnRender := temp;
  167. temp := Close;
  168. MainWindow.OnClose := temp;
  169. MainWindow.OnUpdate := Update;
  170. end;
  171. procedure TGameInterface.Update(aElapsedTime: single);
  172. begin
  173. end;
  174. end.