labs_solar_system_1.0.0_4f650637/src/000_getting_started/src/getting_started_main.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
-----------------------------------------------------------------------
--                              Ada Labs                             --
--                                                                   --
--                 Copyright (C) 2008-2023, AdaCore                  --
--                                                                   --
-- This program is free software: you can redistribute it and/or     --
-- modify it under the terms of the GNU General Public License as    --
-- published by the Free Software Foundation, either version 3 of    --
-- the License, or (at your option) any later version.               --
--                                                                   --
-- This program is distributed in the hope that it will be useful,   --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of    --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     --
-- GNU General Public License for more details.                      --
--                                                                   --
-- You should have received a copy of the GNU General Public License --
-- along with this program.  If not, see                             --
-- <https://www.gnu.org/licenses/>.                                  --
-----------------------------------------------------------------------

with Mage;          use Mage;
with Mage.Draw;     use Mage.Draw;
with Mage.Event;    use Mage.Event;
with Ada.Real_Time; use Ada.Real_Time;

procedure Getting_Started_Main is
   Width       : constant := 240.0;
   Height      : constant := 320.0;
   Ball_Radius : constant := 20.0;

   X       : Float              := 0.0;
   Y       : Float              := 0.0;
   Speed_X : Float              := 2.0;
   Speed_Y : Float              := 4.0;
   Next    : Time;
   Period  : constant Time_Span := Milliseconds (40);

   --  reference to the application window
   Window : Window_ID;

   --  reference to the graphical canvas associated with the application window
   Canvas : Canvas_ID;

begin
   Window :=
     Create_Window
       (Width  => Integer (Width),
        Height => Integer (Height),
        Name   => "Bouncing ball");
   Canvas := Get_Canvas (Window);

   Next := Clock + Period;

   while not Is_Killed loop

      if (abs X) + Ball_Radius >= Width / 2.0 then
         Speed_X := -Speed_X;
      end if;

      if (abs Y) + Ball_Radius >= Height / 2.0 then
         Speed_Y := -Speed_Y;
      end if;

      X := X + Speed_X;
      Y := Y + Speed_Y;

      Draw_Sphere
        (Canvas   => Canvas,
         Position => (X, Y, 0.0),
         Radius   => Ball_Radius,
         Color    => Red);

      Handle_Events (Window);

      delay until Next;
      Next := Next + Period;

   end loop;

end Getting_Started_Main;