ELEC2645 (2018/19) / Mbed 2 deprecated ll16o2l_ELEC2645

Dependencies:   mbed Gamepad

Committer:
ll16o2l
Date:
Tue Mar 05 09:24:19 2019 +0000
Revision:
0:03363a508918
initial commit

Who changed what in which revision?

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