changeset 5:5dcae4dddcd9

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 27 Oct 2019 18:29:23 +0200
parents 575fab206bc6
children 4d6fec8f0c64
files glxdragon.cpp
diffstat 1 files changed, 26 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/glxdragon.cpp	Sun Oct 27 18:16:10 2019 +0200
+++ b/glxdragon.cpp	Sun Oct 27 18:29:23 2019 +0200
@@ -52,12 +52,14 @@
 
 struct Mesh
 {
+    int nvertices, nfaces;
     std::vector<float>    vertices;
     std::vector<unsigned> faces;
 };
 
 
-bool init(const int width, const int height, const char *title)
+
+bool dmInitSDLGL(const int width, const int height, const char *title)
 {
     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
@@ -135,7 +137,7 @@
 }
 
 
-void initScene()
+void dmInitScene(void)
 {
     glEnable(GL_COLOR_MATERIAL);
 
@@ -165,7 +167,7 @@
 }
 
 
-void done()
+void dmFinish()
 {
     SDL_GL_DeleteContext(s_context);
     SDL_DestroyWindow(s_window);
@@ -174,7 +176,7 @@
 }
 
 
-void drawModelVA(const Mesh& mesh)
+void dmDrawModelVA(const Mesh &mesh)
 {
     int maxIndices;
 
@@ -183,15 +185,15 @@
     glVertexPointer(3, GL_FLOAT, 24, &mesh.vertices[0]);
     glNormalPointer(GL_FLOAT, 24, &mesh.vertices[3]);
 
-    for (size_t n = 0; n < mesh.faces.size() / 3; n += maxIndices)
+    for (int n = 0; n < mesh.nfaces; n += maxIndices)
     {
-        const int count = std::min(maxIndices, int(mesh.faces.size() / 3 - n));
+        const int count = std::min(maxIndices, int(mesh.nfaces - n));
         glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &mesh.faces[n * 3]);
     }
 }
 
 
-void paintGL(Mesh &mesh)
+void dmPaintGL(Mesh &mesh)
 {
     glClear(GL_DEPTH_BUFFER_BIT);
 
@@ -231,20 +233,17 @@
     glPopMatrix();
 
     glEnable(GL_DEPTH_TEST);
+    glEnable(GL_LIGHTING);
 
-    glEnable(GL_LIGHTING);
     // Set the color of the model
     glColor3ub(0x90, 0x80, 0x90);
 
-
-
-
     // Draw the model using vertex arrays
-    drawModelVA(mesh);
+    dmDrawModelVA(mesh);
 }
 
 
-void loadMesh(const std::string& filename, Mesh& mesh, size_t nvertices, size_t nfaces)
+void dmLoadMesh(const std::string &filename, Mesh &mesh, int nvertices, int nfaces)
 {
     std::ifstream in(filename.c_str(), std::ios::binary);
 
@@ -255,12 +254,14 @@
         throw std::runtime_error(ss.str());
     }
 
-    mesh.vertices.resize(nvertices * 6);
-    in.read(reinterpret_cast<char*>(&mesh.vertices[0]), nvertices * 6 * 4);
+    mesh.nvertices = nvertices;
+    mesh.vertices.resize(mesh.nvertices * 6);
+    in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4);
 
-    mesh.faces.resize(nfaces * 3);
+    mesh.nfaces = nfaces;
+    mesh.faces.resize(mesh.nfaces * 3);
 
-    for (size_t i = 0; i < nfaces; i++)
+    for (int i = 0; i < nfaces; i++)
     {
         in.seekg(1, std::ios::cur);
         in.read(reinterpret_cast<char*>(&mesh.faces[i * 3]), 3 * 4);
@@ -273,13 +274,13 @@
     try
     {
         struct Mesh dragonMesh;
-        loadMesh("dragon.mesh", dragonMesh, 100139, 200198);
+        dmLoadMesh("dragon.mesh", dragonMesh, 100139, 200198);
 
-        //if (!init(640, 480, "glxdragon"))
-        if (!init(1280, 960, "glxdragon"))
+        //if (!dmInitSDLGL(640, 480, "glxdragon"))
+        if (!dmInitSDLGL(1280, 960, "glxdragon"))
             throw std::runtime_error("Fatal error.");
 
-        initScene();
+        dmInitScene();
 
         bool exitFlag = false;
         int steps = 0;
@@ -307,9 +308,8 @@
                     }
             }
 
-
             // Render the next frame
-            paintGL(dragonMesh);
+            dmPaintGL(dragonMesh);
 
             // Draw the current frame
             SDL_GL_SwapWindow(s_window);
@@ -327,7 +327,7 @@
                 double time = (double(std::clock() - startTime) * 1000.0f) / CLOCKS_PER_SEC;
 
                 // Print the current frames per second
-                std::printf("%.1lf ms for %d frames = %.1lf FPS\n",
+                printf("%.1lf ms for %d frames = %.1lf FPS\n",
                     time, SET_FRAMES, (SET_FRAMES * 1000.0f) / time);
 
                 // Restart the timer
@@ -339,5 +339,6 @@
     {
         std::cerr << e.what();
     }
-    done();
+
+    dmFinish();
 }