Browse Source

basic menu functionality working

Thomas Buck 10 years ago
parent
commit
c0b2f96ab4
4 changed files with 170 additions and 13 deletions
  1. 5
    0
      include/Menu.h
  2. 8
    0
      include/OpenRaider.h
  3. 91
    13
      src/Menu.cpp
  4. 66
    0
      src/OpenRaider.cpp

+ 5
- 0
include/Menu.h View File

36
 
36
 
37
 private:
37
 private:
38
 
38
 
39
+    void displayMapList();
40
+
39
     void drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) __attribute__((format(printf, 5, 0)));
41
     void drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) __attribute__((format(printf, 5, 0)));
40
 
42
 
41
     bool mVisible;
43
     bool mVisible;
44
+    unsigned int mCursor;
45
+
42
     WindowString mainText;
46
     WindowString mainText;
47
+    WindowString tempText;
43
 };
48
 };
44
 
49
 
45
 #endif
50
 #endif

+ 8
- 0
include/OpenRaider.h View File

75
     Sound *mSound;
75
     Sound *mSound;
76
     Menu *mMenu;
76
     Menu *mMenu;
77
 
77
 
78
+    bool mMapListFilled;
79
+    std::vector<char *> mMapList;
80
+
78
 private:
81
 private:
82
+
83
+    void loadPakFolderRecursive(const char *dir);
84
+
85
+    void fillMapList();
86
+
79
     bool mInit;
87
     bool mInit;
80
     bool mRunning;
88
     bool mRunning;
81
 
89
 

+ 91
- 13
src/Menu.cpp View File

22
 
22
 
23
 Menu::Menu() {
23
 Menu::Menu() {
24
     mVisible = false;
24
     mVisible = false;
25
+    mCursor = 0;
26
+
25
     mainText.text = bufferString(VERSION);
27
     mainText.text = bufferString(VERSION);
26
     mainText.color[0] = 0xFF;
28
     mainText.color[0] = 0xFF;
27
     mainText.color[1] = 0xFF;
29
     mainText.color[1] = 0xFF;
28
     mainText.color[2] = 0xFF;
30
     mainText.color[2] = 0xFF;
29
     mainText.color[3] = 0xFF;
31
     mainText.color[3] = 0xFF;
30
     mainText.scale = 1.2f;
32
     mainText.scale = 1.2f;
33
+    mainText.y = 10;
31
     mainText.w = 0;
34
     mainText.w = 0;
32
     mainText.h = 0;
35
     mainText.h = 0;
36
+
37
+    tempText.text = new char[256];
38
+    tempText.color[0] = 0xFF;
39
+    tempText.color[1] = 0xFF;
40
+    tempText.color[2] = 0xFF;
41
+    tempText.color[3] = 0xFF;
42
+    tempText.scale = 1.2f;
43
+    tempText.w = 0;
44
+    tempText.h = 0;
33
 }
45
 }
34
 
46
 
35
 Menu::~Menu() {
47
 Menu::~Menu() {
36
     delete [] mainText.text;
48
     delete [] mainText.text;
49
+    delete [] tempText.text;
37
 }
50
 }
38
 
51
 
39
 void Menu::setVisible(bool visible) {
52
 void Menu::setVisible(bool visible) {
47
 void Menu::drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) {
60
 void Menu::drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) {
48
     va_list args;
61
     va_list args;
49
     va_start(args, s);
62
     va_start(args, s);
50
-    WindowString w;
51
-    w.text = bufferString(s, args);
63
+    vsnprintf(tempText.text, 256, s, args);
64
+    tempText.text[255] = '\0';
52
     va_end(args);
65
     va_end(args);
53
-    w.scale = scale;
54
-    w.x = x;
55
-    w.y = y;
56
-    w.color[0] = 0xFF;
57
-    w.color[1] = 0xFF;
58
-    w.color[2] = 0xFF;
59
-    w.color[3] = 0xFF;
60
-    gOpenRaider->mWindow->writeString(&w);
61
-    delete [] w.text;
66
+
67
+    tempText.scale = scale;
68
+    tempText.x = x;
69
+    tempText.y = y;
70
+    gOpenRaider->mWindow->writeString(&tempText);
71
+}
72
+
73
+void Menu::displayMapList() {
74
+    // Estimate displayable number of items
75
+    int items = (gOpenRaider->mWindow->mHeight - 110) / 25;
76
+
77
+    // Select which part of the list to show
78
+    int min, max;
79
+    if (((int)mCursor - (items / 2)) > 0)
80
+        min = mCursor - (items / 2);
81
+    else
82
+        min = 0;
83
+
84
+    if ((mCursor + (items / 2)) < gOpenRaider->mMapList.size())
85
+        max = mCursor + (items / 2);
86
+    else
87
+        max = gOpenRaider->mMapList.size();
88
+
89
+    while ((max - min) < items) {
90
+        if (min > 0)
91
+            min--;
92
+        else if (max < (gOpenRaider->mMapList.size()))
93
+            max++;
94
+        else
95
+            break;
96
+    }
97
+
98
+    for (int i = 0; i < (max - min); i++) {
99
+        char *map = gOpenRaider->mMapList[i + min];
100
+        if ((i + min) == mCursor) {
101
+            // Less greem & red --> highlight in red
102
+            tempText.color[1] = 0x42;
103
+            tempText.color[2] = 0x42;
104
+        } else {
105
+            tempText.color[1] = 0xFF;
106
+            tempText.color[2] = 0xFF;
107
+        }
108
+        drawText(25, 100 + (25 * i), 0.75f, "%s", map);
109
+    }
62
 }
110
 }
63
 
111
 
64
 void Menu::display() {
112
 void Menu::display() {
73
 
121
 
74
         // Draw heading text
122
         // Draw heading text
75
         mainText.x = (window->mWidth / 2) - (mainText.w / 2);
123
         mainText.x = (window->mWidth / 2) - (mainText.w / 2);
76
-        mainText.y = 10;
77
         window->writeString(&mainText);
124
         window->writeString(&mainText);
78
 
125
 
79
-        drawText(20, (window->mHeight / 2) - 20, 0.75f, "Generating map list...");
126
+        if (!gOpenRaider->mMapListFilled) {
127
+            drawText(25, (window->mHeight / 2) - 20, 0.75f, "Generating map list...");
128
+        } else {
129
+            displayMapList();
130
+        }
80
     }
131
     }
81
 }
132
 }
82
 
133
 
83
 void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
134
 void Menu::handleKeyboard(KeyboardButton key, bool pressed) {
135
+    if (!pressed)
136
+        return;
137
+
138
+    if (key == up) {
139
+        if (mCursor > 0)
140
+            mCursor--;
141
+        else
142
+            mCursor = gOpenRaider->mMapList.size() - 1;
143
+    } else if (key == down) {
144
+        if (mCursor < (gOpenRaider->mMapList.size() - 1))
145
+            mCursor++;
146
+        else
147
+            mCursor = 0;
148
+    } else if (key == right) {
149
+        int i = 10;
150
+        if (mCursor > (gOpenRaider->mMapList.size() - 11))
151
+            i = gOpenRaider->mMapList.size() - 1 - mCursor;
152
+        while (i-- > 0)
153
+            handleKeyboard(down, true);
154
+    } else if (key == left) {
155
+        int i = 10;
156
+        if (mCursor < 10)
157
+            i = mCursor;
158
+        while (i-- > 0)
159
+            handleKeyboard(up, true);
160
+    } else if (key == enter) {
84
 
161
 
162
+    }
85
 }
163
 }
86
 
164
 

+ 66
- 0
src/OpenRaider.cpp View File

8
 #include <cstdio>
8
 #include <cstdio>
9
 #include <cstring>
9
 #include <cstring>
10
 #include <assert.h>
10
 #include <assert.h>
11
+#include <dirent.h>
11
 
12
 
12
 #include "WindowSDL.h"
13
 #include "WindowSDL.h"
13
 
14
 
25
     mPakDir = NULL;
26
     mPakDir = NULL;
26
     mAudioDir = NULL;
27
     mAudioDir = NULL;
27
     mDataDir = NULL;
28
     mDataDir = NULL;
29
+    mMapListFilled = false;
28
 
30
 
29
     mMenu = new Menu();
31
     mMenu = new Menu();
30
     mSound = new Sound();
32
     mSound = new Sound();
55
 
57
 
56
     if (mDataDir)
58
     if (mDataDir)
57
         delete mDataDir;
59
         delete mDataDir;
60
+
61
+    while (mMapList.size() > 0) {
62
+        delete [] mMapList.back();
63
+        mMapList.pop_back();
64
+    }
58
 }
65
 }
59
 
66
 
60
 int OpenRaider::loadConfig(const char *config) {
67
 int OpenRaider::loadConfig(const char *config) {
425
     return 0;
432
     return 0;
426
 }
433
 }
427
 
434
 
435
+void OpenRaider::loadPakFolderRecursive(const char *dir) {
436
+    struct dirent *ep;
437
+    DIR *pakDir;
438
+
439
+    pakDir = opendir(dir);
440
+    if (pakDir != NULL) {
441
+        while ((ep = readdir(pakDir)) != NULL) {
442
+            if (ep->d_type == DT_DIR) {
443
+                if ((strcmp(".", ep->d_name) != 0)
444
+                 && (strcmp("..", ep->d_name) != 0)) {
445
+                    char *tmp = bufferString("%s%s", dir, ep->d_name);
446
+                    char *next = fullPath(tmp, '/');
447
+                    loadPakFolderRecursive(next);
448
+                    delete next;
449
+                    delete tmp;
450
+                }
451
+            } else {
452
+                char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
453
+
454
+                char *lowerPath = bufferString("%s", fullPathMap);
455
+                for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
456
+
457
+                // Check for valid extension
458
+                if (stringEndsWith(lowerPath, ".phd")
459
+                     || stringEndsWith(lowerPath, ".tr2")
460
+                     || stringEndsWith(lowerPath, ".tr4")
461
+                     || stringEndsWith(lowerPath, ".trc")) {
462
+                    //if (m_tombraider.checkMime(fullPathMap) == 0) {
463
+                        // printf("Validated pak: '%s'\n", fullPathMap);
464
+
465
+                        // Just load relative filename
466
+                        mMapList.push_back(bufferString("%s", (fullPathMap + strlen(mPakDir))));
467
+                    //} else {
468
+                    //    printf("ERROR: pak file '%s' not found or invalid\n", fullPathMap);
469
+                    //}
470
+                }
471
+
472
+                delete [] lowerPath;
473
+                delete [] fullPathMap;
474
+            }
475
+        }
476
+        closedir(pakDir);
477
+    } else {
478
+        printf("Could not open PAK dir %s!\n", dir);
479
+    }
480
+}
481
+
482
+void OpenRaider::fillMapList() {
483
+    char *tmp = fullPath(mPakDir, '/');
484
+    loadPakFolderRecursive(tmp);
485
+    delete [] tmp;
486
+    mMapListFilled = true;
487
+}
488
+
428
 int OpenRaider::initialize() {
489
 int OpenRaider::initialize() {
429
     assert(mInit == false);
490
     assert(mInit == false);
430
     assert(mRunning == false);
491
     assert(mRunning == false);
474
 
535
 
475
         mWindow->swapBuffersGL();
536
         mWindow->swapBuffersGL();
476
 
537
 
538
+        // Fill map list after first render pass,
539
+        // so menu *loading screen* is visible
540
+        if (!mMapListFilled)
541
+            fillMapList();
542
+
477
         clock_t stopTime = systemTimerGet();
543
         clock_t stopTime = systemTimerGet();
478
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
544
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
479
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
545
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));

Loading…
Cancel
Save