From ced3ff44628ddba41f1ef39e6955842bcedb29e7 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 9 Jun 2016 17:56:37 +0200 Subject: [PATCH 01/10] Attempt implement menu (unresolved symbol error) --- Button.cpp | 48 ++++++++++++++++++++++++++++++++++ Button.h | 21 +++++++++++++++ CrystalPoint.cpp | 18 ++++++++----- CrystalPoint.h | 5 ++++ CrystalPoint.vcxproj | 10 ++++++++ CrystalPoint.vcxproj.filters | 45 +++++++++++++++++++++++++++----- Cursor.cpp | 13 ++++++++++ Cursor.h | 3 +-- Interface.cpp | 15 ++--------- Main.cpp | 3 +++ Menu.cpp | 50 ++++++++++++++++++++++++++++++++++++ Menu.h | 20 +++++++++++++++ MenuElement.cpp | 12 +++++++++ MenuElement.h | 17 ++++++++++++ Model.cpp | 1 - Skybox.cpp | 41 +++++------------------------ Skybox.h | 1 - Text.cpp | 29 +++++++++++++++++++++ Text.h | 20 +++++++++++++++ Util.cpp | 48 ++++++++++++++++++++++++++++++++++ Util.h | 14 ++++++++++ 21 files changed, 370 insertions(+), 64 deletions(-) create mode 100644 Button.cpp create mode 100644 Button.h create mode 100644 Menu.cpp create mode 100644 Menu.h create mode 100644 MenuElement.cpp create mode 100644 MenuElement.h create mode 100644 Text.cpp create mode 100644 Text.h create mode 100644 Util.cpp create mode 100644 Util.h diff --git a/Button.cpp b/Button.cpp new file mode 100644 index 0000000..81d60a7 --- /dev/null +++ b/Button.cpp @@ -0,0 +1,48 @@ +#include "Button.h" +#include +#include "Util.h" +#include "Vector.h" + +Button::Button(std::string & text, Vec2f position, float width, float height) : MenuElement(position) +{ + this->width = width; + this->height = height; + this->text = text; + + background = Vec3f(10, 10, 10); + foreground = Vec3f(255, 255, 255); +} + +Button::~Button() +{ +} + +void Button::draw(void) +{ + glColor4f(background.x, background.y, background.z, 1.0f); + + glBegin(GL_QUADS); + glVertex2f(position.x, position.y); + glVertex2f(position.x, position.y + height); + glVertex2f(position.x + width, position.y + height); + glVertex2f(position.x + width, position.y); + glEnd(); + + glColor4f(foreground.x, foreground.y, foreground.z, 1.0f); + Util::glutBitmapString(text, position.x + (width / 2), position.y + (height / 2)); +} + +void Button::update(int x, int y) +{ + //Nothing +} + +void Button::setForeground(Vec3f color) +{ + foreground = color; +} + +void Button::setBackground(Vec3f color) +{ + background = color; +} diff --git a/Button.h b/Button.h new file mode 100644 index 0000000..7b7883b --- /dev/null +++ b/Button.h @@ -0,0 +1,21 @@ +#pragma once +#include "MenuElement.h" + +class Button : public MenuElement +{ +private: + std::string text; + float width, height; + Vec3f foreground; + Vec3f background; +public: + Button(std::string &text, Vec2f position, float width, float height); + ~Button(); + + void draw(void); + void update(int x, int y); + + void setForeground(Vec3f color); + void setBackground(Vec3f color); +}; + diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 867a0b6..05296be 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -5,6 +5,10 @@ #include #include "WorldHandler.h" #include "Player.h" +#include "Cursor.h" +#include "Menu.h" +#include "Text.h" +#include "Vector.h" int CrystalPoint::width = 0; int CrystalPoint::height = 0; @@ -15,13 +19,14 @@ void CrystalPoint::init() { player = Player::getInstance(); worldhandler = WorldHandler::getInstance(); - //cursor = Cursor::getInstance(); + cursor = Cursor::getInstance(); + + menu = new Menu(); + menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); lastFrameTime = 0; glClearColor(0.7f, 0.7f, 1.0f, 1.0f); - - mousePosition = Vec2f(width / 2, height / 2); } @@ -40,8 +45,7 @@ void CrystalPoint::draw() glLoadIdentity(); worldhandler->draw(); - - //cursor->draw(); + menu->draw(); glutSwapBuffers(); } @@ -86,8 +90,8 @@ void CrystalPoint::update() worldhandler->update(deltaTime); - mousePosition = mousePosition + mouseOffset; - //cursor->update(mousePosition); + cursor->update(cursor->mousePosition + mouseOffset); + menu->update(); mouseOffset = Vec2f(0, 0); prevKeyboardState = keyboardState; diff --git a/CrystalPoint.h b/CrystalPoint.h index 5437d5c..b223d8f 100644 --- a/CrystalPoint.h +++ b/CrystalPoint.h @@ -3,6 +3,8 @@ class WorldHandler; class SoundSystem; class Player; +class Cursor; +class Menu; #include "Vector.h" #include "SoundSystem.h" @@ -25,6 +27,9 @@ public: WorldHandler* worldhandler; Player* player; + Cursor* cursor; + + Menu* menu; static int width, height; KeyboardState keyboardState; diff --git a/CrystalPoint.vcxproj b/CrystalPoint.vcxproj index a14f55a..1988e9a 100644 --- a/CrystalPoint.vcxproj +++ b/CrystalPoint.vcxproj @@ -154,6 +154,7 @@ + @@ -164,17 +165,22 @@ + + + + + @@ -185,12 +191,16 @@ + + + + diff --git a/CrystalPoint.vcxproj.filters b/CrystalPoint.vcxproj.filters index df8bece..b2713d2 100644 --- a/CrystalPoint.vcxproj.filters +++ b/CrystalPoint.vcxproj.filters @@ -22,6 +22,9 @@ {9c655946-3f99-44ea-bc97-2817656954e0} + + {6843c8c9-b70f-40a3-a112-d9506726e5ed} + @@ -33,9 +36,6 @@ Source Files - - Source Files - Source Files\Object @@ -79,10 +79,28 @@ Source Files - Source Files + Source Files - Source Files + Source Files + + + Source Files\World + + + Source Files\Menu + + + Source Files\Menu + + + Source Files\Menu + + + Source Files\Menu + + + Source Files @@ -138,7 +156,7 @@ Header Files - Header Files + Header Files Header Files @@ -146,6 +164,21 @@ Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + diff --git a/Cursor.cpp b/Cursor.cpp index 69895a3..39613af 100644 --- a/Cursor.cpp +++ b/Cursor.cpp @@ -8,6 +8,7 @@ Cursor* Cursor::instance = NULL; Cursor::Cursor() { enabled = false; + mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2); } Cursor::~Cursor() @@ -54,5 +55,17 @@ void Cursor::draw(void) void Cursor::update(Vec2f newPosition) { + if (newPosition.x < 0) + newPosition.x = 0; + + if (newPosition.y < 0) + newPosition.y = 0; + + if (newPosition.x > CrystalPoint::width) + newPosition.x = CrystalPoint::width; + + if (newPosition.y > CrystalPoint::height) + newPosition.y = CrystalPoint::height; + mousePosition = newPosition; } diff --git a/Cursor.h b/Cursor.h index 78764f0..3bc3044 100644 --- a/Cursor.h +++ b/Cursor.h @@ -8,9 +8,8 @@ private: static Cursor* instance; bool enabled; - Vec2f mousePosition; public: - + Vec2f mousePosition; ~Cursor(); static Cursor* getInstance(void); diff --git a/Interface.cpp b/Interface.cpp index c97602a..23c1465 100644 --- a/Interface.cpp +++ b/Interface.cpp @@ -5,9 +5,7 @@ #include #include "Player.h" - -//Prototype -void glutBitmapString(std::string str, int x, int y); +#include "Util.h" Interface::Interface() { @@ -78,7 +76,7 @@ void Interface::draw() //Text: level glColor4f(1.0f, 1.0f, 0.1f, 1.0); - glutBitmapString("Level: " + std::to_string(player->level), 490, 900); + Util::glutBitmapString("Level: " + std::to_string(player->level), 490, 900); int cw, ch, offset; cw = 20; @@ -104,13 +102,4 @@ void Interface::draw() void Interface::update(float deltaTime) { -} - -void glutBitmapString(std::string str, int x, int y) -{ - glRasterPos2f(x, y); - for (int i = 0; i < str.size(); i++) - { - glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]); - } } \ No newline at end of file diff --git a/Main.cpp b/Main.cpp index 2660258..740ea80 100644 --- a/Main.cpp +++ b/Main.cpp @@ -4,6 +4,9 @@ #include #include "Vector.h" +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" + void configureOpenGL(void); CrystalPoint* app; diff --git a/Menu.cpp b/Menu.cpp new file mode 100644 index 0000000..0419336 --- /dev/null +++ b/Menu.cpp @@ -0,0 +1,50 @@ +#include +#include "Menu.h" + +Menu::Menu() +{ + cursor = Cursor::getInstance(); +} + + +Menu::~Menu() +{ +} + +void Menu::draw(void) +{ + //Switch view to Ortho + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, 1000, 1000, 0, -10, 10); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + glDisable(GL_TEXTURE_2D); + + + for (MenuElement* e : elements) + { + e->draw(); + } + + cursor->draw(); + + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); +} + +void Menu::update() +{ + for (MenuElement* e : elements) + { + e->update(cursor->mousePosition.x, cursor->mousePosition.y); + } +} + +void Menu::AddMenuElement(MenuElement * e) +{ + elements.push_back(e); +} diff --git a/Menu.h b/Menu.h new file mode 100644 index 0000000..afed8ee --- /dev/null +++ b/Menu.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include "MenuElement.h" +#include "Cursor.h" + +class Menu +{ +private: + std::vector elements; + Cursor* cursor; +public: + Menu(); + ~Menu(); + + void draw(void); + void update(void); + + void AddMenuElement(MenuElement* e); +}; + diff --git a/MenuElement.cpp b/MenuElement.cpp new file mode 100644 index 0000000..e5bc349 --- /dev/null +++ b/MenuElement.cpp @@ -0,0 +1,12 @@ +#include "MenuElement.h" + +MenuElement::MenuElement(Vec2f position) +{ + hover = false; + this->position = position; +} + + +MenuElement::~MenuElement() +{ +} diff --git a/MenuElement.h b/MenuElement.h new file mode 100644 index 0000000..efd263d --- /dev/null +++ b/MenuElement.h @@ -0,0 +1,17 @@ +#pragma once +#include "Vector.h" +#include + +class MenuElement +{ +protected: + bool hover; + Vec2f position; +public: + MenuElement(Vec2f position); + ~MenuElement(); + + virtual void draw(void) = 0; + virtual void update(int x, int y) = 0; +}; + diff --git a/Model.cpp b/Model.cpp index 48c91a5..e280775 100644 --- a/Model.cpp +++ b/Model.cpp @@ -1,6 +1,5 @@ #include "Model.h" -#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include diff --git a/Skybox.cpp b/Skybox.cpp index 1d268ab..eb16080 100644 --- a/Skybox.cpp +++ b/Skybox.cpp @@ -1,6 +1,7 @@ #include "cmath" #include +#include "Util.h" #include "stb_image.h" #include "Skybox.h" #include @@ -21,12 +22,12 @@ Skybox::~Skybox() void Skybox::init() { - skybox[SKY_LEFT] = loadTexture(folder + "left.png"); - skybox[SKY_BACK] = loadTexture(folder + "back.png"); - skybox[SKY_RIGHT] = loadTexture(folder + "right.png"); - skybox[SKY_FRONT] = loadTexture(folder + "front.png"); - skybox[SKY_TOP] = loadTexture(folder + "top.png"); - skybox[SKY_BOTTOM] = loadTexture(folder + "bottom.png"); + skybox[SKY_LEFT] = Util::loadTexture(folder + "left.png"); + skybox[SKY_BACK] = Util::loadTexture(folder + "back.png"); + skybox[SKY_RIGHT] = Util::loadTexture(folder + "right.png"); + skybox[SKY_FRONT] = Util::loadTexture(folder + "front.png"); + skybox[SKY_TOP] = Util::loadTexture(folder + "top.png"); + skybox[SKY_BOTTOM] = Util::loadTexture(folder + "bottom.png"); } void Skybox::draw() @@ -114,31 +115,3 @@ void Skybox::draw() glDisable(GL_TEXTURE_2D); } -GLuint Skybox::loadTexture(const std::string & fileName) //load the filename named texture -{ - int width, height, bpp; - - stbi_set_flip_vertically_on_load(true); - unsigned char* imgData = stbi_load(fileName.c_str(), &width, &height, &bpp, 4); - GLuint num; - glGenTextures(1, &num); - glBindTexture(GL_TEXTURE_2D, num); - - glTexImage2D(GL_TEXTURE_2D, - 0, //level - GL_RGBA, //internal format - width, //width - height, //height - 0, //border - GL_RGBA, //data format - GL_UNSIGNED_BYTE, //data type - imgData); //data - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - - stbi_image_free(imgData); - return num; -} - diff --git a/Skybox.h b/Skybox.h index 5b51fbb..5682e3e 100644 --- a/Skybox.h +++ b/Skybox.h @@ -12,5 +12,4 @@ public: void init(); void draw(); - GLuint loadTexture(const std::string &fileName); }; diff --git a/Text.cpp b/Text.cpp new file mode 100644 index 0000000..9981492 --- /dev/null +++ b/Text.cpp @@ -0,0 +1,29 @@ +#include "Text.h" + + +Text::Text(const std::string &text, Vec2f position) : MenuElement(position) +{ + this->text = text; + color = Vec3f(50, 150, 150); +} + + +Text::~Text() +{ +} + +void Text::draw() +{ + glColor4f(color.x, color.y, color.z, 1.0f); + Util::glutBitmapString(text, position.x, position.y); +} + +void Text::update(int x, int y) +{ + //Do nothing +} + +void Text::setColor(Vec3f color) +{ + this->color = color; +} diff --git a/Text.h b/Text.h new file mode 100644 index 0000000..98a962c --- /dev/null +++ b/Text.h @@ -0,0 +1,20 @@ +#pragma once +#include "MenuElement.h" +#include "Vector.h" +#include "Util.h" + +class Text : public MenuElement +{ +private: + std::string text; + Vec3f color; +public: + Text(const std::string &text, Vec2f position); + ~Text(); + + void draw(); + void update(int x, int y); + + void setColor(Vec3f color); +}; + diff --git a/Util.cpp b/Util.cpp new file mode 100644 index 0000000..e5ebd9c --- /dev/null +++ b/Util.cpp @@ -0,0 +1,48 @@ +#include "Util.h" +#include "stb_image.h" + +Util::Util() +{ +} + + +Util::~Util() +{ +} + +GLuint Util::loadTexture(const std::string &filename) +{ + int width, height, bpp; + + stbi_set_flip_vertically_on_load(true); + unsigned char* imgData = stbi_load(filename.c_str(), &width, &height, &bpp, 4); + GLuint num; + glGenTextures(1, &num); + glBindTexture(GL_TEXTURE_2D, num); + + glTexImage2D(GL_TEXTURE_2D, + 0, //level + GL_RGBA, //internal format + width, //width + height, //height + 0, //border + GL_RGBA, //data format + GL_UNSIGNED_BYTE, //data type + imgData); //data + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + + stbi_image_free(imgData); + return num; +} + +void Util::glutBitmapString(std::string str, int x, int y) +{ + glRasterPos2f(x, y); + for (int i = 0; i < str.size(); i++) + { + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]); + } +} diff --git a/Util.h b/Util.h new file mode 100644 index 0000000..7719eed --- /dev/null +++ b/Util.h @@ -0,0 +1,14 @@ +#pragma once +#include +#include + +class Util +{ +private: + Util(); + ~Util(); +public: + static GLuint loadTexture(const std::string &filename); + static inline void glutBitmapString(const std::string str, int x, int y); +}; + From 6cf94376207578bc73fb2ffb52e72246b4442ad5 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 12:11:24 +0200 Subject: [PATCH 02/10] button is draw by menu --- Button.cpp | 12 +++++++----- Button.h | 2 +- CrystalPoint.cpp | 6 ++++-- Menu.cpp | 3 ++- MenuElement.h | 4 ++-- Util.h | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Button.cpp b/Button.cpp index 81d60a7..65e1506 100644 --- a/Button.cpp +++ b/Button.cpp @@ -3,7 +3,7 @@ #include "Util.h" #include "Vector.h" -Button::Button(std::string & text, Vec2f position, float width, float height) : MenuElement(position) +Button::Button(const std::string & text, Vec2f position, float width, float height) : MenuElement(position) { this->width = width; this->height = height; @@ -19,17 +19,19 @@ Button::~Button() void Button::draw(void) { - glColor4f(background.x, background.y, background.z, 1.0f); + + glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, 1.0f); glBegin(GL_QUADS); glVertex2f(position.x, position.y); glVertex2f(position.x, position.y + height); glVertex2f(position.x + width, position.y + height); glVertex2f(position.x + width, position.y); - glEnd(); + glEnd(); + + glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f); + Util::glutBitmapString(text, position.x, position.y+height/2+18/2); - glColor4f(foreground.x, foreground.y, foreground.z, 1.0f); - Util::glutBitmapString(text, position.x + (width / 2), position.y + (height / 2)); } void Button::update(int x, int y) diff --git a/Button.h b/Button.h index 7b7883b..fe7e0f7 100644 --- a/Button.h +++ b/Button.h @@ -9,7 +9,7 @@ private: Vec3f foreground; Vec3f background; public: - Button(std::string &text, Vec2f position, float width, float height); + Button(const std::string &text, Vec2f position, float width, float height); ~Button(); void draw(void); diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 05296be..02617f4 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -9,6 +9,7 @@ #include "Menu.h" #include "Text.h" #include "Vector.h" +#include "Button.h" int CrystalPoint::width = 0; int CrystalPoint::height = 0; @@ -22,7 +23,8 @@ void CrystalPoint::init() cursor = Cursor::getInstance(); menu = new Menu(); - menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); + menu->AddMenuElement(new Text("Hello", Vec2f(10, 18))); + menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2, 1080 / 2), 100, 50)); lastFrameTime = 0; @@ -44,7 +46,7 @@ void CrystalPoint::draw() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - worldhandler->draw(); + //worldhandler->draw(); menu->draw(); glutSwapBuffers(); diff --git a/Menu.cpp b/Menu.cpp index 0419336..c9a2f99 100644 --- a/Menu.cpp +++ b/Menu.cpp @@ -1,5 +1,6 @@ #include #include "Menu.h" +#include "CrystalPoint.h" Menu::Menu() { @@ -16,7 +17,7 @@ void Menu::draw(void) //Switch view to Ortho glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(0, 1000, 1000, 0, -10, 10); + glOrtho(0, CrystalPoint::width, CrystalPoint::height, 0, -10, 10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); diff --git a/MenuElement.h b/MenuElement.h index efd263d..4f09c21 100644 --- a/MenuElement.h +++ b/MenuElement.h @@ -11,7 +11,7 @@ public: MenuElement(Vec2f position); ~MenuElement(); - virtual void draw(void) = 0; - virtual void update(int x, int y) = 0; + virtual void draw(void) {}; + virtual void update(int x, int y) {}; }; diff --git a/Util.h b/Util.h index 7719eed..63bd840 100644 --- a/Util.h +++ b/Util.h @@ -9,6 +9,6 @@ private: ~Util(); public: static GLuint loadTexture(const std::string &filename); - static inline void glutBitmapString(const std::string str, int x, int y); + static void glutBitmapString(std::string str, int x, int y); }; From 8dcee7032423a8ccee515068ec914d4a89b45390 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 12:38:16 +0200 Subject: [PATCH 03/10] button, text is placed in middle of button, plane is placed behind text --- Button.cpp | 26 +++++++++++++++----------- Button.h | 13 ++++++------- CrystalPoint.cpp | 4 ++-- Text.cpp | 6 ++++-- Text.h | 9 ++++++--- Util.cpp | 17 +++++++++++++++++ Util.h | 4 +++- 7 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Button.cpp b/Button.cpp index 65e1506..7154605 100644 --- a/Button.cpp +++ b/Button.cpp @@ -3,14 +3,18 @@ #include "Util.h" #include "Vector.h" -Button::Button(const std::string & text, Vec2f position, float width, float height) : MenuElement(position) +Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position) { this->width = width; this->height = height; - this->text = text; + this->planePosition = position; background = Vec3f(10, 10, 10); - foreground = Vec3f(255, 255, 255); + //foreground = Vec3f(255, 255, 255); + this->setColor(Vec3f(255, 255, 255)); + + this->position.x += width / 2 - textWidth / 2; + this->position.y += height / 2 - textHeight / 2; } Button::~Button() @@ -23,15 +27,15 @@ void Button::draw(void) glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, 1.0f); glBegin(GL_QUADS); - glVertex2f(position.x, position.y); - glVertex2f(position.x, position.y + height); - glVertex2f(position.x + width, position.y + height); - glVertex2f(position.x + width, position.y); + glVertex2f(planePosition.x, planePosition.y); + glVertex2f(planePosition.x, planePosition.y + height); + glVertex2f(planePosition.x + width, planePosition.y + height); + glVertex2f(planePosition.x + width, planePosition.y); glEnd(); - glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f); - Util::glutBitmapString(text, position.x, position.y+height/2+18/2); - + /*glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f); + Util::glutBitmapString(text, position.x, position.y+height/2+14/2);*/ + Text::draw(); } void Button::update(int x, int y) @@ -41,7 +45,7 @@ void Button::update(int x, int y) void Button::setForeground(Vec3f color) { - foreground = color; + this->setColor(color); } void Button::setBackground(Vec3f color) diff --git a/Button.h b/Button.h index fe7e0f7..8da9023 100644 --- a/Button.h +++ b/Button.h @@ -1,18 +1,17 @@ #pragma once -#include "MenuElement.h" +#include "Text.h" -class Button : public MenuElement +class Button : public Text { -private: - std::string text; - float width, height; - Vec3f foreground; +private: + float width, height; Vec3f background; + Vec2f planePosition; public: Button(const std::string &text, Vec2f position, float width, float height); ~Button(); - void draw(void); + void draw(); void update(int x, int y); void setForeground(Vec3f color); diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 02617f4..bac936d 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -23,8 +23,8 @@ void CrystalPoint::init() cursor = Cursor::getInstance(); menu = new Menu(); - menu->AddMenuElement(new Text("Hello", Vec2f(10, 18))); - menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2, 1080 / 2), 100, 50)); + menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); + menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50)); lastFrameTime = 0; diff --git a/Text.cpp b/Text.cpp index 9981492..67afd42 100644 --- a/Text.cpp +++ b/Text.cpp @@ -5,6 +5,8 @@ Text::Text(const std::string &text, Vec2f position) : MenuElement(position) { this->text = text; color = Vec3f(50, 150, 150); + textHeight = 14; + textWidth = Util::glutTextWidth(text); } @@ -14,8 +16,8 @@ Text::~Text() void Text::draw() { - glColor4f(color.x, color.y, color.z, 1.0f); - Util::glutBitmapString(text, position.x, position.y); + glColor4f(color.x/255.0f, color.y/255.0f, color.z/255.0f, 1.0f); + Util::glutBitmapString(text, position.x-1, position.y+textHeight); } void Text::update(int x, int y) diff --git a/Text.h b/Text.h index 98a962c..b24bb25 100644 --- a/Text.h +++ b/Text.h @@ -7,13 +7,16 @@ class Text : public MenuElement { private: std::string text; - Vec3f color; + Vec3f color; +protected: + int textWidth; + int textHeight; public: Text(const std::string &text, Vec2f position); ~Text(); - void draw(); - void update(int x, int y); + virtual void draw(); + virtual void update(int x, int y); void setColor(Vec3f color); }; diff --git a/Util.cpp b/Util.cpp index e5ebd9c..5f052de 100644 --- a/Util.cpp +++ b/Util.cpp @@ -3,6 +3,7 @@ Util::Util() { + } @@ -46,3 +47,19 @@ void Util::glutBitmapString(std::string str, int x, int y) glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]); } } + +int Util::glutTextWidth(const std::string str) +{ + int total = 0; + for (int i = 0; i < str.size(); i++) + { + total += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, str[i]); + } + return total; +} + +//int Util::glutTextHeight() +//{ +// return glutBitmapHeight(GLUT_BITMAP_HELVETICA_18); +//} + diff --git a/Util.h b/Util.h index 63bd840..54089ee 100644 --- a/Util.h +++ b/Util.h @@ -6,9 +6,11 @@ class Util { private: Util(); - ~Util(); + ~Util(); public: static GLuint loadTexture(const std::string &filename); static void glutBitmapString(std::string str, int x, int y); + static int glutTextWidth(const std::string str); + //static int glutTextHeight(); }; From 1ec3c8f346e5f498cc092bb200260b817194a460 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 12:50:51 +0200 Subject: [PATCH 04/10] buttons shows when ever the cursor is in the button --- Button.cpp | 16 +++++++++++++--- Button.h | 2 ++ CrystalPoint.cpp | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Button.cpp b/Button.cpp index 7154605..004820c 100644 --- a/Button.cpp +++ b/Button.cpp @@ -9,6 +9,8 @@ Button::Button(const std::string & text, Vec2f position, float width, float heig this->height = height; this->planePosition = position; + cursorOnButton = false; + alfa = 0.5f; background = Vec3f(10, 10, 10); //foreground = Vec3f(255, 255, 255); this->setColor(Vec3f(255, 255, 255)); @@ -24,7 +26,7 @@ Button::~Button() void Button::draw(void) { - glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, 1.0f); + glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, alfa); glBegin(GL_QUADS); glVertex2f(planePosition.x, planePosition.y); @@ -39,8 +41,16 @@ void Button::draw(void) } void Button::update(int x, int y) -{ - //Nothing +{ + cursorOnButton = + (x > planePosition.x && x < planePosition.x + width) && + y > planePosition.y && y < planePosition.y + height; + + + if (cursorOnButton) + alfa = 1.0f; + else + alfa = 0.5f; } void Button::setForeground(Vec3f color) diff --git a/Button.h b/Button.h index 8da9023..ebb6a93 100644 --- a/Button.h +++ b/Button.h @@ -7,6 +7,8 @@ private: float width, height; Vec3f background; Vec2f planePosition; + bool cursorOnButton; + float alfa; public: Button(const std::string &text, Vec2f position, float width, float height); ~Button(); diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index bac936d..7c115f8 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -88,9 +88,9 @@ void CrystalPoint::update() if (!worldhandler->isPlayerPositionValid()) player->position = oldPosition; - player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f; + //player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f; - worldhandler->update(deltaTime); + //worldhandler->update(deltaTime); cursor->update(cursor->mousePosition + mouseOffset); menu->update(); From 2e50d11e07375c7cdcec7e1689192f877dfd5465 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 13:29:28 +0200 Subject: [PATCH 05/10] able to click with cursor, button detect clicking event --- Button.cpp | 4 ++++ CrystalPoint.cpp | 4 ++-- Cursor.cpp | 8 ++++++++ Cursor.h | 5 +++++ Main.cpp | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Button.cpp b/Button.cpp index 004820c..9b02b3c 100644 --- a/Button.cpp +++ b/Button.cpp @@ -2,6 +2,7 @@ #include #include "Util.h" #include "Vector.h" +#include "Cursor.h" Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position) { @@ -51,6 +52,9 @@ void Button::update(int x, int y) alfa = 1.0f; else alfa = 0.5f; + + if (cursorOnButton && Cursor::getInstance()->clicked) + this->setColor(Vec3f(0, 255, 0)); } void Button::setForeground(Vec3f color) diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 7c115f8..9d835d2 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -25,7 +25,7 @@ void CrystalPoint::init() menu = new Menu(); menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50)); - + menu->AddMenuElement(new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50)); lastFrameTime = 0; glClearColor(0.7f, 0.7f, 1.0f, 1.0f); @@ -92,8 +92,8 @@ void CrystalPoint::update() //worldhandler->update(deltaTime); - cursor->update(cursor->mousePosition + mouseOffset); menu->update(); + cursor->update(cursor->mousePosition + mouseOffset); mouseOffset = Vec2f(0, 0); prevKeyboardState = keyboardState; diff --git a/Cursor.cpp b/Cursor.cpp index 39613af..25e39e6 100644 --- a/Cursor.cpp +++ b/Cursor.cpp @@ -9,6 +9,7 @@ Cursor::Cursor() { enabled = false; mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2); + clicked = false; } Cursor::~Cursor() @@ -68,4 +69,11 @@ void Cursor::update(Vec2f newPosition) newPosition.y = CrystalPoint::height; mousePosition = newPosition; + + if (clicked) + clicked = !clicked; + if (state != prev) + if(state == GLUT_UP) + clicked = true; + prev = state; } diff --git a/Cursor.h b/Cursor.h index 3bc3044..dd0835e 100644 --- a/Cursor.h +++ b/Cursor.h @@ -8,6 +8,7 @@ private: static Cursor* instance; bool enabled; + public: Vec2f mousePosition; ~Cursor(); @@ -17,6 +18,10 @@ public: void enable(bool enable); bool isEnabled(void); + bool clicked; + int state, prev; + + void draw(void); void update(Vec2f newPosition); }; diff --git a/Main.cpp b/Main.cpp index 740ea80..3c4649c 100644 --- a/Main.cpp +++ b/Main.cpp @@ -7,6 +7,8 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +#include "Cursor.h" + void configureOpenGL(void); CrystalPoint* app; @@ -53,6 +55,17 @@ int main(int argc, char* argv[]) glutPassiveMotionFunc(mousemotion); glutMotionFunc(mousemotion); + auto mouseclick = [](int button, int state, + int x, int y) + { + if (button == GLUT_LEFT_BUTTON) + Cursor::getInstance()->state = state; + + //std::cout << "Left button is down" << std::endl; + }; + + glutMouseFunc(mouseclick); + CrystalPoint::height = GLUT_WINDOW_HEIGHT; CrystalPoint::width = GLUT_WINDOW_WIDTH; @@ -67,6 +80,7 @@ void configureOpenGL() //Init window and glut display mode glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(800, 600); + //glutInitWindowPosition(1000,800); glutCreateWindow("Crystal Point"); glutFullScreen(); From 7758f2c2940f1ff18a8cb10a706bed57203fa0a9 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 13:42:28 +0200 Subject: [PATCH 06/10] able to give button a funtion pointer, who will be called when clicked on the button --- Button.cpp | 8 +++++++- Button.h | 8 ++++++++ CrystalPoint.cpp | 9 ++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Button.cpp b/Button.cpp index 9b02b3c..c53e964 100644 --- a/Button.cpp +++ b/Button.cpp @@ -54,7 +54,13 @@ void Button::update(int x, int y) alfa = 0.5f; if (cursorOnButton && Cursor::getInstance()->clicked) - this->setColor(Vec3f(0, 255, 0)); + if(action != nullptr) + action(this); +} + +void Button::addAction(action_function action) +{ + this->action = action; } void Button::setForeground(Vec3f color) diff --git a/Button.h b/Button.h index ebb6a93..27d617e 100644 --- a/Button.h +++ b/Button.h @@ -1,21 +1,29 @@ #pragma once #include "Text.h" + + class Button : public Text { private: + typedef void(*action_function)(Button* b); float width, height; Vec3f background; Vec2f planePosition; bool cursorOnButton; float alfa; + action_function action = nullptr; public: Button(const std::string &text, Vec2f position, float width, float height); ~Button(); + + void draw(); void update(int x, int y); + void addAction(action_function action); + void setForeground(Vec3f color); void setBackground(Vec3f color); }; diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index 9d835d2..e0aa185 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -25,7 +25,14 @@ void CrystalPoint::init() menu = new Menu(); menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50)); - menu->AddMenuElement(new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50)); + Button* b = new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50); + + b->addAction([](Button* b) + { + b->setColor(Vec3f(0, 255, 0)); + }); + menu->AddMenuElement(b); + lastFrameTime = 0; glClearColor(0.7f, 0.7f, 1.0f, 1.0f); From 03c06e0974abb1b8d2001869f07f1dfe88f6517a Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 13:44:02 +0200 Subject: [PATCH 07/10] delete action pointer --- Button.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Button.cpp b/Button.cpp index c53e964..a3ec170 100644 --- a/Button.cpp +++ b/Button.cpp @@ -22,6 +22,8 @@ Button::Button(const std::string & text, Vec2f position, float width, float heig Button::~Button() { + if (action != nullptr) + delete action; } void Button::draw(void) From 98f3645b6b9dd982a28e39b73585596bf78c378c Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 20 Jun 2016 15:20:12 +0200 Subject: [PATCH 08/10] menu pauses game, able to resume the game. exit game by pause menu --- Button.cpp | 4 +- CrystalPoint.cpp | 104 +++++++++++++++++++++++++++++------------------ CrystalPoint.h | 2 + Cursor.cpp | 1 + Menu.cpp | 8 ++++ 5 files changed, 77 insertions(+), 42 deletions(-) diff --git a/Button.cpp b/Button.cpp index a3ec170..4672323 100644 --- a/Button.cpp +++ b/Button.cpp @@ -22,8 +22,8 @@ Button::Button(const std::string & text, Vec2f position, float width, float heig Button::~Button() { - if (action != nullptr) - delete action; + /*if (action != nullptr) + delete action;*/ } void Button::draw(void) diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp index e0aa185..f72d400 100644 --- a/CrystalPoint.cpp +++ b/CrystalPoint.cpp @@ -15,6 +15,9 @@ int CrystalPoint::width = 0; int CrystalPoint::height = 0; SoundSystem CrystalPoint::sound_system; +bool state = false; + + void CrystalPoint::init() { @@ -23,17 +26,10 @@ void CrystalPoint::init() cursor = Cursor::getInstance(); menu = new Menu(); - menu->AddMenuElement(new Text("Hello", Vec2f(10, 10))); - menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50)); - Button* b = new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50); - - b->addAction([](Button* b) - { - b->setColor(Vec3f(0, 255, 0)); - }); - menu->AddMenuElement(b); + buildMenu(); lastFrameTime = 0; + state = true; glClearColor(0.7f, 0.7f, 1.0f, 1.0f); } @@ -53,8 +49,10 @@ void CrystalPoint::draw() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - //worldhandler->draw(); - menu->draw(); + + worldhandler->draw(); + if(!state) + menu->draw(); glutSwapBuffers(); } @@ -66,41 +64,45 @@ void CrystalPoint::update() float deltaTime = frameTime - lastFrameTime; lastFrameTime = frameTime; - if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT]) - worldhandler->PreviousWorld(); - if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT]) - worldhandler->NextWorld(); - if (keyboardState.keys[27]) - exit(0); + if (state) + { + if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT]) + worldhandler->PreviousWorld(); + if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT]) + worldhandler->NextWorld(); + if (keyboardState.keys[27]) + state = false; - Player* player = Player::getInstance(); + Player* player = Player::getInstance(); - player->rotation.y += mouseOffset.x / 10.0f; - player->rotation.x += mouseOffset.y / 10.0f; - if (player->rotation.x > 90) - player->rotation.x = 90; - if (player->rotation.x < -90) - player->rotation.x = -90; + player->rotation.y += mouseOffset.x / 10.0f; + player->rotation.x += mouseOffset.y / 10.0f; + if (player->rotation.x > 90) + player->rotation.x = 90; + if (player->rotation.x < -90) + player->rotation.x = -90; - float speed = 10; + float speed = 10; - Vec3f oldPosition = player->position; - if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false); - if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false); - if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false); - if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false); - if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true); - if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true); + Vec3f oldPosition = player->position; + if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false); + if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false); + if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false); + if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false); + if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true); + if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true); - if (!worldhandler->isPlayerPositionValid()) - player->position = oldPosition; + if (!worldhandler->isPlayerPositionValid()) + player->position = oldPosition; + player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f; + worldhandler->update(deltaTime); + } + else + { + menu->update(); + cursor->update(cursor->mousePosition + mouseOffset); + } - //player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f; - - //worldhandler->update(deltaTime); - - menu->update(); - cursor->update(cursor->mousePosition + mouseOffset); mouseOffset = Vec2f(0, 0); prevKeyboardState = keyboardState; @@ -109,6 +111,28 @@ void CrystalPoint::update() sound_system.SetListener(player->position, Vec3f(), Vec3f()); } +void CrystalPoint::buildMenu() +{ + Button* start = new Button("Resume", Vec2f(1920 / 2 - 50, 1080 / 2 - 30), 100, 50); + auto toWorld = [](Button* b) + { + state = true; + }; + start->addAction(toWorld); + menu->AddMenuElement(start); + + + Button* test = new Button("Exit", Vec2f(1920 / 2 - 50, 1080 / 2 + 30), 100, 50); + test->addAction([](Button* b) + { + exit(0); + }); + menu->AddMenuElement(test); + Text* t = new Text("Pause", Vec2f(1920 / 2 - Util::glutTextWidth("Pause") / 2, 1080 / 2 - 75)); + t->setColor(Vec3f(255, 255, 0)); + menu->AddMenuElement(t); +} + KeyboardState::KeyboardState() diff --git a/CrystalPoint.h b/CrystalPoint.h index b223d8f..c22f3f5 100644 --- a/CrystalPoint.h +++ b/CrystalPoint.h @@ -41,7 +41,9 @@ public: float lastFrameTime; static SoundSystem& GetSoundSystem() { return sound_system; } + private: static SoundSystem sound_system; + void buildMenu(); }; \ No newline at end of file diff --git a/Cursor.cpp b/Cursor.cpp index 25e39e6..2e92a05 100644 --- a/Cursor.cpp +++ b/Cursor.cpp @@ -14,6 +14,7 @@ Cursor::Cursor() Cursor::~Cursor() { + } Cursor* Cursor::getInstance(void) diff --git a/Menu.cpp b/Menu.cpp index c9a2f99..083a9db 100644 --- a/Menu.cpp +++ b/Menu.cpp @@ -25,6 +25,14 @@ void Menu::draw(void) glDisable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); + glColor4f(60/255.0f, 60/255.0f, 60/255.0f, 0.8f); + glBegin(GL_QUADS); + glVertex2f(0,0); + glVertex2f(0, CrystalPoint::height); + glVertex2f(CrystalPoint::width, CrystalPoint::height); + glVertex2f(CrystalPoint::width, 0); + glEnd(); + for (MenuElement* e : elements) { From 3701918ac98a210631952fb7aff9659511a06336 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Mon, 20 Jun 2016 20:38:56 +0200 Subject: [PATCH 09/10] Bugfix --- World.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/World.cpp b/World.cpp index 00d6bf9..4691557 100644 --- a/World.cpp +++ b/World.cpp @@ -215,6 +215,7 @@ World::~World() { delete heightmap; delete skybox; + delete portal; } std::pair World::getObjectFromValue(int val) From e42779f6c7a85f74f7a4f903ede8781e8371dbf9 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Mon, 20 Jun 2016 20:42:35 +0200 Subject: [PATCH 10/10] Fixed project files --- CrystalPoint.vcxproj | 6 ++--- CrystalPoint.vcxproj.filters | 46 +++++++++++++++++------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/CrystalPoint.vcxproj b/CrystalPoint.vcxproj index 71e2d64..e6702fa 100644 --- a/CrystalPoint.vcxproj +++ b/CrystalPoint.vcxproj @@ -211,14 +211,12 @@ - - - - + + diff --git a/CrystalPoint.vcxproj.filters b/CrystalPoint.vcxproj.filters index 010dfe9..8a2269b 100644 --- a/CrystalPoint.vcxproj.filters +++ b/CrystalPoint.vcxproj.filters @@ -23,7 +23,7 @@ {9c655946-3f99-44ea-bc97-2817656954e0} - {6843c8c9-b70f-40a3-a112-d9506726e5ed} + {e7a88896-78e1-4544-bfc8-cfd55323728e} @@ -36,6 +36,9 @@ Source Files + + Source Files + Source Files\Object @@ -84,8 +87,11 @@ Source Files - - Source Files\World + + Source Files + + + Source Files\Menu Source Files\Menu @@ -93,15 +99,11 @@ Source Files\Menu - - Source Files\Menu - Source Files\Menu - - Source Files + Source Files\Menu @@ -165,20 +167,22 @@ Header Files - - Header Files - - - Header Files - - + Header Files Header Files - + Header Files + + + Header Files + + + Header Files + + Header Files @@ -195,18 +199,12 @@ Source Files\json - - Source Files\json - - + Resource Files - - Resource Files - - + Resource Files