diff --git a/CMakeLists.txt b/CMakeLists.txt index 180810f..abebe75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ 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") \ No newline at end of file diff --git a/CrystalPoint.vcxproj b/CrystalPoint.vcxproj index e6702fa..71e2d64 100644 --- a/CrystalPoint.vcxproj +++ b/CrystalPoint.vcxproj @@ -211,12 +211,14 @@ + - - + + + diff --git a/CrystalPoint.vcxproj.filters b/CrystalPoint.vcxproj.filters index 7368ed2..010dfe9 100644 --- a/CrystalPoint.vcxproj.filters +++ b/CrystalPoint.vcxproj.filters @@ -195,12 +195,18 @@ Source Files\json + + Source Files\json + - + Resource Files - + + Resource Files + + Resource Files diff --git a/Cursor.cpp b/Cursor.cpp index 2e92a05..d5ce794 100644 --- a/Cursor.cpp +++ b/Cursor.cpp @@ -1,5 +1,5 @@ #include "Cursor.h" -#include +#include #include #include "CrystalPoint.h" diff --git a/Enemy.cpp b/Enemy.cpp index 3c802b2..36c3f44 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -2,7 +2,6 @@ #include #include "Enemy.h" #include "Model.h" -#include "CrystalPoint.h" #include 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(); - } } \ No newline at end of file diff --git a/Enemy.h b/Enemy.h index 4908738..d1376c8 100644 --- a/Enemy.h +++ b/Enemy.h @@ -3,6 +3,7 @@ #include "Entity.h" #include #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; diff --git a/HeightMap.cpp b/HeightMap.cpp index 99e4924..61aef40 100644 --- a/HeightMap.cpp +++ b/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; } diff --git a/Interface.cpp b/Interface.cpp index c3afebc..0f912de 100644 --- a/Interface.cpp +++ b/Interface.cpp @@ -1,5 +1,5 @@ #include "Interface.h" -#include +#include #include "CrystalPoint.h" #include diff --git a/Model.cpp b/Model.cpp index e280775..8fa61d5 100644 --- a/Model.cpp +++ b/Model.cpp @@ -137,7 +137,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); } @@ -147,7 +147,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, diff --git a/Sound.cpp b/Sound.cpp index cc990da..a3cc983 100644 --- a/Sound.cpp +++ b/Sound.cpp @@ -1,14 +1,22 @@ #include "Sound.h" #include +#ifdef WIN32 #include #include +#else +#include +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); +} + diff --git a/Sound.h b/Sound.h index 74e2f9f..02fe227 100644 --- a/Sound.h +++ b/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; diff --git a/SoundSystem.cpp b/SoundSystem.cpp index bd207be..f57a526 100644 --- a/SoundSystem.cpp +++ b/SoundSystem.cpp @@ -1,11 +1,5 @@ #include "SoundSystem.h" -#include -#include -#include - - - SoundSystem::SoundSystem(): device(nullptr), context(nullptr) diff --git a/SoundSystem.h b/SoundSystem.h index d6908ce..b9e289b 100644 --- a/SoundSystem.h +++ b/SoundSystem.h @@ -1,9 +1,16 @@ #pragma once #include + +#ifdef WIN32 #include #include -#include "vector.h" +#else +#include +#include +#endif + +#include "Vector.h" #include "Sound.h" diff --git a/Vertex.cpp b/Vertex.cpp index 8afd8a1..35591cf 100644 --- a/Vertex.cpp +++ b/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); } diff --git a/Vertex.h b/Vertex.h index 3411000..ff3b2cc 100644 --- a/Vertex.h +++ b/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; }; diff --git a/WAVE/Sound.wav b/WAVE/Sound.wav deleted file mode 100644 index 74ccfd2..0000000 Binary files a/WAVE/Sound.wav and /dev/null differ diff --git a/WAVE/bond.wav b/WAVE/bond.wav deleted file mode 100644 index c478afe..0000000 Binary files a/WAVE/bond.wav and /dev/null differ diff --git a/WAVE/enemy.wav b/WAVE/enemy.wav new file mode 100644 index 0000000..89e9dae Binary files /dev/null and b/WAVE/enemy.wav differ diff --git a/WAVE/test1.wav b/WAVE/test1.wav new file mode 100644 index 0000000..e8e4fb8 Binary files /dev/null and b/WAVE/test1.wav differ diff --git a/WAVE/test2.wav b/WAVE/test2.wav new file mode 100644 index 0000000..364ff42 Binary files /dev/null and b/WAVE/test2.wav differ diff --git a/World.cpp b/World.cpp index 4691557..5df11dc 100644 --- a/World.cpp +++ b/World.cpp @@ -3,7 +3,6 @@ #include "Entity.h" #include "json.h" #include "Model.h" -#include "CrystalPoint.h" #include #include #include @@ -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,6 +211,8 @@ World::World(const std::string &fileName): World::~World() { delete heightmap; + music->Stop(); + delete music; delete skybox; delete portal; } @@ -258,6 +257,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); diff --git a/World.h b/World.h index f12d1e2..a33385f 100644 --- a/World.h +++ b/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>> objecttemplates; + Sound* music; Player* player; HeightMap* heightmap; diff --git a/worlds/rock.json b/worlds/rock.json index 55f5132..08d3311 100644 --- a/worlds/rock.json +++ b/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, diff --git a/worlds/small.json b/worlds/small.json index af4cecf..606c410 100644 --- a/worlds/small.json +++ b/worlds/small.json @@ -9,7 +9,7 @@ "collision": true } ], - "music": "WAVE/bond.wav" + "music": "WAVE/test1.wav" }, "player": { "startposition": [ 20, 5, 20 ]