]> Pileus Git - grits/blobdiff - src/objects/grits-tile.c
Fix a variety of memory leaks
[grits] / src / objects / grits-tile.c
index 649d0f42970936e225e7c6b6ab2715f4f6e09bd7..6ea70f88cbf052e38073b8087e3ea300c7f19c79 100644 (file)
  * representing the OpenGL texture to use when drawing the tile.
  */
 
+#define GL_GLEXT_PROTOTYPES
 #include <config.h>
 #include <math.h>
+#include <string.h>
 #include "gtkgl.h"
 #include "grits-tile.h"
 
-guint  grits_tile_mask = 0;
+static guint  grits_tile_mask = 0;
 
 gchar *grits_tile_path_table[2][2] = {
        {"00.", "01."},
@@ -64,10 +66,8 @@ GritsTile *grits_tile_new(GritsTile *parent,
        tile->atime  = time(NULL);
        grits_bounds_set_bounds(&tile->coords, 0, 1, 1, 0);
        grits_bounds_set_bounds(&tile->edge, n, s, e, w);
-       if (parent) {
+       if (parent)
                tile->proj   = parent->proj;
-               tile->zindex = parent->zindex+1;
-       }
        return tile;
 }
 
@@ -217,7 +217,8 @@ void grits_tile_update(GritsTile *tile, GritsPoint *eye,
         * resolution for this part? */
        gint xs = G_N_ELEMENTS(tile->children);
        gint ys = G_N_ELEMENTS(tile->children[0]);
-       if (_grits_tile_precise(eye, &tile->edge, res, width/xs, height/ys)) {
+       if (tile->parent && _grits_tile_precise(eye, &tile->edge,
+                               res, width/xs, height/ys)) {
                GRITS_OBJECT(tile)->hidden = TRUE;
                return;
        }
@@ -311,9 +312,9 @@ GritsTile *grits_tile_gc(GritsTile *root, time_t atime,
                if (root->children[x][y])
                        has_children = TRUE;
        }
-       //g_debug("GritsTile: gc - %p->atime=%u < atime=%u",
-       //              root, (guint)root->atime, (guint)atime);
-       if (!has_children && root->atime < atime &&
+       //g_debug("GritsTile: gc - %p kids=%d time=%d data=%d load=%d",
+       //      root, !!has_children, root->atime < atime, !!root->data, !!root->load);
+       if (root->parent && !has_children && root->atime < atime &&
                        (root->data || !root->load)) {
                //g_debug("GritsTile: gc/free - %p", root);
                if (root->data)
@@ -348,19 +349,22 @@ void grits_tile_free(GritsTile *root, GritsTileFreeFunc free_func, gpointer user
 /* Load texture mask so we can draw a texture to just a part of a triangle */
 static guint _grits_tile_load_mask(void)
 {
-       guint  tex;
-       guint8 byte = 0xff;
+       guint tex;
+       const int width = 256, height = 256;
+       guint8 *bytes = g_malloc(width*height);
+       memset(bytes, 0xff, width*height);
        glGenTextures(1, &tex);
        glBindTexture(GL_TEXTURE_2D, tex);
 
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1, 1, 0,
-                       GL_ALPHA, GL_UNSIGNED_BYTE, &byte);
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0,
+                       GL_ALPHA, GL_UNSIGNED_BYTE, bytes);
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+       g_free(bytes);
        return tex;
 }
 
@@ -455,31 +459,21 @@ static gboolean grits_tile_draw_rec(GritsTile *tile, GritsOpenGL *opengl)
                return FALSE;
 
        GritsTile *child = NULL;
-       gboolean   done  = FALSE;
-       while (!done) {
-               /* Only draw children if possible */
-               gboolean draw_parent = FALSE;
-               grits_tile_foreach(tile, child)
-                       if (!child || !child->data || GRITS_OBJECT(child)->hidden)
-                               draw_parent = TRUE;
-
-               /* Draw parent tile underneath */
-               if (draw_parent) {
-                       GList *triangles = roam_sphere_get_intersect(opengl->sphere, FALSE,
-                                       tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w);
-                       grits_tile_draw_one(tile, opengl, triangles);
-                       g_list_free(triangles);
-               }
-
-               /* Draw child tiles */
-               gboolean drew_all_children = TRUE;
-               grits_tile_foreach(tile, child)
-                       if (!grits_tile_draw_rec(child, opengl))
-                               drew_all_children = FALSE;
 
-               /* Check if tiles were hidden by a thread while drawing */
-               done = draw_parent || drew_all_children;
+       /* Draw child tiles */
+       gboolean draw_parent = FALSE;
+       grits_tile_foreach(tile, child)
+               if (!grits_tile_draw_rec(child, opengl))
+                       draw_parent = TRUE;
+
+       /* Draw parent tile underneath using depth test */
+       if (draw_parent) {
+               GList *triangles = roam_sphere_get_intersect(opengl->sphere, FALSE,
+                               tile->edge.n, tile->edge.s, tile->edge.e, tile->edge.w);
+               grits_tile_draw_one(tile, opengl, triangles);
+               g_list_free(triangles);
        }
+
        return TRUE;
 }