Bladeren bron

Merge remote-tracking branch 'origin/developer' into feature/Controller
Added support for second controller
# Conflicts:
# CrystalPoint.cpp
# CrystalPoint.h
# CrystalPoint.vcxproj
# CrystalPoint.vcxproj.filters

jancoow 9 jaren geleden
bovenliggende
commit
3f8623d0bf
45 gewijzigde bestanden met toevoegingen van 1334 en 827 verwijderingen
  1. 76 0
      Button.cpp
  2. 30 0
      Button.h
  3. 6 4
      CMakeLists.txt
  4. 1 0
      Controller.cpp
  5. 21 9
      ControllerHandler.cpp
  6. 9 1
      ControllerHandler.h
  7. 103 52
      CrystalPoint.cpp
  8. 6 0
      CrystalPoint.h
  9. 23 1
      Cursor.cpp
  10. 6 2
      Cursor.h
  11. 13 9
      Enemy.cpp
  12. 3 0
      Enemy.h
  13. 2 1
      HeightMap.cpp
  14. 3 14
      Interface.cpp
  15. 18 1
      Main.cpp
  16. 59 0
      Menu.cpp
  17. 20 0
      Menu.h
  18. 12 0
      MenuElement.cpp
  19. 17 0
      MenuElement.h
  20. 3 3
      Model.cpp
  21. 4 0
      Player.cpp
  22. 7 6
      Skybox.cpp
  23. 2 0
      Skybox.h
  24. 18 1
      Sound.cpp
  25. 2 1
      Sound.h
  26. 0 6
      SoundSystem.cpp
  27. 8 1
      SoundSystem.h
  28. 31 0
      Text.cpp
  29. 23 0
      Text.h
  30. 65 0
      Util.cpp
  31. 16 0
      Util.h
  32. 4 4
      Vertex.cpp
  33. 4 4
      Vertex.h
  34. BIN
      WAVE/Sound.wav
  35. BIN
      WAVE/bond.wav
  36. BIN
      WAVE/enemy.wav
  37. BIN
      WAVE/test1.wav
  38. BIN
      WAVE/test2.wav
  39. 2 2
      Weapon.cpp
  40. 14 5
      World.cpp
  41. 2 0
      World.h
  42. 666 666
      lib/serial/include/serial.h
  43. 33 33
      lib/serial/src/serial.cc
  44. 1 0
      worlds/rock.json
  45. 1 1
      worlds/small.json

+ 76 - 0
Button.cpp

@@ -0,0 +1,76 @@
+#include "Button.h"
+#include <string>
+#include "Util.h"
+#include "Vector.h"
+#include "Cursor.h"
+
+Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position)
+{
+	this->width = width;
+	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));
+
+	this->position.x += width / 2 - textWidth / 2;
+	this->position.y += height / 2 - textHeight / 2;
+}
+
+Button::~Button()
+{
+	/*if (action != nullptr)
+		delete action;*/
+}
+
+void Button::draw(void)
+{
+	
+	glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, alfa);
+
+	glBegin(GL_QUADS);
+	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+14/2);*/
+	Text::draw();
+}
+
+void Button::update(int x, int y)
+{	
+	cursorOnButton =
+		(x > planePosition.x && x < planePosition.x + width) &&
+		y > planePosition.y && y < planePosition.y + height;
+
+
+	if (cursorOnButton)
+		alfa = 1.0f;
+	else
+		alfa = 0.5f;
+
+	if (cursorOnButton && Cursor::getInstance()->clicked)
+		if(action != nullptr)
+			action(this);
+}
+
+void Button::addAction(action_function action)
+{
+	this->action = action;
+}
+
+void Button::setForeground(Vec3f color)
+{
+	this->setColor(color);
+}
+
+void Button::setBackground(Vec3f color)
+{
+	background = color;
+}

+ 30 - 0
Button.h

@@ -0,0 +1,30 @@
+#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);
+};
+

+ 6 - 4
CMakeLists.txt

@@ -1,15 +1,17 @@
 cmake_minimum_required(VERSION 3.5)
 project(CrystalPoint)
 
-file(GLOB SOURCE_FILES
+file(GLOB_RECURSE SOURCE_FILES
         "*.h"
         "*.cpp"
+        "*.cc"
         )
 
 add_executable(CrystalPoint ${SOURCE_FILES})
 
 find_package(OpenGL REQUIRED)
 find_package(GLUT REQUIRED)
-include_directories( ${OPENGL_INCLUDE_DIRS}  ${GLUT_INCLUDE_DIRS} )
-target_link_libraries(CrystalPoint ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall ")
+find_package(OpenAL REQUIRED)
+include_directories( ${OPENGL_INCLUDE_DIRS}  ${GLUT_INCLUDE_DIRS} ${OPENAL_INCLUDE_DIRS} )
+target_link_libraries(CrystalPoint ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${OPENAL_LIBRARY} )
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -lpthread")

+ 1 - 0
Controller.cpp

@@ -8,6 +8,7 @@ Controller::Controller(int controllerId) {
     this->controllerId = controllerId;
     this->ypr = Vec3f();
     this->joystick = Vec2f();
+    this->setConnected(false);
     this->setConnected(true);
 }
 

+ 21 - 9
ControllerHandler.cpp

@@ -4,9 +4,6 @@
 
 #include "ControllerHandler.h"
 #include <iostream>
-#include <stdlib.h>
-#include <chrono>
-#include <thread>
 
 /*
  * String split helper functions
@@ -30,10 +27,24 @@ ControllerHandler::ControllerHandler(){
 };
 
 Controller* ControllerHandler::getLeftController(void){
-    for (auto e : controllers)
+    for (int i = 0; i < 4; i++)
     {
-        if(e != nullptr && e->isConnected()){
-            return e;
+        if(controllers[i] != nullptr && controllers[i]->isConnected()){
+            return controllers[i];
+        }
+    }
+    return nullptr;
+}
+
+Controller* ControllerHandler::getRightController(void){
+    bool c1found = false;
+    for (int i = 0; i < 4; i++)
+    {
+        if(controllers[i] != nullptr && controllers[i]->isConnected()){
+            if(c1found){
+                return controllers[i];
+            }
+            c1found = true;
         }
     }
     return nullptr;
@@ -107,8 +118,8 @@ void ControllerHandler::commandControllerData(std::vector<std::string> data) {
         c->ypr.y = std::stoi(data[6])/100.0f;
         c->ypr.z = std::stoi(data[7])/100.0f;
 
-        c->joystick.x = std::stoi(data[2])/3000.0f;
-        c->joystick.y = std::stoi(data[3])/3000.0f;
+        c->joystick.x = std::stoi(data[2])/2000.0f;
+        c->joystick.y = std::stoi(data[3])/2000.0f;
         c->joystickButton = !(data[4] == "0");
 
         c->magnetSwitch = !(data[9] == "0");
@@ -128,9 +139,10 @@ void ControllerHandler::commandControllerEvent(std::vector<std::string> data) {
 }
 
 void ControllerHandler::commandControllerList(std::vector<std::string> data) {
-   for(unsigned int i = 1; i < data.size() -1; i++){
+   for(unsigned int i = 1; i < data.size() - 1; i++){
        int controllerId = std::stoi(data[i]);
        controllers[controllerId] = new Controller(controllerId);
+       rumble(controllerId, 100, 100);
    }
    if(basestationFound)
 	 baseStation->write("start\n");

+ 9 - 1
ControllerHandler.h

@@ -5,13 +5,21 @@
 
 #define BasestationBaudrate 115200
 #include <thread>
+
+#ifdef WIN32
 #include "include/serial.h"
+#else
+#include "lib/serial/include/serial.h"
+#endif
+
+
 #include "Controller.h"
 
 class ControllerHandler{
 public:
     ControllerHandler();
     Controller* getLeftController(void);
+    Controller* getRightController(void);
     void rumble(int idController, int duration, int power);
 private:
     void SearchBasestation(void);
@@ -20,7 +28,7 @@ private:
     bool basestationFound;
     serial::Serial *baseStation;
     std::thread readthread;
-    std::vector<Controller*> controllers {0};
+    Controller* controllers[4];
 
     //Command functions
     void commandDebug(std::vector<std::string> data);

+ 103 - 52
CrystalPoint.cpp

@@ -5,23 +5,33 @@
 #include <cstring>
 #include "WorldHandler.h"
 #include "Player.h"
+#include "Cursor.h"
+#include "Menu.h"
+#include "Text.h"
+#include "Vector.h"
+#include "Button.h"
 
 int CrystalPoint::width = 0;
 int CrystalPoint::height = 0;
 
 SoundSystem CrystalPoint::sound_system;
+bool state = false;
+
+
 
 void CrystalPoint::init()
 {
 	player = Player::getInstance();
 	worldhandler = WorldHandler::getInstance();
-	//cursor = Cursor::getInstance();
+	cursor = Cursor::getInstance();
+
+	menu = new Menu();
+	buildMenu();
 
 	lastFrameTime = 0;
+	state = true;
 
 	glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
-
-	mousePosition = Vec2f(width / 2, height / 2);
 }
 
 
@@ -39,9 +49,10 @@ void CrystalPoint::draw()
 	glMatrixMode(GL_MODELVIEW);
 	glLoadIdentity();
 
+	
 	worldhandler->draw();
-	player->draw();
-
+	if(!state)
+		menu->draw();
 	//cursor->draw();
 
 	glutSwapBuffers();
@@ -54,67 +65,85 @@ 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;
 
-	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);
+		float speed = 10;
 
-	Controller *leftcontroller = controller.getLeftController();
-	if (leftcontroller != nullptr) {
-		Vec2f *leftControllerJoystick = &leftcontroller->joystick;
+		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 (leftcontroller->joystickButton) {
-			controller.rumble(leftcontroller->controllerId, 100, 100);
-		}
+		Controller *leftcontroller = controller.getLeftController();
+		if (leftcontroller != nullptr) {
+			Vec2f *leftControllerJoystick = &leftcontroller->joystick;
 
-		if (leftControllerJoystick->y > 0.3) {
-			player->setPosition(270, leftControllerJoystick->y*deltaTime, false);
-		}
-		else if (leftControllerJoystick->y < -0.3) {
-			player->setPosition(90, leftControllerJoystick->y*-1 * deltaTime, false);
-		}
-		if (leftControllerJoystick->x > 0.3) {
-			player->setPosition(180, leftControllerJoystick->x*deltaTime, false);
-		}
-		else if (leftControllerJoystick->x < -0.3) {
-			player->setPosition(0, leftControllerJoystick->x*-1 * deltaTime, false);
-		}
 
-		player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
+			if (leftcontroller->joystickButton) {
+				controller.rumble(leftcontroller->controllerId, 100, 100);
+			}
 
-	}
 
+			if (leftControllerJoystick->y > 0.3) {
+				player->setPosition(270, leftControllerJoystick->y * deltaTime, false);
+			}
+			else if (leftControllerJoystick->y < -0.3) {
+				player->setPosition(90, leftControllerJoystick->y * -1 * deltaTime, false);
+			}
+			if (leftControllerJoystick->x > 0.3) {
+				player->setPosition(180, leftControllerJoystick->x * deltaTime, false);
+			}
+			else if (leftControllerJoystick->x < -0.3) {
+				player->setPosition(0, leftControllerJoystick->x * -1 * deltaTime, false);
+			}
 
-	if (!worldhandler->isPlayerPositionValid())
-		player->position = oldPosition;
+			player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
 
-	player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
+		}
+		Controller *rightcontroller = controller.getRightController();
+		if(rightcontroller != nullptr){
+			Vec2f *rightControllerJoystick = &rightcontroller->joystick;
+			if (rightControllerJoystick->y > 0.3 || rightControllerJoystick->y < -0.3) {
+				player->rotation.x += rightcontroller->joystick.y/2;
+			}
+
+			if (rightControllerJoystick->x > 0.3 || rightControllerJoystick->x < -0.3) {
+				player->rotation.y += rightcontroller->joystick.x/2;
+			}
+		}
 
-	worldhandler->update(deltaTime);
+		if (player->rotation.x > 90)
+			player->rotation.x = 90;
+		if (player->rotation.x < -90)
+			player->rotation.x = -90;
+
+		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);
+	}
 
-	mousePosition = mousePosition + mouseOffset;
-	//cursor->update(mousePosition);
 
 	mouseOffset = Vec2f(0, 0);
 	prevKeyboardState = keyboardState;
@@ -123,6 +152,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()

+ 6 - 0
CrystalPoint.h

@@ -3,6 +3,8 @@
 class WorldHandler;
 class SoundSystem;
 class Player;
+class Cursor;
+class Menu;
 #include "Vector.h"
 #include "SoundSystem.h"
 #include "ControllerHandler.h"
@@ -27,6 +29,8 @@ public:
 	WorldHandler* worldhandler;
 	Player* player;
 	ControllerHandler controller;
+	Cursor* cursor;
+	Menu* menu;
 
 	static int width, height;
 	KeyboardState keyboardState;
@@ -38,7 +42,9 @@ public:
 	float lastFrameTime;
 
 	static SoundSystem& GetSoundSystem() { return sound_system; }
+	
 
 private:
 	static SoundSystem sound_system;
+	void buildMenu();
 };

+ 23 - 1
Cursor.cpp

@@ -1,5 +1,5 @@
 #include "Cursor.h"
-#include <GL\freeglut.h>
+#include <GL/freeglut.h>
 #include <cmath>
 #include "CrystalPoint.h"
 
@@ -8,10 +8,13 @@ Cursor* Cursor::instance = NULL;
 Cursor::Cursor()
 {
 	enabled = false;
+	mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2);
+	clicked = false;
 }
 
 Cursor::~Cursor()
 {
+	
 }
 
 Cursor* Cursor::getInstance(void)
@@ -54,5 +57,24 @@ 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;
+
+	if (clicked)
+		clicked = !clicked;
+	if (state != prev)
+		if(state == GLUT_UP)
+			clicked = true;
+	prev = state;
 }

+ 6 - 2
Cursor.h

@@ -8,9 +8,9 @@ private:
 
 	static Cursor* instance;
 	bool enabled;
-	Vec2f mousePosition;
-public:
 	
+public:
+	Vec2f mousePosition;
 	~Cursor();
 
 	static Cursor* getInstance(void);
@@ -18,6 +18,10 @@ public:
 	void enable(bool enable);
 	bool isEnabled(void);
 
+	bool clicked;
+	int state, prev;
+
+
 	void draw(void);
 	void update(Vec2f newPosition);
 };

+ 13 - 9
Enemy.cpp

@@ -2,7 +2,6 @@
 #include <cmath>
 #include "Enemy.h"
 #include "Model.h"
-#include "CrystalPoint.h"
 #include <iostream>
 
 Enemy::Enemy(const std::string &fileName,
@@ -19,7 +18,8 @@ Enemy::Enemy(const std::string &fileName,
 	speed = 1;
 	radius = 10;
 	hasTarget = false;
-	hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound("WAVE/Sound.wav", false);
+	hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound("WAVE/enemy.wav", false);
+	music = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
 	attack = false;
 }
 
@@ -72,9 +72,14 @@ void Enemy::collide(const Entity * entity)
 
 void Enemy::update(float delta)
 {
+	music->SetPos(position, Vec3f());
+
 	if (hasTarget)
 	{
-
+		if (music->IsPlaying() == false)
+		{
+			music->Play();
+		}
 		//just 2d walking
 		float dx, dz, length;
 
@@ -98,15 +103,14 @@ void Enemy::update(float delta)
 		else
 		{	
 			attack = true;
+			if (music->IsPlaying() == true)
+			{
+//				music->Pause();
+				music->Stop();
+			}
 		}
 
 		rotation.y = atan2f(dx, dz) * 180 / M_PI;		
 	}
 
-	if (false)
-	{
-		Sound* sound = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
-		sound->SetPos(position, Vec3f());
-		sound->Play();
-	}
 }

+ 3 - 0
Enemy.h

@@ -3,6 +3,7 @@
 #include "Entity.h"
 #include <string>
 #include "Vector.h"
+#include "CrystalPoint.h"
 
 class Enemy : public Entity
 {
@@ -10,6 +11,8 @@ public:
 	Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale);
 	~Enemy();
 
+	Sound* music;
+
 	bool hasTarget;
 	Vec3f target;
 	float speed,radius;

+ 2 - 1
HeightMap.cpp

@@ -1,6 +1,6 @@
 #include "HeightMap.h"
 #include "stb_image.h"
-#include "vector.h"
+#include "Vector.h"
 
 #include "LevelObject.h"
 
@@ -127,6 +127,7 @@ float HeightMap::GetHeight(float x, float y)
 	float labda3 = 1 - labda1 - labda2;
 
 	Vertex z = a * labda1 + b * labda2 + c * labda3;
+//	Vertex z = (a * labda1) + (b * labda2) ;
 
 	return z.y;
 }

+ 3 - 14
Interface.cpp

@@ -1,13 +1,11 @@
 #include "Interface.h"
-#include <GL\freeglut.h>
+#include <GL/freeglut.h>
 #include "CrystalPoint.h"
 
 #include <string>
 
 #include "Player.h"
-
-//Prototype
-void glutBitmapString(std::string str, int x, int y);
+#include "Util.h"
 
 Interface::Interface()
 {
@@ -87,7 +85,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);
 
 	for (int i = 0; i < maxCrystals; i++)
 	{
@@ -118,13 +116,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]);
-	}
 }

+ 18 - 1
Main.cpp

@@ -4,6 +4,11 @@
 #include <stdio.h>
 #include "Vector.h"
 
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+
+#include "Cursor.h"
+
 void configureOpenGL(void);
 
 CrystalPoint* app;
@@ -50,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;
 
@@ -66,7 +82,8 @@ void configureOpenGL()
 	glutInitWindowSize(800, 600);
 	//glutInitWindowPosition(glutGet(GLUT_WINDOW_WIDTH) / 2 - 800/2, glutGet(GLUT_WINDOW_HEIGHT) / 2 - 600/2);
 	glutCreateWindow("Crystal Point");	
-	//glutFullScreen();
+	glutFullScreen();
+
 
 	//Depth testing
 	glEnable(GL_DEPTH_TEST);

+ 59 - 0
Menu.cpp

@@ -0,0 +1,59 @@
+#include <GL/freeglut.h>
+#include "Menu.h"
+#include "CrystalPoint.h"
+
+Menu::Menu()
+{
+	cursor = Cursor::getInstance();
+}
+
+
+Menu::~Menu()
+{
+}
+
+void Menu::draw(void)
+{
+	//Switch view to Ortho
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+	glOrtho(0, CrystalPoint::width, CrystalPoint::height, 0, -10, 10);
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+
+	glDisable(GL_LIGHTING);
+	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)
+	{
+		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) {};
+	virtual void update(int x, int y) {};
+};
+

+ 3 - 3
Model.cpp

@@ -1,6 +1,5 @@
 #include "Model.h"
 
-#define STB_IMAGE_IMPLEMENTATION
 #include "stb_image.h"
 
 #include <iostream>
@@ -8,6 +7,7 @@
 #include <string>
 #include <algorithm>
 #include <cmath>
+#include <cstring>
 
 //Prototypes
 std::vector<std::string> split(std::string str, std::string sep);
@@ -138,7 +138,7 @@ Model::Model(std::string fileName)
 		radius = fmax(radius, (center.x - v.x) * (center.x - v.x) + (center.z - v.z) * (center.z - v.z));
 	radius = sqrt(radius);
 
-	for each(ObjGroup *group in groups)
+	for (ObjGroup *group : groups)
 	{
 		Optimise(group);
 	}
@@ -148,7 +148,7 @@ void Model::Optimise(ObjGroup *t)
 {
 	for (Face &face : t->faces)
 	{
-		for each(auto &vertex in face.vertices)
+		for (auto &vertex : face.vertices)
 		{
 			t->VertexArray.push_back(Vertex(vertices[vertex.position].x, vertices[vertex.position].y, vertices[vertex.position].z,
 				normals[vertex.normal].x, normals[vertex.normal].y, normals[vertex.normal].z,

+ 4 - 0
Player.cpp

@@ -15,6 +15,9 @@ Player::Player()
 
 	leftWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(4.5, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec2f(170, 70), Vec2f(20, -80));
 	leftWeapon->rotateWeapon(Vec3f(150, 0, 60));
+
+	rightWeapon = new Weapon("models/weapons/ZwaardMetTextures/TextureZwaard.obj", 1, position, rotation, Vec3f(3, -8, -1), Vec3f(-2.0f, 6.0f, -2.1f), Vec2f(170, 70), Vec2f(20, -80));
+	rightWeapon->rotateWeapon(Vec3f(150, 0, 60));
 }
 
 Player* Player::getInstance()
@@ -64,4 +67,5 @@ void Player::setPosition(float angle, float fac, bool height)
 
 void Player::draw() {
 	leftWeapon->draw();
+	rightWeapon->draw();
 }

+ 7 - 6
Skybox.cpp

@@ -1,6 +1,7 @@
 #include "cmath"
 #include <GL/freeglut.h>
 
+#include "Util.h"
 #include "stb_image.h"
 #include "Skybox.h"
 #include <string>
@@ -25,12 +26,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()

+ 2 - 0
Skybox.h

@@ -14,6 +14,8 @@ public:
 
 	void init();
 	void draw();
+
 	void update(float deltaTime, int, int);
 	GLuint loadTexture(const std::string &fileName);
+
 };

+ 18 - 1
Sound.cpp

@@ -1,14 +1,22 @@
 #include "Sound.h"
 
 #include <iostream>
+#ifdef WIN32
 #include <windows.h>
 #include <al.h>
+#else
+#include <AL/al.h>
+typedef unsigned long DWORD;
+typedef unsigned short WORD;
+typedef unsigned int UNINT32;
+typedef unsigned char BYTE;
+#endif
+
 
 Sound::Sound(const char* inWavPath, bool inLooping):
 	buffer_id(0),
 	source_id(0),
 	is_looping(inLooping)
-
 {
 	const char* path = inWavPath;
 
@@ -137,3 +145,12 @@ void Sound::Stop()
 	alSourceStop(source_id);
 }
 
+bool Sound::IsPlaying()
+{
+	ALenum state;
+
+	alGetSourcei(source_id, AL_SOURCE_STATE, &state);
+
+	return (state == AL_PLAYING);
+}
+

+ 2 - 1
Sound.h

@@ -1,6 +1,6 @@
 #pragma once
 
-#include "vector.h"
+#include "Vector.h"
 
 class Sound
 {
@@ -13,6 +13,7 @@ public:
 	void Play();
 	void Pause();
 	void Stop();
+	bool IsPlaying();
 
 private:
 	unsigned int buffer_id;

+ 0 - 6
SoundSystem.cpp

@@ -1,11 +1,5 @@
 #include "SoundSystem.h"
 
-#include <cstdlib>
-#include <iostream>
-#include <windows.h>
-
-
-
 SoundSystem::SoundSystem():
 	device(nullptr),
 	context(nullptr)

+ 8 - 1
SoundSystem.h

@@ -1,9 +1,16 @@
 #pragma once
 
 #include <vector>
+
+#ifdef WIN32
 #include <al.h>
 #include <alc.h>
-#include "vector.h"
+#else
+#include <AL/al.h>
+#include <AL/alc.h>
+#endif
+
+#include "Vector.h"
 #include "Sound.h"
 
 

+ 31 - 0
Text.cpp

@@ -0,0 +1,31 @@
+#include "Text.h"
+
+
+Text::Text(const std::string &text, Vec2f position) : MenuElement(position)
+{
+	this->text = text;
+	color = Vec3f(50, 150, 150);
+	textHeight = 14;
+	textWidth = Util::glutTextWidth(text);
+}
+
+
+Text::~Text()
+{
+}
+
+void Text::draw()
+{
+	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)
+{
+	//Do nothing
+}
+
+void Text::setColor(Vec3f color)
+{
+	this->color = color;
+}

+ 23 - 0
Text.h

@@ -0,0 +1,23 @@
+#pragma once
+#include "MenuElement.h"
+#include "Vector.h"
+#include "Util.h"
+
+class Text : public MenuElement
+{
+private:
+	std::string text;
+	Vec3f color;	
+protected:
+	int textWidth;
+	int textHeight;
+public:
+	Text(const std::string &text, Vec2f position);
+	~Text();
+
+	virtual void draw();
+	virtual void update(int x, int y);
+
+	void setColor(Vec3f color);
+};
+

+ 65 - 0
Util.cpp

@@ -0,0 +1,65 @@
+#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]);
+	}
+}
+
+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);
+//}
+

+ 16 - 0
Util.h

@@ -0,0 +1,16 @@
+#pragma once
+#include <string>
+#include <GL/freeglut.h>
+
+class Util
+{
+private:
+	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();
+};
+

+ 4 - 4
Vertex.cpp

@@ -18,22 +18,22 @@ Vertex::~Vertex()
 {
 }
 
-Vertex Vertex::operator/(float &other)
+Vertex Vertex::operator/(const float &other) const
 {
 	return Vertex(x / other, y / other, z / other, normalX, normalY, normalZ, texX, texY);
 }
 
-Vertex Vertex::operator*(Vertex & other)
+Vertex Vertex::operator*(const Vertex & other) const
 {
 	return Vertex(x*other.x, y*other.y, z*other.z, normalX, normalY, normalZ, texX, texY);
 }
 
-Vertex Vertex::operator*(float & other)
+Vertex Vertex::operator*(const float & other) const
 {
 	return Vertex(x*other, y*other, z*other, normalX, normalY, normalZ, texX, texY);
 }
 
-Vertex Vertex::operator+(Vertex & other)
+Vertex Vertex::operator+(const Vertex & other) const
 {
 	return Vertex(x+other.x, y+other.y, z+other.z, normalX, normalY, normalZ, texX, texY);
 }

+ 4 - 4
Vertex.h

@@ -16,9 +16,9 @@ public:
 	float texX;
 	float texY;
 
-	Vertex operator/(float &other);
-	Vertex operator*(Vertex &other);
-	Vertex operator*(float &other);
-	Vertex operator+(Vertex &other);
+	Vertex operator/(const float &other) const;
+	Vertex operator*(const Vertex &other) const;
+	Vertex operator*(const float &other) const;
+	Vertex operator+(const Vertex &other) const;
 };
 

BIN
WAVE/Sound.wav


BIN
WAVE/bond.wav


BIN
WAVE/enemy.wav


BIN
WAVE/test1.wav


BIN
WAVE/test2.wav


+ 2 - 2
Weapon.cpp

@@ -71,14 +71,14 @@ void Weapon::draw(){
         weaponmodel->draw();
 
         //Test code for finding anker point
-/*        glColor3ub(255, 255, 0);
+        glColor3ub(255, 255, 0);
         glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
         glBegin(GL_LINES);
         glVertex2f(0, 4);
         glVertex2f(0, -4);
         glVertex2f(4, 0);
         glVertex2f(-4, 0);
-        glEnd();*/
+        glEnd();
 
         glPopMatrix();
 

+ 14 - 5
World.cpp

@@ -3,7 +3,6 @@
 #include "Entity.h"
 #include "json.h"
 #include "Model.h"
-#include "CrystalPoint.h"
 #include <fstream>
 #include <iostream>
 #include <algorithm>
@@ -179,9 +178,7 @@ World::World(const std::string &fileName):
 	if (!v["world"]["music"].isNull())
 	{
 		music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
-		Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id);
-		music->SetPos(Vec3f(), Vec3f());
-		music->Play();
+		music = CrystalPoint::GetSoundSystem().GetSound(music_id);
 	}
 
 	if (!v["portal"].isNull())
@@ -214,7 +211,10 @@ World::World(const std::string &fileName):
 World::~World()
 {
 	delete heightmap;
+	music->Stop();
+	delete music;
 	delete skybox;
+	delete portal;
 }
 
 std::pair<std::string, bool> World::getObjectFromValue(int val)
@@ -235,7 +235,7 @@ float World::getHeight(float x, float y)
 
 void World::draw()
 {
-	player->setCamera();
+
 
 	float lightPosition[4] = { 0, 2, 1, 0 };
 	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
@@ -244,6 +244,9 @@ void World::draw()
 
 	skybox->draw();
 
+	player->setCamera();
+	player->draw();
+
 	heightmap->Draw();
 
 	for (auto &enemy : enemies)
@@ -257,6 +260,12 @@ void World::draw()
 
 void World::update(float elapsedTime)
 {
+	music->SetPos(player->position, Vec3f());
+
+	if (music->IsPlaying() ==  false)
+	{
+		music->Play();
+	}
 	for (auto &entity : entities)
 		entity->update(elapsedTime);
 

+ 2 - 0
World.h

@@ -8,6 +8,7 @@
 #include "Interface.h"
 #include "Crystal.h"
 #include "Skybox.h"
+#include "CrystalPoint.h"
 #include "Portal.h"
 
 class Entity;
@@ -16,6 +17,7 @@ class World
 {
 private:
 	std::vector<std::pair<int, std::pair<std::string, bool>>> objecttemplates;
+	Sound* music;
 
 	Player* player;
 	HeightMap* heightmap;

File diff suppressed because it is too large
+ 666 - 666
lib/serial/include/serial.h


+ 33 - 33
lib/serial/src/serial.cc

@@ -35,40 +35,40 @@ using serial::flowcontrol_t;
 
 class Serial::ScopedReadLock {
 public:
-  ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
-    this->pimpl_->readLock();
-  }
-  ~ScopedReadLock() {
-    this->pimpl_->readUnlock();
-  }
+    ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
+      this->pimpl_->readLock();
+    }
+    ~ScopedReadLock() {
+      this->pimpl_->readUnlock();
+    }
 private:
-  // Disable copy constructors
-  ScopedReadLock(const ScopedReadLock&);
-  const ScopedReadLock& operator=(ScopedReadLock);
+    // Disable copy constructors
+    ScopedReadLock(const ScopedReadLock&);
+    const ScopedReadLock& operator=(ScopedReadLock);
 
-  SerialImpl *pimpl_;
+    SerialImpl *pimpl_;
 };
 
 class Serial::ScopedWriteLock {
 public:
-  ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
-    this->pimpl_->writeLock();
-  }
-  ~ScopedWriteLock() {
-    this->pimpl_->writeUnlock();
-  }
+    ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
+      this->pimpl_->writeLock();
+    }
+    ~ScopedWriteLock() {
+      this->pimpl_->writeUnlock();
+    }
 private:
-  // Disable copy constructors
-  ScopedWriteLock(const ScopedWriteLock&);
-  const ScopedWriteLock& operator=(ScopedWriteLock);
-  SerialImpl *pimpl_;
+    // Disable copy constructors
+    ScopedWriteLock(const ScopedWriteLock&);
+    const ScopedWriteLock& operator=(ScopedWriteLock);
+    SerialImpl *pimpl_;
 };
 
 Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout,
                 bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
                 flowcontrol_t flowcontrol)
- : pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
-                                           stopbits, flowcontrol))
+        : pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
+                                 stopbits, flowcontrol))
 {
   pimpl_->setTimeout(timeout);
 }
@@ -164,7 +164,7 @@ Serial::readline (string &buffer, size_t size, string eol)
   ScopedReadLock lock(this->pimpl_);
   size_t eol_len = eol.length ();
   uint8_t *buffer_ = static_cast<uint8_t*>
-                              (alloca (size * sizeof (uint8_t)));
+  (alloca (size * sizeof (uint8_t)));
   size_t read_so_far = 0;
   while (true)
   {
@@ -174,7 +174,7 @@ Serial::readline (string &buffer, size_t size, string eol)
       break; // Timeout occured on reading 1 byte
     }
     if (string (reinterpret_cast<const char*>
-         (buffer_ + read_so_far - eol_len), eol_len) == eol) {
+                (buffer_ + read_so_far - eol_len), eol_len) == eol) {
       break; // EOL found
     }
     if (read_so_far == size) {
@@ -200,7 +200,7 @@ Serial::readlines (size_t size, string eol)
   std::vector<std::string> lines;
   size_t eol_len = eol.length ();
   uint8_t *buffer_ = static_cast<uint8_t*>
-    (alloca (size * sizeof (uint8_t)));
+  (alloca (size * sizeof (uint8_t)));
   size_t read_so_far = 0;
   size_t start_of_line = 0;
   while (read_so_far < size) {
@@ -209,24 +209,24 @@ Serial::readlines (size_t size, string eol)
     if (bytes_read == 0) {
       if (start_of_line != read_so_far) {
         lines.push_back (
-          string (reinterpret_cast<const char*> (buffer_ + start_of_line),
-            read_so_far - start_of_line));
+                string (reinterpret_cast<const char*> (buffer_ + start_of_line),
+                        read_so_far - start_of_line));
       }
       break; // Timeout occured on reading 1 byte
     }
     if (string (reinterpret_cast<const char*>
-         (buffer_ + read_so_far - eol_len), eol_len) == eol) {
+                (buffer_ + read_so_far - eol_len), eol_len) == eol) {
       // EOL found
       lines.push_back(
-        string(reinterpret_cast<const char*> (buffer_ + start_of_line),
-          read_so_far - start_of_line));
+              string(reinterpret_cast<const char*> (buffer_ + start_of_line),
+                     read_so_far - start_of_line));
       start_of_line = read_so_far;
     }
     if (read_so_far == size) {
       if (start_of_line != read_so_far) {
         lines.push_back(
-          string(reinterpret_cast<const char*> (buffer_ + start_of_line),
-            read_so_far - start_of_line));
+                string(reinterpret_cast<const char*> (buffer_ + start_of_line),
+                       read_so_far - start_of_line));
       }
       break; // Reached the maximum read length
     }
@@ -412,4 +412,4 @@ bool Serial::getRI ()
 bool Serial::getCD ()
 {
   return pimpl_->getCD ();
-}
+}

+ 1 - 0
worlds/rock.json

@@ -3,6 +3,7 @@
     "heightmap": "worlds/rockHeightmap.png",
     "texture": "worlds/rockStone2.png",
 	"skybox": "skyboxes/water/",
+	"music":  "WAVE/test2.wav",
     "object-templates": [
       {
         "color": 50,

+ 1 - 1
worlds/small.json

@@ -9,7 +9,7 @@
         "collision": true
       }
     ],
-	"music": "WAVE/bond.wav"
+	"music": "WAVE/test1.wav"
   },
   "player": {
     "startposition": [ 20, 5, 20 ]

Some files were not shown because too many files changed in this diff