ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18lg

Dependencies:   mbed

Committer:
el18lg
Date:
Sat May 23 17:21:24 2020 +0000
Revision:
1:bdafa20e71a0
Started my map, making my way onto my snake programme;

Who changed what in which revision?

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