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