Skip to content

Commit 53539bd

Browse files
committed
05 records, 06 subprograms
1 parent 3288955 commit 53539bd

12 files changed

Lines changed: 998 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
:title: Lab 5 - Records
2+
:subtitle: The Satellite around the Earth
3+
4+
The purpose of this exercise is to use records to store information about the celestial
5+
bodies.
6+
7+
8+
.. figure:: Labs/Solar_System/05_1.png
9+
:height: 300px
10+
:name:
11+
12+
Expected result
13+
14+
==========
15+
Question 1
16+
==========
17+
18+
Create a record type that holds all the information needed to describe an object,
19+
(Speed, Distance, Angle, X, Y, Radius, Color).
20+
21+
Replace the two arrays solution of the previous exercise by one array indexed by
22+
:code:`Bodies_Enum` and containing components of this record.
23+
24+
Remark : Initialize each body using aggregate notation.
25+
26+
==========
27+
Question 2
28+
==========
29+
30+
Add a :code:`Turns_Around` field to the record, in order to store the object around which the
31+
object rotates, instead of forcing it to always rotate around the previous object of the
32+
system.
33+
34+
Have both the :code:`Satellite` and the :code:`Moon` turn around the :code:`Earth`.
35+
36+
==========
37+
Question 3
38+
==========
39+
40+
All the objects are turning counter-clockwise, make the :code:`Moon` turning clockwise.
41+
42+
=====================
43+
Question 4 (Advanced)
44+
=====================
45+
46+
Implement black holes.
47+
48+
Modify your record to handle the case of an invisible object which does not have
49+
:code:`Radius` and :code:`Color` properties (hint: use discriminated record with a
50+
:code:`Boolean` discriminant named :code:`Visible`).
Binary file not shown.
Binary file not shown.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
-----------------------------------------------------------------------
2+
-- Ada Labs --
3+
-- --
4+
-- Copyright (C) 2008-2009, AdaCore --
5+
-- --
6+
-- Labs is free software; you can redistribute it and/or modify it --
7+
-- under the terms of the GNU General Public License as published by --
8+
-- the Free Software Foundation; either version 2 of the License, or --
9+
-- (at your option) any later version. --
10+
-- --
11+
-- This program is distributed in the hope that it will be useful, --
12+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
13+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
14+
-- General Public License for more details. You should have received --
15+
-- a copy of the GNU General Public License along with this program; --
16+
-- if not, write to the Free Software Foundation, Inc., 59 Temple --
17+
-- Place - Suite 330, Boston, MA 02111-1307, USA. --
18+
-----------------------------------------------------------------------
19+
20+
with Ada.Real_Time; use Ada.Real_Time;
21+
22+
with Display; use Display;
23+
with Display.Basic; use Display.Basic;
24+
with Libm_Single; use Libm_Single;
25+
26+
procedure Main is
27+
28+
-- define type Bodies_Enum_T as an enumeration of Sun, Earth, Moon, Satellite
29+
type Bodies_Enum_T is (Sun, Earth, Moon, Satellite);
30+
31+
-- define a type Body_T to store every information about a body
32+
-- X, Y, Distance, Speed, Angle, Color, Radius
33+
type Body_T (Visible : Boolean := True) is record
34+
X : Float;
35+
Y : Float;
36+
Distance : Float;
37+
Speed : Float;
38+
Angle : Float;
39+
Turns_Around : Bodies_Enum_T;
40+
case Visible is
41+
when True =>
42+
Color : RGBA_T;
43+
Radius : Float;
44+
when others =>
45+
null;
46+
end case;
47+
end record;
48+
49+
-- define type Bodies_Array_T as an array of Body_T indexed by bodies
50+
type Bodies_Array_T is array (Bodies_Enum_T) of Body_T;
51+
52+
-- declare variable Bodies which is an array of Body_T
53+
Bodies : Bodies_Array_T;
54+
55+
-- declare a variable Next of type Time to store the Next step time
56+
Next : Time;
57+
58+
-- declare a constant Period of 40 milliseconds of type Time_Span defining the loop period
59+
Period : constant Time_Span := Milliseconds (40);
60+
61+
-- reference to the application window
62+
Window : Window_ID;
63+
64+
-- reference to the graphical canvas associated with the application window
65+
Canvas : Canvas_ID;
66+
67+
begin
68+
69+
-- Create a window 240x320
70+
Window := Create_Window(Width => 240,
71+
Height => 320,
72+
Name => "Solar System");
73+
74+
-- Retrieve the graphical canvas from the window
75+
Canvas := Get_Canvas (Window);
76+
77+
78+
-- initialize Bodies variable with parameters for each body using an aggregate
79+
-- Sun Distance = 0.0, Angle = 0.0, Speed = 0.0, Radius = 20.0, Color = Yellow
80+
-- Earth Distance = 50.0, Angle = 0.0, Speed = 0.02, Radius = 5.0, Color = Blue
81+
-- Moon Distance = 15.0, Angle = 0.0, Speed = 0.04, Radius = 2.0, Color = White
82+
-- Satellite Distance = 8.0, Angle = 0.0, Speed = 0.1, Radius = 1.0, Color = Red
83+
Bodies := (Sun => (Visible => True,
84+
Distance => 0.0,
85+
Speed => 0.0,
86+
Radius => 20.0,
87+
X => 0.0,
88+
Y => 0.0,
89+
Angle => 0.0,
90+
Color => Yellow,
91+
Turns_Around => Sun),
92+
Earth => (Visible => False,
93+
Distance => 50.0,
94+
Speed => 0.02,
95+
--Radius => 5.0,
96+
X => 0.0,
97+
Y => 0.0,
98+
Angle => 0.0,
99+
-- Color => Blue,
100+
Turns_Around => Sun),
101+
Moon => (Visible => True,
102+
Distance => 15.0,
103+
Speed => -0.04,
104+
Radius => 2.0,
105+
X => 0.0,
106+
Y => 0.0,
107+
Angle => 0.0,
108+
Color => White,
109+
Turns_Around => Earth),
110+
Satellite => (Visible => True,
111+
Distance => 8.0,
112+
Speed => 0.1,
113+
Radius => 1.0,
114+
X => 0.0,
115+
Y => 0.0,
116+
Angle => 0.0,
117+
Color => Red,
118+
Turns_Around => Earth));
119+
120+
-- initialize the Next step time begin the current time (Clock) + the period
121+
Next := Clock + Period;
122+
123+
124+
while not Is_Killed loop
125+
126+
-- create a loop to update each body position and angles
127+
-- the position of an object around (0,0) at distance d with an angle a
128+
-- is (d*cos(a), d*sin(a))
129+
-- update angle parameter of each body adding speed to the previous angle
130+
for B in Earth .. Satellite loop
131+
-- Local block statement here to reduce repetition in the body
132+
declare
133+
TA : constant Bodies_Enum_T := Bodies (B).Turns_Around;
134+
A : constant Float := Bodies (B).Angle;
135+
D : constant Float := Bodies (B).Distance;
136+
begin
137+
Bodies (B).X := Bodies (TA).X + D * Cos (A);
138+
Bodies (B).Y := Bodies (TA).Y + D * Sin (A);
139+
Bodies (B).Angle := A + Bodies (B).Speed;
140+
end;
141+
end loop;
142+
143+
-- create a loop to draw every objects
144+
-- use the Draw_Sphere procedure to do it
145+
for B in Bodies_Enum_T loop
146+
if Bodies (B).Visible then
147+
Draw_Sphere(Canvas => Canvas,
148+
Position => (Bodies (B).X, Bodies (B).Y, 0.0),
149+
Radius => Bodies (B).Radius,
150+
Color => Bodies(B).Color);
151+
end if;
152+
end loop;
153+
154+
-- update the screen using procedure Swap_Buffers
155+
Swap_Buffers(Window);
156+
157+
-- wait until Next
158+
delay until Next;
159+
160+
-- update the Next time adding the period for the next step
161+
Next := Next + Period;
162+
163+
end loop;
164+
165+
166+
167+
end Main;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
with "game_support.gpr";
2+
3+
project Solar_System is
4+
5+
type LOADER_Type is
6+
("RAM", "ROM");
7+
LOADER : LOADER_Type := external ("LOADER", "ROM");
8+
9+
10+
type Mode_Type is ("Problem", "Solution");
11+
Mode : Mode_Type := external ("Mode", "Problem");
12+
13+
for Main use ("main");
14+
for Object_Dir use "obj";
15+
for Languages use ("Ada");
16+
17+
case Mode is
18+
when "Problem" =>
19+
for Source_Dirs use ("src");
20+
21+
when "Solution" =>
22+
for Source_Dirs use ("sol");
23+
end case;
24+
25+
for Target use Game_Support'Target;
26+
for Runtime ("Ada") use Game_Support'Runtime("Ada");
27+
28+
package Compiler is
29+
for Default_Switches ("Ada") use ("-gnat12", "-g", "-gnatQ", "-O0");
30+
end Compiler;
31+
32+
package Binder is
33+
for Default_Switches ("Ada") use ("-E");
34+
end Binder;
35+
36+
package Ide renames Game_Support.Ide;
37+
38+
end Solar_System;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
-----------------------------------------------------------------------
2+
-- Ada Labs --
3+
-- --
4+
-- Copyright (C) 2008-2012, AdaCore --
5+
-- --
6+
-- Labs is free software; you can redistribute it and/or modify it --
7+
-- under the terms of the GNU General Public License as published by --
8+
-- the Free Software Foundation; either version 2 of the License, or --
9+
-- (at your option) any later version. --
10+
-- --
11+
-- This program is distributed in the hope that it will be useful, --
12+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
13+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
14+
-- General Public License for more details. You should have received --
15+
-- a copy of the GNU General Public License along with this program; --
16+
-- if not, write to the Free Software Foundation, Inc., 59 Temple --
17+
-- Place - Suite 330, Boston, MA 02111-1307, USA. --
18+
-----------------------------------------------------------------------
19+
20+
with Ada.Text_IO; use Ada.Text_IO;
21+
with Ada.Real_Time; use Ada.Real_Time;
22+
23+
with Display; use Display;
24+
with Display.Basic; use Display.Basic;
25+
with Libm_Single; use Libm_Single;
26+
27+
procedure Main is
28+
29+
-- define type Bodies_Enum_T as an enumeration of Sun, Earth, Moon, Satellite
30+
type Bodies_Enum_T is (Sun, Earth, Moon, Satellite);
31+
32+
-- define a type Body_T to store every information about a body
33+
-- X, Y, Distance, Speed, Angle, Color type is RGBA_T, Radius
34+
35+
-- define type Bodies_Array_T as an array of Body_T indexed by bodies
36+
37+
-- declare variable Bodies which is an array of Body_T
38+
39+
-- declare a variable Next of type Time to store the Next step time
40+
Next : Time;
41+
42+
-- declare a constant Period of 40 milliseconds of type Time_Span defining the loop period
43+
Period : constant Time_Span := Milliseconds (40);
44+
45+
-- reference to the application window
46+
Window : Window_ID;
47+
48+
-- reference to the graphical canvas associated with the application window
49+
Canvas : Canvas_ID;
50+
51+
begin
52+
53+
-- Create a window 240x320
54+
Window :=
55+
Create_Window (Width => 240, Height => 320, Name => "Solar System");
56+
57+
-- Retrieve the graphical canvas from the window
58+
Canvas := Get_Canvas (Window);
59+
60+
-- initialize Bodies variable with parameters for each body using an aggregate
61+
-- Sun Distance = 0.0, Angle = 0.0, Speed = 0.0, Radius = 20.0, Color = Yellow
62+
-- Earth Distance = 50.0, Angle = 0.0, Speed = 0.02, Radius = 5.0, Color = Blue
63+
-- Moon Distance = 15.0, Angle = 0.0, Speed = 0.04, Radius = 2.0, Color = White
64+
-- Satellite Distance = 8.0, Angle = 0.0, Speed = 0.1, Radius = 1.0, Color = Red
65+
66+
-- initialize the Next step time begin the current time (Clock) + the period
67+
Next := Clock + Period;
68+
69+
while not Is_Killed loop
70+
71+
-- create a loop to update each body position and angles
72+
-- the position of an object around (0,0) at distance d with an angle a
73+
-- is (d*cos(a), d*sin(a))
74+
-- update angle parameter of each body adding speed to the previous angle
75+
76+
-- create a loop to draw every objects
77+
-- use the Draw_Sphere procedure to do it
78+
79+
-- update the screen using procedure Swap_Buffers
80+
Swap_Buffers (Window);
81+
82+
-- wait until Next
83+
delay until Next;
84+
85+
-- update the Next time adding the period for the next step
86+
Next := Next + Period;
87+
88+
end loop;
89+
90+
end Main;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
:title: Lab 6 - Subprograms
2+
:subtitle: A Simple Comet
3+
4+
The purpose of this exercise is to animate the planets using subprograms.
5+
6+
.. figure:: Labs/Solar_System/05_1.png
7+
:height: 300px
8+
:name:
9+
10+
Expected result
11+
12+
==========
13+
Question 1
14+
==========
15+
16+
Implement :code:`Compute_X` and :code:`Compute_Y` function and use them in the main loop to
17+
update :code:`X` and :code:`Y` coordinates of every objects.
18+
19+
==========
20+
Question 2
21+
==========
22+
23+
Implement the :code:`Move` subprogram and use it to move the objects instead of doing all
24+
computations in the main loop.
25+
26+
:code:`Move` should also update the angle.
27+
28+
==========
29+
Question 3
30+
==========
31+
32+
Implement a procedure :code:`Draw_Body` which is a wrapper to call :code:`Draw_Sphere`.
33+
34+
From the main loop call :code:`Draw_Body` instead of directly drawing :code:`Draw_Sphere`.
35+
:code:`Draw_Body` should only take 2 parameters.
36+
37+
==========
38+
Question 4
39+
==========
40+
41+
Add a comet in motion around the :code:`Sun`.

0 commit comments

Comments
 (0)