Procházet zdrojové kódy

basic menu functionality working

Thomas Buck před 10 roky
rodič
revize
c0b2f96ab4
4 změnil soubory, kde provedl 170 přidání a 13 odebrání
  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 Zobrazit soubor

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

+ 8
- 0
include/OpenRaider.h Zobrazit soubor

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

+ 91
- 13
src/Menu.cpp Zobrazit soubor

@@ -22,18 +22,31 @@
22 22
 
23 23
 Menu::Menu() {
24 24
     mVisible = false;
25
+    mCursor = 0;
26
+
25 27
     mainText.text = bufferString(VERSION);
26 28
     mainText.color[0] = 0xFF;
27 29
     mainText.color[1] = 0xFF;
28 30
     mainText.color[2] = 0xFF;
29 31
     mainText.color[3] = 0xFF;
30 32
     mainText.scale = 1.2f;
33
+    mainText.y = 10;
31 34
     mainText.w = 0;
32 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 47
 Menu::~Menu() {
36 48
     delete [] mainText.text;
49
+    delete [] tempText.text;
37 50
 }
38 51
 
39 52
 void Menu::setVisible(bool visible) {
@@ -47,18 +60,53 @@ bool Menu::isVisible() {
47 60
 void Menu::drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) {
48 61
     va_list args;
49 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 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 112
 void Menu::display() {
@@ -73,14 +121,44 @@ void Menu::display() {
73 121
 
74 122
         // Draw heading text
75 123
         mainText.x = (window->mWidth / 2) - (mainText.w / 2);
76
-        mainText.y = 10;
77 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 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 Zobrazit soubor

@@ -8,6 +8,7 @@
8 8
 #include <cstdio>
9 9
 #include <cstring>
10 10
 #include <assert.h>
11
+#include <dirent.h>
11 12
 
12 13
 #include "WindowSDL.h"
13 14
 
@@ -25,6 +26,7 @@ OpenRaider::OpenRaider() {
25 26
     mPakDir = NULL;
26 27
     mAudioDir = NULL;
27 28
     mDataDir = NULL;
29
+    mMapListFilled = false;
28 30
 
29 31
     mMenu = new Menu();
30 32
     mSound = new Sound();
@@ -55,6 +57,11 @@ OpenRaider::~OpenRaider() {
55 57
 
56 58
     if (mDataDir)
57 59
         delete mDataDir;
60
+
61
+    while (mMapList.size() > 0) {
62
+        delete [] mMapList.back();
63
+        mMapList.pop_back();
64
+    }
58 65
 }
59 66
 
60 67
 int OpenRaider::loadConfig(const char *config) {
@@ -425,6 +432,60 @@ int OpenRaider::bind(ActionEvents action, const char *key) {
425 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 489
 int OpenRaider::initialize() {
429 490
     assert(mInit == false);
430 491
     assert(mRunning == false);
@@ -474,6 +535,11 @@ void OpenRaider::run() {
474 535
 
475 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 543
         clock_t stopTime = systemTimerGet();
478 544
         if (MAX_MS_PER_FRAME > (stopTime - startTime))
479 545
             mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));

Loading…
Zrušit
Uložit