Fix:Build:Made android compileable again
[navit-package] / navit / graphics.c
index 25d0274..5335955 100644 (file)
@@ -51,7 +51,6 @@
 #include "file.h"
 #include "event.h"
 
-static char *navit_sharedir;
 
 //##############################################################################################################
 //# Description: 
@@ -79,6 +78,7 @@ struct display_context
        struct graphics_gc *gc;
        struct graphics_image *img;
        enum projection pro;
+       int mindist;
        struct transformation *trans;
 };
 
@@ -104,6 +104,16 @@ struct displaylist {
 static void draw_circle(struct point *pnt, int diameter, int scale, int start, int len, struct point *res, int *pos, int dir);
 static void graphics_process_selection(struct graphics *gra, struct displaylist *dl);
 
+int
+graphics_set_attr(struct graphics *gra, struct attr *attr)
+{
+       int ret=1;
+       dbg(0,"enter\n");
+       if (gra->meth.set_attr)
+               ret=gra->meth.set_attr(gra->priv, attr);
+        return ret != 0;
+}
+
 void
 graphics_set_rect(struct graphics *gra, struct point_rect *pr)
 {
@@ -197,6 +207,8 @@ graphics_overlay_resize(struct graphics *this_, struct point *p, int w, int h, i
 */
 void graphics_init(struct graphics *this_)
 {
+       if (this_->gc[0])
+               return;
        this_->gc[0]=graphics_gc_new(this_);
        graphics_gc_set_background(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff});
        graphics_gc_set_foreground(this_->gc[0], &(struct color) { 0xffff, 0xefef, 0xb7b7, 0xffff });
@@ -207,7 +219,6 @@ void graphics_init(struct graphics *this_)
        graphics_gc_set_background(this_->gc[2], &(struct color) { 0xffff, 0xffff, 0xffff, 0xffff });
        graphics_gc_set_foreground(this_->gc[2], &(struct color) { 0x0000, 0x0000, 0x0000, 0xffff });
        graphics_background_gc(this_, this_->gc[0]);
-       navit_sharedir = getenv("NAVIT_SHAREDIR");
 }
 
 /**
@@ -661,7 +672,7 @@ static guint
 displayitem_hash(gconstpointer key)
 {
        const struct displayitem *di=key;
-       return (di->item.id_hi^di->item.id_lo^((int) di->item.map));
+       return (di->item.id_hi^di->item.id_lo^(GPOINTER_TO_INT(di->item.map)));
 }
 
 static gboolean
@@ -1001,21 +1012,25 @@ static int
 int_sqrt(unsigned int n)
 {
        unsigned int h, p= 0, q= 1, r= n;
-       while ( q <= n ) {
-               q <<= 2;
-                if(q == 0) { 
-                        return (int) sqrtf( (float) n ); /* use float sqrt if we reach q MAX */ 
-                } 
-        } 
-
-       while ( q != 1 ) {
+
+       /* avoid q rollover */
+       if(n >= (1<<(sizeof(n)*8-2))) {
+               q = 1<<(sizeof(n)*8-2);
+       } else {
+               while ( q <= n ) {
+                       q <<= 2;
+               }
                q >>= 2;
+       }
+
+       while ( q != 0 ) {
                h = p + q;
                p >>= 1;
                if ( r >= h ) {
                        p += q;
                        r -= h;
-               }
+               }
+               q >>= 2;
        }
        return p;
 }
@@ -1064,13 +1079,27 @@ graphics_draw_polyline_as_polygon(struct graphics *gra, struct graphics_gc *gc,
                wi=*width;
                width+=step;
                if (i < count - 1) {
+                       int dxs,dys,lscales;
+
                        dx = (pnt[i + 1].x - pnt[i].x);
                        dy = (pnt[i + 1].y - pnt[i].y);
+#if 0
                        l = int_sqrt(dx * dx * lscale * lscale + dy * dy * lscale * lscale);
+#else
+                       dxs=dx*dx;
+                       dys=dy*dy;
+                       lscales=lscale*lscale;
+                       if (dxs + dys > lscales)
+                               l = int_sqrt(dxs+dys)*lscale;
+                       else
+                               l = int_sqrt((dxs+dys)*lscales);
+#endif
                        fow=fowler(-dy, dx);
                }
                if (! l) 
                        l=1;
+               if (wi*lscale > 10000)
+                       lscale=10000/wi;
                dbg_assert(wi*lscale < 10000);
                calc_offsets(wi*lscale, l, dx, dy, &o);
                pos.x = pnt[i].x + o.ny;
@@ -1404,15 +1433,19 @@ get_font(struct graphics *gra, int size)
 char *
 graphics_icon_path(char *icon)
 {
-       dbg(0,"enter %s\n",icon);
+       static char *navit_sharedir;
+       dbg(1,"enter %s\n",icon);
        if (icon[0] == '/')
                return g_strdup(icon);
-       else
+       else {
 #ifdef HAVE_API_ANDROID
                return g_strdup_printf("res/drawable/%s", icon);
 #else
+               if (! navit_sharedir)
+                       navit_sharedir = getenv("NAVIT_SHAREDIR");
                return g_strdup_printf("%s/xpm/%s", navit_sharedir, icon);
 #endif
+       }
 }
 
 
@@ -1436,10 +1469,10 @@ displayitem_draw(struct displayitem *di, void *dummy, struct display_context *dc
                dc->gc=gc;
        }
        if (dc->e->type == element_polyline) {
-               count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, e->u.polyline.width, width);
+               count=transform(dc->trans, dc->pro, di->c, pa, di->count, dc->mindist, e->u.polyline.width, width);
        }
        else
-               count=transform(dc->trans, dc->pro, di->c, pa, di->count, 1, 0, NULL);
+               count=transform(dc->trans, dc->pro, di->c, pa, di->count, dc->mindist, 0, NULL);
        switch (e->type) {
        case element_polygon:
 #if 0
@@ -1560,6 +1593,7 @@ static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *dis
        GList *es,*types;
        GHashTable *h;
        enum item_type type;
+       int mindist=0;
 
        es=itm->elements;
        while (es) {
@@ -1570,8 +1604,14 @@ static void xdisplay_draw_elements(struct graphics *gra, struct displaylist *dis
                        type=GPOINTER_TO_INT(types->data);
                        h=g_hash_table_lookup(display_list->dl, GINT_TO_POINTER(type));
                        if (h) {
+                               if (type == type_poly_water_tiled) {
+                                       mindist=display_list->dc.mindist;
+                                       display_list->dc.mindist=0;                             
+                               }
                                g_hash_table_foreach(h, (GHFunc)displayitem_draw, &display_list->dc);
                                display_context_free(&display_list->dc);
+                               if (type == type_poly_water_tiled) 
+                                       display_list->dc.mindist=mindist;
                        }
                        types=g_list_next(types);
                }
@@ -1706,7 +1746,7 @@ static void xdisplay_draw(struct displaylist *display_list, struct graphics *gra
 extern void *route_selection;
 
 static void
-do_draw(struct displaylist *displaylist, int cancel)
+do_draw(struct displaylist *displaylist, int cancel, int flags)
 {
        struct item *item;
        int count,max=16384,workload=0;
@@ -1758,11 +1798,13 @@ do_draw(struct displaylist *displaylist, int cancel)
        profile(1,"process_selection\n");
        event_remove_idle(displaylist->idle_ev);
        displaylist->idle_ev=NULL;
+       callback_destroy(displaylist->idle_cb);
+       displaylist->idle_cb=NULL;
        displaylist->busy=0;
        graphics_process_selection(displaylist->dc.gra, displaylist);
        profile(1,"draw\n");
        if (! cancel) 
-               graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, 1);
+               graphics_displaylist_draw(displaylist->dc.gra, displaylist, displaylist->dc.trans, displaylist->layout, flags);
        map_rect_destroy(displaylist->mr);
        map_selection_destroy(displaylist->sel);
        mapset_close(displaylist->msh);
@@ -1781,11 +1823,12 @@ do_draw(struct displaylist *displaylist, int cancel)
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int callback)
+void graphics_displaylist_draw(struct graphics *gra, struct displaylist *displaylist, struct transformation *trans, struct layout *l, int flags)
 {
        int order=transform_get_order(trans);
        displaylist->dc.trans=trans;
        displaylist->dc.gra=gra;
+       displaylist->dc.mindist=transform_get_scale(trans)/2;
        // FIXME find a better place to set the background color
        if (l) {
                graphics_gc_set_background(gra->gc[0], &l->color);
@@ -1793,16 +1836,18 @@ void graphics_displaylist_draw(struct graphics *gra, struct displaylist *display
                gra->default_font = g_strdup(l->font);
        }
        graphics_background_gc(gra, gra->gc[0]);
-       gra->meth.draw_mode(gra->priv, draw_mode_begin);
-       gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &gra->r.lu, gra->r.rl.x-gra->r.lu.x, gra->r.rl.y-gra->r.lu.y);
+       gra->meth.draw_mode(gra->priv, (flags & 8)?draw_mode_begin_clear:draw_mode_begin);
+       if (!(flags & 2))
+               gra->meth.draw_rectangle(gra->priv, gra->gc[0]->priv, &gra->r.lu, gra->r.rl.x-gra->r.lu.x, gra->r.rl.y-gra->r.lu.y);
        if (l) 
                xdisplay_draw(displaylist, gra, l, order+l->order_delta);
-       if (callback)
+       if (flags & 1)
                callback_list_call_attr_0(gra->cbl, attr_postdraw);
-       gra->meth.draw_mode(gra->priv, draw_mode_end);
+       if (!(flags & 4))
+               gra->meth.draw_mode(gra->priv, draw_mode_end);
 }
 
-static void graphics_load_mapset(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb)
+static void graphics_load_mapset(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb, int flags)
 {
        int order=transform_get_order(trans);
 
@@ -1810,7 +1855,7 @@ static void graphics_load_mapset(struct graphics *gra, struct displaylist *displ
        if (displaylist->busy) {
                if (async == 1)
                        return;
-               do_draw(displaylist, 1);
+               do_draw(displaylist, 1, flags);
        }
        xdisplay_free(displaylist->dl);
        dbg(1,"order=%d\n", order);
@@ -1827,9 +1872,10 @@ static void graphics_load_mapset(struct graphics *gra, struct displaylist *displ
        displaylist->layout=l;
        if (async) {
                if (! displaylist->idle_cb)
-                       displaylist->idle_cb=callback_new_2(callback_cast(do_draw), displaylist, 0);
+                       displaylist->idle_cb=callback_new_3(callback_cast(do_draw), displaylist, 0, flags);
                displaylist->idle_ev=event_add_idle(50, displaylist->idle_cb);
-       }
+       } else
+               do_draw(displaylist, 0, flags);
 }
 /**
  * FIXME
@@ -1837,11 +1883,9 @@ static void graphics_load_mapset(struct graphics *gra, struct displaylist *displ
  * @returns <>
  * @author Martin Schaller (04/2008)
 */
-void graphics_draw(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb)
+void graphics_draw(struct graphics *gra, struct displaylist *displaylist, struct mapset *mapset, struct transformation *trans, struct layout *l, int async, struct callback *cb, int flags)
 {
-       graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb);
-       if (! async)
-               do_draw(displaylist, 0);
+       graphics_load_mapset(gra, displaylist, mapset, trans, l, async, cb, flags);
 }
 
 int
@@ -1849,7 +1893,7 @@ graphics_draw_cancel(struct graphics *gra, struct displaylist *displaylist)
 {
        if (!displaylist->busy)
                return 0;
-       do_draw(displaylist, 1);
+       do_draw(displaylist, 1, 0);
        return 1;
 }