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