瀏覽代碼

Moved Pixel to its own files and made it a class to hide value while keeping r g b public. I love C++ now

Florian DORMONT 10 年之前
父節點
當前提交
4888881897
共有 4 個文件被更改,包括 63 次插入37 次删除
  1. 0 37
      include/Graphics.h
  2. 38 0
      include/Pixel.h
  3. 24 0
      src/Pixel.cpp
  4. 1 0
      src/main.cpp

+ 0 - 37
include/Graphics.h

@@ -1,45 +1,8 @@
 #ifndef INCLUDE_GRAPHICS_H
 #define INCLUDE_GRAPHICS_H
 
-#include <cstdint>
-
 namespace WalrusRPG{ namespace Graphics {
 
-	/*
-	 * Pixel structure
-	 * TODO?: Convert this into a class to hide value?
-	 */
-	union Pixel {
-		std::uint16_t value;
-		struct {
-			unsigned r : 5;
-			unsigned g : 6;
-			unsigned b : 5;
-		};
-
-		Pixel(std::uint16_t color) : value(color) {
-
-		}
-
-		Pixel(Pixel &pix) : value(pix.value) {
-
-		}
-
-		Pixel(std::uint8_t red, std::uint8_t green, std::uint8_t blue) : r(red>>3), g(green>>2), b(blue>>3) {
-
-		}
-
-		// Overloading (unsigned) typecast
-		operator std::uint16_t() {
-			return value;
-		}
-
-		Pixel& operator=(unsigned value) {
-			this->value = value;
-			return *this;
-		}
-	};
-
 	typedef struct Rect Rect_t;
 	struct Rect
 	{

+ 38 - 0
include/Pixel.h

@@ -0,0 +1,38 @@
+#ifndef INCLUDE_PIXEL_H
+#define INCLUDE_PIXEL_H
+
+#include <cstdint>
+
+namespace WalrusRPG { namespace Graphics {
+
+  /*
+	 * Pixel structure
+	 */
+	class Pixel {
+    union {
+  		std::uint16_t value;
+      public: // hack to be able to do pixel.r. Clever!
+  		struct {
+  			unsigned r : 5;
+  			unsigned g : 6;
+  			unsigned b : 5;
+  		};
+    };
+    
+    public:
+		Pixel(std::uint16_t color);
+
+		Pixel(Pixel &pix);
+
+		Pixel(std::uint8_t red, std::uint8_t green, std::uint8_t blue);
+
+		// Overloading (unsigned) typecast
+		operator std::uint16_t() const;
+
+		Pixel& operator=(unsigned value);
+
+	};
+
+}}
+
+#endif

+ 24 - 0
src/Pixel.cpp

@@ -0,0 +1,24 @@
+#include <Pixel.h>
+
+#define PIXEL WalrusRPG::Graphics::Pixel
+
+PIXEL::Pixel(std::uint16_t color) : value(color) {
+
+}
+
+PIXEL::Pixel(Pixel &pix) : value((std::uint8_t)pix) {
+
+}
+
+PIXEL::Pixel(std::uint8_t red, std::uint8_t green, std::uint8_t blue) : r(red>>3), g(green>>2), b(blue>>3) {
+
+}
+
+PIXEL::operator std::uint16_t() const {
+  return value;
+}
+
+PIXEL& PIXEL::operator=(unsigned value) {
+  this->value = value;
+	return *this;
+}

+ 1 - 0
src/main.cpp

@@ -1,6 +1,7 @@
 #include <os.h>
 #include "timers.h"
 #include "Graphics.h"
+#include "Pixel.h"
 #include "Map.h"
 #include "Camera.h"
 #include "misc.h"