changeset 259:be2ca95af493

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 10 Oct 2012 01:02:23 +0300
parents ff542421ed33
children 0aa8647c3dfc
files dmq3d.c dmq3d.h
diffstat 2 files changed, 81 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/dmq3d.c	Tue Oct 09 23:45:05 2012 +0300
+++ b/dmq3d.c	Wed Oct 10 01:02:23 2012 +0300
@@ -45,7 +45,10 @@
     { \
         (*nalloc) += 16; \
         if ((*items = dmRealloc(*items, *nalloc * sizeof(TYPE))) == NULL) \
+        { \
+            dmError("Error allocating memory for " # TYPE ".\n"); \
             return DMERR_MALLOC; \
+        } \
     } \
     memcpy((*items) + (*nitems), item, sizeof(TYPE)); \
     if (res != NULL) *res = *nitems; \
@@ -54,34 +57,34 @@
 }
 
 DM_FIND_ITEM_FUNC(Vertex, DMVector)
-DM_FIND_ITEM_FUNC(Line, DM3DLine)
+DM_FIND_ITEM_FUNC(Vector, DM3DVector)
 DM_FIND_ITEM_FUNC(Sprite, DM3DSprite)
 
 DM_ADD_ITEM_FUNC(Vertex, DMVector)
-DM_ADD_ITEM_FUNC(Line, DM3DLine)
+DM_ADD_ITEM_FUNC(Vector, DM3DVector)
 DM_ADD_ITEM_FUNC(Sprite, DM3DSprite)
 DM_ADD_ITEM_FUNC(Bitmap, DM3DBitmap)
 
 
-int dmAdd3DLineSpriteModelVertex(DM3DLineSpriteModel *model, DMVector *v, int *index)
+int dmAdd3DVectorSpriteModelVertex(DM3DVectorSpriteModel *model, DMVector *v, int *index)
 {
     return dmAdd3DVertex(&model->nvertices, &model->nvertexalloc,
          &model->vertices, v, index);
 }
 
-int dmAdd3DLineSpriteModelLine(DM3DLineSpriteModel *model, DM3DLine *v, int *index)
+int dmAdd3DVectorSpriteModelVector(DM3DVectorSpriteModel *model, DM3DVector *v, int *index)
 {
-    return dmAdd3DLine(&model->nlines, &model->nlinesalloc,
+    return dmAdd3DVector(&model->nlines, &model->nlinesalloc,
          &model->lines, v, index);
 }
 
-int dmAdd3DLineSpriteModelSprite(DM3DLineSpriteModel *model, DM3DSprite *v, int *index)
+int dmAdd3DVectorSpriteModelSprite(DM3DVectorSpriteModel *model, DM3DSprite *v, int *index)
 {
     return dmAdd3DSprite(&model->nsprites, &model->nspritesalloc,
          &model->sprites, v, index);
 }
 
-int dmAdd3DLineSpriteModelBitmap(DM3DLineSpriteModel *model, DM3DBitmap *v, int *index)
+int dmAdd3DVectorSpriteModelBitmap(DM3DVectorSpriteModel *model, DM3DBitmap *v, int *index)
 {
     return dmAdd3DBitmap(&model->nbitmaps, &model->nbitmapsalloc,
          &model->bitmaps, v, index);
@@ -100,13 +103,13 @@
 }
 
 
-void dmDraw3DLineSpriteModel(SDL_Surface *screen, DM3DLineSpriteModel *model, DMVector *pos, DMMatrix *mat, SDL_Surface *fbmap, Uint32 lcol)
+void dmDraw3DVectorSpriteModel(SDL_Surface *screen, DM3DVectorSpriteModel *model, DMVector *pos, DMMatrix *mat, SDL_Surface *fbmap, Uint32 lcol)
 {
     int i;
     int cx = screen->w / 2, cy = screen->h / 2;
     for (i = 0; i < model->nlines; i++)
     {
-        DM3DLine *line = &model->lines[i];
+        DM3DVector *line = &model->lines[i];
         DMVector pv[2];
         dm_vector_copy(&pv[0], &model->vertices[line->v1]);
         dm_vector_copy(&pv[1], &model->vertices[line->v2]);
@@ -149,14 +152,22 @@
 }
 
 
-static BOOL dmReadCoordinate(char **line, float *value, BOOL next)
+static BOOL dmReadCoordinate(const char *orig, char **line, float *value, BOOL next)
 {
     *line = dmSkipWhitespace(*line, FALSE);
-    if (sscanf(*line, "%f", value) != 1) return FALSE;
+    if (sscanf(*line, "%f", value) != 1)
+    {
+        dmError("Expected floating point value @ %d:\n%s\n", (*line - orig), orig);
+        return FALSE;
+    }
     if (next)
     {
         *line = dmSkipUntil(*line, ',');
-        if (**line != ',') return FALSE;
+        if (**line != ',')
+        {
+            dmError("Expected comma @ %d:\n%s\n", (*line - orig), orig);
+            return FALSE;
+        }
         *(*line)++;
     }
     else
@@ -165,34 +176,37 @@
     return TRUE;
 }
 
-static BOOL dmReadLineSegments(char *line, DM3DLineSpriteModel *model, BOOL relative, const DMVector *pt)
+
+static BOOL dmReadVectorSegments(char *line, DM3DVectorSpriteModel *model, BOOL relative, const DMVector *pt)
 {
     int nvertices, vertex, type;
     int *indices = NULL;
-    if (sscanf(line+1, "%d", &nvertices) != 1)
+    char *ptr = line;
+    
+    if (sscanf(ptr+1, "%d", &nvertices) != 1)
     {
-        dmError("No # of segments @ '%s'\n", line);
+        dmError("No # of segments @ '%s'\n", ptr);
         goto error;
     }
     
     if ((indices = dmMalloc(sizeof(int) * (nvertices+1))) == NULL)
         goto error;
     
-    line = dmSkipWhitespace(line, TRUE);
+    ptr = dmSkipWhitespace(ptr, TRUE);
     DMVector v, p, *t;
     dm_vector_copy(&v, pt);
     for (vertex = 0; vertex <= nvertices; vertex++)
     {
-        if (*line == 'Z')
+        if (*ptr == 'Z')
         {
             indices[vertex] = indices[0];
-            line++;
+            ptr++;
         }
         else
         {
-            if (!dmReadCoordinate(&line, &p.x, TRUE)) return FALSE;
-            if (!dmReadCoordinate(&line, &p.y, TRUE)) return FALSE;
-            if (!dmReadCoordinate(&line, &p.z, FALSE)) return FALSE;
+            if (!dmReadCoordinate(line, &ptr, &p.x, TRUE)) return FALSE;
+            if (!dmReadCoordinate(line, &ptr, &p.y, TRUE)) return FALSE;
+            if (!dmReadCoordinate(line, &ptr, &p.z, FALSE)) return FALSE;
             if (relative)
             {
                 dm_vector_add(&v, &p);
@@ -204,26 +218,26 @@
                 t = &v;
             }
 
-            if (dmAdd3DLineSpriteModelVertex(model, t, &indices[vertex]) != DMERR_OK)
+            if (dmAdd3DVectorSpriteModelVertex(model, t, &indices[vertex]) != DMERR_OK)
                 goto error;
         }
 
-        line = dmSkipWhitespace(line, FALSE);
+        ptr = dmSkipWhitespace(ptr, FALSE);
     }
     
-    if (sscanf(line, "%d", &type) != 1)
+    if (sscanf(ptr, "%d", &type) != 1)
     {
-        dmError("No line type @ '%s'\n", line);
+        dmError("No line type @ '%s'\n", ptr);
         goto error;
     }
     
     for (vertex = 1; vertex <= nvertices; vertex++)
     {
-        DM3DLine line;
-        line.v1 = indices[vertex - 1];
-        line.v2 = indices[vertex];
-        line.type = type;
-        if (dmAdd3DLineSpriteModelLine(model, &line, NULL) != DMERR_OK)
+        DM3DVector vec;
+        vec.v1 = indices[vertex - 1];
+        vec.v2 = indices[vertex];
+        vec.type = type;
+        if (dmAdd3DVectorSpriteModelVector(model, &vec, NULL) != DMERR_OK)
             goto error;
     }
 
@@ -233,43 +247,46 @@
     return FALSE;
 }
 
-static BOOL dmReadSprite(char *line, DM3DLineSpriteModel *model, DMVector *pos)
+    char *ptr = line;
+
+static BOOL dmReadSprite(char *line, DM3DVectorSpriteModel *model, DMVector *pos)
 {
     DMVector pt;
     DM3DSprite spr;
 
-    line++;
-    if (!dmReadCoordinate(&line, &pt.x, TRUE)) return FALSE;
-    if (!dmReadCoordinate(&line, &pt.y, TRUE)) return FALSE;
-    if (!dmReadCoordinate(&line, &pt.z, FALSE)) return FALSE;
-    line = dmSkipWhitespace(line, FALSE);
-    if (*line != 'B')
+    ptr++;
+    if (!dmReadCoordinate(line, &ptr, &pt.x, TRUE)) return FALSE;
+    if (!dmReadCoordinate(line, &ptr, &pt.y, TRUE)) return FALSE;
+    if (!dmReadCoordinate(line, &ptr, &pt.z, FALSE)) return FALSE;
+    ptr = dmSkipWhitespace(ptr, FALSE);
+    if (*ptr != 'B')
     {
         dmError("No bitmap definition found for sprite.\n");
         return FALSE;
     }
 
-    spr.bitmap = atoi(line + 1);
+    spr.bitmap = atoi(ptr + 1);
 
     dm_vector_add(&pt, pos);
-    if (dmAdd3DLineSpriteModelVertex(model, &pt, &spr.v) != DMERR_OK)
+    if (dmAdd3DVectorSpriteModelVertex(model, &pt, &spr.v) != DMERR_OK)
         return FALSE;
 
-    if (dmAdd3DLineSpriteModelSprite(model, &spr, NULL) != DMERR_OK)
+    if (dmAdd3DVectorSpriteModelSprite(model, &spr, NULL) != DMERR_OK)
         return FALSE;
 
     return TRUE;
 }
+    char *ptr = line;
 
-static BOOL dmReadBitmap(char *line, DM3DLineSpriteModel *model)
+static BOOL dmReadBitmap(char *line, DM3DVectorSpriteModel *model)
 {
     DM3DBitmap bmp, *rbmp;
     int index;
 
-    strncpy(bmp.name, line + 1, sizeof(bmp.name));
+    strncpy(bmp.name, ptr + 1, sizeof(bmp.name));
     bmp.img = NULL;
 
-    if (dmAdd3DLineSpriteModelBitmap(model, &bmp, &index) != DMERR_OK)
+    if (dmAdd3DVectorSpriteModelBitmap(model, &bmp, &index) != DMERR_OK)
         return FALSE;
 
     rbmp = &(model->bitmaps[index]);
@@ -294,7 +311,7 @@
 }
 
 
-static int dmDoRead3DLineSpriteModel(DMResource *f, DM3DLineSpriteModel *model, DMVector *pos)
+static int dmDoRead3DVectorSpriteModel(DMResource *f, DM3DVectorSpriteModel *model, DMVector *pos)
 {
     char line[512];
 
@@ -312,11 +329,11 @@
                 int res;
                 DMVector pt;
                 start++;
-                if (!dmReadCoordinate(&start, &pt.x, TRUE)) return DMERR_INVALID_DATA;
-                if (!dmReadCoordinate(&start, &pt.y, TRUE)) return DMERR_INVALID_DATA;
-                if (!dmReadCoordinate(&start, &pt.z, FALSE)) return DMERR_INVALID_DATA;
+                if (!dmReadCoordinate(line, &start, &pt.x, TRUE)) return DMERR_INVALID_DATA;
+                if (!dmReadCoordinate(line, &start, &pt.y, TRUE)) return DMERR_INVALID_DATA;
+                if (!dmReadCoordinate(line, &start, &pt.z, FALSE)) return DMERR_INVALID_DATA;
                 dm_vector_add_r(&pt, pos, &pt);
-                if ((res = dmDoRead3DLineSpriteModel(f, model, &pt)) != DMERR_OK)
+                if ((res = dmDoRead3DVectorSpriteModel(f, model, &pt)) != DMERR_OK)
                     return res;
                 }
                 break;
@@ -330,12 +347,12 @@
                 break;
 
             case 'L':
-                if (!dmReadLineSegments(start, model, FALSE, pos))
+                if (!dmReadVectorSegments(start, model, FALSE, pos))
                     return DMERR_INVALID_DATA;
                 break;
 
             case 'R':
-                if (!dmReadLineSegments(start, model, TRUE, pos))
+                if (!dmReadVectorSegments(start, model, TRUE, pos))
                     return DMERR_INVALID_DATA;
                 break;
             
@@ -352,20 +369,20 @@
 }
 
 
-int dmRead3DLineSpriteModel(DMResource *f, DM3DLineSpriteModel **model)
+int dmRead3DVectorSpriteModel(DMResource *f, DM3DVectorSpriteModel **model)
 {
     DMVector pos;
 
-    if ((*model = dmMalloc0(sizeof(DM3DLineSpriteModel))) == NULL)
+    if ((*model = dmMalloc0(sizeof(DM3DVectorSpriteModel))) == NULL)
         return DMERR_MALLOC;
 
     memset(&pos, 0, sizeof(pos));
     
-    return dmDoRead3DLineSpriteModel(f, *model, &pos);
+    return dmDoRead3DVectorSpriteModel(f, *model, &pos);
 }
 
 
-void dmFree3DLineSpriteModel(DM3DLineSpriteModel *model)
+void dmFree3DVectorSpriteModel(DM3DVectorSpriteModel *model)
 {
     int i;
     for (i = 0; i < model->nbitmaps; i++)
--- a/dmq3d.h	Tue Oct 09 23:45:05 2012 +0300
+++ b/dmq3d.h	Wed Oct 10 01:02:23 2012 +0300
@@ -19,7 +19,7 @@
 typedef struct
 {
     int v1, v2, type;
-} DM3DLine;
+} DM3DVector;
 
 
 typedef struct
@@ -41,25 +41,25 @@
     DMVector *vertices;
 
     int nlines, nlinesalloc;
-    DM3DLine *lines;
+    DM3DVector *lines;
 
     int nbitmaps, nbitmapsalloc;
     DM3DBitmap *bitmaps;
     
     int nsprites, nspritesalloc;
     DM3DSprite *sprites;
-} DM3DLineSpriteModel;
+} DM3DVectorSpriteModel;
 
 
-int dmAdd3DLineSpriteModelVertex(DM3DLineSpriteModel *model, DMVector *v, int *index);
-int dmAdd3DLineSpriteModelLine(DM3DLineSpriteModel *model, DM3DLine *v, int *index);
-int dmAdd3DLineSpriteModelSprite(DM3DLineSpriteModel *model, DM3DSprite *v, int *index);
-int dmAdd3DLineSpriteModelBitmap(DM3DLineSpriteModel *model, DM3DBitmap *v, int *index);
+int dmAdd3DVectorSpriteModelVertex(DM3DVectorSpriteModel *model, DMVector *v, int *index);
+int dmAdd3DVectorSpriteModelVector(DM3DVectorSpriteModel *model, DM3DVector *v, int *index);
+int dmAdd3DVectorSpriteModelSprite(DM3DVectorSpriteModel *model, DM3DSprite *v, int *index);
+int dmAdd3DVectorSpriteModelBitmap(DM3DVectorSpriteModel *model, DM3DBitmap *v, int *index);
 
-int dmRead3DLineSpriteModel(DMResource *f, DM3DLineSpriteModel **model);
-void dmFree3DLineSpriteModel(DM3DLineSpriteModel *model);
+int dmRead3DVectorSpriteModel(DMResource *f, DM3DVectorSpriteModel **model);
+void dmFree3DVectorSpriteModel(DM3DVectorSpriteModel *model);
 
-void dmDraw3DLineSpriteModel(SDL_Surface *screen, DM3DLineSpriteModel *model, DMVector *pos, DMMatrix *mat, SDL_Surface *fbmap, Uint32 lcol);
+void dmDraw3DVectorSpriteModel(SDL_Surface *screen, DM3DVectorSpriteModel *model, DMVector *pos, DMMatrix *mat, SDL_Surface *fbmap, Uint32 lcol);
 
 
 #ifdef __cplusplus