Browse Source

Moved drawText into WindowSDL. Added Console.

Thomas Buck 10 years ago
parent
commit
0da8aace78
11 changed files with 179 additions and 56 deletions
  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 View File

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 View File

40
 
40
 
41
     void displayMapList();
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
     bool mVisible;
43
     bool mVisible;
46
     unsigned int mCursor;
44
     unsigned int mCursor;
47
     unsigned int mMin;
45
     unsigned int mMin;
48
 
46
 
49
     WindowString mainText;
47
     WindowString mainText;
50
-    WindowString tempText;
51
 };
48
 };
52
 
49
 
53
 #endif
50
 #endif

+ 2
- 0
include/OpenRaider.h View File

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

+ 3
- 0
include/Window.h View File

99
 
99
 
100
     virtual void writeString(WindowString *s) = 0;
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
     unsigned int mWidth;
105
     unsigned int mWidth;
103
     unsigned int mHeight;
106
     unsigned int mHeight;
104
 
107
 

+ 5
- 0
include/WindowSDL.h View File

61
 
61
 
62
     virtual void writeString(WindowString *s);
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
 private:
67
 private:
65
 
68
 
66
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
69
     SDL_Window *mWindow;      //!< This is the pointer to the SDL surface
70
     GLuint mFontTexture;
73
     GLuint mFontTexture;
71
 
74
 
72
     bool mTextInput;
75
     bool mTextInput;
76
+
77
+    WindowString tempText;
73
 };
78
 };
74
 
79
 
75
 #endif
80
 #endif

+ 1
- 0
src/CMakeLists.txt View File

31
 #################################################################
31
 #################################################################
32
 
32
 
33
 # Set Source files
33
 # Set Source files
34
+set (SRCS ${SRCS} "Console.cpp")
34
 set (SRCS ${SRCS} "main.cpp")
35
 set (SRCS ${SRCS} "main.cpp")
35
 set (SRCS ${SRCS} "Menu.cpp")
36
 set (SRCS ${SRCS} "Menu.cpp")
36
 set (SRCS ${SRCS} "OpenRaider.cpp")
37
 set (SRCS ${SRCS} "OpenRaider.cpp")

+ 59
- 0
src/Console.cpp View File

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 View File

5
  * \author xythobuz
5
  * \author xythobuz
6
  */
6
  */
7
 
7
 
8
-#include <cstdarg>
9
-
10
 #ifdef __APPLE__
8
 #ifdef __APPLE__
11
 #include <OpenGL/gl.h>
9
 #include <OpenGL/gl.h>
12
 #include <OpenGL/glu.h>
10
 #include <OpenGL/glu.h>
34
     mainText.y = 10;
32
     mainText.y = 10;
35
     mainText.w = 0;
33
     mainText.w = 0;
36
     mainText.h = 0;
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
 Menu::~Menu() {
37
 Menu::~Menu() {
49
     delete [] mainText.text;
38
     delete [] mainText.text;
50
-    delete [] tempText.text;
51
 }
39
 }
52
 
40
 
53
 void Menu::setVisible(bool visible) {
41
 void Menu::setVisible(bool visible) {
58
     return mVisible;
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
 void Menu::displayMapList() {
49
 void Menu::displayMapList() {
75
     // Estimate displayable number of items
50
     // Estimate displayable number of items
76
     int items = (gOpenRaider->mWindow->mHeight - 110) / 25;
51
     int items = (gOpenRaider->mWindow->mHeight - 110) / 25;
100
 
75
 
101
     for (int i = 0; i < (max - min); i++) {
76
     for (int i = 0; i < (max - min); i++) {
102
         char *map = gOpenRaider->mMapList[i + min];
77
         char *map = gOpenRaider->mMapList[i + min];
78
+        unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
103
         if ((i + min) == (int)mCursor) {
79
         if ((i + min) == (int)mCursor) {
104
             // Less greem & red --> highlight in red
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
 void Menu::display() {
88
 void Menu::display() {
116
     Window *window = gOpenRaider->mWindow;
89
     Window *window = gOpenRaider->mWindow;
90
+    unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
117
 
91
 
118
     if (mVisible) {
92
     if (mVisible) {
119
         // Draw half-transparent *overlay*
93
         // Draw half-transparent *overlay*
127
         window->writeString(&mainText);
101
         window->writeString(&mainText);
128
 
102
 
129
         if (!gOpenRaider->mMapListFilled) {
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
         } else {
105
         } else {
132
             if (gOpenRaider->mMapList.size() == 0) {
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
             } else {
108
             } else {
135
                 // draw *play button* above list
109
                 // draw *play button* above list
136
                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
110
                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
137
                 glDisable(GL_TEXTURE_2D);
111
                 glDisable(GL_TEXTURE_2D);
138
                 glRecti(25, 25, 100, 75);
112
                 glRecti(25, 25, 100, 75);
139
                 glEnable(GL_TEXTURE_2D);
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
                 displayMapList();
118
                 displayMapList();
149
             }
119
             }

+ 23
- 13
src/OpenRaider.cpp View File

29
     mMapListFilled = false;
29
     mMapListFilled = false;
30
 
30
 
31
     mMenu = new Menu();
31
     mMenu = new Menu();
32
+    mConsole = new Console();
32
     mSound = new Sound();
33
     mSound = new Sound();
33
     mWindow = new WindowSDL();
34
     mWindow = new WindowSDL();
34
 
35
 
40
     if (mMenu)
41
     if (mMenu)
41
         delete mMenu;
42
         delete mMenu;
42
 
43
 
44
+    if (mConsole)
45
+        delete mConsole;
46
+
43
     if (mSound)
47
     if (mSound)
44
         delete mSound;
48
         delete mSound;
45
 
49
 
528
         glClear(GL_COLOR_BUFFER_BIT);
532
         glClear(GL_COLOR_BUFFER_BIT);
529
 
533
 
530
         mWindow->glEnter2D();
534
         mWindow->glEnter2D();
531
-
535
+        mConsole->display();
532
         mMenu->display();
536
         mMenu->display();
533
-
534
         mWindow->glExit2D();
537
         mWindow->glExit2D();
535
 
538
 
539
+        // Put on screen
536
         mWindow->swapBuffersGL();
540
         mWindow->swapBuffersGL();
537
 
541
 
538
         // Fill map list after first render pass,
542
         // Fill map list after first render pass,
550
     if ((keyBindings[menu] == key) && pressed) {
554
     if ((keyBindings[menu] == key) && pressed) {
551
         mMenu->setVisible(!mMenu->isVisible());
555
         mMenu->setVisible(!mMenu->isVisible());
552
     } else if (!mMenu->isVisible()) {
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
     } else {
580
     } else {
573
         mMenu->handleKeyboard(key, pressed);
581
         mMenu->handleKeyboard(key, pressed);
575
 }
583
 }
576
 
584
 
577
 void OpenRaider::handleText(char *text, bool notFinished) {
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
 void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, MouseButton button, bool released) {
591
 void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, MouseButton button, bool released) {

+ 1
- 0
src/Window.cpp View File

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

+ 31
- 0
src/WindowSDL.cpp View File

34
 #elif !defined(__APPLE__)
34
 #elif !defined(__APPLE__)
35
     setDriver("/usr/lib/libGL.so.1");
35
     setDriver("/usr/lib/libGL.so.1");
36
 #endif
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
 WindowSDL::~WindowSDL() {
48
 WindowSDL::~WindowSDL() {
53
 
62
 
54
     if (mFontName)
63
     if (mFontName)
55
         delete [] mFontName;
64
         delete [] mFontName;
65
+
66
+    if (tempText.text)
67
+        delete [] tempText.text;
56
 }
68
 }
57
 
69
 
58
 void WindowSDL::setDriver(const char *driver) {
70
 void WindowSDL::setDriver(const char *driver) {
583
     SDL_FreeSurface(surface);
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
+

Loading…
Cancel
Save