Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Committer:
DannyLee
Date:
Thu May 14 13:12:28 2020 +0000
Revision:
3:cf9fead9c3f4
Child:
8:b4a2954dd74f
aaa

Who changed what in which revision?

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