ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18ac

Dependencies:   mbed

Committer:
ale_carb0ni
Date:
Tue May 26 17:14:44 2020 +0000
Revision:
5:a919651ebf36
working game w/out doxygen

Who changed what in which revision?

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