diff --git a/SQCSim-common/player.cpp b/SQCSim-common/player.cpp index 845b96f..2a14091 100644 --- a/SQCSim-common/player.cpp +++ b/SQCSim-common/player.cpp @@ -4,14 +4,15 @@ Player::Player(const Vector3f& position, float rotX, float rotY) : m_position(position), m_rotX(rotX), m_rotY(rotY) { m_velocity = Vector3f(0, 0, 0); m_airborne = true; - m_hp = 1.0f; //TODO: Remettre � 1.0f + m_hp = 1.0f; + m_sensitivity = 0.5f; m_username = "Zelda Bee-Bop56"; } Player::~Player() {} -void Player::TurnLeftRight(float value) { - m_rotY += value; +void Player::TurnLeftRight(float value, float sensitivity) { + m_rotY += value * sensitivity; if (m_rotY > 360) m_rotY = 0; else if (m_rotY < -360) m_rotY = 0; @@ -25,8 +26,8 @@ void Player::TurnLeftRight(float value) { m_direction.Normalize(); } -void Player::TurnTopBottom(float value) { - m_rotX += value; +void Player::TurnTopBottom(float value, float sensitivity) { + m_rotX += value * sensitivity; if (m_rotX > 80) m_rotX = 80; else if (m_rotX < -80) m_rotX = -80; @@ -78,7 +79,7 @@ Vector3f Player::GetInput(bool front, bool back, bool left, bool right, bool jum } if (shoot) // Recoil! - TurnTopBottom(-1); + TurnTopBottom(-1, 1.0f); return delta; } @@ -270,6 +271,18 @@ Vector3f Player::GetDirection() const { return m_direction; } std::string Player::GetUsername() const { return m_username; } +void Player::SetUsername(std::string username) { + m_username = username; +} + +float Player::GetSensitivity() const { + return m_sensitivity; +} + +void Player::SetSensitivity(float sensitivity) { + m_sensitivity = sensitivity; +} + float Player::GetHP() const { return m_hp; } void Player::SetHP(float hp) { diff --git a/SQCSim-common/player.h b/SQCSim-common/player.h index b9a4211..3a52e92 100644 --- a/SQCSim-common/player.h +++ b/SQCSim-common/player.h @@ -16,8 +16,8 @@ public: Player(const Vector3f& position, float rotX = 0, float rotY = 0); ~Player(); - void TurnLeftRight(float value); - void TurnTopBottom(float value); + void TurnLeftRight(float value, float sensitivity); + void TurnTopBottom(float value, float sensitivity); Vector3f GetInput(bool front, bool back, bool left, bool right, bool jump, bool dash, float elapsedTime); Sound ApplyPhysics(Vector3f input, World* world, float elapsedTime); void GetBooster(Booster boosttype); @@ -32,6 +32,9 @@ public: Vector3f GetVelocity() const; Vector3f GetPOV() const; std::string GetUsername() const; + void SetUsername(std::string username); + float GetSensitivity() const; + void SetSensitivity(float sensitivity); float GetHP() const; void SetHP(float hp); void Teleport(int& x, int& z); @@ -64,6 +67,7 @@ protected: float timeboostspeed; float timeboostdamage; float timeboostinvincible; + float m_sensitivity; float m_hp; @@ -72,8 +76,6 @@ protected: bool boostdamage; bool boostinvincible; - - Vector3f InterpolatePosition(const Vector3f& vec1, const Vector3f& vec2, const Timestamp& tim1, const Timestamp& tim2, const Timestamp& now); }; #endif //_PLAYER_H__ diff --git a/SQCSim2021/audio.cpp b/SQCSim2021/audio.cpp index 6df3489..97bed1c 100644 --- a/SQCSim2021/audio.cpp +++ b/SQCSim2021/audio.cpp @@ -18,6 +18,9 @@ Audio::Audio(const char * music, const char* menumusic) { m_menumusic = m_engine->play2D(menumusic, true, true, true, irrklang::ESM_STREAMING); m_music->setVolume(.5); m_menumusic->setVolume(.5); + m_mainvolume = 0.5f; + m_engine->setSoundVolume(m_mainvolume); + m_sfxvolume = 0.5f; } Audio::~Audio() { @@ -26,8 +29,11 @@ Audio::~Audio() { if (m_engine) m_engine->drop(); } -void Audio::playSound(const char* sound) { - m_engine->play2D(sound); +void Audio::playSound(const char* name, float volume = 1.) { + irrklang::ISound* sfx = m_engine->play2D(name, false, true); + sfx->setVolume(volume); + sfx->setIsPaused(false); + m_sfxes.push_back(sfx); } void Audio::Update3DAudio(Vector3f pos, Vector3f dir, Vector3f vel) { @@ -37,9 +43,10 @@ void Audio::Update3DAudio(Vector3f pos, Vector3f dir, Vector3f vel) { } irrklang::ISound* Audio::Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, bool is_looped = false, float volume = 1) { - sound = m_engine->play3D(name, irrklang::vec3df(pos.x, pos.y, pos.z), is_looped, false, true, is_looped? irrklang::ESM_STREAMING: irrklang::ESM_NO_STREAMING, true); + sound = m_engine->play3D(name, irrklang::vec3df(pos.x, pos.y, pos.z), is_looped, true, true, is_looped? irrklang::ESM_STREAMING: irrklang::ESM_NO_STREAMING, true); sound->setVelocity(irrklang::vec3df(vel.x, vel.y, vel.z)); sound->setVolume(volume); + sound->setIsPaused(false); return sound; } @@ -53,6 +60,17 @@ void Audio::Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& v // m_music = m_engine->play2D(music, false, false, false, irrklang::ESM_STREAMING); //} +void Audio::CleanupSFX() { + while (!m_sfxes.empty()) { + irrklang::ISound* sfx = m_sfxes.back(); + if (sfx->isFinished()) { + sfx->drop(); // drop() fait deja la job du delete sfx. + } + else break; + m_sfxes.pop_back(); + } +} + void Audio::ToggleMusicState(GameState state) { if (m_music_on) { switch (state) { @@ -94,3 +112,24 @@ void Audio::PauseEngine() { m_engine->setAllSoundsPaused(); } float Audio::GetMusicVolume() const { return m_music->getVolume(); } + +void Audio::SetMusicVolume(float volume) { + m_music->setVolume(volume); + m_menumusic->setVolume(volume); +} + +float Audio::GetMainVolume() const { + return m_engine->getSoundVolume(); +} + +void Audio::SetMainVolume(float volume) { + m_engine->setSoundVolume(volume); +} + +float Audio::GetSfxVolume() const { + return m_sfxvolume; +} + +void Audio::SetSfxVolume(float volume) { + m_sfxvolume = volume; +} diff --git a/SQCSim2021/audio.h b/SQCSim2021/audio.h index 28fbee5..008cd62 100644 --- a/SQCSim2021/audio.h +++ b/SQCSim2021/audio.h @@ -16,7 +16,10 @@ class Audio { private: irrklang::ISound* m_music; irrklang::ISound* m_menumusic; + float m_mainvolume; + float m_sfxvolume; bool m_music_on = true; + std::vector m_sfxes; public: Audio(); @@ -26,22 +29,32 @@ public: void Update3DAudio(Vector3f pos, Vector3f dir, Vector3f speed); + irrklang::ISound* Create3DAudioObj(irrklang::ISound* sound, const char* name, Vector3f pos, Vector3f vel, bool is_looped, float volume); void Render3DAudioObj(irrklang::ISound* sound, Vector3f& pos, Vector3f& vel, float volume); //void PlaySong(const char* music); + void CleanupSFX(); + void ToggleMusicState(GameState state); - void playSound(const char* sound); + void playSound(const char* sound, float volume); void SetMusic(bool ison, GameState state); bool GetMusic(); void PauseEngine(); + float GetMainVolume() const; + void SetMainVolume(float volume); + float GetMusicVolume() const; + void SetMusicVolume(float volume); + + float GetSfxVolume() const; + void SetSfxVolume(float volume); }; #endif // AUDIO_H__ diff --git a/SQCSim2021/define.h b/SQCSim2021/define.h index d0382a7..ca3c4f7 100644 --- a/SQCSim2021/define.h +++ b/SQCSim2021/define.h @@ -25,8 +25,8 @@ #define BULLET_UPDATES_PER_FRAME 20 -#define BASE_WIDTH 640 -#define BASE_HEIGHT 480 +#define BASE_WIDTH 1280 +#define BASE_HEIGHT 720 #define ANIME_PATH_JUMP "./media/textures/AssetOtherPlayer/FinalPNGJumping/" @@ -58,7 +58,7 @@ enum Resolution { HD = 0, // 1280x720 (High Definition) FHD, // 1920x1080 (Full HD) QHD, // 2560x1440 (Quad HD) - UHD // 3840x2160 (Ultra HD) + UHD, // 3840x2160 (Ultra HD) }; #endif // DEFINE_H__ diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index e174ac6..f504c3e 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -45,7 +45,6 @@ void Engine::Init() { gluPerspective(45.0f, (float)Width() / (float)Height(), 0.1f, VIEW_DISTANCE); glShadeModel(GL_SMOOTH); - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glDisable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -167,7 +166,6 @@ void Engine::LoadResource() { TextureAtlas::TextureIndex StillQuarterBackRightShootFire = m_animeAtlas.AddTexture(ANIM_PATH_SSHOOT2 "BlueBackRightShootingRightShoot1.png"); ////23 TextureAtlas::TextureIndex StillBackShootFire = m_animeAtlas.AddTexture(ANIM_PATH_SSHOOT2 "BlueShootingBackRightShoot1.png"); ////24 - //JUMP TextureAtlas::TextureIndex JumpFront = m_animeAtlas.AddTexture(ANIME_PATH_JUMP "BlueFrontJumpRight.png"); ////25 TextureAtlas::TextureIndex JumpQuarterFrontLeft = m_animeAtlas.AddTexture(ANIME_PATH_JUMP "BlueLeftFrontJumpLeft.png"); ////26 @@ -178,7 +176,6 @@ void Engine::LoadResource() { TextureAtlas::TextureIndex JumpQuarterBackRight = m_animeAtlas.AddTexture(ANIME_PATH_JUMP "BlueRightBackJumpRight.png"); ////31 TextureAtlas::TextureIndex JumpBack = m_animeAtlas.AddTexture(ANIME_PATH_JUMP "BlueBackJumpRight.png"); ////32 - //SHOOTINGJUMP SANS TIRER TextureAtlas::TextureIndex JumpFrontShoot = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT1 "BlueFrontJumpRightShootingRight.png"); ////33 TextureAtlas::TextureIndex JumpQuarterFrontLeftShoot = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT1 "BlueFrontLeftJumpLeftShootingLeft.png"); ////34 @@ -189,7 +186,6 @@ void Engine::LoadResource() { TextureAtlas::TextureIndex JumpQuarterBackRightShoot = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT1 "BlueBackRightJumpRightShootingRight.png"); ////39 TextureAtlas::TextureIndex JumpBackShoot = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT1 "BlueBackJumpRightShootingRight.png"); ////40 - //SHOOTINGJUMP TIRER TextureAtlas::TextureIndex JumpFrontShootFire = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT2 "BlueFrontJumpRightShootingRightShoot1.png"); ////41 TextureAtlas::TextureIndex JumpQuarterFrontLeftShootFire = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT2 "BlueFrontLeftJumpLeftShootingLeftShoot1.png"); ////42 @@ -200,8 +196,6 @@ void Engine::LoadResource() { TextureAtlas::TextureIndex JumpQuarterBackRightShootFire = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT2 "BlueBackRightJumpRightShootingRightShoot1.png"); ////47 TextureAtlas::TextureIndex JumpBackShootFire = m_animeAtlas.AddTexture(ANIM_PATH_JSHOOT2 "BlueBackJumpRightShootingRightShoot1.png"); ////48 - - if (!m_animeAtlas.Generate(TEXTURE_SIZE, false)) { std::cout << " Unable to generate texture atlas ..." << std::endl; abort(); @@ -244,12 +238,23 @@ void Engine::LoadResource() { std::cout << " Failed to load shader " << std::endl; exit(1); } + + // Settings + m_mainvolume = m_audio.GetMainVolume(); + m_options.SetMainVolume(m_mainvolume); + + m_musicvolume = m_audio.GetMusicVolume(); + m_options.SetMusicVolume(m_musicvolume); + + m_sfxvolume = m_audio.GetSfxVolume(); + m_options.SetSfxVolume(m_sfxvolume); + + m_sensitivity = m_player.GetSensitivity(); + m_options.SetMouseSensitivity(m_sensitivity); } void Engine::UnloadResource() {} - - void Engine::InstantDamage() { m_player.InflictDamage(0.10f); m_damage = false; @@ -721,6 +726,7 @@ void Engine::DisplayLobbyMenu(float elapsedTime) { GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); + glDisable(GL_STENCIL_TEST); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); @@ -732,6 +738,7 @@ void Engine::DisplayLobbyMenu(float elapsedTime) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); m_textureLobbyMenu.Bind(); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(0, 0); @@ -740,7 +747,6 @@ void Engine::DisplayLobbyMenu(float elapsedTime) { glTexCoord2f(0, 1); glVertex2i(0, Height()); glEnd(); - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); if (m_settingUsername) { SetPlayerUsername(elapsedTime); @@ -749,9 +755,20 @@ void Engine::DisplayLobbyMenu(float elapsedTime) { SetServerAddress(elapsedTime); } + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + m_textureOptBack.Bind(); + glBegin(GL_QUADS); + glTexCoord2f(0, 0); glVertex2i(Width() * 0.675, Height() * 0.15); + glTexCoord2f(1, 0); glVertex2i(Width() * 0.9, Height() * 0.15); + glTexCoord2f(1, 1); glVertex2i(Width() * 0.9, Height() * 0.225); + glTexCoord2f(0, 1); glVertex2i(Width() * 0.675, Height() * 0.225); + glEnd(); + glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); + glEnable(GL_STENCIL_TEST); glEnable(GL_DEPTH_TEST); + glDisable(GL_BLEND); glMatrixMode(GL_PROJECTION); glPopMatrix(); @@ -760,6 +777,7 @@ void Engine::DisplayLobbyMenu(float elapsedTime) { } void Engine::SetPlayerUsername(float elapsedTime) { + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); m_textureLobbyIdentify.Bind(); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(Width() * 0.6, Height() * 0.75); @@ -771,11 +789,10 @@ void Engine::SetPlayerUsername(float elapsedTime) { glBlendFunc(GL_SRC_ALPHA, GL_ONE); HandlePlayerInput(elapsedTime); glDisable(GL_BLEND); - - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } void Engine::SetServerAddress(float elapsedTime) { + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); m_textureLobbyServer.Bind(); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(Width() * 0.6, Height() * 0.75); @@ -788,7 +805,6 @@ void Engine::SetServerAddress(float elapsedTime) { HandlePlayerInput(elapsedTime); glDisable(GL_BLEND); - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } void Engine::DisplayPauseMenu(float elapsedTime) { @@ -1004,7 +1020,6 @@ void Engine::DisplayOptionsMenu() { glPushMatrix(); m_textureOptionsMenu.Bind(); - glLoadIdentity(); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(0, 0); glTexCoord2f(1, 0); glVertex2i(Width(), 0); @@ -1116,8 +1131,8 @@ void Engine::DisplayAudioMenu(float centerX, float centerY) { float principalTop = centerY + Height() * 0.2f; DrawSliderBackground(centerX, centerY, minBar, maxBar, principalBottom, principalTop); - DrawSlider(centerX, centerY, m_volPrincipal, minBar, maxBar, principalBottom, principalTop); - DisplayBarPercentValue(centerX, centerY, percentPosX, principalBottom, minBar, maxBar, m_volPrincipal); + DrawSlider(centerX, centerY, m_mainvolume, minBar, maxBar, principalBottom, principalTop); + DisplayBarPercentValue(centerX, centerY, percentPosX, principalBottom, minBar, maxBar, m_mainvolume); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -1136,8 +1151,8 @@ void Engine::DisplayAudioMenu(float centerX, float centerY) { float musiqueTop = centerY + Height() * 0.075f; DrawSliderBackground(centerX, centerY, minBar, maxBar, musiqueBottom, musiqueTop); - DrawSlider(centerX, centerY, m_volMusique, minBar, maxBar, musiqueBottom, musiqueTop); - DisplayBarPercentValue(centerX, centerY, percentPosX, musiqueBottom, minBar, maxBar, m_volMusique); + DrawSlider(centerX, centerY, m_musicvolume, minBar, maxBar, musiqueBottom, musiqueTop); + DisplayBarPercentValue(centerX, centerY, percentPosX, musiqueBottom, minBar, maxBar, m_musicvolume); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -1156,8 +1171,8 @@ void Engine::DisplayAudioMenu(float centerX, float centerY) { float effectsTop = centerY - Height() * 0.05f; DrawSliderBackground(centerX, centerY, minBar, maxBar, effectsBottom, effectsTop); - DrawSlider(centerX, centerY, m_volEffets, minBar, maxBar, effectsBottom, effectsTop); - DisplayBarPercentValue(centerX, centerY, percentPosX, effectsBottom, minBar, maxBar, m_volEffets); + DrawSlider(centerX, centerY, m_sfxvolume, minBar, maxBar, effectsBottom, effectsTop); + DisplayBarPercentValue(centerX, centerY, percentPosX, effectsBottom, minBar, maxBar, m_sfxvolume); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } @@ -1175,10 +1190,10 @@ void Engine::DisplayGraphicsMenu(float centerX, float centerY) { glColor4f(1.0f, 1.0f, 1.0f, 0.5f); glBindTexture(GL_TEXTURE_2D, 0); glBegin(GL_QUADS); - glTexCoord2f(0, 0); glVertex2i(centerX - Width() * 0.16, centerY + Height() * 0.165); - glTexCoord2f(1, 0); glVertex2i(centerX + Width() * 0.1, centerY + Height() * 0.165); - glTexCoord2f(1, 1); glVertex2i(centerX + Width() * 0.1, centerY - Height() * 0.04); - glTexCoord2f(0, 1); glVertex2i(centerX - Width() * 0.16, centerY - Height() * 0.04); + glTexCoord2f(0, 0); glVertex2i(Width() * 0.34, Height() * 0.46); + glTexCoord2f(1, 0); glVertex2i(Width() * 0.6, Height() * 0.46); + glTexCoord2f(1, 1); glVertex2i(Width() * 0.6, Height() * 0.67); + glTexCoord2f(0, 1); glVertex2i(Width() * 0.34, Height() * 0.67); glEnd(); glColor4f(0.0f, 0.0f, 0.0f, 1.0f); @@ -1244,6 +1259,7 @@ void Engine::DisplayGraphicsMenu(float centerX, float centerY) { void Engine::Render(float elapsedTime) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + m_audio.CleanupSFX(); if (m_gamestate == GameState::SPLASH) { if (m_splashTime > 0.0f) { @@ -1281,6 +1297,11 @@ void Engine::Render(float elapsedTime) { m_gamestate = GameState::MAIN_MENU; } } + else if (m_singleReady) { + m_gamestate = GameState::PLAY; + m_audio.ToggleMusicState(m_gamestate); + m_startTime = std::chrono::high_resolution_clock::now(); + } return; } @@ -1332,11 +1353,11 @@ void Engine::Render(float elapsedTime) { if (leftright) vstep = Vector3f(m_player.GetPosition().x + m_player.GetDirection().z, m_player.GetPosition().y - 1.7f, m_player.GetPosition().z + m_player.GetDirection().x); else vstep = Vector3f(m_player.GetPosition().x - m_player.GetDirection().z, m_player.GetPosition().y - 1.7f, m_player.GetPosition().z - m_player.GetDirection().x); - m_audio.Create3DAudioObj(step, AUDIO_PATH "step.wav", vstep, m_player.GetVelocity(), false, .8f); + m_audio.Create3DAudioObj(step, AUDIO_PATH "step.wav", vstep, m_player.GetVelocity(), false, m_sfxvolume); leftright = !leftright; break; case Player::Sound::FALL: - m_audio.Create3DAudioObj(step, AUDIO_PATH "hit.wav", m_player.GetPosition(), m_player.GetVelocity(), false, 1.f); + m_audio.Create3DAudioObj(step, AUDIO_PATH "hit.wav", m_player.GetPosition(), m_player.GetVelocity(), false, m_sfxvolume); break; default: break; } @@ -1365,7 +1386,7 @@ void Engine::Render(float elapsedTime) { m_bullets[0] = new Bullet(m_player.GetPOV() + m_player.GetDirection(), m_player.GetDirection()); } bulletTime = BULLET_TIME; - m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", m_player.GetPOV(), m_player.GetDirection() * 10, false, .5f); + m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", m_player.GetPOV(), m_player.GetDirection() * 10, false, m_sfxvolume); if (m_flash) { // Coupe le rendering et affiche un frame blanc, pour simuler un flash. glClearColor(.8f, .8f, .8f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); @@ -1419,7 +1440,7 @@ void Engine::Render(float elapsedTime) { } static bool died = false; if ((m_player.GetPosition().y < -1.7f || m_player.AmIDead()) && !died) { - m_audio.Create3DAudioObj(m_scream, AUDIO_PATH "scream.wav", m_player.GetPOV(), m_player.GetVelocity(), false, 1.f); + m_audio.Create3DAudioObj(m_scream, AUDIO_PATH "scream.wav", m_player.GetPOV(), m_player.GetVelocity(), false, m_sfxvolume); died = true; } if (m_player.GetPosition().y < -21.f || died) { @@ -1542,7 +1563,7 @@ void Engine::Render(float elapsedTime) { RemotePlayer* rt = static_cast(m_players[out.id]); rt->Feed(out); if (rt->AmIDead()) { - m_audio.Create3DAudioObj(m_scream, AUDIO_PATH "scream.wav", m_player.GetPOV(), m_player.GetVelocity(), false, 1.f); + m_audio.Create3DAudioObj(m_scream, AUDIO_PATH "scream.wav", m_player.GetPOV(), m_player.GetVelocity(), false, m_sfxvolume); } } break; @@ -1572,7 +1593,7 @@ void Engine::Render(float elapsedTime) { m_bullets[0] = bult; break; } - m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", bull.pos, bull.dir, false, 1.f); + m_audio.Create3DAudioObj(m_powpow, AUDIO_PATH "pow.wav", bull.pos, bull.dir, false, m_sfxvolume); } else SystemNotification("Bullet is kraput."); break; @@ -1631,8 +1652,8 @@ void Engine::DisplayGameplayMenu(float centerX, float centerY) { float sensibleTop = centerY + Height() * 0.2f; DrawSliderBackground(centerX, centerY, minBar, maxBar, sensibleBottom, sensibleTop); - DrawSlider(centerX, centerY, m_volSensible, minBar, maxBar, sensibleBottom, sensibleTop); - DisplayBarPercentValue(centerX, centerY, percentPosX, sensibleBottom, minBar, maxBar, m_volSensible); + DrawSlider(centerX, centerY, m_sensitivity, minBar, maxBar, sensibleBottom, sensibleTop); + DisplayBarPercentValue(centerX, centerY, percentPosX, sensibleBottom, minBar, maxBar, m_sensitivity); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); } @@ -1652,14 +1673,22 @@ void Engine::DisplayBarPercentValue(float centerX, float centerY, float posX, fl glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); - float percentage = (value / (maxVal - minVal)) * 100; + if (m_selectedOption == 0) { + if (value < 0.0f) { + value = 0.0f; + } + } + else { + if (value < 0.1f) { + value = 0.1f; + } + } + + if (value > 1.0f) { + value = 1.0f; + } - if (percentage < 0.0f) { - percentage = 0.0f; - } - else if (percentage > 100.0f) { - percentage = 100.0f; - } + float percentage = value * 100; m_textureFont.Bind(); std::ostringstream ss; @@ -1673,19 +1702,31 @@ void Engine::DisplayBarPercentValue(float centerX, float centerY, float posX, fl } void Engine::DrawSlider(float centerX, float centerY, float value, float minVal, float maxVal, float bottomSideValue, float topSideValue) { - if (value < 0.0f) { - value = 0.0f; + float barLength = maxVal - minVal; + + if (m_selectedOption == 0) { + if (value < 0.0f) { + value = 0.0f; + } } - else if (value > (maxVal - minVal)) { - value = (maxVal - minVal); + else { + if (value < 0.1f) { + value = 0.1f; + } } - if (value >= 0.0f && value <= (maxVal - minVal)) { + if (value > 1.0f) { + value = 1.0f; + } + + float scaledLength = minVal + value * barLength; + + if (value >= 0.0f && value <= 1.0f) { glColor4f(0.0f, 1.0f, 0.0f, 1.0f); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2i(minVal, bottomSideValue); - glTexCoord2f(1, 0); glVertex2i(minVal + value, bottomSideValue); - glTexCoord2f(1, 1); glVertex2i(minVal + value, topSideValue); + glTexCoord2f(1, 0); glVertex2i(scaledLength, bottomSideValue); + glTexCoord2f(1, 1); glVertex2i(scaledLength, topSideValue); glTexCoord2f(0, 1); glVertex2i(minVal, topSideValue); glEnd(); } @@ -2070,9 +2111,16 @@ void Engine::HandlePlayerInput(float elapsedTime) { m_currentInputString.size() > 1 && m_currentInputString.size() < 26) { m_username = m_currentInputString; + m_player.SetUsername(m_username); m_currentInputString = ""; m_settingUsername = false; - m_settingServer = true; + + if (m_selectedMultiPlayer) { + m_settingServer = true; + } + else if (m_selectedSinglePlayer) { + m_singleReady = true; + } } } else if (m_settingServer) { @@ -2100,11 +2148,10 @@ void Engine::HandlePlayerInput(float elapsedTime) { PrintText(Width() * 0.6f, Height() * 0.4f, ss.str(), 2.0f); } - void Engine::MouseMoveEvent(int x, int y) { if (m_gamestate == GameState::PLAY) { - m_player.TurnLeftRight(x - (Width() / 2)); - m_player.TurnTopBottom(y - (Height() / 2)); + m_player.TurnLeftRight(x - (Width() / 2), m_sensitivity); + m_player.TurnTopBottom(y - (Height() / 2), m_sensitivity); // Centrer la souris seulement si elle n'est pas d�j� centr�e // Il est n�cessaire de faire la v�rification pour �viter de tomber @@ -2122,18 +2169,24 @@ void Engine::MouseMoveEvent(int x, int y) { float centerY = Height() * 0.5f; float leftBar = centerX - Width() * 0.15f; float rightBar = centerX + Width() * 0.3f; + float barLength = rightBar - leftBar; + + float normalizedPosition = (x - leftBar) / barLength; if (m_selectedOption == 0 && m_selectedOptAudioMainBar) { - m_volPrincipal = x - leftBar; + m_mainvolume = normalizedPosition; + m_audio.SetMainVolume(m_mainvolume); } else if (m_selectedOption == 0 && m_selectedOptAudioMusicBar) { - m_volMusique = x - leftBar; + m_musicvolume = normalizedPosition; + m_audio.SetMusicVolume(m_musicvolume); } else if (m_selectedOption == 0 && m_selectedOptAudioSfxBar) { - m_volEffets = x - leftBar; + m_sfxvolume = normalizedPosition; + m_audio.SetSfxVolume(m_sfxvolume); } else if (m_selectedOption == 2 && m_selectedGameplaySensitivityBar) { - m_volSensible = x - leftBar; + m_sensitivity = normalizedPosition; } } } @@ -2179,19 +2232,23 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomThird = centerY + Height() * 0.198f; if (x > leftButton && x < rightButton && y > bottomFirst && y < topFirst) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); if (m_selectedPlayOptions) { - m_gamestate = GameState::PLAY; - m_audio.ToggleMusicState(m_gamestate); + m_gamestate = GameState::LOBBY; + m_selectedSinglePlayer = true; + m_selectedMultiPlayer = false; + m_settingUsername = true; } else { m_selectedPlayOptions = true; } } else if (x > leftButton && x < rightButton && y > bottomSecond && y < topSecond) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); if (m_selectedPlayOptions) { m_gamestate = GameState::LOBBY; + m_selectedMultiPlayer = true; + m_selectedSinglePlayer = false; m_settingUsername = true; } else { @@ -2199,7 +2256,7 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { } } else if (x > leftButton && x < rightButton && y > bottomThird && y < topThird) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); if (m_selectedPlayOptions) { m_selectedPlayOptions = false; } @@ -2230,25 +2287,26 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomBack = centerY + Height() * 0.2f; if (x > leftAudio && x < rightAudio && y > bottomAudio && y < topAudio) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOption = 0; // Volume } else if (x > leftGraph && x < rightGraph && y > bottomGraph && y < topGraph) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOption = 1; // Graphics } else if (x > leftGameplay && x < rightGameplay && y > bottomGameplay && y < topGameplay) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedOption = 2; // Gameplay } else if (x > leftBack && x < rightBack && y > bottomBack && y < topBack) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_gamestate = GameState::MAIN_MENU; } // Audio float leftBar = centerX - Width() * 0.15f; float rightBar = centerX + Width() * 0.3f; + float barLength = rightBar - leftBar; float topBarPrincipal = centerY - Height() * 0.165f; float bottomBarPrincipal = centerY - Height() * 0.2f; @@ -2259,20 +2317,25 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float topBarEffets = centerY + Height() * 0.085f; float bottomBarEffets = centerY + Height() * 0.05f; + float normalizedPosition = (x - leftBar) / barLength; + if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarPrincipal && y < topBarPrincipal) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_volPrincipal = x - leftBar; m_selectedOptAudioMainBar = true; + m_mainvolume = normalizedPosition; + m_audio.SetMainVolume(m_mainvolume); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); } else if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarMusique && y < topBarMusique) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_volMusique = x - leftBar; m_selectedOptAudioMusicBar = true; + m_musicvolume = normalizedPosition; + m_audio.SetMusicVolume(m_musicvolume); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); } else if (m_selectedOption == 0 && x > leftBar && x < rightBar && y > bottomBarEffets && y < topBarEffets) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_volEffets = x - leftBar; m_selectedOptAudioSfxBar = true; + m_sfxvolume = normalizedPosition; + m_audio.SetSfxVolume(m_sfxvolume); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); } // Resolution @@ -2291,28 +2354,38 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float topChkUHD = centerY + Height() * 0.025f; float bottomChkUHD = centerY; - if (m_selectedOption == 1 && leftChk && x < rightChk && y > bottomChkHD && y < topChkHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_resolution = HD; + if (m_selectedOption == 1) { + if (x > leftChk && x < rightChk && y > bottomChkHD && y < topChkHD) { + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); + ChangeResolution(HD); + } + else if (x > leftChk && x < rightChk && y > bottomChkFHD && y < topChkFHD) { + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); + ChangeResolution(FHD); + } + else if (x > leftChk && x < rightChk && y > bottomChkQHD && y < topChkQHD) { + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); + ChangeResolution(QHD); + } + else if (x > leftChk && x < rightChk && y > bottomChkUHD && y < topChkUHD) { + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); + ChangeResolution(UHD); + } } - else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkFHD && y < topChkFHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_resolution = FHD; + + if (normalizedPosition < 0.1f) { + normalizedPosition = 0.1f; } - else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkQHD && y < topChkQHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_resolution = QHD; - } - else if (m_selectedOption == 1 && x > leftChk && x < rightChk && y > bottomChkUHD && y < topChkUHD) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_resolution = UHD; + else if (normalizedPosition > 1.0f) { + normalizedPosition = 1.0f; } // Gameplay if (m_selectedOption == 2 && x > leftBar && x < rightBar && y > bottomBarPrincipal && y < topBarPrincipal) { - m_audio.playSound(AUDIO_PATH "snap.wav"); - m_volSensible = x - leftBar; + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_selectedGameplaySensitivityBar = true; + m_sensitivity = normalizedPosition; + m_player.SetSensitivity(m_sensitivity); } } else if (m_gamestate == GameState::PAUSE) { @@ -2327,14 +2400,18 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { float bottomMainMenu = centerY - Height() * 0.4415f; if (x > leftResume && x < rightResume && y > bottomResume && y < topResume) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_gamestate = GameState::PLAY; m_audio.ToggleMusicState(m_gamestate); } else if (x > leftMainMenu && x < rightMainMenu && y > bottomMainMenu && y < topMainMenu) { - m_audio.playSound(AUDIO_PATH "snap.wav"); + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); m_gamestate = GameState::MAIN_MENU; m_selectedPlayOptions = false; + m_selectedSinglePlayer = false; + m_selectedMultiPlayer = false; + m_singleReady = false; + m_multiReady = false; m_audio.ToggleMusicState(m_gamestate); if (m_networkgame) { if (m_conn.m_sock_udp) @@ -2344,11 +2421,62 @@ void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y) { } } } + else if (m_gamestate == GameState::LOBBY) { + float leftBack = centerX + Width() * 0.178f; + float rightBack = centerX + Width() * 0.397f; + float topBack = centerY + Height() * 0.346f; + float bottomBack = centerY + Height() * 0.275f; + + if (leftBack && x < rightBack && y > bottomBack && y < topBack) { + m_audio.playSound(AUDIO_PATH "snap.wav", m_sfxvolume); + m_gamestate = GameState::MAIN_MENU; + } + } } void Engine::MouseReleaseEvent(const MOUSE_BUTTON& button, int x, int y) { + float centerX = Width() * 0.5f; + float centerY = Height() * 0.5f; + float leftBar = centerX - Width() * 0.15f; + float rightBar = centerX + Width() * 0.3f; + float barLength = rightBar - leftBar; + + float normalizedPosition = (x - leftBar) / barLength; + + if (normalizedPosition > 1.0f) { + normalizedPosition = 1.0f; + } + switch (button) { case MOUSE_BUTTON_LEFT: + if (m_selectedOption == 0) { + if (normalizedPosition < 0.0f) { + normalizedPosition = 0.0f; + } + + if (m_selectedOptAudioMainBar) { + m_mainvolume = normalizedPosition; + m_audio.SetMainVolume(m_mainvolume); + } + else if (m_selectedOptAudioMusicBar) { + m_musicvolume = normalizedPosition; + m_audio.SetMusicVolume(m_musicvolume); + } + else if (m_selectedOptAudioSfxBar) { + m_sfxvolume = normalizedPosition; + m_audio.SetSfxVolume(m_sfxvolume); + } + } + else { + if (m_selectedGameplaySensitivityBar) { + if (normalizedPosition < 0.1f) { + normalizedPosition = 0.1f; + } + + m_sensitivity = normalizedPosition; + m_player.SetSensitivity(m_sensitivity); + } + } m_mouseL = false; m_block = false; m_selectedOptAudioMainBar = false; @@ -2386,4 +2514,29 @@ bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool use return true; } +void Engine::ChangeResolution(Resolution resolution) { + DeInit(); + + switch (resolution) { + case HD: + OpenglContext::InitWindow(1280, 720); + m_resolution = HD; + break; + case FHD: + OpenglContext::InitWindow(1920, 1080); + m_resolution = FHD; + break; + case QHD: + OpenglContext::InitWindow(2560, 1440); + m_resolution = QHD; + break; + case UHD: + OpenglContext::InitWindow(3840, 2160); + m_resolution = UHD; + break; + } + + Init(); +} + diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index 6e3b33c..1a1303a 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -49,6 +49,7 @@ private: bool StartMultiplayerGame(); bool LoadTexture(Texture& texture, const std::string& filename, bool useMipmaps = true, bool stopOnError = true); + void ChangeResolution(Resolution resolution); void InstantDamage(); void SystemNotification(std::string systemLog); @@ -176,10 +177,10 @@ private: int m_nbReductionChunk = 4; int m_timerReductionChunk = 30; - float m_volPrincipal = 0.0f; - float m_volMusique = 0.0f; - float m_volEffets = 0.0f; - float m_volSensible = 0.0f; + float m_mainvolume; + float m_musicvolume; + float m_sfxvolume; + float m_sensitivity; int m_selectedOption = 0; @@ -208,12 +209,17 @@ private: std::string m_currentInputString; std::string m_username; std::string m_serverAddr; + char m_inputChar = 0; bool m_invalidChar = false; bool m_charChanged = false; bool m_settingUsername = false; bool m_settingServer = false; + bool m_selectedSinglePlayer = false; + bool m_selectedMultiPlayer = false; + bool m_singleReady = false; bool m_multiReady = false; + bool m_key1 = false; bool m_key2 = false; diff --git a/SQCSim2021/openglcontext.h b/SQCSim2021/openglcontext.h index 2718540..3bb711d 100644 --- a/SQCSim2021/openglcontext.h +++ b/SQCSim2021/openglcontext.h @@ -53,9 +53,9 @@ protected: void ShowCrossCursor() const; bool m_istarted = false; + void InitWindow(int width, int height); private: - void InitWindow(int width, int height); MOUSE_BUTTON ConvertMouseButton(sf::Mouse::Button button) const; private: diff --git a/SQCSim2021/settings.cpp b/SQCSim2021/settings.cpp index b84effc..008c4b4 100644 --- a/SQCSim2021/settings.cpp +++ b/SQCSim2021/settings.cpp @@ -9,7 +9,7 @@ Settings::Settings(Audio& audio) m_fullscreen(false), m_brightness(0.5f), m_contrast(0.5f), - m_mouseSensitivity(0.5f) { + m_mouseSensitivity(0.0f) { ApplyResolution(m_resolution); }