A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Sun May 05 18:04:43 2019 +0000
Revision:
30:ec915d24d3e9
Parent:
N5110/Bitmap.cpp@0:8e92b66a0755
Title Screen done;

Who changed what in which revision?

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