Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 14:52:19 2019 +0000
Revision:
104:17040265b7b4
Parent:
51:387249f9b333
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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