Browse Source

Screenshot command and other small improvements

Thomas Buck 9 years ago
parent
commit
16dbdf29cd
14 changed files with 79 additions and 102 deletions
  1. 1
    0
      ChangeLog.md
  2. 2
    1
      include/Game.h
  3. 1
    1
      include/Render.h
  4. 2
    2
      include/TombRaider.h
  5. 1
    0
      include/config.h.in
  6. 0
    8
      include/utils/strings.h
  7. 34
    29
      src/Command.cpp
  8. 9
    20
      src/Game.cpp
  9. 3
    4
      src/Menu.cpp
  10. 10
    14
      src/Render.cpp
  11. 6
    6
      src/TextureManager.cpp
  12. 2
    2
      src/TombRaider.cpp
  13. 2
    0
      src/config.cpp.in
  14. 6
    15
      src/utils/strings.cpp

+ 1
- 0
ChangeLog.md View File

@@ -14,6 +14,7 @@
14 14
     * Added utf8-cpp dependency to allow Console to delete multi-byte chars
15 15
     * Added stream-style printing to Console
16 16
     * Using Console like Stream everywhere, removed old print method
17
+    * Added Screenshot command back
17 18
 
18 19
     [ 20140809 ]
19 20
     * Script Unit Test brings it’s own scripts to test

+ 2
- 1
include/Game.h View File

@@ -8,6 +8,7 @@
8 8
 #ifndef _GAME_H_
9 9
 #define _GAME_H_
10 10
 
11
+#include <string>
11 12
 #include <vector>
12 13
 
13 14
 #include "Entity.h"
@@ -50,7 +51,7 @@ private:
50 51
     void processModels();
51 52
     void processRooms();
52 53
 
53
-    char *mName;
54
+    std::string levelName;
54 55
     bool mLoaded;
55 56
 
56 57
     TombRaider mTombRaider;

+ 1
- 1
include/Render.h View File

@@ -53,7 +53,7 @@ public:
53 53
      * \brief Makes a screenshot, writes to disk
54 54
      * \param filenameBase basename of file to be written
55 55
      */
56
-    void screenShot(char *filenameBase);
56
+    void screenShot(const char *filenameBase);
57 57
 
58 58
     /*!
59 59
      * \brief Gets current rendering mode

+ 2
- 2
include/TombRaider.h View File

@@ -134,7 +134,7 @@ public:
134 134
      * \returns 0 on success, < 0 on error
135 135
      * \sa TombRaider::loadTR5()
136 136
      */
137
-    int Load(char *filename);
137
+    int Load(const char *filename);
138 138
 
139 139
     /*!
140 140
      * \brief Makes a clamped 0.0 to 1.0 texel from coord pair
@@ -586,7 +586,7 @@ public:
586 586
      * \param filename pak to load
587 587
      * \returns < 0 on error
588 588
      */
589
-    int loadSFX(char *filename);
589
+    int loadSFX(const char *filename);
590 590
 
591 591
     void reset();
592 592
 

+ 1
- 0
include/config.h.in View File

@@ -9,6 +9,7 @@
9 9
 #define _CONFIG_H_
10 10
 
11 11
 extern const char *VERSION;
12
+extern const char *VERSION_SHORT;
12 13
 extern const char *BUILD_HOST;
13 14
 
14 15
 #cmakedefine USING_AL

+ 0
- 8
include/utils/strings.h View File

@@ -28,14 +28,6 @@ bool stringEndsWith(const char *str, const char *suffix);
28 28
 /*!
29 29
  * \brief Generates a buffered string for the printf call
30 30
  * \param string format string like for printf
31
- * \param args arguments matching format string
32
- * \returns string in a buffer
33
- */
34
-char *bufferString(const char *string, va_list args) __attribute__((format(printf, 1, 0)));
35
-
36
-/*!
37
- * \brief Generates a buffered string for the printf call
38
- * \param string format string like for printf
39 31
  * \returns string in a buffer
40 32
  */
41 33
 char *bufferString(const char *string, ...) __attribute__((format(printf, 1, 2)));

+ 34
- 29
src/Command.cpp View File

@@ -97,8 +97,8 @@ int OpenRaider::command(std::string c) {
97 97
             getConsole() << "  bind      - bind a keyboard/mouse action" << Console::endl;
98 98
             getConsole() << "  animate   - [BOOL|n|p] - Animate models" << Console::endl;
99 99
             getConsole() << "  move      - [walk|fly|noclip]" << Console::endl;
100
-/*
101 100
             getConsole() << "  sshot     - make a screenshot" << Console::endl;
101
+/*
102 102
             getConsole() << "  sound     - INT - Test play sound" << Console::endl;
103 103
             getConsole() << "  mode      - MODE - Render mode" << Console::endl;
104 104
             getConsole() << "  light     - BOOL - GL Lights" << Console::endl;
@@ -184,7 +184,35 @@ int OpenRaider::command(std::string c) {
184 184
             return -9;
185 185
         }
186 186
         getConsole() << temp.c_str()  << "ing" << Console::endl;
187
-
187
+    } else if (cmd.compare("sshot") == 0) {
188
+        if (!mRunning) {
189
+            getConsole() << "Use sshot command interactively!" << Console::endl;
190
+            return -999;
191
+        }
192
+        std::string filename(mBaseDir);
193
+        filename += "/sshots/";
194
+        filename += VERSION_SHORT;
195
+        bool console = false, menu = false;
196
+        std::string temp;
197
+        command >> temp;
198
+        if (temp.compare("console") == 0)
199
+            console = true;
200
+        if (temp.compare("menu") == 0)
201
+            menu = true;
202
+        if (!console) {
203
+            getConsole().setVisible(false);
204
+            if (menu)
205
+                getMenu().setVisible(true);
206
+            frame();
207
+            frame(); // Double buffered
208
+        }
209
+        getRender().screenShot(filename.c_str());
210
+        if (!console) {
211
+            getConsole().setVisible(true);
212
+            if (menu)
213
+                getMenu().setVisible(false);
214
+        }
215
+        getConsole() << "Screenshot stored..." << Console::endl;
188 216
 /*
189 217
     } else if (cmd.compare("mode") == 0) {
190 218
         std::string mode;
@@ -308,29 +336,6 @@ int OpenRaider::command(std::string c) {
308 336
             getConsole().print("Invalid use of ponytail-command!");
309 337
             return -45;
310 338
         }
311
-    } else if (cmd.compare("sshot") == 0) {
312
-        if (!mRunning) {
313
-            getConsole().print("Use sshot command interactively!");
314
-            return -999;
315
-        }
316
-        char *filename = bufferString("%s/sshots/%s", mBaseDir, VERSION);
317
-        bool console = (args->size() > 0) && (strcmp(args->at(0), "console") == 0);
318
-        bool menu = (args->size() > 0) && (strcmp(args->at(0), "menu") == 0);
319
-        if (!console) {
320
-            getConsole().setVisible(false);
321
-            if (menu)
322
-                getMenu().setVisible(true);
323
-            frame();
324
-            frame(); // Double buffered
325
-        }
326
-        getRender().screenShot(filename);
327
-        if (!console) {
328
-            getConsole().setVisible(true);
329
-            if (menu)
330
-                getMenu().setVisible(false);
331
-        }
332
-        getConsole().print("Screenshot stored...");
333
-        delete filename;
334 339
     } else if (cmd.compare("sound") == 0) {
335 340
         if ((!mRunning) || (!getGame().isLoaded())) {
336 341
             getConsole().print("Use sound command interactively!");
@@ -439,11 +444,11 @@ int OpenRaider::help(std::string &cmd) {
439 444
     } else if (cmd.compare("load") == 0) {
440 445
         getConsole() << "load-Command Usage:" << Console::endl;
441 446
         getConsole() << "  load /path/to/level" << Console::endl;
442
-/*
443 447
     } else if (cmd.compare("sshot") == 0) {
444
-        getConsole().print("sshot-Command Usage:");
445
-        getConsole().print("  sshot [console|menu]");
446
-        getConsole().print("Add console/menu to capture them too");
448
+        getConsole() << "sshot-Command Usage:" << Console::endl;
449
+        getConsole() << "  sshot [console|menu]" << Console::endl;
450
+        getConsole() << "Add console/menu to capture them too" << Console::endl;
451
+/*
447 452
     } else if (cmd.compare("sound") == 0) {
448 453
         getConsole().print("sound-Command Usage:");
449 454
         getConsole().print("  sound INT");

+ 9
- 20
src/Game.cpp View File

@@ -30,7 +30,6 @@ std::map<int, int> gMapTex2Bump;
30 30
 
31 31
 Game::Game() {
32 32
     mLoaded = false;
33
-    mName = NULL;
34 33
     mLara = -1;
35 34
     mTextureStart = 0;
36 35
     mTextureOffset = 0;
@@ -58,9 +57,6 @@ int Game::initialize() {
58 57
 }
59 58
 
60 59
 void Game::destroy() {
61
-    delete [] mName;
62
-    mName = NULL;
63
-
64 60
     mLoaded = false;
65 61
     mLara = -1;
66 62
     getRender().setMode(Render::modeDisabled);
@@ -78,30 +74,22 @@ int Game::loadLevel(const char *level) {
78 74
     if (mLoaded)
79 75
         destroy();
80 76
 
81
-    mName = bufferString("%s", level);
77
+    levelName = level;
82 78
 
83
-    getConsole() << "Loading " << mName << Console::endl;
84
-    int error = mTombRaider.Load(mName);
79
+    getConsole() << "Loading " << levelName << Console::endl;
80
+    int error = mTombRaider.Load(levelName.c_str());
85 81
     if (error != 0)
86 82
         return error;
87 83
 
88 84
     // If required, load the external sound effect file MAIN.SFX into TombRaider
89 85
     if ((mTombRaider.getEngine() == TR_VERSION_2) || (mTombRaider.getEngine() == TR_VERSION_3)) {
90
-        char *tmp = bufferString("%sMAIN.SFX", level); // Ensure theres enough space
91
-        size_t length = strlen(tmp);
92
-        size_t dir = 0;
93
-        for (long i = length - 1; i >= 0; i--) {
94
-            if ((tmp[i] == '/') || (tmp[i] == '\\')) {
95
-                dir = i + 1; // Find where the filename (bla.tr2) starts
96
-                break;
97
-            }
98
-        }
99
-        strcpy(tmp + dir, "MAIN.SFX"); // overwrite the name itself with MAIN.SFX
100
-        tmp[dir + 8] = '\0';
101
-        error = mTombRaider.loadSFX(tmp);
86
+        std::string tmp(levelName);
87
+        size_t pos = tmp.rfind('/');
88
+        tmp.erase(pos + 1);
89
+        tmp += "MAIN.SFX";
90
+        error = mTombRaider.loadSFX(tmp.c_str());
102 91
         if (error != 0)
103 92
             getConsole() << "Could not load " << tmp << Console::endl;
104
-        delete [] tmp;
105 93
     }
106 94
 
107 95
     // Process data
@@ -115,6 +103,7 @@ int Game::loadLevel(const char *level) {
115 103
     mTombRaider.reset();
116 104
 
117 105
     if (mLara == -1) {
106
+        //! \todo Cutscene support
118 107
         getConsole() << "Can't find Lara entity in level pak!" << Console::endl;
119 108
         destroy();
120 109
         return -1;

+ 3
- 4
src/Menu.cpp View File

@@ -120,14 +120,13 @@ void Menu::play() {
120 120
             //! \todo Display something if an error occurs
121 121
         }
122 122
     } else {
123
-        char *tmp = bufferString("load %s",
124
-                mapFolder->getFile((unsigned long)mCursor - 1 - mapFolder->folderCount()).getPath().c_str());
125
-        if (getOpenRaider().command(tmp) == 0) {
123
+        std::string tmp = "load ";
124
+        tmp += mapFolder->getFile((unsigned long)mCursor - 1 - mapFolder->folderCount()).getPath();
125
+        if (getOpenRaider().command(tmp.c_str()) == 0) {
126 126
             setVisible(false);
127 127
         } else {
128 128
             //! \todo Display something if an error occurs
129 129
         }
130
-        delete [] tmp;
131 130
     }
132 131
 }
133 132
 

+ 10
- 14
src/Render.cpp View File

@@ -7,6 +7,7 @@
7 7
  */
8 8
 
9 9
 #include <algorithm>
10
+#include <sstream>
10 11
 
11 12
 #include <stdlib.h>
12 13
 #include <math.h>
@@ -41,35 +42,30 @@ void Render::ClearWorld() {
41 42
 }
42 43
 
43 44
 
44
-void Render::screenShot(char *filenameBase)
45
+void Render::screenShot(const char *filenameBase)
45 46
 {
46 47
     int sz = getWindow().getWidth() * getWindow().getHeight();
47 48
     unsigned char *image = new unsigned char[sz * 3];
48
-    char *filename = NULL;
49 49
     static int count = 0;
50 50
     bool done = false;
51 51
 
52
-    assert(filenameBase != NULL);
52
+    assert(filenameBase != nullptr);
53 53
 
54 54
     // Don't overwrite files
55
+    std::ostringstream filename;
55 56
     while (!done) {
56
-        filename = bufferString("%s-%04i.tga", filenameBase, count++);
57
+        filename << filenameBase << "-" << count++ << ".tga";
57 58
 
58
-        FILE *f = fopen(filename, "rb");
59
-
60
-        if (f)
59
+        FILE *f = fopen(filename.str().c_str(), "rb");
60
+        if (f) {
61 61
             fclose(f);
62
-        else
62
+        } else {
63 63
             done = true;
64
+        }
64 65
     }
65 66
 
66
-    // Capture frame buffer
67 67
     glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
68
-
69
-    tgaSave(filename, image, getWindow().getWidth(), getWindow().getHeight(), 0);
70
-    printf("Took screenshot '%s'.\n", filename);
71
-
72
-    delete [] filename;
68
+    tgaSave(filename.str().c_str(), image, getWindow().getWidth(), getWindow().getHeight(), 0);
73 69
     delete [] image;
74 70
 }
75 71
 

+ 6
- 6
src/TextureManager.cpp View File

@@ -46,14 +46,14 @@ int TextureManager::initialize() {
46 46
     delete [] image;
47 47
 
48 48
     //! \fixme Temporary
49
-    char *filename = bufferString("%s/tr2/TITLE.PCX", getOpenRaider().mPakDir);
50
-    if (loadPCX(filename) < 0) {
51
-        delete [] filename;
49
+    std::string filename(getOpenRaider().mPakDir);
50
+    filename += "/tr2/TITLE.PCX";
51
+    if (loadPCX(filename.c_str()) < 0) {
52 52
         //! \fixme Error Checking. Return negative error code, check in calling place too
53
-        filename = bufferString("%s/%s", getOpenRaider().mDataDir, "splash.tga");
54
-        loadTGA(filename);
53
+        filename = getOpenRaider().mDataDir;
54
+        filename += "/splash.tga";
55
+        loadTGA(filename.c_str());
55 56
     }
56
-    delete [] filename;
57 57
 
58 58
     return (mTextureIds.size() == 0) ? -1 : 0;
59 59
 }

+ 2
- 2
src/TombRaider.cpp View File

@@ -400,7 +400,7 @@ int TombRaider::checkMime(const char *filename) {
400 400
 }
401 401
 
402 402
 
403
-int TombRaider::Load(char *filename)
403
+int TombRaider::Load(const char *filename)
404 404
 {
405 405
     FILE *f;
406 406
     int i, j, l;
@@ -4046,7 +4046,7 @@ bool TombRaider::isRoomValid(int index)
4046 4046
 // Public Mutators
4047 4047
 ////////////////////////////////////////////////////////////
4048 4048
 
4049
-int TombRaider::loadSFX(char *filename)
4049
+int TombRaider::loadSFX(const char *filename)
4050 4050
 {
4051 4051
     FILE *f = fopen(filename, "rb");
4052 4052
     long bytes = 0;

+ 2
- 0
src/config.cpp.in View File

@@ -9,5 +9,7 @@
9 9
 
10 10
 const char *VERSION = "OpenRaider-@OpenRaider_VERSION_MAJOR@.@OpenRaider_VERSION_MINOR@.@OpenRaider_VERSION_MICRO@@OpenRaider_VERSION_RELEASE@ (@CMAKE_BUILD_TYPE@)";
11 11
 
12
+const char *VERSION_SHORT = "OpenRaider-@OpenRaider_VERSION_MAJOR@.@OpenRaider_VERSION_MINOR@.@OpenRaider_VERSION_MICRO@@OpenRaider_VERSION_RELEASE@";
13
+
12 14
 const char *BUILD_HOST = "@OpenRaider_BUILD_HOST@ @OpenRaider_HOSTNAME@";
13 15
 

+ 6
- 15
src/utils/strings.cpp View File

@@ -80,40 +80,31 @@ bool stringEndsWith(const char *str, const char *suffix) {
80 80
     return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
81 81
 }
82 82
 
83
-char *bufferString(const char *string, va_list args) {
83
+char *bufferString(const char *string, ...) {
84
+    va_list args, tmp;
84 85
     int sz = 60;
85 86
     int n;
86 87
     char *text;
87
-    va_list tmp;
88 88
 
89 89
     assert(string != NULL);
90 90
     assert(string[0] != '\0');
91 91
 
92 92
     text = new char[sz];
93 93
 
94
-    va_copy(tmp, args);
95
-    n = vsnprintf(text, sz, string, tmp);
96
-    va_end(tmp);
94
+    va_start(args, string);
95
+    n = vsnprintf(text, sz, string, args);
97 96
 
98 97
     if (n < 0) {
99 98
         delete [] text;
99
+        va_end(args);
100 100
         return NULL; // encoding error
101 101
     } else if (n >= sz) {
102 102
         sz = n + 1; // buffer too small
103 103
         delete [] text;
104 104
         text = new char[sz + 1];
105
-        va_copy(tmp, args);
106
-        vsnprintf(text, sz, string, tmp);
107
-        va_end(tmp);
105
+        vsnprintf(text, sz, string, args);
108 106
     }
109 107
 
110
-    return text;
111
-}
112
-
113
-char *bufferString(const char *string, ...) {
114
-    va_list args;
115
-    va_start(args, string);
116
-    char *text = bufferString(string, args);
117 108
     va_end(args);
118 109
     return text;
119 110
 }

Loading…
Cancel
Save