diff options
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | cmake/FindSDL2_Mixer.cmake | 100 | ||||
| -rw-r--r-- | res/bump.wav | bin | 0 -> 70868 bytes | |||
| -rw-r--r-- | src/main.cpp | 8 | ||||
| -rw-r--r-- | src/mixer.cpp | 16 | ||||
| -rw-r--r-- | src/mixer.h | 67 | ||||
| -rw-r--r-- | src/party.cpp | 5 | ||||
| -rw-r--r-- | src/party.h | 4 | ||||
| -rw-r--r-- | src/renderer.h | 2 |
9 files changed, 198 insertions, 7 deletions
| diff --git a/CMakeLists.txt b/CMakeLists.txt index 45c838c..6357cb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -11,10 +11,12 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${tanetane_SOURCE_DIR}/cmake") | |||
| 11 | # Get dependencies. | 11 | # Get dependencies. |
| 12 | find_package(SDL2 REQUIRED) | 12 | find_package(SDL2 REQUIRED) |
| 13 | find_package(SDL2_Image REQUIRED) | 13 | find_package(SDL2_Image REQUIRED) |
| 14 | find_package(SDL2_Mixer REQUIRED) | ||
| 14 | 15 | ||
| 15 | set(ALL_LIBS | 16 | set(ALL_LIBS |
| 16 | ${SDL2_LIBRARY} | 17 | ${SDL2_LIBRARY} |
| 17 | ${SDL2_IMAGE_LIBRARIES} | 18 | ${SDL2_IMAGE_LIBRARIES} |
| 19 | ${SDL2_MIXER_LIBRARIES} | ||
| 18 | tmxlite | 20 | tmxlite |
| 19 | ) | 21 | ) |
| 20 | 22 | ||
| @@ -32,6 +34,7 @@ link_directories( | |||
| 32 | add_executable(tanetane | 34 | add_executable(tanetane |
| 33 | src/main.cpp | 35 | src/main.cpp |
| 34 | src/renderer.cpp | 36 | src/renderer.cpp |
| 37 | src/mixer.cpp | ||
| 35 | src/sprite.cpp | 38 | src/sprite.cpp |
| 36 | src/party.cpp | 39 | src/party.cpp |
| 37 | src/game.cpp | 40 | src/game.cpp |
| diff --git a/cmake/FindSDL2_Mixer.cmake b/cmake/FindSDL2_Mixer.cmake new file mode 100644 index 0000000..1d9778c --- /dev/null +++ b/cmake/FindSDL2_Mixer.cmake | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | # Locate SDL_MIXER library | ||
| 2 | # | ||
| 3 | # This module defines: | ||
| 4 | # | ||
| 5 | # :: | ||
| 6 | # | ||
| 7 | # SDL2_MIXER_LIBRARIES, the name of the library to link against | ||
| 8 | # SDL2_MIXER_INCLUDE_DIRS, where to find the headers | ||
| 9 | # SDL2_MIXER_FOUND, if false, do not try to link against | ||
| 10 | # SDL2_MIXER_VERSION_STRING - human-readable string containing the version of SDL_MIXER | ||
| 11 | # | ||
| 12 | # | ||
| 13 | # | ||
| 14 | # For backward compatibility the following variables are also set: | ||
| 15 | # | ||
| 16 | # :: | ||
| 17 | # | ||
| 18 | # SDLMIXER_LIBRARY (same value as SDL2_MIXER_LIBRARIES) | ||
| 19 | # SDLMIXER_INCLUDE_DIR (same value as SDL2_MIXER_INCLUDE_DIRS) | ||
| 20 | # SDLMIXER_FOUND (same value as SDL2_MIXER_FOUND) | ||
| 21 | # | ||
| 22 | # | ||
| 23 | # | ||
| 24 | # $SDLDIR is an environment variable that would correspond to the | ||
| 25 | # ./configure --prefix=$SDLDIR used in building SDL. | ||
| 26 | # | ||
| 27 | # Created by Eric Wing. This was influenced by the FindSDL.cmake | ||
| 28 | # module, but with modifications to recognize OS X frameworks and | ||
| 29 | # additional Unix paths (FreeBSD, etc). | ||
| 30 | |||
| 31 | #============================================================================= | ||
| 32 | # Copyright 2005-2009 Kitware, Inc. | ||
| 33 | # Copyright 2012 Benjamin Eikel | ||
| 34 | # | ||
| 35 | # Distributed under the OSI-approved BSD License (the "License"); | ||
| 36 | # see accompanying file Copyright.txt for details. | ||
| 37 | # | ||
| 38 | # This software is distributed WITHOUT ANY WARRANTY; without even the | ||
| 39 | # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 40 | # See the License for more information. | ||
| 41 | #============================================================================= | ||
| 42 | # (To distribute this file outside of CMake, substitute the full | ||
| 43 | # License text for the above reference.) | ||
| 44 | |||
| 45 | find_path(SDL2_MIXER_INCLUDE_DIR SDL_mixer.h | ||
| 46 | HINTS | ||
| 47 | ENV SDL2MIXERDIR | ||
| 48 | ENV SDL2DIR | ||
| 49 | PATH_SUFFIXES SDL2 | ||
| 50 | # path suffixes to search inside ENV{SDLDIR} | ||
| 51 | include/SDL2 include | ||
| 52 | PATHS ${SDL2_MIXER_PATH} | ||
| 53 | ) | ||
| 54 | |||
| 55 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) | ||
| 56 | set(VC_LIB_PATH_SUFFIX lib/x64) | ||
| 57 | else() | ||
| 58 | set(VC_LIB_PATH_SUFFIX lib/x86) | ||
| 59 | endif() | ||
| 60 | |||
| 61 | find_library(SDL2_MIXER_LIBRARY | ||
| 62 | NAMES SDL2_mixer | ||
| 63 | HINTS | ||
| 64 | ENV SDL2MIXERDIR | ||
| 65 | ENV SDL2DIR | ||
| 66 | PATH_SUFFIXES lib bin ${VC_LIB_PATH_SUFFIX} | ||
| 67 | PATHS ${SDL2_MIXER_PATH} | ||
| 68 | ) | ||
| 69 | |||
| 70 | if(SDL2_MIXER_INCLUDE_DIR AND EXISTS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h") | ||
| 71 | file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+[0-9]+$") | ||
| 72 | file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+[0-9]+$") | ||
| 73 | file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+[0-9]+$") | ||
| 74 | string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MAJOR "${SDL2_MIXER_VERSION_MAJOR_LINE}") | ||
| 75 | string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MINOR "${SDL2_MIXER_VERSION_MINOR_LINE}") | ||
| 76 | string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_PATCH "${SDL2_MIXER_VERSION_PATCH_LINE}") | ||
| 77 | set(SDL2_MIXER_VERSION_STRING ${SDL2_MIXER_VERSION_MAJOR}.${SDL2_MIXER_VERSION_MINOR}.${SDL2_MIXER_VERSION_PATCH}) | ||
| 78 | unset(SDL2_MIXER_VERSION_MAJOR_LINE) | ||
| 79 | unset(SDL2_MIXER_VERSION_MINOR_LINE) | ||
| 80 | unset(SDL2_MIXER_VERSION_PATCH_LINE) | ||
| 81 | unset(SDL2_MIXER_VERSION_MAJOR) | ||
| 82 | unset(SDL2_MIXER_VERSION_MINOR) | ||
| 83 | unset(SDL2_MIXER_VERSION_PATCH) | ||
| 84 | endif() | ||
| 85 | |||
| 86 | set(SDL2_MIXER_LIBRARIES ${SDL2_MIXER_LIBRARY}) | ||
| 87 | set(SDL2_MIXER_INCLUDE_DIRS ${SDL2_MIXER_INCLUDE_DIR}) | ||
| 88 | |||
| 89 | include(FindPackageHandleStandardArgs) | ||
| 90 | |||
| 91 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_mixer | ||
| 92 | REQUIRED_VARS SDL2_MIXER_LIBRARIES SDL2_MIXER_INCLUDE_DIRS | ||
| 93 | VERSION_VAR SDL2_MIXER_VERSION_STRING) | ||
| 94 | |||
| 95 | # for backward compatibility | ||
| 96 | set(SDLMIXER_LIBRARY ${SDL2_MIXER_LIBRARIES}) | ||
| 97 | set(SDLMIXER_INCLUDE_DIR ${SDL2_MIXER_INCLUDE_DIRS}) | ||
| 98 | set(SDLMIXER_FOUND ${SDL2_MIXER_FOUND}) | ||
| 99 | |||
| 100 | mark_as_advanced(SDL2_MIXER_LIBRARY SDL2_MIXER_INCLUDE_DIR) | ||
| diff --git a/res/bump.wav b/res/bump.wav new file mode 100644 index 0000000..81386a3 --- /dev/null +++ b/res/bump.wav | |||
| Binary files differ | |||
| diff --git a/src/main.cpp b/src/main.cpp index 5e5953b..1d1583f 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
| @@ -5,8 +5,9 @@ | |||
| 5 | #include "party.h" | 5 | #include "party.h" |
| 6 | #include "timer.h" | 6 | #include "timer.h" |
| 7 | #include "map.h" | 7 | #include "map.h" |
| 8 | #include "mixer.h" | ||
| 8 | 9 | ||
| 9 | void loop(Renderer& renderer) { | 10 | void loop(Renderer& renderer, Mixer& mixer) { |
| 10 | Game game; | 11 | Game game; |
| 11 | Input keystate; | 12 | Input keystate; |
| 12 | 13 | ||
| @@ -61,7 +62,7 @@ void loop(Renderer& renderer) { | |||
| 61 | 62 | ||
| 62 | inputTimer.accumulate(frameTime); | 63 | inputTimer.accumulate(frameTime); |
| 63 | while (inputTimer.step()) { | 64 | while (inputTimer.step()) { |
| 64 | party.move(game, keystate); | 65 | party.move(game, mixer, keystate); |
| 65 | } | 66 | } |
| 66 | 67 | ||
| 67 | animTimer.accumulate(frameTime); | 68 | animTimer.accumulate(frameTime); |
| @@ -79,8 +80,9 @@ int main(int, char**) { | |||
| 79 | try | 80 | try |
| 80 | { | 81 | { |
| 81 | Renderer renderer; | 82 | Renderer renderer; |
| 83 | Mixer mixer; | ||
| 82 | 84 | ||
| 83 | loop(renderer); | 85 | loop(renderer, mixer); |
| 84 | } catch (const sdl_error& ex) | 86 | } catch (const sdl_error& ex) |
| 85 | { | 87 | { |
| 86 | std::cout << "SDL error (" << ex.what() << ")" << std::endl; | 88 | std::cout << "SDL error (" << ex.what() << ")" << std::endl; |
| diff --git a/src/mixer.cpp b/src/mixer.cpp new file mode 100644 index 0000000..bfbedcd --- /dev/null +++ b/src/mixer.cpp | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #include "mixer.h" | ||
| 2 | |||
| 3 | void Mixer::playSound(std::string filename) { | ||
| 4 | if (!sounds_.count(filename)) { | ||
| 5 | Mix_Chunk* sample = Mix_LoadWAV(filename.c_str()); | ||
| 6 | if (!sample) { | ||
| 7 | throw mix_error(); | ||
| 8 | } | ||
| 9 | |||
| 10 | sounds_[filename] = chunk_ptr(sample); | ||
| 11 | } | ||
| 12 | |||
| 13 | if (Mix_PlayChannel(-1, sounds_[filename].get(), 0) == -1) { | ||
| 14 | throw mix_error(); | ||
| 15 | } | ||
| 16 | } | ||
| diff --git a/src/mixer.h b/src/mixer.h new file mode 100644 index 0000000..010bb09 --- /dev/null +++ b/src/mixer.h | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #ifndef MIXER_H_6DF82000 | ||
| 2 | #define MIXER_H_6DF82000 | ||
| 3 | |||
| 4 | #include <SDL_mixer.h> | ||
| 5 | #include <memory> | ||
| 6 | #include <stdexcept> | ||
| 7 | #include <map> | ||
| 8 | #include <string> | ||
| 9 | |||
| 10 | class mix_error : public std::logic_error { | ||
| 11 | public: | ||
| 12 | |||
| 13 | mix_error() : std::logic_error(Mix_GetError()) | ||
| 14 | { | ||
| 15 | } | ||
| 16 | }; | ||
| 17 | |||
| 18 | class mix_wrapper { | ||
| 19 | public: | ||
| 20 | |||
| 21 | mix_wrapper() | ||
| 22 | { | ||
| 23 | if (Mix_Init(0) != 0) { | ||
| 24 | mix_error ex; | ||
| 25 | Mix_Quit(); | ||
| 26 | |||
| 27 | throw ex; | ||
| 28 | } | ||
| 29 | |||
| 30 | if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) != 0) { | ||
| 31 | mix_error ex; | ||
| 32 | Mix_Quit(); | ||
| 33 | |||
| 34 | throw ex; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | ~mix_wrapper() | ||
| 39 | { | ||
| 40 | Mix_CloseAudio(); | ||
| 41 | Mix_Quit(); | ||
| 42 | } | ||
| 43 | }; | ||
| 44 | |||
| 45 | class chunk_deleter { | ||
| 46 | public: | ||
| 47 | |||
| 48 | void operator()(Mix_Chunk* chunk) { | ||
| 49 | Mix_FreeChunk(chunk); | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | |||
| 53 | using chunk_ptr = std::unique_ptr<Mix_Chunk, chunk_deleter>; | ||
| 54 | |||
| 55 | // MUST create the Renderer first! | ||
| 56 | class Mixer { | ||
| 57 | public: | ||
| 58 | |||
| 59 | void playSound(std::string filename); | ||
| 60 | |||
| 61 | private: | ||
| 62 | |||
| 63 | mix_wrapper mix_; | ||
| 64 | std::map<std::string, chunk_ptr> sounds_; | ||
| 65 | }; | ||
| 66 | |||
| 67 | #endif /* end of include guard: MIXER_H_6DF82000 */ | ||
| diff --git a/src/party.cpp b/src/party.cpp index 307bca8..b7284c7 100644 --- a/src/party.cpp +++ b/src/party.cpp | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | #include "party.h" | 1 | #include "party.h" |
| 2 | #include "consts.h" | 2 | #include "consts.h" |
| 3 | #include <iostream> | 3 | #include "mixer.h" |
| 4 | 4 | ||
| 5 | void Party::addMember(Game& game, int spriteId) { | 5 | void Party::addMember(Game& game, int spriteId) { |
| 6 | int index = members_.size(); | 6 | int index = members_.size(); |
| @@ -19,7 +19,7 @@ void Party::addMember(Game& game, int spriteId) { | |||
| 19 | game.setSpriteState(spriteId, "still"); | 19 | game.setSpriteState(spriteId, "still"); |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | void Party::move(Game& game, const Input& keystate) { | 22 | void Party::move(Game& game, Mixer& mixer, const Input& keystate) { |
| 23 | if (members_.empty()) { | 23 | if (members_.empty()) { |
| 24 | return; | 24 | return; |
| 25 | } | 25 | } |
| @@ -151,6 +151,7 @@ void Party::move(Game& game, const Input& keystate) { | |||
| 151 | 151 | ||
| 152 | if (blocked && state_ == State::Running) { | 152 | if (blocked && state_ == State::Running) { |
| 153 | stopRunning(game); | 153 | stopRunning(game); |
| 154 | mixer.playSound("../res/bump.wav"); | ||
| 154 | } | 155 | } |
| 155 | 156 | ||
| 156 | // Move everything | 157 | // Move everything |
| diff --git a/src/party.h b/src/party.h index 2864073..59330c8 100644 --- a/src/party.h +++ b/src/party.h | |||
| @@ -5,12 +5,14 @@ | |||
| 5 | #include <vector> | 5 | #include <vector> |
| 6 | #include "game.h" | 6 | #include "game.h" |
| 7 | 7 | ||
| 8 | class Mixer; | ||
| 9 | |||
| 8 | class Party { | 10 | class Party { |
| 9 | public: | 11 | public: |
| 10 | 12 | ||
| 11 | void addMember(Game& game, int spriteId); | 13 | void addMember(Game& game, int spriteId); |
| 12 | 14 | ||
| 13 | void move(Game& game, const Input& keystate); | 15 | void move(Game& game, Mixer& mixer, const Input& keystate); |
| 14 | 16 | ||
| 15 | void beginCrouch(Game& game); | 17 | void beginCrouch(Game& game); |
| 16 | 18 | ||
| diff --git a/src/renderer.h b/src/renderer.h index f952ab3..71c4ade 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
| @@ -32,7 +32,7 @@ public: | |||
| 32 | 32 | ||
| 33 | sdl_wrapper() | 33 | sdl_wrapper() |
| 34 | { | 34 | { |
| 35 | if (SDL_Init(SDL_INIT_VIDEO) != 0) | 35 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) |
| 36 | { | 36 | { |
| 37 | sdl_error ex; | 37 | sdl_error ex; |
| 38 | SDL_Quit(); | 38 | SDL_Quit(); |
