From 2ae4b0ff77a2b2be2c485ac3ba6c1ef3facb874c Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Mon, 26 Mar 2018 13:33:49 +0200 Subject: Basic audio system + firing sound. --- src/audio.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/common.hpp | 11 +++++++++++ src/game/assets.cpp | 2 ++ src/game/game.hpp | 6 ++++++ src/game/units.cpp | 5 +++++ 5 files changed, 61 insertions(+) create mode 100644 src/audio.cpp (limited to 'src') diff --git a/src/audio.cpp b/src/audio.cpp new file mode 100644 index 0000000..012c396 --- /dev/null +++ b/src/audio.cpp @@ -0,0 +1,37 @@ +/* +This file is part of Minitrem. + +Minitrem is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +Minitrem is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Minitrem. If not, see . +*/ + +#include "common.hpp" + +namespace audio { + +void sound_t::load(const char *path) +{ + printf("load %s\n", path); + buffer.loadFromFile(path); +} + +static std::list playing_sounds; + +void sound_t::play(void) +{ + auto sound = playing_sounds.emplace(playing_sounds.end()); + sound->setBuffer(buffer); + sound->play(); +} + +} // namespace audio diff --git a/src/common.hpp b/src/common.hpp index 87706c8..846b166 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -25,6 +25,7 @@ along with Minitrem. If not, see . #include #include #include +#include #include "math.hpp" #define COUNT(A) (sizeof(A) / sizeof((A)[0])) @@ -377,6 +378,16 @@ namespace render { }; } +namespace audio { + class sound_t { + sf::SoundBuffer buffer; + + public: + void load(const char *path); + void play(void); + }; +} + extern render::state_t *debug_render; // Divide and round to minus infinity. diff --git a/src/game/assets.cpp b/src/game/assets.cpp index 1aed662..ef7be9b 100644 --- a/src/game/assets.cpp +++ b/src/game/assets.cpp @@ -40,6 +40,8 @@ void load(void) soldier.legs_walking.load("assets/units/soldier/legs_walking", 2, 2); soldier.dead.load("assets/units/soldier/dead_", 1); + soldier.fire.load("assets/units/soldier/fire.ogg"); + spider.idle.load("assets/units/spider/idle", 2, 2, 2); spider.walking.load("assets/units/spider/walking", 2, 2, 2); spider.dead.load("assets/units/spider/dead_", 1); diff --git a/src/game/game.hpp b/src/game/game.hpp index 0143edb..85e3849 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -54,6 +54,8 @@ namespace game { render::animated_texture_t body_panic; render::oriented_sprite_4M2_t legs_idle, legs_walking; render::animated_texture_t dead; + + audio::sound_t fire; } soldier_assets_t; typedef struct { @@ -194,6 +196,10 @@ namespace game { die_t hit_die; } cs; + struct { + audio::sound_t *attack = NULL; + } sounds; + bool dead = false; double death_time = -INFINITY; int health = 1, max_health = 1; diff --git a/src/game/units.cpp b/src/game/units.cpp index d072f20..edb41ec 100644 --- a/src/game/units.cpp +++ b/src/game/units.cpp @@ -221,6 +221,9 @@ void unit_t::try_attack(unit_t *target) size_t hit_roll; size_t dmg_roll; + if (sounds.attack) + sounds.attack->play(); + ss << name << " " << text::get(text::UNIT_ATTACK) << " " << target->name << ": "; hit_roll = game->roll(die_t(20)); @@ -300,6 +303,8 @@ unit_soldier_t::unit_soldier_t(game::state_t *game) : unit_t(game, UNIT_SOLDIER) health = max_health = 20; cs.armor_class = 10; cs.hit_die = die_t(8); + + sounds.attack = &assets::soldier.fire; } void unit_soldier_t::check_area(void) -- cgit