Просмотр исходного кода

Moved drawText into WindowSDL. Added Console.

Thomas Buck 10 лет назад
Родитель
Сommit
0da8aace78
11 измененных файлов: 179 добавлений и 56 удалений
  1. 44
    0
      include/Console.h
  2. 0
    3
      include/Menu.h
  3. 2
    0
      include/OpenRaider.h
  4. 3
    0
      include/Window.h
  5. 5
    0
      include/WindowSDL.h
  6. 1
    0
      src/CMakeLists.txt
  7. 59
    0
      src/Console.cpp
  8. 10
    40
      src/Menu.cpp
  9. 23
    13
      src/OpenRaider.cpp
  10. 1
    0
      src/Window.cpp
  11. 31
    0
      src/WindowSDL.cpp

+ 44
- 0
include/Console.h Просмотреть файл

@@ -0,0 +1,44 @@
1
+/*!
2
+ * \file include/Console.h
3
+ * \brief Console 'overlay'
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _CONSOLE_H_
9
+#define _CONSOLE_H_
10
+
11
+#include "Window.h"
12
+
13
+/*!
14
+ * \brief Console 'overlay'
15
+ */
16
+class Console {
17
+public:
18
+
19
+    /*!
20
+     * \brief Constructs an object of Console
21
+     */
22
+    Console();
23
+
24
+    /*!
25
+     * \brief Deconstructs an object of Console
26
+     */
27
+    ~Console();
28
+
29
+    void setVisible(bool visible);
30
+
31
+    bool isVisible();
32
+
33
+    void display();
34
+
35
+    void handleKeyboard(KeyboardButton key, bool pressed);
36
+
37
+    void handleText(char *text, bool notFinished);
38
+
39
+private:
40
+
41
+    bool mVisible;
42
+};
43
+
44
+#endif

+ 0
- 3
include/Menu.h Просмотреть файл

@@ -40,14 +40,11 @@ private:
40 40
 
41 41
     void displayMapList();
42 42
 
43
-    void drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) __attribute__((format(printf, 5, 0)));
44
-
45 43
     bool mVisible;
46 44
     unsigned int mCursor;
47 45
     unsigned int mMin;
48 46
 
49 47
     WindowString mainText;
50
-    WindowString tempText;
51 48
 };
52 49
 
53 50
 #endif

+ 2
- 0
include/OpenRaider.h Просмотреть файл

@@ -10,6 +10,7 @@
10 10
 
11 11
 #include <vector>
12 12
 
13
+#include "Console.h"
13 14
 #include "Menu.h"
14 15
 #include "Sound.h"
15 16
 #include "Window.h"
@@ -78,6 +79,7 @@ public:
78 79
     Window *mWindow;
79 80
     Sound *mSound;
80 81
     Menu *mMenu;
82
+    Console *mConsole;
81 83
 
82 84
     bool mMapListFilled;
83 85
     std::vector<char *> mMapList;

+ 3
- 0
include/Window.h Просмотреть файл

@@ -99,6 +99,9 @@ public:
99 99
 
100 100
     virtual void writeString(WindowString *s) = 0;
101 101
 
102
+    virtual void drawText(unsigned int x, unsigned int y, float scale, unsigned char *color, const char *s, ...)
103
+        __attribute__((format(printf, 6, 0))) = 0;
104
+
102 105
     unsigned int mWidth;
103 106
     unsigned int mHeight;
104 107
 

+ 5
- 0
include/WindowSDL.h Просмотреть файл

@@ -61,6 +61,9 @@ public:
61 61
 
62 62
     virtual void writeString(WindowString *s);
63 63
 
64
+    virtual void drawText(unsigned int x, unsigned int y, float scale, unsigned char *color, const char *s, ...)
65
+        __attribute__((format(printf, 6, 0)));
66
+
64 67
 private:
65 68
 
66 69
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
@@ -70,6 +73,8 @@ private:
70 73
     GLuint mFontTexture;
71 74
 
72 75
     bool mTextInput;
76
+
77
+    WindowString tempText;
73 78
 };
74 79
 
75 80
 #endif

+ 1
- 0
src/CMakeLists.txt Просмотреть файл

@@ -31,6 +31,7 @@ set (LIBS ${LIBS} ${ZLIB_LIBRARIES})
31 31
 #################################################################
32 32
 
33 33
 # Set Source files
34
+set (SRCS ${SRCS} "Console.cpp")
34 35
 set (SRCS ${SRCS} "main.cpp")
35 36
 set (SRCS ${SRCS} "Menu.cpp")
36 37
 set (SRCS ${SRCS} "OpenRaider.cpp")

+ 59
- 0
src/Console.cpp Просмотреть файл

@@ -0,0 +1,59 @@
1
+/*!
2
+ * \file src/Console.cpp
3
+ * \brief Console 'overlay'
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifdef __APPLE__
9
+#include <OpenGL/gl.h>
10
+#include <OpenGL/glu.h>
11
+#else
12
+#include <GL/gl.h>
13
+#include <GL/glu.h>
14
+#endif
15
+
16
+#include "config.h"
17
+#include "main.h"
18
+#include "Console.h"
19
+#include "utils/strings.h"
20
+
21
+Console::Console() {
22
+    mVisible = false;
23
+}
24
+
25
+Console::~Console() {
26
+}
27
+
28
+void Console::setVisible(bool visible) {
29
+    mVisible = visible;
30
+    gOpenRaider->mWindow->setTextInput(mVisible);
31
+}
32
+
33
+bool Console::isVisible() {
34
+    return mVisible;
35
+}
36
+
37
+void Console::display() {
38
+    Window *window = gOpenRaider->mWindow;
39
+    unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
40
+
41
+    if (mVisible) {
42
+        // Draw half-transparent *overlay*
43
+        glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
44
+        glDisable(GL_TEXTURE_2D);
45
+        glRecti(0, 0, window->mWidth, window->mHeight / 2);
46
+        glEnable(GL_TEXTURE_2D);
47
+
48
+        gOpenRaider->mWindow->drawText(25, (window->mHeight / 4) - 20, 0.75f, color, "Console");
49
+    }
50
+}
51
+
52
+void Console::handleKeyboard(KeyboardButton key, bool pressed) {
53
+
54
+}
55
+
56
+void Console::handleText(char *text, bool notFinished) {
57
+    printf("Got %s (%s)\n", (notFinished ? "not finished" : "finished"));
58
+}
59
+

+ 10
- 40
src/Menu.cpp Просмотреть файл

@@ -5,8 +5,6 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
-#include <cstdarg>
9
-
10 8
 #ifdef __APPLE__
11 9
 #include <OpenGL/gl.h>
12 10
 #include <OpenGL/glu.h>
@@ -34,20 +32,10 @@ Menu::Menu() {
34 32
     mainText.y = 10;
35 33
     mainText.w = 0;
36 34
     mainText.h = 0;
37
-
38
-    tempText.text = new char[256];
39
-    tempText.color[0] = 0xFF;
40
-    tempText.color[1] = 0xFF;
41
-    tempText.color[2] = 0xFF;
42
-    tempText.color[3] = 0xFF;
43
-    tempText.scale = 1.2f;
44
-    tempText.w = 0;
45
-    tempText.h = 0;
46 35
 }
47 36
 
48 37
 Menu::~Menu() {
49 38
     delete [] mainText.text;
50
-    delete [] tempText.text;
51 39
 }
52 40
 
53 41
 void Menu::setVisible(bool visible) {
@@ -58,19 +46,6 @@ bool Menu::isVisible() {
58 46
     return mVisible;
59 47
 }
60 48
 
61
-void Menu::drawText(unsigned int x, unsigned int y, float scale, const char *s, ...) {
62
-    va_list args;
63
-    va_start(args, s);
64
-    vsnprintf(tempText.text, 256, s, args);
65
-    tempText.text[255] = '\0';
66
-    va_end(args);
67
-
68
-    tempText.scale = scale;
69
-    tempText.x = x;
70
-    tempText.y = y;
71
-    gOpenRaider->mWindow->writeString(&tempText);
72
-}
73
-
74 49
 void Menu::displayMapList() {
75 50
     // Estimate displayable number of items
76 51
     int items = (gOpenRaider->mWindow->mHeight - 110) / 25;
@@ -100,20 +75,19 @@ void Menu::displayMapList() {
100 75
 
101 76
     for (int i = 0; i < (max - min); i++) {
102 77
         char *map = gOpenRaider->mMapList[i + min];
78
+        unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
103 79
         if ((i + min) == (int)mCursor) {
104 80
             // Less greem & red --> highlight in red
105
-            tempText.color[1] = 0x42;
106
-            tempText.color[2] = 0x42;
107
-        } else {
108
-            tempText.color[1] = 0xFF;
109
-            tempText.color[2] = 0xFF;
81
+            color[1] = 0x42;
82
+            color[2] = 0x42;
110 83
         }
111
-        drawText(25, 100 + (25 * i), 0.75f, "%s", map);
84
+        gOpenRaider->mWindow->drawText(25, 100 + (25 * i), 0.75f, color, "%s", map);
112 85
     }
113 86
 }
114 87
 
115 88
 void Menu::display() {
116 89
     Window *window = gOpenRaider->mWindow;
90
+    unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
117 91
 
118 92
     if (mVisible) {
119 93
         // Draw half-transparent *overlay*
@@ -127,23 +101,19 @@ void Menu::display() {
127 101
         window->writeString(&mainText);
128 102
 
129 103
         if (!gOpenRaider->mMapListFilled) {
130
-            drawText(25, (window->mHeight / 2) - 20, 0.75f, "Generating map list...");
104
+            gOpenRaider->mWindow->drawText(25, (window->mHeight / 2) - 20, 0.75f, color, "Generating map list...");
131 105
         } else {
132 106
             if (gOpenRaider->mMapList.size() == 0) {
133
-                drawText(25, (window->mHeight / 2) - 20, 0.75f, "No maps found! See README.md");
107
+                gOpenRaider->mWindow->drawText(25, (window->mHeight / 2) - 20, 0.75f, color, "No maps found! See README.md");
134 108
             } else {
135 109
                 // draw *play button* above list
136 110
                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
137 111
                 glDisable(GL_TEXTURE_2D);
138 112
                 glRecti(25, 25, 100, 75);
139 113
                 glEnable(GL_TEXTURE_2D);
140
-                tempText.color[0] = 0x00;
141
-                tempText.color[1] = 0x00;
142
-                tempText.color[2] = 0x00;
143
-                drawText(40, 35, 0.75f, "Play");
144
-                tempText.color[0] = 0xFF;
145
-                tempText.color[1] = 0xFF;
146
-                tempText.color[2] = 0xFF;
114
+                color[0] = color[1] = color[2] = 0x00;
115
+                gOpenRaider->mWindow->drawText(40, 35, 0.75f, color, "Play");
116
+                color[0] = color[1] = color[2] = 0xFF;
147 117
 
148 118
                 displayMapList();
149 119
             }

+ 23
- 13
src/OpenRaider.cpp Просмотреть файл

@@ -29,6 +29,7 @@ OpenRaider::OpenRaider() {
29 29
     mMapListFilled = false;
30 30
 
31 31
     mMenu = new Menu();
32
+    mConsole = new Console();
32 33
     mSound = new Sound();
33 34
     mWindow = new WindowSDL();
34 35
 
@@ -40,6 +41,9 @@ OpenRaider::~OpenRaider() {
40 41
     if (mMenu)
41 42
         delete mMenu;
42 43
 
44
+    if (mConsole)
45
+        delete mConsole;
46
+
43 47
     if (mSound)
44 48
         delete mSound;
45 49
 
@@ -528,11 +532,11 @@ void OpenRaider::run() {
528 532
         glClear(GL_COLOR_BUFFER_BIT);
529 533
 
530 534
         mWindow->glEnter2D();
531
-
535
+        mConsole->display();
532 536
         mMenu->display();
533
-
534 537
         mWindow->glExit2D();
535 538
 
539
+        // Put on screen
536 540
         mWindow->swapBuffersGL();
537 541
 
538 542
         // Fill map list after first render pass,
@@ -550,24 +554,28 @@ void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
550 554
     if ((keyBindings[menu] == key) && pressed) {
551 555
         mMenu->setVisible(!mMenu->isVisible());
552 556
     } else if (!mMenu->isVisible()) {
553
-        if (keyBindings[console] == key) {
554
-
555
-        } else if (keyBindings[forward] == key) {
557
+        if ((keyBindings[console] == key) && pressed) {
558
+            mConsole->setVisible(!mConsole->isVisible());
559
+        } else if (!mConsole->isVisible()) {
560
+            if (keyBindings[forward] == key) {
556 561
 
557
-        } else if (keyBindings[backward] == key) {
562
+            } else if (keyBindings[backward] == key) {
558 563
 
559
-        } else if (keyBindings[left] == key) {
564
+            } else if (keyBindings[left] == key) {
560 565
 
561
-        } else if (keyBindings[right] == key) {
566
+            } else if (keyBindings[right] == key) {
562 567
 
563
-        } else if (keyBindings[jump] == key) {
568
+            } else if (keyBindings[jump] == key) {
564 569
 
565
-        } else if (keyBindings[crouch] == key) {
570
+            } else if (keyBindings[crouch] == key) {
566 571
 
567
-        } else if (keyBindings[use] == key) {
572
+            } else if (keyBindings[use] == key) {
568 573
 
569
-        } else if (keyBindings[holster] == key) {
574
+            } else if (keyBindings[holster] == key) {
570 575
 
576
+            }
577
+        } else {
578
+            mConsole->handleKeyboard(key, pressed);
571 579
         }
572 580
     } else {
573 581
         mMenu->handleKeyboard(key, pressed);
@@ -575,7 +583,9 @@ void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
575 583
 }
576 584
 
577 585
 void OpenRaider::handleText(char *text, bool notFinished) {
578
-
586
+    if ((mConsole->isVisible()) && (!mMenu->isVisible())) {
587
+        mConsole->handleText(text, notFinished);
588
+    }
579 589
 }
580 590
 
581 591
 void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, MouseButton button, bool released) {

+ 1
- 0
src/Window.cpp Просмотреть файл

@@ -7,6 +7,7 @@
7 7
 
8 8
 #include <cstdio>
9 9
 #include <cstring>
10
+#include <cstdarg>
10 11
 #include <assert.h>
11 12
 
12 13
 #ifdef __APPLE__

+ 31
- 0
src/WindowSDL.cpp Просмотреть файл

@@ -34,6 +34,15 @@ WindowSDL::WindowSDL() {
34 34
 #elif !defined(__APPLE__)
35 35
     setDriver("/usr/lib/libGL.so.1");
36 36
 #endif
37
+
38
+    tempText.text = new char[256];
39
+    tempText.color[0] = 0xFF;
40
+    tempText.color[1] = 0xFF;
41
+    tempText.color[2] = 0xFF;
42
+    tempText.color[3] = 0xFF;
43
+    tempText.scale = 1.2f;
44
+    tempText.w = 0;
45
+    tempText.h = 0;
37 46
 }
38 47
 
39 48
 WindowSDL::~WindowSDL() {
@@ -53,6 +62,9 @@ WindowSDL::~WindowSDL() {
53 62
 
54 63
     if (mFontName)
55 64
         delete [] mFontName;
65
+
66
+    if (tempText.text)
67
+        delete [] tempText.text;
56 68
 }
57 69
 
58 70
 void WindowSDL::setDriver(const char *driver) {
@@ -583,3 +595,22 @@ void WindowSDL::writeString(WindowString *s) {
583 595
     SDL_FreeSurface(surface);
584 596
 }
585 597
 
598
+void WindowSDL::drawText(unsigned int x, unsigned int y, float scale, unsigned char *color, const char *s, ...) {
599
+    va_list args;
600
+    va_start(args, s);
601
+    vsnprintf(tempText.text, 256, s, args);
602
+    tempText.text[255] = '\0';
603
+    va_end(args);
604
+
605
+    tempText.scale = scale;
606
+    tempText.x = x;
607
+    tempText.y = y;
608
+    if (color) {
609
+        tempText.color[0] = color[0];
610
+        tempText.color[1] = color[1];
611
+        tempText.color[2] = color[2];
612
+        tempText.color[3] = color[3];
613
+    }
614
+    writeString(&tempText);
615
+}
616
+

Загрузка…
Отмена
Сохранить