added vectors_to_glyphs filter
[swftools.git] / lib / devices / opengl.c
index c8efc05..fd1b745 100644 (file)
@@ -47,7 +47,7 @@ static void dbg(char*format, ...)
     if(!verbose)
        return;
     va_start(arglist, format);
-    vsprintf(buf, format, arglist);
+    vsnprintf(buf, sizeof(buf)-1, format, arglist);
     va_end(arglist);
     l = strlen(buf);
     while(l && buf[l-1]=='\n') {
@@ -77,7 +77,7 @@ void CALLBACK beginCallback(GLenum which)
 }
 void CALLBACK endCallback(void)
 {
-   glEnd();
+    glEnd();
 }
 void CALLBACK vertexCallback(GLvoid *vertex)
 {
@@ -101,14 +101,18 @@ void CALLBACK vertexCallbackTex(GLvoid *vertex)
 }
 void CALLBACK combineCallbackTex(GLdouble coords[3], GLdouble *data[4], GLfloat w[4], GLdouble **out)
 {
-   GLdouble *vertex, *texCoord;
+   GLdouble *vertex;
    vertex = (GLdouble *) malloc(5 * sizeof(GLdouble));
    vertex[0] = coords[0];
    vertex[1] = coords[1];
    vertex[2] = coords[2];
-   texCoord = &vertex[3];
-   vertex[3] = w[0]*data[0][3] + w[1]*data[1][3] + w[2]*data[2][3] + w[3]*data[3][3];
-   vertex[4] = w[0]*data[0][4] + w[1]*data[1][4] + w[2]*data[2][4] + w[3]*data[3][4];
+   if(data[2] && data[3]) {
+       vertex[3] = w[0]*data[0][3] + w[1]*data[1][3] + w[2]*data[2][3] + w[3]*data[3][3];
+       vertex[4] = w[0]*data[0][4] + w[1]*data[1][4] + w[2]*data[2][4] + w[3]*data[3][4];
+   } else {
+       vertex[3] = w[0]*data[0][3] + w[1]*data[1][3];
+       vertex[4] = w[0]*data[0][4] + w[1]*data[1][4];
+   }
    *out = vertex;
 }
 
@@ -262,9 +266,9 @@ void tesselatePolygon(GLUtesselator*tesselator, double z, gfxline_t*line)
 void opengl_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
 {
     double z;
-    dbg("fill");
+    dbg("fill %02x%02x%02x%02x", color->a, color->r, color->g, color->b);
     internal_t*i = (internal_t*)dev->internal;
-  
+
     glDisable(GL_TEXTURE_2D);
     glColor4f(color->r/255.0, color->g/255.0, color->b/255.0, color->a/255.0);
     
@@ -290,12 +294,13 @@ typedef struct _imgopengl
     gfxhash_t hash;
     GLuint texID;
     int width, height;
+    unsigned char*data;
     struct _imgopengl*next;
 } imgopengl_t;
 
-imgopengl_t*img2texid = 0;
+static imgopengl_t*img2texid = 0;
 
-gfxhash_t gfximage_hash(gfximage_t*img)
+static gfxhash_t gfximage_hash(gfximage_t*img)
 {
     int t;
     int size = img->width*img->height*4;
@@ -305,7 +310,22 @@ gfxhash_t gfximage_hash(gfximage_t*img)
     return hash;
 }
 
-imgopengl_t*addTexture(gfximage_t*img)
+static void delTextures()
+{
+    imgopengl_t*i = img2texid;
+    while(i) {
+        imgopengl_t*next = i->next;
+        if(i->data) {
+            glDeleteTextures(1, &i->texID);
+            free(i->data);
+        }
+        memset(i, 0, sizeof(imgopengl_t));
+        free(i);
+        i = next;
+    }
+}
+
+static imgopengl_t*addTexture(gfximage_t*img)
 {
     gfxhash_t hash = gfximage_hash(img);
     imgopengl_t*i = img2texid;
@@ -339,6 +359,7 @@ imgopengl_t*addTexture(gfximage_t*img)
     i->height = newheight;
 
     unsigned char*data = malloc(newwidth*newheight*4);
+    i->data = data;
     int x,y;
     for(y=0;y<img->height;y++) {
        for(x=0;x<img->width;x++) {
@@ -380,7 +401,7 @@ void opengl_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gf
     int len = 0;
     double*xyz=0;
     gfxline_t*l=0;
-    glColor4f(1.0,0,0,1.0);
+    glColor4f(1.0,0,0.7,1.0);
     
     i->currentz ++;
   
@@ -515,7 +536,7 @@ void opengl_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*co
 
 
 
-void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
+void opengl_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
 {
     dbg("link");
 }
@@ -525,12 +546,12 @@ void opengl_endpage(struct _gfxdevice*dev)
     dbg("endpage");
 }
 
-int opengl_result_save(struct _gfxresult*gfx, char*filename)
+int opengl_result_save(struct _gfxresult*gfx, const char*filename)
 {
     dbg("result:save");
     return 0;
 }
-void* opengl_result_get(struct _gfxresult*gfx, char*name)
+void* opengl_result_get(struct _gfxresult*gfx, const char*name)
 {
     dbg("result:get");
     return 0;
@@ -539,6 +560,7 @@ void opengl_result_destroy(struct _gfxresult*gfx)
 {
     dbg("result:destroy");
     free(gfx);
+    delTextures();
 }
 
 gfxresult_t*opengl_finish(struct _gfxdevice*dev)