changeset 47:9909014498f0

Add helper functions dmError() and dmMsg() and use them.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Dec 2019 23:52:45 +0200
parents 0c75c5f5c6b6
children 0ae1ff609626
files dmmodel.cpp dmutil.cpp dmutil.h gldragon.cpp
diffstat 4 files changed, 75 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/dmmodel.cpp	Thu Dec 05 22:45:21 2019 +0200
+++ b/dmmodel.cpp	Thu Dec 05 23:52:45 2019 +0200
@@ -10,9 +10,9 @@
 #include <SDL_endian.h>
 
 
-static bool dmError(DMTextFileInfo &info, const std::string &msg)
+static bool dmTextError(DMTextFileInfo &info, const std::string &msg)
 {
-    printf("ERROR: %s on line #%d: %s\n",
+    dmError("%s on line #%d: %s\n",
         msg.c_str(), info.nline, info.line.c_str());
     return false;
 }
@@ -20,7 +20,7 @@
 
 static bool dmSyntaxError(DMTextFileInfo &info, const std::string &msg)
 {
-    printf("ERROR: Syntax error on line #%d: %s\n",
+    dmError("Syntax error on line #%d: %s\n",
         info.nline, msg.c_str());
     return false;
 }
@@ -97,7 +97,7 @@
             break;
 
         default:
-            return dmError(info,
+            return dmTextError(info,
                 "Internal error, unimplemented PLY property type");
     }
 
@@ -146,7 +146,7 @@
     // Read one line
     if (!dmReadLine(info))
     {
-        return dmError(info,
+        return dmTextError(info,
             "Unexpected end of file");
     }
 
@@ -225,7 +225,7 @@
             break;
 
         default:
-            return dmError(info,
+            return dmTextError(info,
                 "Internal error, unimplemented PLY property type");
     }
 
@@ -301,12 +301,12 @@
     info.nline = info.state = 0;
     info.file.open(info.filename.c_str(), std::fstream::in | std::fstream::binary);
 
-    printf("INFO: Trying to read mesh from '%s'.\n",
+    dmMsg("Trying to read mesh from '%s'.\n",
         info.filename.c_str());
 
     if (!info.file.is_open())
     {
-        printf("ERROR: Unable to open file '%s'.\n",
+        dmError("Unable to open file '%s'.\n",
             info.filename.c_str());
         return false;
     }
@@ -358,7 +358,7 @@
             if (info.format == PLY_FMT_UNKNOWN ||
                 tokens[2] != "1.0")
             {
-                printf("ERROR: Unknown or unsupported PLY file format '%s'.\n",
+                dmError("Unknown or unsupported PLY file format '%s'.\n",
                     (tokens[1] +" "+ tokens[2]).c_str());
                 return false;
             }
@@ -480,7 +480,7 @@
         (prop = elem->checkProp("z")) == 0 || prop->type != PLY_TYPE_FLOAT
         )
     {
-        printf("ERROR: PLY file did not contain expected information.\n");
+        dmError("PLY file did not contain expected information.\n");
         return false;
     }
 
@@ -490,12 +490,12 @@
 
     if (nvertices < 3 || nfaces < 1)
     {
-        printf("ERROR: Invalid nvertices (%d) and/or nfaces (%d).\n",
+        dmError("Invalid nvertices (%d) and/or nfaces (%d).\n",
             nvertices, nfaces);
         return false;
     }
 
-    printf("INFO: Should have %d vertices, %d faces\n",
+    dmMsg("Should have %d vertices, %d faces\n",
         nvertices, nfaces);
 
     // Pre-allocate space
@@ -560,7 +560,7 @@
         }
     }
 
-    printf("INFO: Found %ld vertices, %ld normals, %ld faces\n",
+    dmMsg("Found %ld vertices, %ld normals, %ld faces\n",
         vertices.size(),
         normals.size(),
         faces.size() / 3);
@@ -629,12 +629,12 @@
     info.nline = info.state = 0;
     info.file.open(info.filename.c_str(), std::fstream::in | std::fstream::binary);
 
-    printf("INFO: Trying to read scene data from '%s'.\n",
+    dmMsg("Trying to read scene data from '%s'.\n",
         info.filename.c_str());
 
     if (!info.file.is_open())
     {
-        printf("ERROR: Unable to open file '%s'.\n",
+        dmError("Unable to open file '%s'.\n",
             info.filename.c_str());
         return false;
     }
@@ -755,7 +755,7 @@
 
             if (lights.size() >= 4)
             {
-                return dmError(info,
+                return dmTextError(info,
                 "Too many lights defined (max 4)");
             }
 
--- a/dmutil.cpp	Thu Dec 05 22:45:21 2019 +0200
+++ b/dmutil.cpp	Thu Dec 05 23:52:45 2019 +0200
@@ -10,6 +10,38 @@
 #include <fstream>
 
 
+void dmMsg_V(const char *fmt, va_list ap)
+{
+    fprintf(stdout, "INFO: ");
+    vfprintf(stdout, fmt, ap);
+}
+
+
+void dmMsg(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    dmMsg_V(fmt, ap);
+    va_end(ap);
+}
+
+
+void dmError_V(const char *fmt, va_list ap)
+{
+    fprintf(stdout, "ERROR: ");
+    vfprintf(stdout, fmt, ap);
+}
+
+
+void dmError(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    dmError_V(fmt, ap);
+    va_end(ap);
+}
+
+
 std::string dmStrLTrim(const std::string& str, const std::string& delim)
 {
     return str.substr(str.find_first_not_of(delim));
@@ -91,7 +123,7 @@
 
     if (!in.is_open())
     {
-        printf("ERROR: Unable to open file '%s'.\n",
+        dmError("Unable to open file '%s'.\n",
             filename.c_str());
         return false;
     }
@@ -100,7 +132,7 @@
 
     if (in.tellg() > maxSize)
     {
-        printf("ERROR: File '%s' is too large.\n",
+        dmError("File '%s' is too large.\n",
             filename.c_str());
         return false;
     }
--- a/dmutil.h	Thu Dec 05 22:45:21 2019 +0200
+++ b/dmutil.h	Thu Dec 05 23:52:45 2019 +0200
@@ -13,9 +13,16 @@
 #include <vector>
 #include <cstdio>
 #include <iostream>
+#include <cstdarg>
 
 
-#define DMUTIL_WHITESPACE           "\t\n\v\f\r "
+#define DMUTIL_WHITESPACE "\t\n\v\f\r "
+
+
+void dmMsg_V(const char *fmt, va_list ap);
+void dmMsg(const char *fmt, ...);
+void dmError_V(const char *fmt, va_list ap);
+void dmError(const char *fmt, ...);
 
 
 std::string dmStrLTrim(const std::string& str, const std::string& delim = DMUTIL_WHITESPACE);
--- a/gldragon.cpp	Thu Dec 05 22:45:21 2019 +0200
+++ b/gldragon.cpp	Thu Dec 05 23:52:45 2019 +0200
@@ -54,7 +54,7 @@
     bool res = SDL_GL_ExtensionSupported(name.c_str());
     void *ptr = SDL_GL_GetProcAddress(name.c_str());
 
-    printf(" - Checking '%s' : %s : %p\n",
+    dmMsg(" - Checking '%s' : %s : %p\n",
         name.c_str(),
         res ? "YES" : "no",
         ptr);
@@ -110,7 +110,7 @@
     // Attempt to initialize libSDL
     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) != 0)
     {
-        printf("ERROR: Unable to initialize SDL: %s\n",
+        dmError("Unable to initialize SDL: %s\n",
             SDL_GetError());
         return false;
     }
@@ -135,14 +135,14 @@
             width, height,
             SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL)
     {
-        printf("ERROR: Could not create SDL window: %s",
+        dmError("Could not create SDL window: %s",
             SDL_GetError());
         return false;
     }
 
     if ((dmGLContext = SDL_GL_CreateContext(dmWindow)) == NULL)
     {
-        printf("ERROR: Unable to create SDL OpenGL context: %s\n",
+        dmError("Unable to create SDL OpenGL context: %s\n",
             SDL_GetError());
         return false;
     }
@@ -178,7 +178,7 @@
 
     if (ret != 0)
     {
-        printf("ERROR: Could not set vsync mode to %s.\n",
+        dmError("Could not set vsync mode to %s.\n",
             msg.c_str());
         return false;
     }
@@ -188,15 +188,10 @@
         return false;
 
     // Dump some information
-    printf(
-        "GL_VENDOR   : %s\n"
-        "GL_RENDERER : %s\n"
-        "GL_VERSION  : %s\n"
-        "VSync mode  : %s\n",
-        glGetString(GL_VENDOR),
-        glGetString(GL_RENDERER),
-        glGetString(GL_VERSION),
-        msg.c_str());
+    dmMsg("GL_VENDOR   : %s\n", glGetString(GL_VENDOR));
+    dmMsg("GL_RENDERER : %s\n", glGetString(GL_RENDERER));
+    dmMsg("GL_VERSION  : %s\n", glGetString(GL_VERSION));
+    dmMsg("VSync mode  : %s\n", msg.c_str());
 
     // Setup the window and view port
     glViewport(0, 0, width, height);
@@ -360,13 +355,13 @@
         {
             char *buf = new char[bufLen];
             glGetShaderInfoLog(shader, bufLen, NULL, buf);
-            printf("ERROR: Shader compliation error:\n%s\n",
+            dmError("Shader compliation error:\n%s\n",
                 buf);
             delete buf;
         }
         else
         {
-            printf("ERROR: Shader compilation error occured, but no error information got.\n");
+            dmError("Shader compilation error occured, but no error information got.\n");
         }
         return false;
     }
@@ -453,7 +448,7 @@
         {
             if (optSetInputFilename)
             {
-                printf("ERROR: Please specify only one scene file.\n");
+                dmError("Please specify only one scene file.\n");
                 goto exit;
             }
 
@@ -461,7 +456,7 @@
             optInputFilename = std::string(arg);
             if (optInputFilename.empty())
             {
-                printf("ERROR: Invalid input filename.\n");
+                dmError("Invalid input filename.\n");
                 goto exit;
             }
 
@@ -491,7 +486,7 @@
 
     if (optWidth < 100 || optWidth > 8192 || optHeight < 100 || optHeight > 8192)
     {
-        printf("ERROR: Invalid window width or height (%d x %d).\n",
+        dmError("Invalid window width or height (%d x %d).\n",
             optWidth, optHeight);
         goto exit;
     }
@@ -502,7 +497,7 @@
 
     if (scene.models.size() == 0)
     {
-        printf("ERROR: Scenefile '%s' contains no models.\n",
+        dmError("Scenefile '%s' contains no models.\n",
             optInputFilename.c_str());
         goto exit;
     }
@@ -514,11 +509,11 @@
         scene.lights.push_back(light);
     }
 
-    printf("INFO: Loading %ld model(s) ..\n",
+    dmMsg("Loading %ld model(s) ..\n",
         scene.models.size());
 
     basePath = dmGetPath(optInputFilename);
-    printf("INFO: Model base path '%s'\n", basePath.c_str());
+    dmMsg("Scene base path '%s'\n", basePath.c_str());
 
     for (DMModel &model : scene.models)
     {