ecere/src/com; gfx; gui: Temporary patches for alignment issues
[sdk] / ecere / src / gfx / 3D / Object.ec
index 461c803..3ab47ae 100644 (file)
@@ -6,14 +6,14 @@ public enum FrustumPlacement { outside, inside, intersecting };
 
 public class ObjectFormat
 {
-   class_data char * extension;
-   class_property char * extension
+   class_data const char * extension;
+   class_property const char * extension
    {
       set { class_data(extension) = value; }
       get { return class_data(extension); }
    }
 
-   virtual bool ::Load(Object object, char * fileName, DisplaySystem displaySystem);
+   virtual bool ::Load(Object object, const char * fileName, DisplaySystem displaySystem);
 };
 
 // TODO: Review these:
@@ -500,7 +500,7 @@ public:
             mesh.SetMinMaxRadius();
          min = mesh.min;
          max = mesh.max;
-         volume = true;
+         volume = (max.x >= min.x && max.y >= min.y && max.z >= min.z);
       }
       else
       {
@@ -532,13 +532,13 @@ public:
                Vector3Df point;
                point.MultMatrix(points[c], child.localMatrix);
 
-               if(point.x < this.min.x) this.min.x = point.x;
-               if(point.y < this.min.y) this.min.y = point.y;
-               if(point.z < this.min.z) this.min.z = point.z;
+               if(point.x < min.x) min.x = point.x;
+               if(point.y < min.y) min.y = point.y;
+               if(point.z < min.z) min.z = point.z;
 
-               if(point.x > this.max.x) this.max.x = point.x;
-               if(point.y > this.max.y) this.max.y = point.y;
-               if(point.z > this.max.z) this.max.z = point.z;
+               if(point.x > max.x) max.x = point.x;
+               if(point.y > max.y) max.y = point.y;
+               if(point.z > max.z) max.z = point.z;
             }
             volume = true;
          }
@@ -603,7 +603,7 @@ public:
       {
          Object modelChild;
 
-         name = model.name;
+         name = CopyString(model.name);
          flags = model.flags;
          flags.ownMesh = false;
          mesh = model.mesh;
@@ -633,6 +633,11 @@ public:
          {
             children.Remove(child);
             child.Free(displaySystem);
+
+            // We did not do this before so as to keep transform on reloading for new DisplaySystem
+            // However since children are removed, it seems that purpose has been gone, we'd need
+            // a new mechanism to handle lost resources reloading...
+            delete child;
          }
          if(flags.ownMesh && mesh)
          {
@@ -649,7 +654,7 @@ public:
       }
    }
 
-   bool Load(char * fileName, char * type, DisplaySystem displaySystem)
+   bool Load(const char * fileName, const char * type, DisplaySystem displaySystem)
    {
       char ext[MAX_EXTENSION];
       subclass(ObjectFormat) format;
@@ -657,10 +662,7 @@ public:
       bool result = false;
 
       if(!type && fileName)
-      {
-         type = GetExtension(fileName, ext);
-         strlwr(type);
-      }
+         type = strlwr(GetExtension(fileName, ext));
 
       for(link = class(ObjectFormat).derivatives.first; link; link = link.next)
       {
@@ -691,7 +693,7 @@ public:
       }
    }
 
-   Object Find(char * name)
+   Object Find(const char * name)
    {
       if(this && name)
       {
@@ -745,9 +747,9 @@ public:
       return null;
    }
 
-   bool AddName(Object object, char * name)
+   bool AddName(Object object, const char * name)
    {
-      bool result;
+      bool result = false;
       if(this)
       {
          char * newName = CopyString(name);
@@ -765,7 +767,7 @@ public:
    {
       bool result = false;
 
-      if(!children.first)
+      if(!children.first && this.flags.ownMesh)
          result = true;
       else
       {
@@ -852,7 +854,7 @@ public:
             while(true)
             {
                int nIndices = 0;
-               PrimitiveGroupType type;
+               PrimitiveGroupType type = (PrimitiveGroupType)-1;
                Material material = null;
                bool foundGroup = false;
 
@@ -916,8 +918,12 @@ public:
                            if(newGroup.material == group.material && newGroup.type == group.type)
                            {
                               int c;
-                              for(c = 0; c<group.nIndices; c++)
-                                 newGroup.indices[nIndices++] = group.indices[c] + (uint16)vertexOffset;
+                              if(group.type.indices32bit)
+                                 for(c = 0; c<group.nIndices; c++)
+                                    newGroup.indices32[nIndices++] = group.indices32[c] + vertexOffset;
+                              else
+                                 for(c = 0; c<group.nIndices; c++)
+                                    newGroup.indices[nIndices++] = (uint16)(group.indices[c] + vertexOffset);
                            }
                         }
                         vertexOffset += objectMesh.nVertices;
@@ -933,8 +939,12 @@ public:
                                  newGroup.type == group.type)
                               {
                                  int c;
-                                 for(c = 0; c<group.nIndices; c++)
-                                    newGroup.indices[nIndices++] = group.indices[c] + (uint16)vertexOffset;
+                                 if(group.type.indices32bit)
+                                    for(c = 0; c<group.nIndices; c++)
+                                       newGroup.indices32[nIndices++] = group.indices32[c] + vertexOffset;
+                                 else
+                                    for(c = 0; c<group.nIndices; c++)
+                                       newGroup.indices[nIndices++] = (uint16)(group.indices[c] + vertexOffset);
                               }
                            }
                            vertexOffset += child.mesh.nVertices;
@@ -1021,8 +1031,12 @@ public:
                   objectMesh.primitives[c].data = null;
                   */
 
-                  for(i = 0; i<triangle->nIndices; i++)
-                     triangle->indices[i] += (uint16)vertexOffset;
+                  if(triangle->type.indices32bit)
+                     for(i = 0; i<triangle->nIndices; i++)
+                        triangle->indices32[i] += vertexOffset;
+                  else
+                     for(i = 0; i<triangle->nIndices; i++)
+                        triangle->indices[i] += (uint16)vertexOffset;
                   mesh.UnlockPrimitive(triangle);
                }
                vertexOffset += objectMesh.nVertices;
@@ -1048,8 +1062,12 @@ public:
                      child.mesh.primitives[c].data = null;
                      */
 
-                     for(i = 0; i<triangle->nIndices; i++)
-                        triangle->indices[i] += (uint16)vertexOffset;
+                     if(triangle->type.indices32bit)
+                        for(i = 0; i<triangle->nIndices; i++)
+                           triangle->indices[i] += (uint16)vertexOffset;
+                     else
+                        for(i = 0; i<triangle->nIndices; i++)
+                           triangle->indices32[i] += vertexOffset;
                      mesh.UnlockPrimitive(triangle);
                   }
                   vertexOffset += child.mesh.nVertices;
@@ -1246,7 +1264,7 @@ public:
    property Camera camera { get { return camera; } }; // Fix this with inheritance? camera inherit from Object?
    property Object firstChild { get { return children.first; } };
    property Object next { get { return next; } };
-   property char * name { get { return name; } };
+   property const char * name { get { return name; } };
    property Matrix matrix { get { value = matrix; } };
    property Object cameraTarget { set { cameraTarget = value; } get { return cameraTarget; } };
    property OldList * tracks { /* set { tracks = value; } */ get { return &tracks; } };
@@ -1255,7 +1273,7 @@ public:
 private:
    Object()
    {
-      children.offset = (uint)&((Object)0).prev;
+      children.offset = (uint)(uintptr)&((Object)0).prev;
       transform.scaling = { 1, 1, 1 };
       transform.orientation = { 1,0,0,0 };
       flags.transform = true;
@@ -1289,7 +1307,7 @@ private:
 
          if(flags.camera && cameraTarget)
          {
-            // DeterMine angle to look at target
+            // Determine angle to look at target
             Vector3D position, direction;
             if(flags.root || !parent)
                position = transform.position;
@@ -1495,7 +1513,8 @@ private:
    unsigned startFrame, endFrame;
    int frame;
    Vector3Df pivot;
-   Transform transform;
+
+   public Transform transform;
    Matrix matrix;
    Matrix localMatrix;
    void * tag;