Skip to content

Commit

Permalink
Merge branch 'port_160_170_to_alire' into 'main'
Browse files Browse the repository at this point in the history
Port 160 Genericity and 170 Multiple Inheritance

Closes #3, #11, and #15

See merge request services/training/labs_solar_system!22
  • Loading branch information
leogermond committed Sep 27, 2023
2 parents f7c1b13 + 048a272 commit b333f9e
Show file tree
Hide file tree
Showing 26 changed files with 1,926 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ generate: \
generate_packages \
generate_private_types \
generate_access_types \
generate_genericity \
generate_interfacing_with_c \
generate_tasking_protected_objects \
generate_subprogram_contracts \
Expand Down Expand Up @@ -55,6 +56,13 @@ generate_access_types: $(template_access_types)
adacut -m answer $$f > src/140_access_types/answers/$$(basename $$f); \
done

template_genericity := $(wildcard src/160_genericity/template/*.ad?)
generate_genericity: $(template_genericity)
for f in $^; do \
adacut -m question $$f > src/160_genericity/src/$$(basename $$f); \
adacut -m answer $$f > src/160_genericity/answers/$$(basename $$f); \
done

template_interfacing_with_c := $(wildcard src/230_interfacing_with_c/template/*.ad?)
generate_interfacing_with_c: $(template_interfacing_with_c)
for f in $^; do \
Expand Down
13 changes: 12 additions & 1 deletion labs_solar_system.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ project Labs_Solar_System is
"Packages",
"Private_Types",
"Access_Types",
"Genericity",
"Multiple_Inheritance",
"Exceptions",
"Interfacing_With_C",
"Tasking_Protected_Objects",
Expand All @@ -33,6 +35,7 @@ project Labs_Solar_System is
Lab_Name := "";
Lab_Languages := ("Ada");
Lab_Source_Dir := ();
Lab_Switches_Ada := ();
case Lab is
when "Getting_Started" =>
Lab_Number := "000";
Expand All @@ -57,6 +60,14 @@ project Labs_Solar_System is
Lab_Number := "140";
Lab_Name := "access_types";
Lab_Source_Dir := ("src/" & Lab_Number & "_" & Lab_Name & "/common");
when "Genericity" =>
Lab_Number := "160";
Lab_Name := "genericity";
Lab_Source_Dir := ("src/" & Lab_Number & "_" & Lab_Name & "/common");
when "Multiple_Inheritance" =>
Lab_Number := "adv_170";
Lab_Name := "multiple_inheritance";
Lab_Switches_Ada := ("-gnatw_A");
when "Exceptions" =>
Lab_Number := "190";
Lab_Name := "exceptions";
Expand Down Expand Up @@ -91,7 +102,7 @@ project Labs_Solar_System is

package Compiler is
for Default_Switches ("Ada") use Labs_Solar_System_Config.Ada_Compiler_Switches
& ("-O0", "-g", "-gnata");
& ("-O0", "-g", "-gnata") & Lab_Switches_Ada;
for Default_Switches ("C") use ("-Wall", "-pedantic", "-Werror");
end Compiler;

Expand Down
138 changes: 138 additions & 0 deletions src/160_genericity/answers/genericity_main.adb
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;
12 changes: 12 additions & 0 deletions src/160_genericity/answers/solar_system-graphics.ads
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;
109 changes: 109 additions & 0 deletions src/160_genericity/answers/solar_system.adb
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;
62 changes: 62 additions & 0 deletions src/160_genericity/answers/solar_system.ads
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;
Loading

0 comments on commit b333f9e

Please sign in to comment.