Matis Requis 201241242

Dependencies:   mbed

Tempest Game

Game Screen

https://os.mbed.com/media/uploads/MatisRequis/tempest_board_wiki.png The board is made of 12 columns. The Hero stays at the top of the column

Game Controls

https://os.mbed.com/media/uploads/MatisRequis/gamepad_buttons.png

To control the hero spaceship point the joystick to the column you want the hero to go to.

Press the A button to shoot a bullet in the column you are currently in.

Committer:
MatisRequis
Date:
Wed May 27 05:53:03 2020 +0000
Revision:
12:1f1907ebebeb
Parent:
1:03d1c29c2f8a
Final Submission. I have read and agreed with Statement of Academic Integrity

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MatisRequis 1:03d1c29c2f8a 1 #include "Bitmap.h"
MatisRequis 1:03d1c29c2f8a 2
MatisRequis 1:03d1c29c2f8a 3 #include <iostream>
MatisRequis 1:03d1c29c2f8a 4
MatisRequis 1:03d1c29c2f8a 5 #include "N5110.h"
MatisRequis 1:03d1c29c2f8a 6
MatisRequis 1:03d1c29c2f8a 7 Bitmap::Bitmap(int const *contents,
MatisRequis 1:03d1c29c2f8a 8 unsigned int const height,
MatisRequis 1:03d1c29c2f8a 9 unsigned int const width)
MatisRequis 1:03d1c29c2f8a 10 :
MatisRequis 1:03d1c29c2f8a 11 _contents(std::vector<int>(height*width)),
MatisRequis 1:03d1c29c2f8a 12 _height(height),
MatisRequis 1:03d1c29c2f8a 13 _width(width)
MatisRequis 1:03d1c29c2f8a 14 {
MatisRequis 1:03d1c29c2f8a 15 // Perform a quick sanity check of the dimensions
MatisRequis 1:03d1c29c2f8a 16 if (_contents.size() != height * width) {
MatisRequis 1:03d1c29c2f8a 17 std::cerr << "Contents of bitmap has size " << _contents.size()
MatisRequis 1:03d1c29c2f8a 18 << " pixels, but its dimensions were specified as "
MatisRequis 1:03d1c29c2f8a 19 << width << " * " << height << " = " << width * height << std::endl;
MatisRequis 1:03d1c29c2f8a 20 }
MatisRequis 1:03d1c29c2f8a 21
MatisRequis 1:03d1c29c2f8a 22 for(unsigned int i = 0; i < height*width; ++i) _contents[i] = contents[i];
MatisRequis 1:03d1c29c2f8a 23 }
MatisRequis 1:03d1c29c2f8a 24
MatisRequis 1:03d1c29c2f8a 25 /**
MatisRequis 1:03d1c29c2f8a 26 * @returns the value of the pixel at the given position
MatisRequis 1:03d1c29c2f8a 27 */
MatisRequis 1:03d1c29c2f8a 28 int Bitmap::get_pixel(unsigned int const row,
MatisRequis 1:03d1c29c2f8a 29 unsigned int const column) const
MatisRequis 1:03d1c29c2f8a 30 {
MatisRequis 1:03d1c29c2f8a 31 // First check that row and column indices are within bounds
MatisRequis 1:03d1c29c2f8a 32 if(column >= _width || row >= _height)
MatisRequis 1:03d1c29c2f8a 33 {
MatisRequis 1:03d1c29c2f8a 34 std::cerr << "The requested pixel with index " << row << "," << column
MatisRequis 1:03d1c29c2f8a 35 << "is outside the bitmap dimensions: " << _width << ","
MatisRequis 1:03d1c29c2f8a 36 << _height << std::endl;
MatisRequis 1:03d1c29c2f8a 37 }
MatisRequis 1:03d1c29c2f8a 38
MatisRequis 1:03d1c29c2f8a 39 // Now return the pixel value, using row-major indexing
MatisRequis 1:03d1c29c2f8a 40 return _contents[row * _width + column];
MatisRequis 1:03d1c29c2f8a 41 }
MatisRequis 1:03d1c29c2f8a 42
MatisRequis 1:03d1c29c2f8a 43 /**
MatisRequis 1:03d1c29c2f8a 44 * @brief Prints the contents of the bitmap to the terminal
MatisRequis 1:03d1c29c2f8a 45 */
MatisRequis 1:03d1c29c2f8a 46 void Bitmap::print() const
MatisRequis 1:03d1c29c2f8a 47 {
MatisRequis 1:03d1c29c2f8a 48 for (unsigned int row = 0; row < _height; ++row)
MatisRequis 1:03d1c29c2f8a 49 {
MatisRequis 1:03d1c29c2f8a 50 // Print each element of the row
MatisRequis 1:03d1c29c2f8a 51 for (unsigned int column = 0; column < _width; ++column)
MatisRequis 1:03d1c29c2f8a 52 {
MatisRequis 1:03d1c29c2f8a 53 int pixel = get_pixel(row, column);
MatisRequis 1:03d1c29c2f8a 54 std::cout << pixel;
MatisRequis 1:03d1c29c2f8a 55 }
MatisRequis 1:03d1c29c2f8a 56
MatisRequis 1:03d1c29c2f8a 57 // And then terminate with a new-line character
MatisRequis 1:03d1c29c2f8a 58 std::cout << std::endl;
MatisRequis 1:03d1c29c2f8a 59 }
MatisRequis 1:03d1c29c2f8a 60 }
MatisRequis 1:03d1c29c2f8a 61
MatisRequis 1:03d1c29c2f8a 62 /**
MatisRequis 1:03d1c29c2f8a 63 * @brief Renders the contents of the bitmap onto an N5110 screen
MatisRequis 1:03d1c29c2f8a 64 *
MatisRequis 1:03d1c29c2f8a 65 * @param[in] lcd The screen to use for rendering
MatisRequis 1:03d1c29c2f8a 66 * @param[in] x0 The horizontal position in pixels at which to render the bitmap
MatisRequis 1:03d1c29c2f8a 67 * @param[in] y0 The vertical position in pixels at which to render the bitmap
MatisRequis 1:03d1c29c2f8a 68 *
MatisRequis 1:03d1c29c2f8a 69 * @details Note that x0, y0 gives the location of the top-left of the bitmap on
MatisRequis 1:03d1c29c2f8a 70 * the screen.
MatisRequis 1:03d1c29c2f8a 71 * This function only updates the buffer on the screen. You still need
MatisRequis 1:03d1c29c2f8a 72 * to refresh the screen in order to actually see the bitmap.
MatisRequis 1:03d1c29c2f8a 73 */
MatisRequis 1:03d1c29c2f8a 74 void Bitmap::render(N5110 &lcd,
MatisRequis 1:03d1c29c2f8a 75 unsigned int const x0,
MatisRequis 1:03d1c29c2f8a 76 unsigned int const y0) const
MatisRequis 1:03d1c29c2f8a 77 {
MatisRequis 1:03d1c29c2f8a 78 // Loop through each row of the bitmap image
MatisRequis 1:03d1c29c2f8a 79 for (unsigned int bitmap_row = 0; bitmap_row < _height; ++bitmap_row)
MatisRequis 1:03d1c29c2f8a 80 {
MatisRequis 1:03d1c29c2f8a 81 // Row index on the screen for rendering the row of pixels
MatisRequis 1:03d1c29c2f8a 82 unsigned int screen_row = y0 + bitmap_row;
MatisRequis 1:03d1c29c2f8a 83
MatisRequis 1:03d1c29c2f8a 84 // Render each pixel in the row
MatisRequis 1:03d1c29c2f8a 85 for (unsigned int bitmap_col = 0; bitmap_col < _width; ++bitmap_col)
MatisRequis 1:03d1c29c2f8a 86 {
MatisRequis 1:03d1c29c2f8a 87 // Column index on the screen for rendering this pixel
MatisRequis 1:03d1c29c2f8a 88 int screen_col = x0 + bitmap_col;
MatisRequis 1:03d1c29c2f8a 89
MatisRequis 1:03d1c29c2f8a 90 // Find the required value of the pixel at the given location within
MatisRequis 1:03d1c29c2f8a 91 // the bitmap data and then write it to the LCD screen
MatisRequis 1:03d1c29c2f8a 92 int pixel = get_pixel(bitmap_row, bitmap_col);
MatisRequis 1:03d1c29c2f8a 93 lcd.setPixel(screen_col, screen_row, pixel);
MatisRequis 1:03d1c29c2f8a 94 }
MatisRequis 1:03d1c29c2f8a 95 }
MatisRequis 1:03d1c29c2f8a 96 }