Project: Step 2.1
Living Space


Disclaimer: the automatically generated English translation is provided only for convenience and it may contain wording flaws. The original French document must be taken as reference!


Goals: Set up the living space for the future animals

Introduction

In this step, we aim to create a first graphical program that allows us to visualize the simulated "environment" (class Environment) and the different entities evolving within it over time (animals, plants, etc.).

Our program will now have as its main component a class Application; the "simulation core" capable of doing graphics and handling events, provided in src/Application.[hpp/cpp].

The class Application serves the same purpose as the class of the same name provided in the advanced solution of the mini-graphics project. More specifically, this class has: It is through the Environment member that the link is made between the provided simulation core and what you code. The class Application has a method handleEvent (line 468 of Application.cpp) that allows the simulation to respond to keyboard keys: for example, the Esc key allows ending the simulation (closing the window). Take note of the existence of this method for future use.

The class Environment

The wildlife we are going to simulate will evolve in an environment. Therefore, we need to start by modeling this concept in a basic way.

A class Environment will naturally be needed here. At this stage, we will consider that an environment is characterized by:

You will code the class Environment in the directory src/Environment.

To model a set of animals and targets, we will anticipate a little bit of our future course on data structures. The concept of list which does not allow indexing will have the advantage of enabling potentially less costly insertions/removals of elements.

Example usage:

#include <list>
...
list<int> integers; // or list<int> integers(3) or list<int> integers(3,1)
                   // with the same meaning as for vector constructors

integers.push_back(3); // insertion at the end
integers.push_front(2); // insertion at the front
integers.pop_front(); // removal at the front
integers.pop_back();
Since there is no indexing, the traversal of a list is done through iterations over the entire set:
for (auto animal: animals)
or
for (auto& animal: animals)

The following methods are to be expected for the class Environment  :

You are free to add any other method you think is useful later.

[Question Q2.1] Which methods do you think it would be wise to declare as const?

[Question Q2.2] We want to prevent the copying of an Environment. What solution would you propose to satisfy this constraint ?

[Question Q2.3] Question Q2.2 suggests that each simulation will deal with a unique environment with its own wildlife. Moreover, it doesn't make much sense to let the animals live without attaching them to an environment. An Environment can therefore be considered responsible for the lifetime of the animals created in the simulation. What impact does this have on the destruction of an environment ?

Think about these questions and answer them in your RESPONSES file, and adapt your code accordingly.

Test 2: A Basic Environment

To test the work done so far, you will use the test provided in src/Tests/GraphicalTests/EnvTest.cpp. The class EnvTest inherits from Application. It therefore has an environment on which the drawing and update methods you have drafted will be invoked in a loop. The R key on the keyboard (handled by Application::handleEvent) actually triggers a call to the clean method you just programmed.

This class also overrides the onEvent method so that pressing the 'T' key results in a call to the addTarget method you just programmed.

The provided CMakeLists.txt file allows you to compile the test via the envTest target. Don't forget to uncomment this target in the CMakeLists.txt file (lines 100, 101, and lines 143, 144) and run Build > Run Cmake when you are ready to test.

The behavior of your program at this stage should resemble what is shown in this short video (restart it if necessary):

← You can use the 'T' key to place targets/resources in the environment where the mouse is located.
Make sure that the 'R' key clears the environment of all created targets.
[Video: placing targets in the environment]

The keys that can be used by the program are documented in the small help menu that appears in the gray portion displayed to the right of the simulation window.


Back to the project description (part 2) Next module (part 2.2)