merge
commit
54ea3c21d4
|
@ -16,6 +16,8 @@
|
||||||
#include "Engine.h"
|
#include "Engine.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
|
||||||
|
#include "IMGUIControls.h"
|
||||||
|
|
||||||
namespace asteroids {
|
namespace asteroids {
|
||||||
|
|
||||||
// static float left = 0;
|
// static float left = 0;
|
||||||
|
@ -68,15 +70,31 @@ void MainMenuOverlay::Draw () {
|
||||||
glPushMatrix ();
|
glPushMatrix ();
|
||||||
glLoadIdentity ();
|
glLoadIdentity ();
|
||||||
|
|
||||||
|
// Engine::DrawGLString ( right * 0.5 - 100, 100, "A s t e r o i d s");
|
||||||
|
|
||||||
|
Engine::GUI::Label (4, "A s t e r o i d s", right * 0.5 - 100, 180);
|
||||||
|
|
||||||
|
if (Engine::GUI::Button (1, "Start Game", right * 0.5 - 100, 200, 250, 40)) {
|
||||||
|
GetModel()->SetGameState(GameStateEnterPlayername);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Engine::GUI::Button (2, "Highscores", right * 0.5 - 100, 250, 250, 40)) {
|
||||||
|
GetModel()->SetGameState(GameStateShowHighscore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Engine::GUI::Button (3, "Quit", right * 0.5 - 100, 330, 250, 40)) {
|
||||||
|
Engine::RunCommand("quit");
|
||||||
|
}
|
||||||
|
|
||||||
GetView()->SelectFont("console.ttf");
|
GetView()->SelectFont("console.ttf");
|
||||||
|
|
||||||
|
/*
|
||||||
// then we do the drawings
|
// then we do the drawings
|
||||||
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 64, "A s t e r o i d s");
|
|
||||||
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 32, "Main Menu");
|
Engine::DrawGLString ( right * 0.5 - 100, bottom * 0.5 - 8 - 32, "Main Menu");
|
||||||
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "[Return] - Start Game");
|
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8 - 16, "[Return] - Start Game");
|
||||||
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[h] - Show Highscore");
|
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 - 8, "[h] - Show Highscore");
|
||||||
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 + 8, "[Escape] - Quit");
|
Engine::DrawGLString ( right * 0.5 - 80, bottom * 0.5 + 8, "[Escape] - Quit");
|
||||||
|
*/
|
||||||
glPopMatrix ();
|
glPopMatrix ();
|
||||||
|
|
||||||
glMatrixMode (GL_PROJECTION);
|
glMatrixMode (GL_PROJECTION);
|
||||||
|
@ -602,7 +620,9 @@ void EnterPlayernameOverlay::Draw () {
|
||||||
Engine::DrawGLString ( x, y, "Enter your name: ");
|
Engine::DrawGLString ( x, y, "Enter your name: ");
|
||||||
|
|
||||||
std::string name_output (mPlayerNameInput);
|
std::string name_output (mPlayerNameInput);
|
||||||
|
if (SDL_GetTicks() >> 9 & 1)
|
||||||
name_output += "_";
|
name_output += "_";
|
||||||
|
|
||||||
Engine::DrawGLString ( x + 15*8, y, name_output.c_str());
|
Engine::DrawGLString ( x + 15*8, y, name_output.c_str());
|
||||||
Engine::DrawGLString ( x + 16, y + 16, "Press [Return] to continue.");
|
Engine::DrawGLString ( x + 16, y + 16, "Press [Return] to continue.");
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ class OverlayBase;
|
||||||
|
|
||||||
#include "OverlayBase.h"
|
#include "OverlayBase.h"
|
||||||
#include "Sprite.h"
|
#include "Sprite.h"
|
||||||
|
#include "IMGUIControls.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace asteroids {
|
namespace asteroids {
|
||||||
|
|
|
@ -32,6 +32,7 @@ SET ( ENGINE_SRCS
|
||||||
VariablesCommands.cc
|
VariablesCommands.cc
|
||||||
SimpleConsoleOverlay.cc
|
SimpleConsoleOverlay.cc
|
||||||
Sprite.cc
|
Sprite.cc
|
||||||
|
IMGUIControls.cc
|
||||||
|
|
||||||
Engine.cc
|
Engine.cc
|
||||||
Logging.cc
|
Logging.cc
|
||||||
|
|
|
@ -49,8 +49,13 @@ int ControllerBase::OnInit (int argc, char* argv[]) {
|
||||||
|
|
||||||
// clear all bindings
|
// clear all bindings
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < BINDING_KEYS_LAST; i++)
|
for (i = 0; i < BINDING_KEYS_LAST; i++) {
|
||||||
mBindings[i] = "";
|
mBindings[i] = "";
|
||||||
|
mButtonStates.set(i, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
uistate.activeitem = 0;
|
||||||
|
uistate.hotitem = 0;
|
||||||
|
|
||||||
ControllerInstance = this;
|
ControllerInstance = this;
|
||||||
|
|
||||||
|
@ -141,8 +146,19 @@ void ControllerBase::EnableTextinput (bool textinput_state) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ControllerBase::IMGUIFinish () {
|
||||||
|
if (GetButtonState(MouseButtonLeft) == false) {
|
||||||
|
uistate.activeitem = 0;
|
||||||
|
} else {
|
||||||
|
if (uistate.activeitem == 0)
|
||||||
|
uistate.activeitem = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** \brief Keyboard processing */
|
/** \brief Keyboard processing */
|
||||||
bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
|
bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
|
||||||
|
mButtonStates.set(keysym.sym, true);
|
||||||
|
|
||||||
if (mView->mOverlayManager.SendKeyDown (keysym))
|
if (mView->mOverlayManager.SendKeyDown (keysym))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -156,6 +172,8 @@ bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
|
||||||
|
|
||||||
/** \brief Keyboard processing */
|
/** \brief Keyboard processing */
|
||||||
bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) {
|
bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) {
|
||||||
|
mButtonStates.set(keysym.sym, false);
|
||||||
|
|
||||||
if (mView->mOverlayManager.SendKeyUp (keysym))
|
if (mView->mOverlayManager.SendKeyUp (keysym))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -174,6 +192,7 @@ bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) {
|
||||||
/** \brief Mouse processing */
|
/** \brief Mouse processing */
|
||||||
bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) {
|
bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) {
|
||||||
MouseButton mouse_button = convert_sdl_button (button);
|
MouseButton mouse_button = convert_sdl_button (button);
|
||||||
|
mButtonStates.set(mouse_button, true);
|
||||||
|
|
||||||
if (mView->mOverlayManager.SendMouseButtonDown (button, xpos, ypos))
|
if (mView->mOverlayManager.SendMouseButtonDown (button, xpos, ypos))
|
||||||
return true;
|
return true;
|
||||||
|
@ -189,6 +208,7 @@ bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos)
|
||||||
/** \brief Mouse processing */
|
/** \brief Mouse processing */
|
||||||
bool ControllerBase::OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) {
|
bool ControllerBase::OnMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) {
|
||||||
MouseButton mouse_button = convert_sdl_button (button);
|
MouseButton mouse_button = convert_sdl_button (button);
|
||||||
|
mButtonStates.set(mouse_button, false);
|
||||||
|
|
||||||
if (mView->mOverlayManager.SendMouseButtonUp (button, xpos, ypos))
|
if (mView->mOverlayManager.SendMouseButtonUp (button, xpos, ypos))
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -9,6 +9,15 @@ class ModelBase;
|
||||||
class Console;
|
class Console;
|
||||||
class Module;
|
class Module;
|
||||||
|
|
||||||
|
/** \brief The IMGUI state
|
||||||
|
*
|
||||||
|
* See also http://sol.gfxile.net/imgui/
|
||||||
|
*/
|
||||||
|
struct UIState {
|
||||||
|
int hotitem;
|
||||||
|
int activeitem;
|
||||||
|
};
|
||||||
|
|
||||||
/** \brief Defines the number of keys (keyboard + mous) that we can bind to.
|
/** \brief Defines the number of keys (keyboard + mous) that we can bind to.
|
||||||
*
|
*
|
||||||
* As the keysym enum of SDL has about 320 keys defined and we might have some
|
* As the keysym enum of SDL has about 320 keys defined and we might have some
|
||||||
|
@ -42,11 +51,26 @@ class ControllerBase : public Module {
|
||||||
pos_out[1] = mMouseWorldPosition[1];
|
pos_out[1] = mMouseWorldPosition[1];
|
||||||
pos_out[2] = mMouseWorldPosition[2];
|
pos_out[2] = mMouseWorldPosition[2];
|
||||||
}
|
}
|
||||||
|
bool GetButtonState (unsigned int key) {
|
||||||
|
assert (key < BINDING_KEYS_LAST);
|
||||||
|
return mButtonStates.test(key);
|
||||||
|
}
|
||||||
|
void SetButtonState (unsigned int key, bool state) {
|
||||||
|
assert (key < BINDING_KEYS_LAST);
|
||||||
|
mButtonStates.set (key, state);
|
||||||
|
}
|
||||||
bool BindKey (int key, const char *command);
|
bool BindKey (int key, const char *command);
|
||||||
|
|
||||||
/** \brief Activates or deactivates unicode processing and key delays of the keyboard inputs */
|
/** \brief Activates or deactivates unicode processing and key delays of the keyboard inputs */
|
||||||
void EnableTextinput (bool textinput_state);
|
void EnableTextinput (bool textinput_state);
|
||||||
|
|
||||||
|
void IMGUIPrepare () {
|
||||||
|
uistate.hotitem = 0;
|
||||||
|
}
|
||||||
|
void IMGUIFinish ();
|
||||||
|
|
||||||
|
UIState uistate;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** \brief Initializes the system */
|
/** \brief Initializes the system */
|
||||||
virtual int OnInit (int argc, char* argv[]);
|
virtual int OnInit (int argc, char* argv[]);
|
||||||
|
@ -82,9 +106,13 @@ class ControllerBase : public Module {
|
||||||
|
|
||||||
/** \brief Stores the current mouse position in screen coordinates */
|
/** \brief Stores the current mouse position in screen coordinates */
|
||||||
int mMouseScreenPosition[2];
|
int mMouseScreenPosition[2];
|
||||||
/** \brief Stores the current mouse position on the y=0 plane in wolrd * coordinates */
|
/** \brief Stores the current mouse position on the y=0 plane in world coordinates */
|
||||||
float mMouseWorldPosition[3];
|
float mMouseWorldPosition[3];
|
||||||
|
|
||||||
|
/** \brief Stores for each button of the mouse whether it is clicked
|
||||||
|
* \TODO [low] move it to a bitset and remove the fixed amount of buttons */
|
||||||
|
std::bitset<BINDING_KEYS_LAST> mButtonStates;
|
||||||
|
|
||||||
/** \brief Contains all the bindings for the keyboard */
|
/** \brief Contains all the bindings for the keyboard */
|
||||||
std::string mBindings[BINDING_KEYS_LAST];
|
std::string mBindings[BINDING_KEYS_LAST];
|
||||||
|
|
||||||
|
|
|
@ -245,3 +245,12 @@ void DrawVector(vector3d start, vector3d end) {
|
||||||
glEnd ();
|
glEnd ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DrawRect2D (float x, float y, float w, float h) {
|
||||||
|
glBegin (GL_QUADS);
|
||||||
|
glVertex3f (x, y, 0.);
|
||||||
|
glVertex3f (x, y + h, 0.);
|
||||||
|
glVertex3f (x + w, y + h, 0.);
|
||||||
|
glVertex3f (x + w, y, 0.);
|
||||||
|
glEnd ();
|
||||||
|
}
|
||||||
|
|
|
@ -17,5 +17,6 @@ void DrawDisc(float radius, int segments);
|
||||||
void DrawCircle(float radius, int segments);
|
void DrawCircle(float radius, int segments);
|
||||||
void DrawCone(int segments);
|
void DrawCone(int segments);
|
||||||
void DrawVector(vector3d start, vector3d end);
|
void DrawVector(vector3d start, vector3d end);
|
||||||
|
void DrawRect2D (float x, float y, float w, float h);
|
||||||
|
|
||||||
#endif /* _DRAWINGSGL_H */
|
#endif /* _DRAWINGSGL_H */
|
||||||
|
|
|
@ -244,7 +244,9 @@ void Engine::OnMainLoop () {
|
||||||
mEventManager->Process();
|
mEventManager->Process();
|
||||||
mModel->UpdateTimer ();
|
mModel->UpdateTimer ();
|
||||||
mModel->Process ();
|
mModel->Process ();
|
||||||
|
mController->IMGUIPrepare();
|
||||||
mView->Draw ();
|
mView->Draw ();
|
||||||
|
mController->IMGUIFinish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
#include "IMGUIControls.h"
|
||||||
|
|
||||||
|
#include "Engine.h"
|
||||||
|
#include "ControllerBase.h"
|
||||||
|
#include "ViewBase.h"
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#include "DrawingsGL.h"
|
||||||
|
#include "keytable.h"
|
||||||
|
|
||||||
|
Engine::ControllerBase *controller = NULL;
|
||||||
|
Engine::ViewBase *view = NULL;
|
||||||
|
|
||||||
|
namespace Engine {
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
|
/** \brief Checks whether the mouse is in the given rectangle */
|
||||||
|
bool regionhit (int x, int y, int w, int h) {
|
||||||
|
controller = EngineGetController();
|
||||||
|
assert (controller);
|
||||||
|
|
||||||
|
int mouse_pos[2];
|
||||||
|
|
||||||
|
controller->GetMouseScreenPosition(mouse_pos);
|
||||||
|
|
||||||
|
if (mouse_pos[0] < x ||
|
||||||
|
mouse_pos[1] < y ||
|
||||||
|
mouse_pos[0] >= x + w ||
|
||||||
|
mouse_pos[1] >= y + h)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Label (int id, const char* caption, int x, int y) {
|
||||||
|
if (caption != NULL) {
|
||||||
|
float width, height;
|
||||||
|
view = EngineGetView ();
|
||||||
|
assert (view);
|
||||||
|
|
||||||
|
glColor3f (1., 1., 1.);
|
||||||
|
view->DrawGLStringMeasure(caption, &width, &height);
|
||||||
|
view->DrawGLString(x , y + height * 0.5, caption);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \brief Draws a button at the given position
|
||||||
|
*
|
||||||
|
* \returns true if it was clicked
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool Button (int id, const char* caption, int x, int y, int w, int h) {
|
||||||
|
controller = EngineGetController();
|
||||||
|
assert (controller);
|
||||||
|
|
||||||
|
// Check for hotness
|
||||||
|
if (regionhit (x, y, w, h)) {
|
||||||
|
controller->uistate.hotitem = id;
|
||||||
|
if (controller->uistate.activeitem == 0
|
||||||
|
&& controller->GetButtonState(MouseButtonLeft))
|
||||||
|
controller->uistate.activeitem = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render
|
||||||
|
glColor3f (0.2, 0.2, 0.2);
|
||||||
|
DrawRect2D (x + 4, y + 4, w, h);
|
||||||
|
|
||||||
|
if (controller->uistate.hotitem == id) {
|
||||||
|
if (controller->uistate.activeitem == id) {
|
||||||
|
glColor3f (0.8, 0.8, 0.8);
|
||||||
|
DrawRect2D (x, y, w, h);
|
||||||
|
} else {
|
||||||
|
glColor3f (0.7, 0.7, 0.7);
|
||||||
|
DrawRect2D (x, y, w, h);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
glColor3f (0.4, 0.4, 0.4);
|
||||||
|
DrawRect2D (x, y, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Caption
|
||||||
|
if (caption != NULL) {
|
||||||
|
float width, height;
|
||||||
|
view = EngineGetView ();
|
||||||
|
assert (view);
|
||||||
|
|
||||||
|
glColor3f (1., 1., 1.);
|
||||||
|
view->DrawGLStringMeasure(caption, &width, &height);
|
||||||
|
view->DrawGLString(x + w * 0.5 - width * 0.5, y + h * 0.5 - height * 0.5, caption);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logic
|
||||||
|
if (controller->GetButtonState(MouseButtonLeft) == false
|
||||||
|
&& controller->uistate.hotitem == id
|
||||||
|
&& controller->uistate.activeitem == id)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
};
|
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef _IMGUICONTROLS_H
|
||||||
|
#define _IMGUICONTROLS_H
|
||||||
|
|
||||||
|
namespace Engine {
|
||||||
|
|
||||||
|
namespace GUI {
|
||||||
|
|
||||||
|
/** \brief Checks whether the mouse is in the given rectangle */
|
||||||
|
bool regionhit (int x, int y, int w, int h);
|
||||||
|
|
||||||
|
/** \brief Draws a label at the given position with vertical center at y */
|
||||||
|
void Label (int id, const char* caption, int x, int y);
|
||||||
|
|
||||||
|
/** \brief Draws a button at the given position
|
||||||
|
*
|
||||||
|
* The caption will be centered in the middle of the clickable area.
|
||||||
|
*
|
||||||
|
* \returns true if it was clicked
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool Button (int id, const char* caption, int x, int y, int w, int h);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* _IMGUICONTROLS_H */
|
|
@ -22,7 +22,7 @@ static ViewBase* ViewInstance = NULL;
|
||||||
void InitGL () {
|
void InitGL () {
|
||||||
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
|
||||||
glClearDepth(1.0);
|
glClearDepth(1.0);
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LEQUAL);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glShadeModel(GL_SMOOTH);
|
glShadeModel(GL_SMOOTH);
|
||||||
glEnable (GL_CULL_FACE);
|
glEnable (GL_CULL_FACE);
|
||||||
|
@ -245,6 +245,12 @@ void ViewBase::DrawGLString (float x, float y, const char* str) {
|
||||||
mCurrentFont->draw (x, y, str);
|
mCurrentFont->draw (x, y, str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ViewBase::DrawGLStringMeasure (const char* str, float *width, float *height) {
|
||||||
|
OGLFT::BBox bbox = mCurrentFont->measure (str);
|
||||||
|
*width = bbox.x_max_ - bbox.x_min_;
|
||||||
|
*height = bbox.y_max_ - bbox.y_min_;
|
||||||
|
}
|
||||||
|
|
||||||
void ViewBase::GetCamereEye (float *eye_out) {
|
void ViewBase::GetCamereEye (float *eye_out) {
|
||||||
assert (mCamera);
|
assert (mCamera);
|
||||||
mCamera->GetEye (eye_out);
|
mCamera->GetEye (eye_out);
|
||||||
|
|
|
@ -42,6 +42,8 @@ class ViewBase : public Module{
|
||||||
/** \brief Draws a string at the given position using current projection
|
/** \brief Draws a string at the given position using current projection
|
||||||
* and modelview matrices */
|
* and modelview matrices */
|
||||||
void DrawGLString (float x, float y, const char* str);
|
void DrawGLString (float x, float y, const char* str);
|
||||||
|
/** \brief Computes the width and height of the rasterized string */
|
||||||
|
void DrawGLStringMeasure (const char* str, float *width, float *height);
|
||||||
|
|
||||||
/** \brief Stores the eye poisition in eye_out */
|
/** \brief Stores the eye poisition in eye_out */
|
||||||
void GetCamereEye (float *eye_out);
|
void GetCamereEye (float *eye_out);
|
||||||
|
|
Loading…
Reference in New Issue