contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

Committer:
OmarAlebiary
Date:
Tue May 07 15:17:21 2019 +0000
Revision:
40:13b8467526d0
Parent:
39:822b66b1c935
Final Submission. I have read and agreed with Statement of Academic Integrity

Who changed what in which revision?

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