ecere/gfx: Moved CubeMap to Bitmap.ec
[sdk] / ecere / src / gfx / Bitmap.ec
index fc0973e..c1d3725 100644 (file)
@@ -4,27 +4,27 @@ import "Display"
 
 public class BitmapFormat
 {
-   class_data char ** extensions;
+   class_data const char ** extensions;
 
-   class_property char ** extensions
+   class_property const char ** extensions
    {
       get { return class_data(extensions); }
       set { class_data(extensions) = value; }   }
 
 
    virtual bool ::Load(Bitmap bitmap, File f);
-   virtual bool ::Save(Bitmap bitmap, char * fileName, void * options);
-   virtual ColorAlpha * ::LoadPalette(char * fileName, char * type);
+   virtual bool ::Save(Bitmap bitmap, const char * fileName, void * options);
+   virtual ColorAlpha * ::LoadPalette(const char * fileName, const char * type);
 };
 
-static char * typesToTry[] =
+static const char * typesToTry[] =
 {
    "gif", "jpg", "png", "bmp", "pcx", "memorybmp"
 };
 
 #define NUM_TYPES_TO_TRY   ((int)(sizeof(typesToTry) / sizeof(char *)))
 
-static subclass(BitmapFormat) FindFormat(char * type)
+static subclass(BitmapFormat) FindFormat(const char * type)
 {
    subclass(BitmapFormat) format = null;
    if(type)
@@ -32,7 +32,7 @@ static subclass(BitmapFormat) FindFormat(char * type)
       OldLink link;
       for(link = class(BitmapFormat).derivatives.first; link; link = link.next)
       {
-         char ** extensions;
+         const char ** extensions;
          format = link.data;
          extensions = format.extensions;
          if(extensions)
@@ -50,19 +50,16 @@ static subclass(BitmapFormat) FindFormat(char * type)
    return format;
 }
 
-public ColorAlpha * LoadPalette(char * fileName, char * type)
+public ColorAlpha * LoadPalette(const char * fileName, const char * type)
 {
    char ext[MAX_EXTENSION];
-   subclass(BitmapFormat) format;
+   subclass(BitmapFormat) format = null;
    ColorAlpha * palette = null;
    int typeToTry = -1;
    Bitmap bitmap { };
 
-   if(!type) 
-   {
-      type = GetExtension(fileName, ext);
-      strlwr(type);
-   }
+   if(!type)
+      type = strlwr(GetExtension(fileName, ext));
 
    if(type)
       format = FindFormat(type);
@@ -83,7 +80,7 @@ public ColorAlpha * LoadPalette(char * fileName, char * type)
       }
       if(!palette)
       {
-         
+
          if(bitmap.Load(fileName, type, null))
          {
             palette = bitmap.Quantize(0, 255);
@@ -267,6 +264,7 @@ public:
    subclass(DisplayDriver) driver;
    void * driverData;
    bool keepData;
+   bool mipMaps;
 
 public:
 
@@ -284,7 +282,7 @@ public:
 
    Surface GetSurface(int x, int y, Box clip)
    {
-      Surface result;
+      Surface result = null;
       Surface surface { };
       if(surface)
       {
@@ -296,6 +294,7 @@ public:
          surface.driver = (driver != null) ? driver : ((subclass(DisplayDriver))class(LFBDisplayDriver));
          surface.displaySystem = displaySystem;
          surface.display = null; // For now... with render to textures, the texture should have a display...
+         //surface.alphaWrite = write;
 
          if(surface.driver.GetBitmapSurface(displaySystem, surface, this, x, y, box))
          {
@@ -320,7 +319,7 @@ public:
          int acrossY = height / size;
 
          for(cy = 0; cy < acrossY; cy++)
-            for(cx = 0; cx < acrossX; cx++)         
+            for(cx = 0; cx < acrossX; cx++)
             {
                int x,y;
                Color in1, in2, res;
@@ -394,7 +393,7 @@ public:
    {
       if(driver)
          return driver.ConvertBitmap(displaySystem, this, format, palette);
-      return false;
+      return pixelFormat == format;
    }
 
    bool Copy(Bitmap source)
@@ -440,7 +439,7 @@ public:
       bool result = false;
       if(this && displaySystem && (!driver || driver == class(LFBDisplayDriver)))
       {
-         if(displaySystem.driver.MakeDDBitmap(displaySystem, this, false))
+         if(displaySystem.driver.MakeDDBitmap(displaySystem, this, false, 0))
          {
             this.displaySystem = displaySystem;
             driver = displaySystem ? displaySystem.driver : ((subclass(DisplayDriver))class(LFBDisplayDriver));
@@ -455,8 +454,9 @@ public:
       bool result = false;
       if(this && displaySystem && (!driver || driver == class(LFBDisplayDriver)))
       {
-         if(displaySystem.driver.MakeDDBitmap(displaySystem, this, true))
+         if(displaySystem.driver.MakeDDBitmap(displaySystem, this, true, 0))
          {
+            this.mipMaps = true;
             this.displaySystem = displaySystem;
             result = true;
          }
@@ -465,13 +465,14 @@ public:
    }
 
    // --- Bitmap loading ---
-   bool LoadFromFile(File file, char * type, DisplaySystem displaySystem)
+   bool LoadFromFile(File file, const char * type, DisplaySystem displaySystem)
    {
       bool result = false;
       if(file)
       {
          subclass(BitmapFormat) format = null;
          int typeToTry = -1;
+         uintsize pos = file.Tell();
 
          if(type)
             format = FindFormat(type);
@@ -481,7 +482,7 @@ public:
 
          for(; typeToTry < NUM_TYPES_TO_TRY; typeToTry++)
          {
-            file.Seek(0, start);
+            file.Seek(pos, start);
             if(typeToTry >= 0)
                format = FindFormat(typesToTry[typeToTry]);
 
@@ -509,22 +510,20 @@ public:
       return result;
    }
 
-   bool Load(char * fileName, char * type, DisplaySystem displaySystem)
+   bool Load(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       bool result = false;
       char ext[MAX_EXTENSION];
-      subclass(BitmapFormat) format;
+      subclass(BitmapFormat) format = null;
       int typeToTry = -1;
+      const char * guessedType = type;
 
       if(!fileName) return false;
-      if(!type) 
-      {
-         type = GetExtension(fileName, ext);
-         strlwr(type);
-      }
+      if(!guessedType)
+         guessedType = strlwr(GetExtension(fileName, ext));
 
-      if(type)
-         format = FindFormat(type);
+      if(guessedType)
+         format = FindFormat(guessedType);
       if(!format)
          typeToTry = 0;
 
@@ -552,9 +551,13 @@ public:
                   break;
                }
                delete f;
-            }         
+            }
+         }
+         if(typeToTry == -1)
+         {
+            if(type) break;
+            typeToTry = 0;
          }
-         if(typeToTry == -1) break;
       }
 
       if(!result)
@@ -562,7 +565,7 @@ public:
       return result;
    }
 
-   bool LoadT(char * fileName, char * type, DisplaySystem displaySystem)
+   bool LoadT(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       bool result = Load(fileName, type, null);
       if(result)
@@ -580,7 +583,7 @@ public:
 
    #define TRESHOLD  384
 
-   bool LoadGrayed(char * fileName, char * type, DisplaySystem displaySystem)
+   bool LoadGrayed(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       bool result = Load(fileName, type, null);
       if(result)
@@ -592,7 +595,7 @@ public:
             Bitmap grayed { };
 
             grayed.Allocate(null, width, height, 0, pixelFormat888, false);
-         
+
             for(y = 0;  y <height - 1; y++)
             {
                for(x = 0; x < width - 1; x++)
@@ -602,23 +605,23 @@ public:
                   if(/*b.a > 128 && */(b.r + b.g + b.b) < TRESHOLD)
                   {
                      // TODO: Precomp syntax error here without brackets
-                     ((ColorAlpha *)grayed.picture)[(y + 1) * grayed.stride + (x + 1)] = 
+                     ((ColorAlpha *)grayed.picture)[(y + 1) * grayed.stride + (x + 1)] =
                         ColorAlpha { b.a, white };
                   }
                }
             }
-            
+
             for(c = 0; c<size; c++)
             {
                ColorRGBA b = ((ColorRGBA *)picture)[c];
                //if(b.a > 128)
                {
-                  ((ColorAlpha *)grayed.picture)[c] = 
-                     (/*b.a > 128 && */b.r + b.g + b.b < TRESHOLD) ? 
+                  ((ColorAlpha *)grayed.picture)[c] =
+                     (/*b.a > 128 && */b.r + b.g + b.b < TRESHOLD) ?
                         ColorAlpha { b.a, { 128, 128, 128 } } : ColorAlpha { b.a, { 212, 208, 200 } };
                }
             }
-            
+
             Free();
 
             pixelFormat = grayed.pixelFormat;
@@ -650,8 +653,8 @@ public:
             Bitmap grayed { };
 
             grayed.Allocate(null, width, height, 0, pixelFormat888, false);
-         
-            
+
+
             for(y = 0;  y <height - 1; y++)
             {
                for(x = 0; x < width - 1; x++)
@@ -661,24 +664,24 @@ public:
                   if(b && (palette[b].color.r + palette[b].color.g + palette[b].color.b) < TRESHOLD)
                   {
                      // TODO: Precomp syntax error here without brackets
-                     ((ColorAlpha *)grayed.picture)[(y + 1) * grayed.stride + (x + 1)] = 
+                     ((ColorAlpha *)grayed.picture)[(y + 1) * grayed.stride + (x + 1)] =
                         ColorAlpha { 255, white };
                   }
                }
             }
-            
+
             for(c = 0; c<size; c++)
             {
                byte b = picture[c];
                if(b)
                {
-                  ((ColorAlpha *)grayed.picture)[c] = 
+                  ((ColorAlpha *)grayed.picture)[c] =
                      //(bitmap.palette[b].color) ? Color { 212, 208, 200 } : Color { 128,128,128 };
-                     (palette[b].color.r + palette[b].color.g + palette[b].color.b < TRESHOLD) ? 
+                     (palette[b].color.r + palette[b].color.g + palette[b].color.b < TRESHOLD) ?
                         ColorAlpha { 255, { 128, 128, 128 } } : ColorAlpha { 255, { 212, 208, 200 } };
                }
             }
-            
+
             Free();
 
             pixelFormat = grayed.pixelFormat;
@@ -706,7 +709,7 @@ public:
       return result;
    }
 
-   bool LoadMonochrome(char * fileName, char * type, DisplaySystem displaySystem)
+   bool LoadMonochrome(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       bool result = Load(fileName, type, null);
       if(result)
@@ -717,9 +720,9 @@ public:
          {
             int x, y;
             Bitmap grayed { };
-            
+
             grayed.Allocate(null, width, height, 0, pixelFormat888, false);
-         
+
             for(y = 0; y<height - 1; y++)
             {
                for(x = 0; x<width - 1; x++)
@@ -734,6 +737,8 @@ public:
             Free();
 
             pixelFormat = grayed.pixelFormat;
+            size = grayed.size;
+            sizeBytes = grayed.sizeBytes;
             stride = grayed.stride;
             picture = grayed.picture;
             grayed.picture = null;
@@ -757,7 +762,7 @@ public:
       return result;
    }
 
-   bool LoadMipMaps(char * fileName, char * type, DisplaySystem displaySystem)
+   bool LoadMipMaps(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       bool result = Load(fileName, type, null);
       if(result)
@@ -769,7 +774,7 @@ public:
       return result;
    }
 
-   bool LoadTMipMaps(char * fileName, char * type, DisplaySystem displaySystem)
+   bool LoadTMipMaps(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       bool result = Load(fileName, type, null);
       if(result)
@@ -782,16 +787,13 @@ public:
       return result;
    }
 
-   bool Save(char * fileName, char * type, void * options)
+   bool Save(const char * fileName, const char * type, void * options)
    {
       char ext[MAX_EXTENSION];
-      subclass(BitmapFormat) format;
+      subclass(BitmapFormat) format = null;
 
-      if(!type) 
-      {
-         type = GetExtension(fileName, ext);
-         strlwr(type);
-      }
+      if(!type)
+         type = strlwr(GetExtension(fileName, ext));
 
       if(type)
          format = FindFormat(type);
@@ -832,7 +834,7 @@ public:
       return result;
    }
 
-   bool Allocate(char * driverName, int width, int height, int stride, PixelFormat format, bool allocatePalette)
+   bool Allocate(const char * driverName, int width, int height, int stride, PixelFormat format, bool allocatePalette)
    {
       bool result = false;
       subclass(DisplayDriver) displayDriver = driverName ? GetDisplayDriver(driverName) : ((subclass(DisplayDriver))class(LFBDisplayDriver));
@@ -853,6 +855,8 @@ public:
       if(this && driver)
       {
          driver.FreeBitmap(displaySystem, this);
+         driverData = null;
+         driver = class(LFBDisplayDriver);
       }
       if(this && keepData)
          delete picture;
@@ -904,7 +908,7 @@ public:
 
          c = 0;
          mainNode.AddNodeToColorTable(palette + start, &c);
-         
+
          {
             Bitmap newBitmap { };
             if(newBitmap.Allocate(null, width, height, 0, pixelFormat8, false))
@@ -912,12 +916,12 @@ public:
                int y, x;
                ColorAlpha * picture = (ColorAlpha *)this.picture;
                byte * newPicture = newBitmap.picture;
-            
+
                for(y = 0; y < height; y++)
                   for(x = 0; x < width; x++, picture++, newPicture++)
                   {
                      byte color;
-                     
+
                      if(transparent && start > 0 && !picture->a)
                         color = 0;
                      else
@@ -930,6 +934,7 @@ public:
                this.palette = palette;
                allocatePalette = true;
                pixelFormat = pixelFormat8;
+               sizeBytes = stride * height;
 
                newBitmap.picture = null;
             }
@@ -940,3 +945,30 @@ public:
       return palette;
    }
 };
+
+public class CubeMap : Bitmap
+{
+public:
+   void Load(DisplaySystem displaySystem, const String * names, const String extension, bool oldStyle)
+   {
+      int i;
+      for(i = 0; i < 6; i++)
+      {
+         char location[MAX_LOCATION];
+         Bitmap face = i > 0 ? { } : this;
+         strcpy(location, names[i]);
+         if(extension)
+            ChangeExtension(location, extension, location);
+         if(face.Load(location, null, null))
+         {
+            face.driverData = driverData;
+            displaySystem.driver.MakeDDBitmap(displaySystem, face, true, (i + 1) | (oldStyle << 3));
+         }
+         if(i > 0)
+         {
+            face.driverData = 0;
+            delete face;
+         }
+      }
+   }
+};