sdlada_2.5.20_cd53c280/test/test.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
--------------------------------------------------------------------------------------------------------------------
--  This source code is subject to the Zlib license, see the LICENCE file in the root of this directory.
--------------------------------------------------------------------------------------------------------------------
with Ada.Real_Time; use Ada.Real_Time;
with SDL;
with SDL.Error;
with SDL.Events.Events;
with SDL.Events.Keyboards;
with SDL.Events.Joysticks;
with SDL.Events.Windows;
with SDL.Inputs.Joysticks.Makers;
with SDL.Inputs.Joysticks.Game_Controllers;
with SDL.Events.Mice;
with SDL.Log;
with SDL.Video.Displays;
with SDL.Video.Windows;
with SDL.Video.Windows.Makers;
with SDL.Video.Windows.Manager;
with SDL.Versions;
with System;

procedure Test is
   W              : SDL.Video.Windows.Window;
   Total_Drivers  : constant Positive := SDL.Video.Total_Drivers;
   Linked_Version : SDL.Versions.Version;
begin
   SDL.Log.Set (Category => SDL.Log.Application, Priority => SDL.Log.Debug);

   SDL.Versions.Linked_With (Info => Linked_Version);

   SDL.Log.Put_Debug ("System.Word_Size: " & Integer'Image (System.Word_Size));
   SDL.Log.Put_Debug ("Revision        : " & SDL.Versions.Revision);
   SDL.Log.Put_Debug ("Linked with     : " & SDL.Versions.Version_Level'Image (Linked_Version.Major) &
                        "." & SDL.Versions.Version_Level'Image (Linked_Version.Minor) &
                        "." & SDL.Versions.Version_Level'Image (Linked_Version.Patch));
   SDL.Log.Put_Debug ("Compiled with   : " & SDL.Versions.Version_Level'Image (SDL.Versions.Compiled_Major) &
                        "." & SDL.Versions.Version_Level'Image (SDL.Versions.Compiled_Minor) &
                        "." & SDL.Versions.Version_Level'Image (SDL.Versions.Compiled_Patch));
   SDL.Log.Put_Debug ("Bit Order       : " & System.Bit_Order'Image (SDL.Video.Windows.Window'Bit_Order));
   SDL.Log.Put_Debug ("Total drivers   : " & Positive'Image (Total_Drivers));

   for Index in Positive'First .. Total_Drivers loop
      SDL.Log.Put_Debug ("Driver (" & Positive'Image (Index) & ")     : " & SDL.Video.Driver_Name (Natural (Index)));
   end loop;

   if SDL.Initialise = True then
      SDL.Log.Put_Debug ("Current driver  : " & SDL.Video.Current_Driver_Name);
      SDL.Log.Put_Debug ("Total displays  : " & SDL.Video.Displays.Display_Indices'Image (SDL.Video.Displays.Total));

      SDL.Error.Clear;

      SDL.Log.Put_Debug ("Error           : " & SDL.Error.Get);

      SDL.Video.Windows.Makers.Create (Win      => W,
                                       Title    => "Test SDLAda 2.0 - हिन्दी समाचार",
                                       Position => SDL.Natural_Coordinates'(X => 100, Y => 100),
                                       Size     => SDL.Positive_Sizes'(800, 640));

      SDL.Log.Put_Debug ("Window Grabbed  : " & Boolean'Image (W.Is_Grabbed));

      --  W.Set_Grabbed;

      SDL.Log.Put_Debug ("Window Grabbed  : " & Boolean'Image (W.Is_Grabbed));
      SDL.Log.Put_Debug ("Window ID       : " & SDL.Video.Windows.ID'Image (W.Get_ID));
      SDL.Log.Put_Debug ("Window Title    : " & W.Get_Title);

      SDL.Log.Put_Debug ("Window on display : " & SDL.Video.Displays.Display_Indices'Image (W.Display_Index));

      --  W.Set_Mode (SDL.Video.Windows.Full_Screen);

      declare
         ID : constant SDL.Video.Windows.ID     := SDL.Video.Windows.Get_ID (W);
         W2 : constant SDL.Video.Windows.Window := SDL.Video.Windows.From_ID (ID);
      begin
         SDL.Video.Windows.Set_Title (W2, "Grabbed second window!");
      end;

      --  Joysticks.
      declare
         Total_Sticks : constant SDL.Inputs.Joysticks.All_Devices := SDL.Inputs.Joysticks.Total;
         Stick        : SDL.Inputs.Joysticks.Joystick;
         GUID_1       : SDL.Inputs.Joysticks.GUIDs;
         GUID_2       : SDL.Inputs.Joysticks.GUIDs;

         use type SDL.Inputs.Joysticks.Devices;
         use type SDL.Inputs.Joysticks.Joystick;
         use type SDL.Inputs.Joysticks.GUIDs;
      begin
         if Total_Sticks = 0 then
            SDL.Log.Put_Debug ("Joysticks       : None");
         else
            SDL.Log.Put_Debug ("Joystick polling: " & Boolean'Image (SDL.Events.Joysticks.Is_Polling_Enabled));
            SDL.Log.Put_Debug ("Total Joysticks : " & SDL.Inputs.Joysticks.Devices'Image (Total_Sticks));

            for Joystick in SDL.Inputs.Joysticks.Devices'First .. Total_Sticks loop
               GUID_1 := SDL.Inputs.Joysticks.GUID (Joystick);
               GUID_2 := SDL.Inputs.Joysticks.Value (SDL.Inputs.Joysticks.Image (GUID_1));

               if GUID_1 = GUID_2 then
                  SDL.Log.Put_Debug ("GUID Image<->Value works");
               end if;

               SDL.Log.Put_Debug ("Joystick              : (" & SDL.Inputs.Joysticks.Devices'Image (Joystick) & ") - " &
                                    SDL.Inputs.Joysticks.Name (Joystick) & " - " &
                                    Integer'Image (SDL.Inputs.Joysticks.GUIDs'Size) & " - GUID: " & ' ' &
                                    SDL.Inputs.Joysticks.Image (GUID_1));

               SDL.Inputs.Joysticks.Makers.Create (Device => Joystick, Actual_Stick => Stick);

               if Stick /= SDL.Inputs.Joysticks.Null_Joystick then
                  SDL.Log.Put_Debug ("  Name               : " & Stick.Name);
                  SDL.Log.Put_Debug ("  Axes               : " & SDL.Events.Joysticks.Axes'Image (Stick.Axes));
                  SDL.Log.Put_Debug ("  Balls              : " & SDL.Events.Joysticks.Balls'Image (Stick.Balls));
                  SDL.Log.Put_Debug ("  Buttons            : " & SDL.Events.Joysticks.Buttons'Image (Stick.Buttons));
                  SDL.Log.Put_Debug ("  Hats               : " & SDL.Events.Joysticks.Hats'Image (Stick.Hats));
                  SDL.Log.Put_Debug ("  Haptic             : " & Boolean'Image (Stick.Is_Haptic));
                  SDL.Log.Put_Debug ("  Attached           : " & Boolean'Image (Stick.Is_Attached));
                  SDL.Log.Put_Debug ("  GUID               : " & SDL.Inputs.Joysticks.Image (Stick.GUID));
                  SDL.Log.Put_Debug ("  Instance           : " & SDL.Inputs.Joysticks.Instances'Image (Stick.Instance));

                  Stick.Close;
               end if;

               if SDL.Inputs.Joysticks.Game_Controllers.Is_Game_Controller (Joystick) = True then
                  SDL.Log.Put_Debug ("  Is game controller : Yes");
               else
                  SDL.Log.Put_Debug ("  Is game controller : No");
               end if;
            end loop;
         end if;
      end;

      --  Window manager.
      declare
         Info : SDL.Video.Windows.Manager.WM_Info;
      begin
         Info.Version := SDL.Versions.Compiled;

         if SDL.Video.Windows.Manager.Get_WM_Info (W, Info) = True then
            SDL.Log.Put_Debug ("Window manager in use: " & SDL.Video.Windows.Manager.WM_Types'Image (Info.Sub_System));
         else
            SDL.Log.Put_Debug ("Cannot get window manager info.");
            SDL.Log.Put_Debug ("Error                : " & SDL.Error.Get);
         end if;
      end;

      --  Main loop.
      declare
         Event    : SDL.Events.Events.Events;
         Finished : Boolean := False;

         Loop_Start_Time_Goal : Ada.Real_Time.Time;

         Frame_Duration : constant Ada.Real_Time.Time_Span :=
           Ada.Real_Time.Microseconds (6_944);
         --  144 Hz refresh rate

         use type SDL.Events.Keyboards.Key_Codes;
         use type SDL.Events.Windows.Window_Event_ID;
      begin
         --  Set next frame delay target using monotonic clock time
         Loop_Start_Time_Goal := Ada.Real_Time.Clock;

         loop
            --  Limit event loop to 144 Hz using realtime "delay until"
            Loop_Start_Time_Goal := Loop_Start_Time_Goal + Frame_Duration;
            delay until Loop_Start_Time_Goal;

            while SDL.Events.Events.Poll (Event) loop
               case Event.Common.Event_Type is
                  when SDL.Events.Quit =>
                     Finished := True;

                  when SDL.Events.Keyboards.Key_Up =>
                     SDL.Log.Put_Debug ("Key up event    : " &
                                          SDL.Events.Keyboards.Key_Codes'Image (Event.Keyboard.Key_Sym.Key_Code) &
                                          "    Scan code: " &
                                          SDL.Events.Keyboards.Scan_Codes'Image (Event.Keyboard.Key_Sym.Scan_Code));

                     if Event.Keyboard.Key_Sym.Key_Code = SDL.Events.Keyboards.Code_Escape then
                        Finished := True;
                     end if;

                  when SDL.Events.Joysticks.Axis_Motion =>
                     SDL.Log.Put_Debug
                       ("Joystick axis event (ID = " & SDL.Events.Joysticks.IDs'Image (Event.Joystick_Axis.Which) &
                          "): Axis: " & SDL.Events.Joysticks.Axes'Image (Event.Joystick_Axis.Axis) &
                          "    Value: " & SDL.Events.Joysticks.Axes_Values'Image (Event.Joystick_Axis.Value));

                  when SDL.Events.Mice.Motion =>
                     SDL.Log.Put_Debug
                       ("Mouse motion event (ID = " & SDL.Events.Mice.IDs'Image (Event.Mouse_Motion.Which) &
                          "): (X => " & SDL.Coordinate'Image (Event.Mouse_Motion.X) &
                          "): (Y => " & SDL.Coordinate'Image (Event.Mouse_Motion.Y) &
                          "): (X Rel => " & SDL.Events.Mice.Movement_Values'Image (Event.Mouse_Motion.X_Relative) &
                          ", Y Rel => " & SDL.Events.Mice.Movement_Values'Image (Event.Mouse_Motion.Y_Relative) &
                          ")");

                  when SDL.Events.Mice.Button_Up | SDL.Events.Mice.Button_Down =>
                     SDL.Log.Put_Debug
                       ("Mouse button event (ID = " & SDL.Events.Mice.IDs'Image (Event.Mouse_Motion.Which) &
                          "): Button = " & SDL.Events.Mice.Buttons'Image (Event.Mouse_Button.Button) &
                          "    State: " & SDL.Events.Button_State'Image (Event.Mouse_Button.State) &
                          "    Clicks: " & SDL.Events.Mice.Button_Clicks'Image (Event.Mouse_Button.Clicks) &
                          "    (X => " & SDL.Coordinate'Image (Event.Mouse_Button.X) &
                          ", Y => " & SDL.Coordinate'Image (Event.Mouse_Button.Y) &
                          ")");

                  when SDL.Events.Mice.Wheel =>
                     SDL.Log.Put_Debug
                       ("Mouse wheel event (ID = " & SDL.Events.Mice.IDs'Image (Event.Mouse_Wheel.Which) &
                          "): (X => " & SDL.Events.Mice.Wheel_Values'Image (Event.Mouse_Wheel.X) &
                          "): (Y => " & SDL.Events.Mice.Wheel_Values'Image (Event.Mouse_Wheel.Y) &
                          "): (Direction => " & SDL.Events.Mice.Wheel_Directions'Image (Event.Mouse_Wheel.Direction) &
                          ")");

                  when SDL.Events.Windows.Window =>
                     if Event.Window.Event_ID = SDL.Events.Windows.Moved then
                        SDL.Log.Put_Debug ("Window on display : " &
                          SDL.Video.Displays.Display_Indices'Image (W.Display_Index));
                     end if;

                  when others =>
                     null;
               end case;
            end loop;

            exit when Finished;
         end loop;
      end;

      W.Finalize;
      SDL.Finalise;
   end if;
end Test;