Merge remote-tracking branch 'origin/developer' into feature/Controller

Added support for second controller
# Conflicts:
#	CrystalPoint.cpp
#	CrystalPoint.h
#	CrystalPoint.vcxproj
#	CrystalPoint.vcxproj.filters
This commit is contained in:
jancoow
2016-06-21 11:24:55 +02:00
45 changed files with 1284 additions and 777 deletions
+76
View File
@@ -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
View File
@@ -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
View File
@@ -1,15 +1,17 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
project(CrystalPoint) project(CrystalPoint)
file(GLOB SOURCE_FILES file(GLOB_RECURSE SOURCE_FILES
"*.h" "*.h"
"*.cpp" "*.cpp"
"*.cc"
) )
add_executable(CrystalPoint ${SOURCE_FILES}) add_executable(CrystalPoint ${SOURCE_FILES})
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED) find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ) find_package(OpenAL REQUIRED)
target_link_libraries(CrystalPoint ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ) include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${OPENAL_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall ") target_link_libraries(CrystalPoint ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} ${OPENAL_LIBRARY} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -lpthread")
+1
View File
@@ -8,6 +8,7 @@ Controller::Controller(int controllerId) {
this->controllerId = controllerId; this->controllerId = controllerId;
this->ypr = Vec3f(); this->ypr = Vec3f();
this->joystick = Vec2f(); this->joystick = Vec2f();
this->setConnected(false);
this->setConnected(true); this->setConnected(true);
} }
+21 -9
View File
@@ -4,9 +4,6 @@
#include "ControllerHandler.h" #include "ControllerHandler.h"
#include <iostream> #include <iostream>
#include <stdlib.h>
#include <chrono>
#include <thread>
/* /*
* String split helper functions * String split helper functions
@@ -30,10 +27,24 @@ ControllerHandler::ControllerHandler(){
}; };
Controller* ControllerHandler::getLeftController(void){ Controller* ControllerHandler::getLeftController(void){
for (auto e : controllers) for (int i = 0; i < 4; i++)
{ {
if(e != nullptr && e->isConnected()){ if(controllers[i] != nullptr && controllers[i]->isConnected()){
return e; 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; 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.y = std::stoi(data[6])/100.0f;
c->ypr.z = std::stoi(data[7])/100.0f; c->ypr.z = std::stoi(data[7])/100.0f;
c->joystick.x = std::stoi(data[2])/3000.0f; c->joystick.x = std::stoi(data[2])/2000.0f;
c->joystick.y = std::stoi(data[3])/3000.0f; c->joystick.y = std::stoi(data[3])/2000.0f;
c->joystickButton = !(data[4] == "0"); c->joystickButton = !(data[4] == "0");
c->magnetSwitch = !(data[9] == "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) { 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]); int controllerId = std::stoi(data[i]);
controllers[controllerId] = new Controller(controllerId); controllers[controllerId] = new Controller(controllerId);
rumble(controllerId, 100, 100);
} }
if(basestationFound) if(basestationFound)
baseStation->write("start\n"); baseStation->write("start\n");
+9 -1
View File
@@ -5,13 +5,21 @@
#define BasestationBaudrate 115200 #define BasestationBaudrate 115200
#include <thread> #include <thread>
#ifdef WIN32
#include "include/serial.h" #include "include/serial.h"
#else
#include "lib/serial/include/serial.h"
#endif
#include "Controller.h" #include "Controller.h"
class ControllerHandler{ class ControllerHandler{
public: public:
ControllerHandler(); ControllerHandler();
Controller* getLeftController(void); Controller* getLeftController(void);
Controller* getRightController(void);
void rumble(int idController, int duration, int power); void rumble(int idController, int duration, int power);
private: private:
void SearchBasestation(void); void SearchBasestation(void);
@@ -20,7 +28,7 @@ private:
bool basestationFound; bool basestationFound;
serial::Serial *baseStation; serial::Serial *baseStation;
std::thread readthread; std::thread readthread;
std::vector<Controller*> controllers {0}; Controller* controllers[4];
//Command functions //Command functions
void commandDebug(std::vector<std::string> data); void commandDebug(std::vector<std::string> data);
+69 -18
View File
@@ -5,23 +5,33 @@
#include <cstring> #include <cstring>
#include "WorldHandler.h" #include "WorldHandler.h"
#include "Player.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::width = 0;
int CrystalPoint::height = 0; int CrystalPoint::height = 0;
SoundSystem CrystalPoint::sound_system; SoundSystem CrystalPoint::sound_system;
bool state = false;
void CrystalPoint::init() void CrystalPoint::init()
{ {
player = Player::getInstance(); player = Player::getInstance();
worldhandler = WorldHandler::getInstance(); worldhandler = WorldHandler::getInstance();
//cursor = Cursor::getInstance(); cursor = Cursor::getInstance();
menu = new Menu();
buildMenu();
lastFrameTime = 0; lastFrameTime = 0;
state = true;
glClearColor(0.7f, 0.7f, 1.0f, 1.0f); 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); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
worldhandler->draw();
player->draw();
worldhandler->draw();
if(!state)
menu->draw();
//cursor->draw(); //cursor->draw();
glutSwapBuffers(); glutSwapBuffers();
@@ -54,21 +65,20 @@ void CrystalPoint::update()
float deltaTime = frameTime - lastFrameTime; float deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime; lastFrameTime = frameTime;
if (state)
{
if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT]) if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
worldhandler->PreviousWorld(); worldhandler->PreviousWorld();
if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT]) if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
worldhandler->NextWorld(); worldhandler->NextWorld();
if (keyboardState.keys[27]) if (keyboardState.keys[27])
exit(0); state = false;
Player* player = Player::getInstance(); Player* player = Player::getInstance();
player->rotation.y += mouseOffset.x / 10.0f; player->rotation.y += mouseOffset.x / 10.0f;
player->rotation.x += mouseOffset.y / 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;
@@ -84,37 +94,56 @@ void CrystalPoint::update()
if (leftcontroller != nullptr) { if (leftcontroller != nullptr) {
Vec2f *leftControllerJoystick = &leftcontroller->joystick; Vec2f *leftControllerJoystick = &leftcontroller->joystick;
if (leftcontroller->joystickButton) { if (leftcontroller->joystickButton) {
controller.rumble(leftcontroller->controllerId, 100, 100); controller.rumble(leftcontroller->controllerId, 100, 100);
} }
if (leftControllerJoystick->y > 0.3) { if (leftControllerJoystick->y > 0.3) {
player->setPosition(270, leftControllerJoystick->y*deltaTime, false); player->setPosition(270, leftControllerJoystick->y * deltaTime, false);
} }
else if (leftControllerJoystick->y < -0.3) { else if (leftControllerJoystick->y < -0.3) {
player->setPosition(90, leftControllerJoystick->y*-1 * deltaTime, false); player->setPosition(90, leftControllerJoystick->y * -1 * deltaTime, false);
} }
if (leftControllerJoystick->x > 0.3) { if (leftControllerJoystick->x > 0.3) {
player->setPosition(180, leftControllerJoystick->x*deltaTime, false); player->setPosition(180, leftControllerJoystick->x * deltaTime, false);
} }
else if (leftControllerJoystick->x < -0.3) { else if (leftControllerJoystick->x < -0.3) {
player->setPosition(0, leftControllerJoystick->x*-1 * deltaTime, false); player->setPosition(0, leftControllerJoystick->x * -1 * deltaTime, false);
} }
player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z)); player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
} }
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;
}
}
if (player->rotation.x > 90)
player->rotation.x = 90;
if (player->rotation.x < -90)
player->rotation.x = -90;
if (!worldhandler->isPlayerPositionValid()) if (!worldhandler->isPlayerPositionValid())
player->position = oldPosition; 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);
}
else
{
menu->update();
cursor->update(cursor->mousePosition + mouseOffset);
}
mousePosition = mousePosition + mouseOffset;
//cursor->update(mousePosition);
mouseOffset = Vec2f(0, 0); mouseOffset = Vec2f(0, 0);
prevKeyboardState = keyboardState; prevKeyboardState = keyboardState;
@@ -123,6 +152,28 @@ void CrystalPoint::update()
sound_system.SetListener(player->position, Vec3f(), Vec3f()); 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() KeyboardState::KeyboardState()
+6
View File
@@ -3,6 +3,8 @@
class WorldHandler; class WorldHandler;
class SoundSystem; class SoundSystem;
class Player; class Player;
class Cursor;
class Menu;
#include "Vector.h" #include "Vector.h"
#include "SoundSystem.h" #include "SoundSystem.h"
#include "ControllerHandler.h" #include "ControllerHandler.h"
@@ -27,6 +29,8 @@ public:
WorldHandler* worldhandler; WorldHandler* worldhandler;
Player* player; Player* player;
ControllerHandler controller; ControllerHandler controller;
Cursor* cursor;
Menu* menu;
static int width, height; static int width, height;
KeyboardState keyboardState; KeyboardState keyboardState;
@@ -39,6 +43,8 @@ public:
static SoundSystem& GetSoundSystem() { return sound_system; } static SoundSystem& GetSoundSystem() { return sound_system; }
private: private:
static SoundSystem sound_system; static SoundSystem sound_system;
void buildMenu();
}; };
+23 -1
View File
@@ -1,5 +1,5 @@
#include "Cursor.h" #include "Cursor.h"
#include <GL\freeglut.h> #include <GL/freeglut.h>
#include <cmath> #include <cmath>
#include "CrystalPoint.h" #include "CrystalPoint.h"
@@ -8,10 +8,13 @@ Cursor* Cursor::instance = NULL;
Cursor::Cursor() Cursor::Cursor()
{ {
enabled = false; enabled = false;
mousePosition = Vec2f(CrystalPoint::width / 2, CrystalPoint::height / 2);
clicked = false;
} }
Cursor::~Cursor() Cursor::~Cursor()
{ {
} }
Cursor* Cursor::getInstance(void) Cursor* Cursor::getInstance(void)
@@ -54,5 +57,24 @@ void Cursor::draw(void)
void Cursor::update(Vec2f newPosition) 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; mousePosition = newPosition;
if (clicked)
clicked = !clicked;
if (state != prev)
if(state == GLUT_UP)
clicked = true;
prev = state;
} }
+6 -2
View File
@@ -8,9 +8,9 @@ private:
static Cursor* instance; static Cursor* instance;
bool enabled; bool enabled;
Vec2f mousePosition;
public:
public:
Vec2f mousePosition;
~Cursor(); ~Cursor();
static Cursor* getInstance(void); static Cursor* getInstance(void);
@@ -18,6 +18,10 @@ public:
void enable(bool enable); void enable(bool enable);
bool isEnabled(void); bool isEnabled(void);
bool clicked;
int state, prev;
void draw(void); void draw(void);
void update(Vec2f newPosition); void update(Vec2f newPosition);
}; };
+13 -9
View File
@@ -2,7 +2,6 @@
#include <cmath> #include <cmath>
#include "Enemy.h" #include "Enemy.h"
#include "Model.h" #include "Model.h"
#include "CrystalPoint.h"
#include <iostream> #include <iostream>
Enemy::Enemy(const std::string &fileName, Enemy::Enemy(const std::string &fileName,
@@ -19,7 +18,8 @@ Enemy::Enemy(const std::string &fileName,
speed = 1; speed = 1;
radius = 10; radius = 10;
hasTarget = false; 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; attack = false;
} }
@@ -72,9 +72,14 @@ void Enemy::collide(const Entity * entity)
void Enemy::update(float delta) void Enemy::update(float delta)
{ {
music->SetPos(position, Vec3f());
if (hasTarget) if (hasTarget)
{ {
if (music->IsPlaying() == false)
{
music->Play();
}
//just 2d walking //just 2d walking
float dx, dz, length; float dx, dz, length;
@@ -98,15 +103,14 @@ void Enemy::update(float delta)
else else
{ {
attack = true; attack = true;
if (music->IsPlaying() == true)
{
// music->Pause();
music->Stop();
}
} }
rotation.y = atan2f(dx, dz) * 180 / M_PI; 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
View File
@@ -3,6 +3,7 @@
#include "Entity.h" #include "Entity.h"
#include <string> #include <string>
#include "Vector.h" #include "Vector.h"
#include "CrystalPoint.h"
class Enemy : public Entity class Enemy : public Entity
{ {
@@ -10,6 +11,8 @@ public:
Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale); Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale);
~Enemy(); ~Enemy();
Sound* music;
bool hasTarget; bool hasTarget;
Vec3f target; Vec3f target;
float speed,radius; float speed,radius;
+2 -1
View File
@@ -1,6 +1,6 @@
#include "HeightMap.h" #include "HeightMap.h"
#include "stb_image.h" #include "stb_image.h"
#include "vector.h" #include "Vector.h"
#include "LevelObject.h" #include "LevelObject.h"
@@ -127,6 +127,7 @@ float HeightMap::GetHeight(float x, float y)
float labda3 = 1 - labda1 - labda2; float labda3 = 1 - labda1 - labda2;
Vertex z = a * labda1 + b * labda2 + c * labda3; Vertex z = a * labda1 + b * labda2 + c * labda3;
// Vertex z = (a * labda1) + (b * labda2) ;
return z.y; return z.y;
} }
+3 -14
View File
@@ -1,13 +1,11 @@
#include "Interface.h" #include "Interface.h"
#include <GL\freeglut.h> #include <GL/freeglut.h>
#include "CrystalPoint.h" #include "CrystalPoint.h"
#include <string> #include <string>
#include "Player.h" #include "Player.h"
#include "Util.h"
//Prototype
void glutBitmapString(std::string str, int x, int y);
Interface::Interface() Interface::Interface()
{ {
@@ -87,7 +85,7 @@ void Interface::draw()
//Text: level //Text: level
glColor4f(1.0f, 1.0f, 0.1f, 1.0); 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++) for (int i = 0; i < maxCrystals; i++)
{ {
@@ -119,12 +117,3 @@ 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
View File
@@ -4,6 +4,11 @@
#include <stdio.h> #include <stdio.h>
#include "Vector.h" #include "Vector.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "Cursor.h"
void configureOpenGL(void); void configureOpenGL(void);
CrystalPoint* app; CrystalPoint* app;
@@ -50,6 +55,17 @@ int main(int argc, char* argv[])
glutPassiveMotionFunc(mousemotion); glutPassiveMotionFunc(mousemotion);
glutMotionFunc(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::height = GLUT_WINDOW_HEIGHT;
CrystalPoint::width = GLUT_WINDOW_WIDTH; CrystalPoint::width = GLUT_WINDOW_WIDTH;
@@ -66,7 +82,8 @@ void configureOpenGL()
glutInitWindowSize(800, 600); glutInitWindowSize(800, 600);
//glutInitWindowPosition(glutGet(GLUT_WINDOW_WIDTH) / 2 - 800/2, glutGet(GLUT_WINDOW_HEIGHT) / 2 - 600/2); //glutInitWindowPosition(glutGet(GLUT_WINDOW_WIDTH) / 2 - 800/2, glutGet(GLUT_WINDOW_HEIGHT) / 2 - 600/2);
glutCreateWindow("Crystal Point"); glutCreateWindow("Crystal Point");
//glutFullScreen(); glutFullScreen();
//Depth testing //Depth testing
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
+59
View File
@@ -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
View File
@@ -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
View File
@@ -0,0 +1,12 @@
#include "MenuElement.h"
MenuElement::MenuElement(Vec2f position)
{
hover = false;
this->position = position;
}
MenuElement::~MenuElement()
{
}
+17
View File
@@ -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
View File
@@ -1,6 +1,5 @@
#include "Model.h" #include "Model.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#include <iostream> #include <iostream>
@@ -8,6 +7,7 @@
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <cstring>
//Prototypes //Prototypes
std::vector<std::string> split(std::string str, std::string sep); 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 = fmax(radius, (center.x - v.x) * (center.x - v.x) + (center.z - v.z) * (center.z - v.z));
radius = sqrt(radius); radius = sqrt(radius);
for each(ObjGroup *group in groups) for (ObjGroup *group : groups)
{ {
Optimise(group); Optimise(group);
} }
@@ -148,7 +148,7 @@ void Model::Optimise(ObjGroup *t)
{ {
for (Face &face : t->faces) 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, 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, normals[vertex.normal].x, normals[vertex.normal].y, normals[vertex.normal].z,
+4
View File
@@ -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 = 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)); 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() Player* Player::getInstance()
@@ -64,4 +67,5 @@ void Player::setPosition(float angle, float fac, bool height)
void Player::draw() { void Player::draw() {
leftWeapon->draw(); leftWeapon->draw();
rightWeapon->draw();
} }
+7 -6
View File
@@ -1,6 +1,7 @@
#include "cmath" #include "cmath"
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include "Util.h"
#include "stb_image.h" #include "stb_image.h"
#include "Skybox.h" #include "Skybox.h"
#include <string> #include <string>
@@ -25,12 +26,12 @@ Skybox::~Skybox()
void Skybox::init() void Skybox::init()
{ {
skybox[SKY_LEFT] = loadTexture(folder + "left.png"); skybox[SKY_LEFT] = Util::loadTexture(folder + "left.png");
skybox[SKY_BACK] = loadTexture(folder + "back.png"); skybox[SKY_BACK] = Util::loadTexture(folder + "back.png");
skybox[SKY_RIGHT] = loadTexture(folder + "right.png"); skybox[SKY_RIGHT] = Util::loadTexture(folder + "right.png");
skybox[SKY_FRONT] = loadTexture(folder + "front.png"); skybox[SKY_FRONT] = Util::loadTexture(folder + "front.png");
skybox[SKY_TOP] = loadTexture(folder + "top.png"); skybox[SKY_TOP] = Util::loadTexture(folder + "top.png");
skybox[SKY_BOTTOM] = loadTexture(folder + "bottom.png"); skybox[SKY_BOTTOM] = Util::loadTexture(folder + "bottom.png");
} }
void Skybox::draw() void Skybox::draw()
+2
View File
@@ -14,6 +14,8 @@ public:
void init(); void init();
void draw(); void draw();
void update(float deltaTime, int, int); void update(float deltaTime, int, int);
GLuint loadTexture(const std::string &fileName); GLuint loadTexture(const std::string &fileName);
}; };
+18 -1
View File
@@ -1,14 +1,22 @@
#include "Sound.h" #include "Sound.h"
#include <iostream> #include <iostream>
#ifdef WIN32
#include <windows.h> #include <windows.h>
#include <al.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): Sound::Sound(const char* inWavPath, bool inLooping):
buffer_id(0), buffer_id(0),
source_id(0), source_id(0),
is_looping(inLooping) is_looping(inLooping)
{ {
const char* path = inWavPath; const char* path = inWavPath;
@@ -137,3 +145,12 @@ void Sound::Stop()
alSourceStop(source_id); alSourceStop(source_id);
} }
bool Sound::IsPlaying()
{
ALenum state;
alGetSourcei(source_id, AL_SOURCE_STATE, &state);
return (state == AL_PLAYING);
}
+2 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "vector.h" #include "Vector.h"
class Sound class Sound
{ {
@@ -13,6 +13,7 @@ public:
void Play(); void Play();
void Pause(); void Pause();
void Stop(); void Stop();
bool IsPlaying();
private: private:
unsigned int buffer_id; unsigned int buffer_id;
-6
View File
@@ -1,11 +1,5 @@
#include "SoundSystem.h" #include "SoundSystem.h"
#include <cstdlib>
#include <iostream>
#include <windows.h>
SoundSystem::SoundSystem(): SoundSystem::SoundSystem():
device(nullptr), device(nullptr),
context(nullptr) context(nullptr)
+8 -1
View File
@@ -1,9 +1,16 @@
#pragma once #pragma once
#include <vector> #include <vector>
#ifdef WIN32
#include <al.h> #include <al.h>
#include <alc.h> #include <alc.h>
#include "vector.h" #else
#include <AL/al.h>
#include <AL/alc.h>
#endif
#include "Vector.h"
#include "Sound.h" #include "Sound.h"
+31
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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); 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); 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); 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); return Vertex(x+other.x, y+other.y, z+other.z, normalX, normalY, normalZ, texX, texY);
} }
+4 -4
View File
@@ -16,9 +16,9 @@ public:
float texX; float texX;
float texY; float texY;
Vertex operator/(float &other); Vertex operator/(const float &other) const;
Vertex operator*(Vertex &other); Vertex operator*(const Vertex &other) const;
Vertex operator*(float &other); Vertex operator*(const float &other) const;
Vertex operator+(Vertex &other); Vertex operator+(const Vertex &other) const;
}; };
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+2 -2
View File
@@ -71,14 +71,14 @@ void Weapon::draw(){
weaponmodel->draw(); weaponmodel->draw();
//Test code for finding anker point //Test code for finding anker point
/* glColor3ub(255, 255, 0); glColor3ub(255, 255, 0);
glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z); glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2f(0, 4); glVertex2f(0, 4);
glVertex2f(0, -4); glVertex2f(0, -4);
glVertex2f(4, 0); glVertex2f(4, 0);
glVertex2f(-4, 0); glVertex2f(-4, 0);
glEnd();*/ glEnd();
glPopMatrix(); glPopMatrix();
+14 -5
View File
@@ -3,7 +3,6 @@
#include "Entity.h" #include "Entity.h"
#include "json.h" #include "json.h"
#include "Model.h" #include "Model.h"
#include "CrystalPoint.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
@@ -179,9 +178,7 @@ World::World(const std::string &fileName):
if (!v["world"]["music"].isNull()) if (!v["world"]["music"].isNull())
{ {
music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true); music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id); music = CrystalPoint::GetSoundSystem().GetSound(music_id);
music->SetPos(Vec3f(), Vec3f());
music->Play();
} }
if (!v["portal"].isNull()) if (!v["portal"].isNull())
@@ -214,7 +211,10 @@ World::World(const std::string &fileName):
World::~World() World::~World()
{ {
delete heightmap; delete heightmap;
music->Stop();
delete music;
delete skybox; delete skybox;
delete portal;
} }
std::pair<std::string, bool> World::getObjectFromValue(int val) std::pair<std::string, bool> World::getObjectFromValue(int val)
@@ -235,7 +235,7 @@ float World::getHeight(float x, float y)
void World::draw() void World::draw()
{ {
player->setCamera();
float lightPosition[4] = { 0, 2, 1, 0 }; float lightPosition[4] = { 0, 2, 1, 0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
@@ -244,6 +244,9 @@ void World::draw()
skybox->draw(); skybox->draw();
player->setCamera();
player->draw();
heightmap->Draw(); heightmap->Draw();
for (auto &enemy : enemies) for (auto &enemy : enemies)
@@ -257,6 +260,12 @@ void World::draw()
void World::update(float elapsedTime) void World::update(float elapsedTime)
{ {
music->SetPos(player->position, Vec3f());
if (music->IsPlaying() == false)
{
music->Play();
}
for (auto &entity : entities) for (auto &entity : entities)
entity->update(elapsedTime); entity->update(elapsedTime);
+2
View File
@@ -8,6 +8,7 @@
#include "Interface.h" #include "Interface.h"
#include "Crystal.h" #include "Crystal.h"
#include "Skybox.h" #include "Skybox.h"
#include "CrystalPoint.h"
#include "Portal.h" #include "Portal.h"
class Entity; class Entity;
@@ -16,6 +17,7 @@ class World
{ {
private: private:
std::vector<std::pair<int, std::pair<std::string, bool>>> objecttemplates; std::vector<std::pair<int, std::pair<std::string, bool>>> objecttemplates;
Sound* music;
Player* player; Player* player;
HeightMap* heightmap; HeightMap* heightmap;
+30 -30
View File
@@ -52,41 +52,41 @@ namespace serial {
/*! /*!
* Enumeration defines the possible bytesizes for the serial port. * Enumeration defines the possible bytesizes for the serial port.
*/ */
typedef enum { typedef enum {
fivebits = 5, fivebits = 5,
sixbits = 6, sixbits = 6,
sevenbits = 7, sevenbits = 7,
eightbits = 8 eightbits = 8
} bytesize_t; } bytesize_t;
/*! /*!
* Enumeration defines the possible parity types for the serial port. * Enumeration defines the possible parity types for the serial port.
*/ */
typedef enum { typedef enum {
parity_none = 0, parity_none = 0,
parity_odd = 1, parity_odd = 1,
parity_even = 2, parity_even = 2,
parity_mark = 3, parity_mark = 3,
parity_space = 4 parity_space = 4
} parity_t; } parity_t;
/*! /*!
* Enumeration defines the possible stopbit types for the serial port. * Enumeration defines the possible stopbit types for the serial port.
*/ */
typedef enum { typedef enum {
stopbits_one = 1, stopbits_one = 1,
stopbits_two = 2, stopbits_two = 2,
stopbits_one_point_five stopbits_one_point_five
} stopbits_t; } stopbits_t;
/*! /*!
* Enumeration defines the possible flowcontrol types for the serial port. * Enumeration defines the possible flowcontrol types for the serial port.
*/ */
typedef enum { typedef enum {
flowcontrol_none = 0, flowcontrol_none = 0,
flowcontrol_software, flowcontrol_software,
flowcontrol_hardware flowcontrol_hardware
} flowcontrol_t; } flowcontrol_t;
/*! /*!
* Structure for setting the timeout of the serial port, times are * Structure for setting the timeout of the serial port, times are
@@ -94,7 +94,7 @@ typedef enum {
* *
* In order to disable the interbyte timeout, set it to Timeout::max(). * In order to disable the interbyte timeout, set it to Timeout::max().
*/ */
struct Timeout { struct Timeout {
#ifdef max #ifdef max
# undef max # undef max
#endif #endif
@@ -138,13 +138,13 @@ struct Timeout {
write_timeout_constant(write_timeout_constant_), write_timeout_constant(write_timeout_constant_),
write_timeout_multiplier(write_timeout_multiplier_) write_timeout_multiplier(write_timeout_multiplier_)
{} {}
}; };
/*! /*!
* Class that provides a portable serial port interface. * Class that provides a portable serial port interface.
*/ */
class Serial { class Serial {
public: public:
/*! /*!
* Creates a Serial object and opens the port if a port is specified, * Creates a Serial object and opens the port if a port is specified,
* otherwise it remains closed until serial::Serial::open is called. * otherwise it remains closed until serial::Serial::open is called.
@@ -644,7 +644,7 @@ public:
bool bool
getCD (); getCD ();
private: private:
// Disable copy constructors // Disable copy constructors
Serial(const Serial&); Serial(const Serial&);
Serial& operator=(const Serial&); Serial& operator=(const Serial&);
@@ -664,14 +664,14 @@ private:
size_t size_t
write_ (const uint8_t *data, size_t length); write_ (const uint8_t *data, size_t length);
}; };
class SerialException : public std::exception class SerialException : public std::exception
{ {
// Disable copy constructors // Disable copy constructors
SerialException& operator=(const SerialException&); SerialException& operator=(const SerialException&);
std::string e_what_; std::string e_what_;
public: public:
SerialException (const char *description) { SerialException (const char *description) {
std::stringstream ss; std::stringstream ss;
ss << "SerialException " << description << " failed."; ss << "SerialException " << description << " failed.";
@@ -682,17 +682,17 @@ public:
virtual const char* what () const throw () { virtual const char* what () const throw () {
return e_what_.c_str(); return e_what_.c_str();
} }
}; };
class IOException : public std::exception class IOException : public std::exception
{ {
// Disable copy constructors // Disable copy constructors
IOException& operator=(const IOException&); IOException& operator=(const IOException&);
std::string file_; std::string file_;
int line_; int line_;
std::string e_what_; std::string e_what_;
int errno_; int errno_;
public: public:
explicit IOException (std::string file, int line, int errnum) explicit IOException (std::string file, int line, int errnum)
: file_(file), line_(line), errno_(errnum) { : file_(file), line_(line), errno_(errnum) {
std::stringstream ss; std::stringstream ss;
@@ -721,14 +721,14 @@ public:
virtual const char* what () const throw () { virtual const char* what () const throw () {
return e_what_.c_str(); return e_what_.c_str();
} }
}; };
class PortNotOpenedException : public std::exception class PortNotOpenedException : public std::exception
{ {
// Disable copy constructors // Disable copy constructors
const PortNotOpenedException& operator=(PortNotOpenedException); const PortNotOpenedException& operator=(PortNotOpenedException);
std::string e_what_; std::string e_what_;
public: public:
PortNotOpenedException (const char * description) { PortNotOpenedException (const char * description) {
std::stringstream ss; std::stringstream ss;
ss << "PortNotOpenedException " << description << " failed."; ss << "PortNotOpenedException " << description << " failed.";
@@ -739,12 +739,12 @@ public:
virtual const char* what () const throw () { virtual const char* what () const throw () {
return e_what_.c_str(); return e_what_.c_str();
} }
}; };
/*! /*!
* Structure that describes a serial device. * Structure that describes a serial device.
*/ */
struct PortInfo { struct PortInfo {
/*! Address of the serial port (this can be passed to the constructor of Serial). */ /*! Address of the serial port (this can be passed to the constructor of Serial). */
std::string port; std::string port;
@@ -755,7 +755,7 @@ struct PortInfo {
/*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */ /*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */
std::string hardware_id; std::string hardware_id;
}; };
/* Lists the serial ports available on the system /* Lists the serial ports available on the system
* *
@@ -764,8 +764,8 @@ struct PortInfo {
* *
* \return vector of serial::PortInfo. * \return vector of serial::PortInfo.
*/ */
std::vector<PortInfo> std::vector<PortInfo>
list_ports(); list_ports();
} // namespace serial } // namespace serial
+1
View File
@@ -3,6 +3,7 @@
"heightmap": "worlds/rockHeightmap.png", "heightmap": "worlds/rockHeightmap.png",
"texture": "worlds/rockStone2.png", "texture": "worlds/rockStone2.png",
"skybox": "skyboxes/water/", "skybox": "skyboxes/water/",
"music": "WAVE/test2.wav",
"object-templates": [ "object-templates": [
{ {
"color": 50, "color": 50,
+1 -1
View File
@@ -9,7 +9,7 @@
"collision": true "collision": true
} }
], ],
"music": "WAVE/bond.wav" "music": "WAVE/test1.wav"
}, },
"player": { "player": {
"startposition": [ 20, 5, 20 ] "startposition": [ 20, 5, 20 ]