-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'port_160_170_to_alire' into 'main'
Port 160 Genericity and 170 Multiple Inheritance Closes #3, #11, and #15 See merge request services/training/labs_solar_system!22
- Loading branch information
Showing
26 changed files
with
1,926 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
----------------------------------------------------------------------- | ||
-- Ada Labs -- | ||
-- -- | ||
-- Copyright (C) 2008-2009, AdaCore -- | ||
-- -- | ||
-- Labs 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 2 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, write to the Free Software Foundation, Inc., 59 Temple -- | ||
-- Place - Suite 330, Boston, MA 02111-1307, USA. -- | ||
----------------------------------------------------------------------- | ||
|
||
with Ada.Real_Time; use Ada.Real_Time; | ||
|
||
with Solar_System; | ||
with Mage; use Mage; | ||
with Mage.Event; use Mage.Event; | ||
with Mage.Draw; use Mage.Draw; | ||
with Solar_System.Graphics; | ||
|
||
procedure Genericity_Main is | ||
type Bodies_Enum_1_T is (Sun1, Earth); | ||
type Bodies_Enum_2_T is (Sun2, Jupiter); | ||
|
||
package My_Solar_System1 is new Solar_System (Bodies_Enum_1_T); | ||
package My_Sol_Sys_Graphics1 is new My_Solar_System1.Graphics; | ||
use My_Solar_System1; | ||
use My_Sol_Sys_Graphics1; | ||
|
||
package My_Solar_System2 is new Solar_System (Bodies_Enum_2_T); | ||
package My_Sol_Sys_Graphics2 is new My_Solar_System2.Graphics; | ||
use My_Solar_System2; | ||
use My_Sol_Sys_Graphics2; | ||
|
||
-- declare variable Bodies which is an array of Body_T | ||
Bodies1 : My_Solar_System1.Bodies_Array_T; | ||
Bodies2 : My_Solar_System2.Bodies_Array_T; | ||
|
||
-- declare a variable Next of type Time to store the Next step time | ||
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 | ||
|
||
-- Create the main window | ||
Window := | ||
Create_Window (Width => 240, Height => 320, Name => "Solar System"); | ||
-- retrieve the graphical canvas associated with the main window | ||
Canvas := Get_Canvas (Window); | ||
|
||
-- initialize Bodies using Init_Body procedure | ||
Init_Body | ||
(B => Sun1, | ||
Bodies => Bodies1, | ||
Radius => 20.0, | ||
Color => Yellow, | ||
Distance => 0.0, | ||
Angle => 0.0, | ||
Speed => 0.0, | ||
Turns_Around => Sun1); | ||
|
||
Init_Body | ||
(B => Earth, | ||
Bodies => Bodies1, | ||
Radius => 5.0, | ||
Color => Blue, | ||
Distance => 50.0, | ||
Angle => 0.0, | ||
Speed => 0.02, | ||
Turns_Around => Sun1); | ||
|
||
Init_Body | ||
(B => Sun2, | ||
Bodies => Bodies2, | ||
Radius => 20.0, | ||
Color => Yellow, | ||
Distance => 0.0, | ||
Angle => 0.0, | ||
Speed => 0.0, | ||
Turns_Around => Sun2); | ||
|
||
Init_Body | ||
(B => Jupiter, | ||
Bodies => Bodies2, | ||
Radius => 10.0, | ||
Color => (255, 150, 0, 255), | ||
Distance => 80.0, | ||
Angle => 180.0, | ||
Speed => 0.01, | ||
Turns_Around => Sun2); | ||
|
||
Set_Center (Bodies1, -30.0, -30.0); | ||
Set_Center (Bodies2, 50.0, 50.0); | ||
|
||
Init_Body | ||
(B => Jupiter, | ||
Bodies => Bodies2, | ||
Radius => 10.0, | ||
Color => (255, 150, 0, 255), | ||
Distance => 80.0, | ||
Angle => 180.0, | ||
Speed => 0.01, | ||
Turns_Around => Sun2); | ||
|
||
Set_Center (Bodies1, -30.0, -30.0); | ||
Set_Center (Bodies2, 50.0, 50.0); | ||
|
||
Next := Clock + Period; | ||
|
||
while not Is_Killed loop | ||
|
||
Move_All (Bodies1); | ||
Move_All (Bodies2); | ||
|
||
Draw_All (Bodies1, Canvas); | ||
Draw_All (Bodies2, Canvas); | ||
|
||
Handle_Events (Window); | ||
|
||
delay until Next; | ||
Next := Next + Period; | ||
end loop; | ||
|
||
end Genericity_Main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
with Mage.Draw; use Mage.Draw; | ||
|
||
generic | ||
package Solar_System.Graphics is | ||
|
||
procedure Draw_All (Bodies : Bodies_Array_T; Canvas : Canvas_ID); | ||
|
||
private | ||
|
||
procedure Draw_Body (Object : Body_T; Canvas : Canvas_ID); | ||
|
||
end Solar_System.Graphics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
----------------------------------------------------------------------- | ||
-- Ada Labs -- | ||
-- -- | ||
-- Copyright (C) 2008-2009, AdaCore -- | ||
-- -- | ||
-- Labs 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 2 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, write to the Free Software Foundation, Inc., 59 Temple -- | ||
-- Place - Suite 330, Boston, MA 02111-1307, USA. -- | ||
----------------------------------------------------------------------- | ||
|
||
with Float_Maths; use Float_Maths; | ||
|
||
package body Solar_System is | ||
|
||
procedure Init_Body | ||
(B : Bodies_Enum_T; | ||
Bodies : in out Bodies_Array_T; | ||
Radius : Float; | ||
Color : RGBA_T; | ||
Distance : Float; | ||
Angle : Float; | ||
Speed : Float; | ||
Turns_Around : Bodies_Enum_T; | ||
Visible : Boolean := True) | ||
is | ||
begin | ||
|
||
Bodies (B) := | ||
(Distance => Distance, | ||
Speed => Speed, | ||
Angle => Angle, | ||
Turns_Around => Turns_Around, | ||
Visible => Visible, | ||
Color => Color, | ||
Radius => Radius, | ||
others => <>); | ||
|
||
end Init_Body; | ||
|
||
procedure Set_Center | ||
(Bodies : in out Bodies_Array_T; X : Float; Y : Float) is | ||
begin | ||
Bodies (Bodies_Enum_T'First).X := X; | ||
Bodies (Bodies_Enum_T'First).Y := Y; | ||
end Set_Center; | ||
|
||
-- implement a function to compute the X coordinate | ||
-- x of the reference + distance * cos(angle) | ||
function Compute_X | ||
(Body_To_Move : Body_T; | ||
Turns_Around : Body_T) return Float; | ||
|
||
-- implement a function to compute the Y coordinate | ||
-- y of the reference + distance * sin(angle) | ||
function Compute_Y | ||
(Body_To_Move : Body_T; | ||
Turns_Around : Body_T) return Float; | ||
|
||
function Compute_X | ||
(Body_To_Move : Body_T; | ||
Turns_Around : Body_T) return Float | ||
is | ||
begin | ||
return Turns_Around.X + Body_To_Move.Distance * Cos (Body_To_Move.Angle); | ||
end Compute_X; | ||
|
||
function Compute_Y | ||
(Body_To_Move : Body_T; | ||
Turns_Around : Body_T) return Float | ||
is | ||
begin | ||
return Turns_Around.Y + Body_To_Move.Distance * Sin (Body_To_Move.Angle); | ||
end Compute_Y; | ||
|
||
procedure Move (Body_To_Move : in out Body_T; Bodies : Bodies_Array_T) is | ||
begin | ||
|
||
Body_To_Move.X := | ||
Compute_X (Body_To_Move, Bodies (Body_To_Move.Turns_Around)); | ||
|
||
Body_To_Move.Y := | ||
Compute_Y (Body_To_Move, Bodies (Body_To_Move.Turns_Around)); | ||
|
||
Body_To_Move.Angle := Body_To_Move.Angle + Body_To_Move.Speed; | ||
|
||
end Move; | ||
|
||
procedure Move_All (Bodies : in out Bodies_Array_T) is | ||
begin | ||
|
||
-- loop over all bodies and call Move procedure | ||
for B of Bodies loop | ||
|
||
-- call the move procedure for each body | ||
Move (B, Bodies); | ||
end loop; | ||
|
||
end Move_All; | ||
|
||
end Solar_System; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
----------------------------------------------------------------------- | ||
-- Ada Labs -- | ||
-- -- | ||
-- Copyright (C) 2008-2009, AdaCore -- | ||
-- -- | ||
-- Labs 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 2 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, write to the Free Software Foundation, Inc., 59 Temple -- | ||
-- Place - Suite 330, Boston, MA 02111-1307, USA. -- | ||
----------------------------------------------------------------------- | ||
|
||
with Mage; use Mage; | ||
|
||
generic | ||
type Bodies_Enum_T is (<>); | ||
package Solar_System is | ||
|
||
type Bodies_Array_T is private; | ||
|
||
procedure Init_Body | ||
(B : Bodies_Enum_T; | ||
Bodies : in out Bodies_Array_T; | ||
Radius : Float; | ||
Color : RGBA_T; | ||
Distance : Float; | ||
Angle : Float; | ||
Speed : Float; | ||
Turns_Around : Bodies_Enum_T; | ||
Visible : Boolean := True); | ||
|
||
procedure Set_Center (Bodies : in out Bodies_Array_T; X : Float; Y : Float); | ||
procedure Move_All (Bodies : in out Bodies_Array_T); | ||
|
||
private | ||
|
||
-- define a type Body_T to store every information about a body | ||
-- X, Y, Distance, Speed, Angle, Radius, Color | ||
type Body_T is record | ||
X : Float := 0.0; | ||
Y : Float := 0.0; | ||
Distance : Float; | ||
Speed : Float; | ||
Angle : Float; | ||
Radius : Float; | ||
Color : RGBA_T; | ||
Visible : Boolean := True; | ||
Turns_Around : Bodies_Enum_T; | ||
end record; | ||
|
||
type Bodies_Array_T is array (Bodies_Enum_T) of Body_T; | ||
|
||
procedure Move (Body_To_Move : in out Body_T; Bodies : Bodies_Array_T); | ||
|
||
end Solar_System; |
Oops, something went wrong.