Skip to content

Commit

Permalink
Add sound mute button
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Jan 20, 2024
1 parent 020d419 commit da03922
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Build Product
run: |
cd ${{ env.PROJECT_NAME }}
cmake -B build
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
shell: bash

Expand Down
1 change: 1 addition & 0 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ void Game::set_room(const Room::Type &room_type) noexcept
{
current_music = station_music[GetRandomValue(0, station_music.size() - 1)];
PlayMusicStream(current_music);
SetMusicVolume(current_music, music_volume);
}

room = Room::get(room_type);
Expand Down
33 changes: 29 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ void Game::update()
gui->messages.pop_front();
}

if (IsKeyPressed(KEY_M))
{
static bool music_enabled = true;
static float previous_music_volume = music_volume;
static float previous_sound_volume = SoundManager::volume;

music_enabled = !music_enabled;
if (music_enabled)
{
music_volume = previous_music_volume;
SoundManager::volume = previous_sound_volume;
}
else
{
previous_music_volume = music_volume;
music_volume = 0.0f;

previous_sound_volume = SoundManager::volume;
SoundManager::volume = 0.0f;
}

SetMusicVolume(current_music, music_volume);
}

frame++;
}

Expand Down Expand Up @@ -341,7 +365,7 @@ void Game::update_game()
{
if (IsKeyPressed(KEY_F1) && asteroids)
{
for (size_t i = 0; i < NUMBER_OF_ASTEROIDS; i++)
for (size_t i = 0; i < 10; i++)
{
const Vector2 position = { static_cast<float>(GetRandomValue(0, width)),
static_cast<float>(GetRandomValue(0, height)) };
Expand Down Expand Up @@ -508,10 +532,10 @@ void Game::set_state(GameState new_state) noexcept
{
state = new_state;

bullets = std::make_unique<ObjectCircularBuffer<Bullet, 128>>();
asteroids = std::make_unique<ObjectCircularBuffer<Asteroid, 2048>>();
bullets = std::make_unique<ObjectCircularBuffer<Bullet, 64>>();
asteroids = std::make_unique<ObjectCircularBuffer<Asteroid, 1024>>();
particles = std::make_unique<ObjectCircularBuffer<Particle, 4096>>();
pickables = std::make_unique<ObjectCircularBuffer<Pickable, 1024>>();
pickables = std::make_unique<ObjectCircularBuffer<Pickable, 512>>();
survive_time = 0.0f;

switch (state)
Expand All @@ -520,6 +544,7 @@ void Game::set_state(GameState new_state) noexcept
{
current_music = asteroid_music[GetRandomValue(0, asteroid_music.size() - 1)];
PlayMusicStream(current_music);
SetMusicVolume(current_music, music_volume);

assert(!missions.empty());
assert(current_mission < missions.size());
Expand Down
8 changes: 4 additions & 4 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,19 @@ class Game
std::unique_ptr<Player> player;
std::shared_ptr<Room> room;
std::unique_ptr<Sprite> tileset_sprite;
std::unique_ptr<ObjectCircularBuffer<Bullet, 128>> bullets;
std::unique_ptr<ObjectCircularBuffer<Asteroid, 2048>> asteroids;
std::unique_ptr<ObjectCircularBuffer<Bullet, 64>> bullets;
std::unique_ptr<ObjectCircularBuffer<Asteroid, 1024>> asteroids;
std::unique_ptr<ObjectCircularBuffer<Particle, 4096>> particles;
std::unique_ptr<ObjectCircularBuffer<Pickable, 1024>> pickables;
std::unique_ptr<ObjectCircularBuffer<Pickable, 512>> pickables;

std::vector<Music> station_music;
std::vector<Music> asteroid_music;
Music current_music;

static constexpr int width = 480;
static constexpr int height = 270;
static constexpr int NUMBER_OF_ASTEROIDS = 6;
static Config config;
float music_volume { 0.7f };
static uint64_t frame;

void init();
Expand Down
12 changes: 6 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
#include "render_pass.hpp"
#include "utils.hpp"

const int AUDIO_BUFFER_SIZE = (4096 * 12);
const constexpr int AUDIO_BUFFER_SIZE = (4096 * 12);
const constexpr size_t MAX_UPDATE_STEPS = 5;

const int window_width = Game::width * 2;
const int window_height = Game::height * 2;
const constexpr int window_width = Game::width * 2;
const constexpr int window_height = Game::height * 2;

const bool integer_scaling = false;
const constexpr bool integer_scaling = false;

static std::unique_ptr<RenderPass> game_render_pass;
static std::unique_ptr<RenderPass> ui_render_pass;
Expand All @@ -44,13 +45,12 @@ void update_draw_frame()
render_destination.height = Game::height * scale;

const float interval = DELTA_TIME;
size_t steps = 6;
const float dt = GetFrameTime();

static float accumulator = 0.0f;
accumulator += dt;

while (accumulator >= interval)
for (size_t steps = 0; accumulator >= interval && steps < MAX_UPDATE_STEPS; ++steps)
{
accumulator -= interval;

Expand Down

0 comments on commit da03922

Please sign in to comment.