From ced3ff44628ddba41f1ef39e6955842bcedb29e7 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 9 Jun 2016 17:56:37 +0200 Subject: [PATCH] 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); +}; +