Browse Source

make po2 stuff better

Bill Meltsner 14 years ago
parent
commit
2e9e5a1502
3 changed files with 14 additions and 21 deletions
  1. 10 0
      src/common/math.h
  2. 4 14
      src/modules/image/devil/ImageData.cpp
  3. 0 7
      src/modules/image/devil/ImageData.h

+ 10 - 0
src/common/math.h

@@ -21,6 +21,8 @@
 #ifndef LOVE_MATH_H
 #ifndef LOVE_MATH_H
 #define LOVE_MATH_H
 #define LOVE_MATH_H
 
 
+#include <climits> // for CHAR_BIT
+
 /* Definitions of useful mathematical constants
 /* Definitions of useful mathematical constants
  * M_E        - e
  * M_E        - e
  * M_LOG2E    - log2(e)
  * M_LOG2E    - log2(e)
@@ -64,6 +66,14 @@ struct vertex
 	float x, y;
 	float x, y;
 	float s, t;
 	float s, t;
 };
 };
+	
+inline int next_p2(int x)
+{
+	x += (x == 0);
+	x--;
+	for (unsigned int i = 1; i < sizeof(int)*CHAR_BIT; i <<= 1) x |= x >> i;
+	return ++x;
+}
 
 
 } // love
 } // love
 
 

+ 4 - 14
src/modules/image/devil/ImageData.cpp

@@ -21,24 +21,14 @@
 #include "ImageData.h"
 #include "ImageData.h"
 
 
 // STD
 // STD
+#include <cstring>
 #include <iostream>
 #include <iostream>
 
 
 // LOVE
 // LOVE
 #include <common/Exception.h>
 #include <common/Exception.h>
+#include <common/math.h>
 #include <filesystem/File.h>
 #include <filesystem/File.h>
 
 
-int toPo2(int n)
-{
-	n -= 1;
-	n |= n >> 1;
-	n |= n >> 2;
-	n |= n >> 4;
-	n |= n >> 8;
-	n |= n >> 16;
-	n |= n >> 32;
-	return n+1;
-}
-
 namespace love
 namespace love
 {
 {
 namespace image
 namespace image
@@ -54,8 +44,8 @@ namespace devil
 		ilBindImage(image);
 		ilBindImage(image);
 
 
 		//scale to nearest bigger po2
 		//scale to nearest bigger po2
-		width = toPo2(width);
-		height = toPo2(height);
+		width  = next_p2(width);
+		height = next_p2(height);
 
 
 		//create and populate the image
 		//create and populate the image
 		bool success = (ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data) == IL_TRUE);
 		bool success = (ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data) == IL_TRUE);

+ 0 - 7
src/modules/image/devil/ImageData.h

@@ -23,18 +23,11 @@
 
 
 // LOVE
 // LOVE
 #include <filesystem/File.h>
 #include <filesystem/File.h>
-#include <image/Image.h>
 #include <image/ImageData.h>
 #include <image/ImageData.h>
 
 
 // DevIL
 // DevIL
 #include <IL/il.h>
 #include <IL/il.h>
 
 
-// String
-#include <string.h>
-
-// Math
-#include <math.h>
-
 namespace love
 namespace love
 {
 {
 namespace image
 namespace image