Browse Source

Attempt implement menu (unresolved symbol error)

Kenneth van Ewijk 10 năm trước cách đây
mục cha
commit
ced3ff4462
21 tập tin đã thay đổi với 370 bổ sung64 xóa
  1. 48 0
      Button.cpp
  2. 21 0
      Button.h
  3. 11 7
      CrystalPoint.cpp
  4. 5 0
      CrystalPoint.h
  5. 10 0
      CrystalPoint.vcxproj
  6. 39 6
      CrystalPoint.vcxproj.filters
  7. 13 0
      Cursor.cpp
  8. 1 2
      Cursor.h
  9. 2 13
      Interface.cpp
  10. 3 0
      Main.cpp
  11. 50 0
      Menu.cpp
  12. 20 0
      Menu.h
  13. 12 0
      MenuElement.cpp
  14. 17 0
      MenuElement.h
  15. 0 1
      Model.cpp
  16. 7 34
      Skybox.cpp
  17. 0 1
      Skybox.h
  18. 29 0
      Text.cpp
  19. 20 0
      Text.h
  20. 48 0
      Util.cpp
  21. 14 0
      Util.h

+ 48 - 0
Button.cpp

@@ -0,0 +1,48 @@
+#include "Button.h"
+#include <string>
+#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;
+}

+ 21 - 0
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);
+};
+

+ 11 - 7
CrystalPoint.cpp

@@ -5,6 +5,10 @@
 #include <cstring>
 #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;

+ 5 - 0
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;

+ 10 - 0
CrystalPoint.vcxproj

@@ -154,6 +154,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="Button.cpp" />
     <ClCompile Include="Crystal.cpp" />
     <ClCompile Include="CrystalPoint.cpp" />
     <ClCompile Include="Cursor.cpp" />
@@ -164,17 +165,22 @@
     <ClCompile Include="json.cpp" />
     <ClCompile Include="LevelObject.cpp" />
     <ClCompile Include="Main.cpp" />
+    <ClCompile Include="Menu.cpp" />
+    <ClCompile Include="MenuElement.cpp" />
     <ClCompile Include="Model.cpp" />
     <ClCompile Include="Sound.cpp" />
     <ClCompile Include="SoundSystem.cpp" />
     <ClCompile Include="Player.cpp" />
     <ClCompile Include="Skybox.cpp" />
+    <ClCompile Include="Text.cpp" />
+    <ClCompile Include="Util.cpp" />
     <ClCompile Include="Vector.cpp" />
     <ClCompile Include="Vertex.cpp" />
     <ClCompile Include="World.cpp" />
     <ClCompile Include="WorldHandler.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="Button.h" />
     <ClInclude Include="Crystal.h" />
     <ClInclude Include="CrystalPoint.h" />
     <ClInclude Include="Cursor.h" />
@@ -185,12 +191,16 @@
     <ClInclude Include="json.h" />
     <ClInclude Include="LevelObject.h" />
     <ClInclude Include="Main.h" />
+    <ClInclude Include="Menu.h" />
+    <ClInclude Include="MenuElement.h" />
     <ClInclude Include="Model.h" />
     <ClInclude Include="Sound.h" />
     <ClInclude Include="SoundSystem.h" />
     <ClInclude Include="Player.h" />
     <ClInclude Include="Skybox.h" />
     <ClInclude Include="stb_image.h" />
+    <ClInclude Include="Text.h" />
+    <ClInclude Include="Util.h" />
     <ClInclude Include="vector.h" />
     <ClInclude Include="Vertex.h" />
     <ClInclude Include="World.h" />

+ 39 - 6
CrystalPoint.vcxproj.filters

@@ -22,6 +22,9 @@
     <Filter Include="Source Files\json">
       <UniqueIdentifier>{9c655946-3f99-44ea-bc97-2817656954e0}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Source Files\Menu">
+      <UniqueIdentifier>{6843c8c9-b70f-40a3-a112-d9506726e5ed}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Main.cpp">
@@ -33,9 +36,6 @@
     <ClCompile Include="json.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="WorldHandler.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="Entity.cpp">
       <Filter>Source Files\Object</Filter>
     </ClCompile>
@@ -79,10 +79,28 @@
       <Filter>Source Files</Filter>
     </ClCompile>
     <ClCompile Include="Sound.cpp">
-		<Filter>Source Files</Filter>
+      <Filter>Source Files</Filter>
     </ClCompile>
     <ClCompile Include="Crystal.cpp">
-		<Filter>Source Files</Filter>
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="WorldHandler.cpp">
+      <Filter>Source Files\World</Filter>
+    </ClCompile>
+    <ClCompile Include="Menu.cpp">
+      <Filter>Source Files\Menu</Filter>
+    </ClCompile>
+    <ClCompile Include="MenuElement.cpp">
+      <Filter>Source Files\Menu</Filter>
+    </ClCompile>
+    <ClCompile Include="Button.cpp">
+      <Filter>Source Files\Menu</Filter>
+    </ClCompile>
+    <ClCompile Include="Text.cpp">
+      <Filter>Source Files\Menu</Filter>
+    </ClCompile>
+    <ClCompile Include="Util.cpp">
+      <Filter>Source Files</Filter>
     </ClCompile>
   </ItemGroup>
   <ItemGroup>
@@ -138,7 +156,7 @@
       <Filter>Header Files</Filter>
     </ClInclude>
     <ClInclude Include="Sound.h">
-	  <Filter>Header Files</Filter>
+      <Filter>Header Files</Filter>
     </ClInclude>
     <ClInclude Include="Crystal.h">
       <Filter>Header Files</Filter>
@@ -146,6 +164,21 @@
     <ClInclude Include="Skybox.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="Menu.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="MenuElement.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Button.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Text.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Util.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="worlds\worlds.json">

+ 13 - 0
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;
 }

+ 1 - 2
Cursor.h

@@ -8,9 +8,8 @@ private:
 
 	static Cursor* instance;
 	bool enabled;
-	Vec2f mousePosition;
 public:
-	
+	Vec2f mousePosition;
 	~Cursor();
 
 	static Cursor* getInstance(void);

+ 2 - 13
Interface.cpp

@@ -5,9 +5,7 @@
 #include <string>
 
 #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]);
-	}
 }

+ 3 - 0
Main.cpp

@@ -4,6 +4,9 @@
 #include <stdio.h>
 #include "Vector.h"
 
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+
 void configureOpenGL(void);
 
 CrystalPoint* app;

+ 50 - 0
Menu.cpp

@@ -0,0 +1,50 @@
+#include <GL\freeglut.h>
+#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);
+}

+ 20 - 0
Menu.h

@@ -0,0 +1,20 @@
+#pragma once
+#include <vector>
+#include "MenuElement.h"
+#include "Cursor.h"
+
+class Menu
+{
+private:
+	std::vector<MenuElement*> elements;
+	Cursor* cursor;
+public:
+	Menu();
+	~Menu();
+
+	void draw(void);
+	void update(void);
+
+	void AddMenuElement(MenuElement* e);
+};
+

+ 12 - 0
MenuElement.cpp

@@ -0,0 +1,12 @@
+#include "MenuElement.h"
+
+MenuElement::MenuElement(Vec2f position)
+{
+	hover = false;
+	this->position = position;
+}
+
+
+MenuElement::~MenuElement()
+{
+}

+ 17 - 0
MenuElement.h

@@ -0,0 +1,17 @@
+#pragma once
+#include "Vector.h"
+#include <string>
+
+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;
+};
+

+ 0 - 1
Model.cpp

@@ -1,6 +1,5 @@
 #include "Model.h"
 
-#define STB_IMAGE_IMPLEMENTATION
 #include "stb_image.h"
 
 #include <iostream>

+ 7 - 34
Skybox.cpp

@@ -1,6 +1,7 @@
 #include "cmath"
 #include <GL/freeglut.h>
 
+#include "Util.h"
 #include "stb_image.h"
 #include "Skybox.h"
 #include <string>
@@ -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;
-}
-

+ 0 - 1
Skybox.h

@@ -12,5 +12,4 @@ public:
 
 	void init();
 	void draw();
-	GLuint loadTexture(const std::string &fileName);
 };

+ 29 - 0
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;
+}

+ 20 - 0
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);
+};
+

+ 48 - 0
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]);
+	}
+}

+ 14 - 0
Util.h

@@ -0,0 +1,14 @@
+#pragma once
+#include <string>
+#include <GL\freeglut.h>
+
+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);
+};
+