LAB 12A - NBODIES
What is a file?
At the start of the semester we asked the question "what is a file?", and we learned that it's just a series of bits (0's and 1's)! How, then, does an MP3 file differ from a PNG file? Format! PNG image files are formatted very differently from MP3 files. The ".mp3" or ".png" extension tell the user (and the operating system) the format the data is in within the file, and that is how the user (or the operating system) knows which program will be able to correctly understand that data. If some user was feeling a little crazy and manually changed the ".mp3" extension in an audio file to ".png", then the data contained within that file is still in 'mp3' format; however, now the operating system would try to open the file with Paint (or whatever the default image viewer is) and would fail to correctly understand the data (series of bits) contained within that file.
In this episode of CSCI261...
For this lab you need to create your own software that can make sense of (i.e., visualize) some data contained within a file. For now, we will provide you with simulation files (which in this case is really just a normal text file) to visualize; however, in Homework 12, you will create your own simulations (i.e., files) that you will then visualize with the software you create in this lab. The final product should look similar to:
Instructions
Your task in this lab is to read a simulation file into a C++ program and visualize its contents. The simulation file you are to read is of a certain format (like mp3), which we will call the 'bs' format (for body simulation people!). As you can see in the example above, the simulation file describes a system of variable-sized bodies that move around on the screen. More specifically, the simulation files are formatted in the following fashion:
- Line 1: [Number of bodies to be drawn (N)] [X Boundary] [Y Boundary]
- Line 2: [Body 1 size] [Body 2 size] [...] [Body N size]
- Line 3: [Body 1 X-Pos] [Body 1 Y-Pos] [Body 2 X-Pos] [Body 2 Y-Pos] [...] [Body N X-Pos] [Body N Y-Pos]
- Line 4: [Body 1 X-Pos] [Body 1 Y-Pos] [Body 2 X-Pos] [Body 2 Y-Pos] [...] [Body N X-Pos] [Body N Y-Pos]
- ...
- Line M: [Body 1 X-Pos] [Body 1 Y-Pos] [Body 2 X-Pos] [Body 2 Y-Pos] [...] [Body N X-Pos] [Body N Y-Pos]
To complete this lab, you need to develop a program that will (1) read the file described above and (2) visualize its contents. We provide three different simulation files to test your solution against, and each of these files instructs your program to draw a different number of bodies; in other words, your code must be able to accommodate any number of bodies. You can download these simulation files HERE. In order to accomplish this task we suggest you do the following:
- Create an SFML window with the X/Y dimensions provided in the file.
- Create a structure (array or vector) of CircleShape objects of size N.
- Limit the framerate of your window to 30 frames per second by using the RenderWindow object's "setFramerateLimit()" member function (note lower case r).
- Create a function which takes your CircleShape structure as an input, reads a line in the simulation file, and updates (using the "setPosition()" function) the position of each CircleShape in the window.
- After the moves have all been visualized, have the simulation repeat (loop).
Your solution to this lab will be due with Homework 12. Your submission will be graded on:
- Whether your program asks the user to input the name of a simulation file and, if no name entered, provides a default file name for ease of use (such as "bodySim.bs").
- Whether your program works for any number of bodies.
- Whether your program can draw the bodies with variable sizes.
- Whether the positions of your bodies are updated correctly (i.e., the bodies move smoothly).
- EXTRA CREDIT (3 pts) possible if the colors of your bodies are adjusted (in a logical way) according to their size.