Game codes for Pokemon Academy Yiu Fai Kwok - 201198802 I have read the University Regulations on Plagiarism and state that the work covered by this declaration is my own and does not contain any unacknowledged work from other sources.

Dependencies:   mbed FXOS8700CQ mbed-rtos

Committer:
yfkwok
Date:
Thu May 09 00:41:05 2019 +0000
Revision:
34:3ddfaa217eca
Parent:
0:1da4db5de653
09/05/2019 - Last commit before submission

Who changed what in which revision?

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