Craig Evans / N5110

Dependents:   LV7_LCDtest LV7_Grupa5_Tim003_Zadatak1 lv7_Grupa5_Tim008_zad1 LV7_PAI_Grupa5_tim10_Zadatak1 ... more

Files at this revision

API Documentation at this revision

Comitter:
valavanisalex
Date:
Wed Mar 08 14:12:42 2017 +0000
Parent:
35:2d5931a66fba
Child:
39:5998f0d22113
Commit message:
Added Bitmap class to simplify sprite support

Changed in this revision

Bitmap.cpp Show annotated file Show diff for this revision Revisions of this file
Bitmap.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bitmap.cpp	Wed Mar 08 14:12:42 2017 +0000
@@ -0,0 +1,34 @@
+#include "Bitmap.h"
+
+#include <iostream>
+
+Bitmap::Bitmap(std::vector<int> const &contents,
+               unsigned int const       height,
+               unsigned int const       width)
+    :
+    _contents(contents),
+    _height(height),
+    _width(width)
+{
+    // Perform a quick sanity check of the dimensions
+    if (contents.size() != height * height) {
+        std::cerr << "Contents of bitmap has size " << contents.size()
+                  << " pixels, but its dimensions were specified as "
+                  << width << " * " << height << " = " << width * height;
+    }
+}
+
+int Bitmap::get_pixel(unsigned int const row,
+                      unsigned int const column) const
+{
+    // First check that row and column indices are within bounds
+    if(column >= _width || row >= _height)
+    {
+        std::cerr << "The requested pixel with index " << row << "," << column
+                  << "is outside the bitmap dimensions: " << _width << ","
+                  << _height;
+    }
+
+    // Now return the pixel value, using row-major indexing
+    return _contents[row * _width + column];
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bitmap.h	Wed Mar 08 14:12:42 2017 +0000
@@ -0,0 +1,30 @@
+#ifndef BITMAP_H
+#define BITMAP_H
+
+#include <vector>
+
+/**
+ * A monochrome bitmap drawing
+ */
+class Bitmap
+{
+private:
+    /**
+     * @brief The contents of the drawing, with pixels stored in row-major order
+     * @details '1' represents a black pixel; '0' represents white
+     */
+    std::vector<int> _contents;
+    
+    unsigned int _height; ///< The height of the drawing in pixels
+    unsigned int _width;  ///< The width of the drawing in pixels
+    
+public:
+    Bitmap(std::vector<int> const &contents,
+           unsigned int const       height,
+           unsigned int const       width);
+
+    int get_pixel(unsigned int const row,
+                  unsigned int const column) const;
+};
+
+#endif // SPRITE_H
\ No newline at end of file