Browse code

play music during intro

devnewton authored on 15/10/2014 13:37:43
Showing 6 changed files
... ...
@@ -20,8 +20,11 @@ include_directories(${SDL2_INCLUDE_DIR})
20 20
 find_package(SDL2_image REQUIRED)
21 21
 include_directories(${SDL2_IMAGE_INCLUDE_DIR})
22 22
 
23
+find_package(SDL2_mixer REQUIRED)
24
+include_directories(${SDL2_MIXER_INCLUDE_DIR})
25
+
23 26
 find_package(SDL2_ttf REQUIRED)
24 27
 include_directories(${SDL2_TTF_INCLUDE_DIR})
25 28
 
26 29
 add_executable(superpaflaballe src/assets.cpp src/bourrines_benchmark.cpp src/framerate.cpp src/game.cpp src/intro.cpp src/main.cpp src/nanim.cpp src/sdl_exception.cpp)
27
-target_link_libraries(superpaflaballe ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARY} ${SDL2_TTF_LIBRARY} ${Boost_LIBRARIES})
30
+target_link_libraries(superpaflaballe ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARY} ${SDL2_MIXER_LIBRARY} ${SDL2_TTF_LIBRARY} ${Boost_LIBRARIES})
28 31
new file mode 100644
... ...
@@ -0,0 +1,155 @@
1
+# Locate SDL2_mixer library
2
+# This module defines
3
+# SDL2_MIXER_LIBRARY, the name of the library to link against
4
+# SDL2_MIXER_FOUND, if false, do not try to link to SDL2_mixer
5
+# SDL2_MIXER_INCLUDE_DIR, where to find SDL_mixer.h
6
+#
7
+# Additional Note: If you see an empty SDL2_MIXER_LIBRARY_TEMP in your configuration
8
+# and no SDL2_MIXER_LIBRARY, it means CMake did not find your SDL2_Mixer library
9
+# (SDL2_mixer.dll, libsdl2_mixer.so, SDL2_mixer.framework, etc).
10
+# Set SDL2_MIXER_LIBRARY_TEMP to point to your SDL2 library, and configure again.
11
+# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
12
+# as appropriate. These values are used to generate the final SDL2_MIXER_LIBRARY
13
+# variable, but when these values are unset, SDL2_MIXER_LIBRARY does not get created.
14
+#
15
+# $SDL2 is an environment variable that would
16
+# correspond to the ./configure --prefix=$SDL2
17
+# used in building SDL2.
18
+# l.e.galup 9-20-02
19
+#
20
+# Modified by Eric Wing.
21
+# Added code to assist with automated building by using environmental variables
22
+# and providing a more controlled/consistent search behavior.
23
+# Added new modifications to recognize OS X frameworks and
24
+# additional Unix paths (FreeBSD, etc).
25
+# Also corrected the header search path to follow "proper" SDL2 guidelines.
26
+# Added a search for SDL2main which is needed by some platforms.
27
+# Added a search for threads which is needed by some platforms.
28
+# Added needed compile switches for MinGW.
29
+#
30
+# On OSX, this will prefer the Framework version (if found) over others.
31
+# People will have to manually change the cache values of
32
+# SDL2_MIXER_LIBRARY to override this selection or set the CMake environment
33
+# CMAKE_INCLUDE_PATH to modify the search paths.
34
+#
35
+# Note that the header path has changed from SDL2/SDL.h to just SDL.h
36
+# This needed to change because "proper" SDL2 convention
37
+# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
38
+# reasons because not all systems place things in SDL2/ (see FreeBSD).
39
+#
40
+# Ported by Johnny Patterson. This is a literal port for SDL2 of the FindSDL.cmake
41
+# module with the minor edit of changing "SDL" to "SDL2" where necessary. This
42
+# was not created for redistribution, and exists temporarily pending official
43
+# SDL2 CMake modules.
44
+# 
45
+# Note that on windows this will only search for the 32bit libraries, to search
46
+# for 64bit change x86/i686-w64 to x64/x86_64-w64
47
+
48
+#=============================================================================
49
+# Copyright 2003-2009 Kitware, Inc.
50
+#
51
+# CMake - Cross Platform Makefile Generator
52
+# Copyright 2000-2014 Kitware, Inc.
53
+# Copyright 2000-2011 Insight Software Consortium
54
+# All rights reserved.
55
+#
56
+# Redistribution and use in source and binary forms, with or without
57
+# modification, are permitted provided that the following conditions
58
+# are met:
59
+#
60
+# * Redistributions of source code must retain the above copyright
61
+# notice, this list of conditions and the following disclaimer.
62
+#
63
+# * Redistributions in binary form must reproduce the above copyright
64
+# notice, this list of conditions and the following disclaimer in the
65
+# documentation and/or other materials provided with the distribution.
66
+#
67
+# * Neither the names of Kitware, Inc., the Insight Software Consortium,
68
+# nor the names of their contributors may be used to endorse or promote
69
+# products derived from this software without specific prior written
70
+# permission.
71
+#
72
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
73
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
74
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
75
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
76
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
77
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
78
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
79
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
80
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
82
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83
+#
84
+# This software is distributed WITHOUT ANY WARRANTY; without even the
85
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
86
+# See the License for more information.
87
+#=============================================================================
88
+# (To distribute this file outside of CMake, substitute the full
89
+# License text for the above reference.)
90
+
91
+FIND_PATH(SDL2_MIXER_INCLUDE_DIR SDL_mixer.h
92
+	HINTS
93
+	$ENV{SDL2}
94
+	$ENV{SDL2_MIXER}
95
+	PATH_SUFFIXES include/SDL2 include SDL2
96
+	i686-w64-mingw32/include/SDL2
97
+	x86_64-w64-mingw32/include/SDL2
98
+	PATHS
99
+	~/Library/Frameworks
100
+	/Library/Frameworks
101
+	/usr/local/include/SDL2
102
+	/usr/include/SDL2
103
+	/sw # Fink
104
+	/opt/local # DarwinPorts
105
+	/opt/csw # Blastwave
106
+	/opt
107
+)
108
+
109
+# Lookup the 64 bit libs on x64
110
+IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
111
+	FIND_LIBRARY(SDL2_MIXER_LIBRARY_TEMP
112
+		NAMES SDL2_mixer
113
+		HINTS
114
+		$ENV{SDL2}
115
+		$ENV{SDL2_MIXER}
116
+		PATH_SUFFIXES lib64 lib
117
+		lib/x64
118
+		x86_64-w64-mingw32/lib
119
+		PATHS
120
+		/sw
121
+		/opt/local
122
+		/opt/csw
123
+		/opt
124
+	)
125
+# On 32bit build find the 32bit libs
126
+ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
127
+	FIND_LIBRARY(SDL2_MIXER_LIBRARY_TEMP
128
+		NAMES SDL2_mixer
129
+		HINTS
130
+		$ENV{SDL2}
131
+		$ENV{SDL2_MIXER}
132
+		PATH_SUFFIXES lib
133
+		lib/x86
134
+		i686-w64-mingw32/lib
135
+		PATHS
136
+		/sw
137
+		/opt/local
138
+		/opt/csw
139
+		/opt
140
+	)
141
+ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)
142
+
143
+SET(SDL2_MIXER_FOUND "NO")
144
+	IF(SDL2_MIXER_LIBRARY_TEMP)
145
+	# Set the final string here so the GUI reflects the final state.
146
+	SET(SDL2_MIXER_LIBRARY ${SDL2_MIXER_LIBRARY_TEMP} CACHE STRING "Where the SDL2_mixer Library can be found")
147
+	# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
148
+	SET(SDL2_MIXER_LIBRARY_TEMP "${SDL2_MIXER_LIBRARY_TEMP}" CACHE INTERNAL "")
149
+	SET(SDL2_MIXER_FOUND "YES")
150
+ENDIF(SDL2_MIXER_LIBRARY_TEMP)
151
+
152
+INCLUDE(FindPackageHandleStandardArgs)
153
+
154
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_MIXER REQUIRED_VARS SDL2_MIXER_LIBRARY SDL2_MIXER_INCLUDE_DIR)
155
+
... ...
@@ -16,14 +16,24 @@ namespace superpaflaballe {
16 16
         if (TTF_Init() != 0) {
17 17
             throw sdl_exception();
18 18
         }
19
-        int flags = IMG_INIT_PNG;
20
-        int initted = IMG_Init(flags);
21
-        if ((initted & flags) != flags) {
19
+        const int img_flags = IMG_INIT_PNG;
20
+        int img_initted = IMG_Init(img_flags);
21
+        if ((img_initted & img_flags) != img_flags) {
22
+            throw sdl_exception();
23
+        }
24
+        int mix_flags = MIX_INIT_OGG;
25
+        int mix_initted = Mix_Init(mix_flags);
26
+        if ((mix_initted & mix_flags) != mix_flags) {
27
+            throw sdl_exception();
28
+        }
29
+       if( Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT , 2, 1024) != 0 ) {
22 30
             throw sdl_exception();
23 31
         }
24 32
     }
25 33
 
26 34
     assets::~assets() {
35
+        Mix_CloseAudio();
36
+        Mix_Quit();
27 37
         IMG_Quit();
28 38
         TTF_Quit();
29 39
     }
... ...
@@ -45,10 +55,31 @@ namespace superpaflaballe {
45 55
         }
46 56
         throw std::runtime_error("Cannot load font " + path);
47 57
     }
48
-    
49
-     std::shared_ptr< TTF_Font > assets::load_font(const std::string path, int size) {
50
-         return std::shared_ptr< TTF_Font >(TTF_OpenFont(path.c_str(), size), TTF_CloseFont);
51
-     }
58
+
59
+    std::shared_ptr< TTF_Font > assets::load_font(const std::string path, int size) {
60
+        return std::shared_ptr< TTF_Font >(TTF_OpenFont(path.c_str(), size), TTF_CloseFont);
61
+    }
62
+
63
+    std::shared_ptr< Mix_Music > assets::music(const std::string& path) {
64
+        auto music = musics_[path].lock();
65
+        if (music) {
66
+            return music;
67
+        }
68
+        boost::filesystem::path fspath(path);
69
+        for (auto& prefix : pathPrefixes_) {
70
+            boost::filesystem::path fsprefix(prefix);
71
+            music = load_music((fsprefix / fspath).string());
72
+            if (music) {
73
+                musics_[path] = music;
74
+                return music;
75
+            }
76
+        }
77
+        throw std::runtime_error("Cannot load music " + path);
78
+    }
79
+
80
+     std::shared_ptr< Mix_Music > assets::load_music(const std::string& path) {
81
+        return std::shared_ptr< Mix_Music >(Mix_LoadMUS(path.c_str()), Mix_FreeMusic);
82
+    }
52 83
 
53 84
     std::shared_ptr< SDL_Texture > assets::texture(const std::string& path) {
54 85
         auto texture = textures_[path].lock();
... ...
@@ -1,6 +1,7 @@
1 1
 #pragma once
2 2
 
3 3
 #include "nanim.h"
4
+#include <SDL_mixer.h>
4 5
 #include <SDL_ttf.h>
5 6
 
6 7
 namespace superpaflaballe {
... ...
@@ -14,18 +15,22 @@ namespace superpaflaballe {
14 15
 
15 16
         std::shared_ptr< nanim::collection > animations(const std::string& path);
16 17
         std::shared_ptr< TTF_Font > font(const std::string& path, int size);
18
+        std::shared_ptr< Mix_Music > music(const std::string& path);
17 19
         std::shared_ptr< SDL_Texture > texture(const std::string& path);
18 20
 
21
+
19 22
     private:
20 23
         std::shared_ptr< TTF_Font > load_font(const std::string path, int size);
21 24
         std::shared_ptr<nanim::collection> load_json_nanim(const std::string& path);
25
+        std::shared_ptr< Mix_Music > load_music(const std::string& path);
22 26
         std::shared_ptr< SDL_Texture > load_texture(const std::string path);
23 27
 
24 28
         game& game_;
25 29
         std::vector<std::string> pathPrefixes_;
26
-        std::map<std::string, std::weak_ptr< SDL_Texture > > textures_;
27 30
         std::map<std::string, std::weak_ptr< nanim::collection > > animations_;
28 31
         std::map<std::pair<std::string, int/*font size*/>, std::weak_ptr< TTF_Font > > fonts_;
32
+        std::map<std::string, std::weak_ptr< SDL_Texture > > textures_;
33
+        std::map<std::string, std::weak_ptr< Mix_Music > > musics_;
29 34
     };
30 35
 
31 36
 }
... ...
@@ -9,7 +9,13 @@ namespace superpaflaballe {
9 9
 
10 10
     intro::intro(game& ga)
11 11
     : game_(ga)
12
-    , play_(ga.assets().animations("intro/devnewton.json")->play(superpaflaballe::nanim::once)) {
12
+    , play_(ga.assets().animations("intro/devnewton.json")->play(superpaflaballe::nanim::once))
13
+    , music_(ga.assets().music("intro/devnewton.ogg")) {
14
+        Mix_PlayMusic(music_.get(), 1);
15
+    }
16
+
17
+    intro::~intro() {
18
+        Mix_HaltMusic();
13 19
     }
14 20
 
15 21
     void intro::tick() {
... ...
@@ -21,7 +27,7 @@ namespace superpaflaballe {
21 27
         rect.y = (superpaflaballe::logical_screen_height - rect.h) / 2;
22 28
         SDL_RenderCopy(game_.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect);
23 29
     }
24
-    
30
+
25 31
     bool intro::is_finished() {
26 32
         return play_->is_stopped();
27 33
     }
... ...
@@ -9,11 +9,13 @@ namespace superpaflaballe {
9 9
     class intro {
10 10
     public:
11 11
         intro(game& g);
12
+        ~intro();
12 13
         void tick();
13 14
         bool is_finished();
14 15
     private:
15 16
         game& game_;
16 17
         std::shared_ptr<superpaflaballe::nanim::play> play_;
18
+        std::shared_ptr<Mix_Music> music_;
17 19
     };
18 20
 
19 21
 }