annotate src/libmlk-core/core/sys.c @ 419:1b4e61bdb9ab

core: introduce err.h
author David Demelier <markand@malikania.fr>
date Wed, 12 Oct 2022 15:06:18 +0200
parents d74f53299252
children 7d2ebc334c8c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * sys.c -- system routines
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
378
460c78706989 misc: update copyright years
David Demelier <markand@malikania.fr>
parents: 371
diff changeset
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
301
f04b4ee04db3 make: switch to GNU make
David Demelier <markand@malikania.fr>
parents: 298
diff changeset
19 #include "config.h"
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
20
320
8f9937403749 misc: improve loading of data
David Demelier <markand@malikania.fr>
parents: 316
diff changeset
21 #include <sys/stat.h>
32
91bc2329ab0c core: implement data directory access, closes #2454 @1h
David Demelier <markand@malikania.fr>
parents: 31
diff changeset
22 #include <assert.h>
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
23 #include <errno.h>
56
43d1102a367e core: disable buffering on MinGW
David Demelier <markand@malikania.fr>
parents: 52
diff changeset
24 #include <stdio.h>
43d1102a367e core: disable buffering on MinGW
David Demelier <markand@malikania.fr>
parents: 52
diff changeset
25 #include <stdlib.h>
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
26 #include <string.h>
84
a6c2067709ce core: implement basic save routines, closes #2476 @2h
David Demelier <markand@malikania.fr>
parents: 59
diff changeset
27 #include <limits.h>
32
91bc2329ab0c core: implement data directory access, closes #2454 @1h
David Demelier <markand@malikania.fr>
parents: 31
diff changeset
28
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
29 #if defined(_WIN32)
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
30 # include <shlwapi.h>
255
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
31 # include <windows.h>
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
32 #else
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
33 # include <errno.h>
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
34 # include <string.h>
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
35 #endif
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
36
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 #include <SDL.h>
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 #include <SDL_image.h>
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 #include <SDL_ttf.h>
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
40
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
41 #include <sndfile.h>
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
42
315
3bfaaf5342a9 cmake: add support for Visual Studio 2019
David Demelier <markand@malikania.fr>
parents: 314
diff changeset
43 #include <port/port.h>
3bfaaf5342a9 cmake: add support for Visual Studio 2019
David Demelier <markand@malikania.fr>
parents: 314
diff changeset
44
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
45 #include "alloc.h"
39
9d1421c09dfb core: add more utilities to improve code simplicity
David Demelier <markand@malikania.fr>
parents: 36
diff changeset
46 #include "error.h"
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
47 #include "panic.h"
204
c9fbb822d269 core: allocate sound channels at startup
David Demelier <markand@malikania.fr>
parents: 169
diff changeset
48 #include "sound.h"
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 #include "sys.h"
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
50 #include "sys_p.h"
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
51
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
52 ALCdevice *audio_dev = NULL;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
53 ALCcontext *audio_ctx = NULL;
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
54
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
55 static struct {
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
56 char organization[128];
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
57 char name[128];
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
58 } info = {
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
59 .organization = "fr.malikania",
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
60 .name = "molko"
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
61 };
36
798baf7f3ec0 core: implement sys_data* functions for Windows
David Demelier <markand@malikania.fr>
parents: 32
diff changeset
62
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
63 static inline char *
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
64 normalize(char *str)
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
65 {
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
66 for (char *p = str; *p; ++p)
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
67 if (*p == '\\')
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
68 *p = '/';
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
69
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
70 return str;
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
71 }
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
72
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
73 static const char *
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
74 user_directory(enum sys_dir kind)
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
75 {
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
76 /* Kept for future use. */
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
77 (void)kind;
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
78
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
79 static char path[PATH_MAX];
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
80 char *pref;
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
81
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
82 if ((pref = SDL_GetPrefPath(info.organization, info.name))) {
348
7d7991f97acf misc: be more barebone portable
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
83 port_strlcpy(path, pref, sizeof (path));
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
84 SDL_free(pref);
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
85 } else
348
7d7991f97acf misc: be more barebone portable
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
86 port_strlcpy(path, "./", sizeof (path));
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
87
279
5217c195c5b9 core: forgot to return the path
David Demelier <markand@malikania.fr>
parents: 259
diff changeset
88 return path;
32
91bc2329ab0c core: implement data directory access, closes #2454 @1h
David Demelier <markand@malikania.fr>
parents: 31
diff changeset
89 }
91bc2329ab0c core: implement data directory access, closes #2454 @1h
David Demelier <markand@malikania.fr>
parents: 31
diff changeset
90
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
91 static inline int
255
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
92 mkpath(const char *path)
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
93 {
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
94 #ifdef _WIN32
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
95 /* TODO: add error using the convenient FormatMessage function. */
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
96 if (!CreateDirectoryA(path, NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
97 return errorf("unable to create directory: %s", path);
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
98 #else
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
99 if (mkdir(path, 0755) < 0 && errno != EEXIST)
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
100 return errorf("%s", strerror(errno));
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
101 #endif
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
102
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
103 return 0;
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
104 }
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
105
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
106 int
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
107 sys_init(const char *organization, const char *name)
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
108 {
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
109 #if defined(__MINGW64__)
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
110 /* On MinGW buffering leads to painful debugging. */
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
111 setbuf(stderr, NULL);
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
112 setbuf(stdout, NULL);
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
113 #endif
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
114
411
d74f53299252 make: add basic GNU make support
David Demelier <markand@malikania.fr>
parents: 394
diff changeset
115 /* Kept for future use. */
d74f53299252 make: add basic GNU make support
David Demelier <markand@malikania.fr>
parents: 394
diff changeset
116 (void)organization;
d74f53299252 make: add basic GNU make support
David Demelier <markand@malikania.fr>
parents: 394
diff changeset
117 (void)name;
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
118
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
119 /* SDL2. */
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
120 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
121 return errorf("%s", SDL_GetError());
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
122 if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
123 return errorf("%s", SDL_GetError());
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
124 if (TTF_Init() < 0)
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
125 return errorf("%s", SDL_GetError());
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
126
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
127 /* OpenAL. */
394
8273c40a5691 core: disable openal-soft logs
David Demelier <markand@malikania.fr>
parents: 380
diff changeset
128 #if defined(_WIN32)
8273c40a5691 core: disable openal-soft logs
David Demelier <markand@malikania.fr>
parents: 380
diff changeset
129 SetEnvironmentVariable("ALSOFT_LOGLEVEL", "0");
8273c40a5691 core: disable openal-soft logs
David Demelier <markand@malikania.fr>
parents: 380
diff changeset
130 #else
8273c40a5691 core: disable openal-soft logs
David Demelier <markand@malikania.fr>
parents: 380
diff changeset
131 putenv("ALSOFT_LOGLEVEL=0");
8273c40a5691 core: disable openal-soft logs
David Demelier <markand@malikania.fr>
parents: 380
diff changeset
132 #endif
8273c40a5691 core: disable openal-soft logs
David Demelier <markand@malikania.fr>
parents: 380
diff changeset
133
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
134 if (!(audio_dev = alcOpenDevice(NULL)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
135 return errorf("unable to create audio device");
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
136 if (!(audio_ctx = alcCreateContext(audio_dev, NULL)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
137 return errorf("unable to create audio context");
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
138
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
139 alcMakeContextCurrent(audio_ctx);
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
140
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
141 return 0;
255
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
142 }
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
143
32
91bc2329ab0c core: implement data directory access, closes #2454 @1h
David Demelier <markand@malikania.fr>
parents: 31
diff changeset
144 const char *
241
76afe639fd72 misc: add support for NLS, closes #22510 @4h
David Demelier <markand@malikania.fr>
parents: 204
diff changeset
145 sys_dir(enum sys_dir kind)
32
91bc2329ab0c core: implement data directory access, closes #2454 @1h
David Demelier <markand@malikania.fr>
parents: 31
diff changeset
146 {
411
d74f53299252 make: add basic GNU make support
David Demelier <markand@malikania.fr>
parents: 394
diff changeset
147 return user_directory(kind);
84
a6c2067709ce core: implement basic save routines, closes #2476 @2h
David Demelier <markand@malikania.fr>
parents: 59
diff changeset
148 }
a6c2067709ce core: implement basic save routines, closes #2476 @2h
David Demelier <markand@malikania.fr>
parents: 59
diff changeset
149
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
150 int
255
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
151 sys_mkdir(const char *directory)
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
152 {
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
153 char path[PATH_MAX], *p;
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
154
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
155 /* Copy the directory to normalize and iterate over '/'. */
348
7d7991f97acf misc: be more barebone portable
David Demelier <markand@malikania.fr>
parents: 320
diff changeset
156 port_strlcpy(path, directory, sizeof (path));
255
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
157 normalize(path);
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
158
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
159 #if defined(_WIN32)
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
160 /* Remove drive letter that we don't need. */
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
161 if ((p = strchr(path, ':')))
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
162 ++p;
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
163 else
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
164 p = path;
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
165 #else
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
166 p = path;
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
167 #endif
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
168
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
169 for (p = p + 1; *p; ++p) {
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
170 if (*p == '/') {
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
171 *p = 0;
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
172
298
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
173 if (mkpath(path) < 0)
196264679079 misc: remove usage of bool
David Demelier <markand@malikania.fr>
parents: 279
diff changeset
174 return -1;
255
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
175
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
176 *p = '/';
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
177 }
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
178 }
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
179
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
180 return mkpath(path);
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
181 }
d987a5fc4a3e core: add sys_mkdir function
David Demelier <markand@malikania.fr>
parents: 243
diff changeset
182
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
183 void
97
58133933ea17 window: expose some data to avoid functions
David Demelier <markand@malikania.fr>
parents: 87
diff changeset
184 sys_finish(void)
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
185 {
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
186 TTF_Quit();
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
187 IMG_Quit();
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
188 SDL_Quit();
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
189
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
190 alcMakeContextCurrent(NULL);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
191
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
192 if (audio_ctx) {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
193 alcDestroyContext(audio_ctx);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
194 audio_ctx = NULL;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
195 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
196 if (audio_dev) {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
197 alcCloseDevice(audio_dev);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
198 audio_dev = NULL;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
199 }
23
bc9637a2601b core: add basic system routines
David Demelier <markand@malikania.fr>
parents:
diff changeset
200 }
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
201
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
202 struct audiostream *
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
203 audiostream_create(SNDFILE *file, const SF_INFO *info)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
204 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
205 struct audiostream *stream;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
206
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
207 stream = alloc_new(sizeof (*stream));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
208 stream->samplerate = info->samplerate;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
209 stream->samplesz = info->frames * info->channels;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
210 stream->samples = alloc_array(stream->samplesz, sizeof (*stream->samples));
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
211 stream->format = info->channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
212
380
31e2f6d35c34 core: fix sndfile distortion issue
David Demelier <markand@malikania.fr>
parents: 379
diff changeset
213 sf_command(file, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
31e2f6d35c34 core: fix sndfile distortion issue
David Demelier <markand@malikania.fr>
parents: 379
diff changeset
214
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
215 if (sf_read_short(file, stream->samples, stream->samplesz) != stream->samplesz) {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
216 free(stream->samples);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
217 free(stream);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
218 stream = NULL;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
219 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
220
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
221 alGenBuffers(1, &stream->buffer);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
222 alBufferData(stream->buffer, stream->format, stream->samples,
380
31e2f6d35c34 core: fix sndfile distortion issue
David Demelier <markand@malikania.fr>
parents: 379
diff changeset
223 stream->samplesz * sizeof (*stream->samples), stream->samplerate);
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
224 alGenSources(1, &stream->source);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
225 alSourcei(stream->source, AL_BUFFER, stream->buffer);
411
d74f53299252 make: add basic GNU make support
David Demelier <markand@malikania.fr>
parents: 394
diff changeset
226
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
227 sf_close(file);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
228
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
229 return stream;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
230 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
231
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
232 struct audiostream *
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
233 audiostream_open(const char *path)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
234 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
235 assert(path);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
236
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
237 SF_INFO info;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
238 SNDFILE *file;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
239
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
240 if (!(file = sf_open(path, SFM_READ, &info)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
241 return NULL;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
242
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
243 return audiostream_create(file, &info);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
244 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
245
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
246 struct viodata {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
247 const unsigned char *data;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
248 const size_t datasz;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
249 sf_count_t offset;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
250 };
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
251
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
252 static sf_count_t
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
253 vio_get_filelen(void *data)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
254 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
255 const struct viodata *vio = data;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
256
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
257 return (sf_count_t)vio->datasz;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
258 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
259
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
260 static sf_count_t
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
261 vio_seek(sf_count_t offset, int whence, void *data)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
262 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
263 struct viodata *vio = data;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
264
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
265 switch (whence) {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
266 case SEEK_SET:
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
267 vio->offset = offset;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
268 break;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
269 case SEEK_CUR:
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
270 vio->offset += offset;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
271 break;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
272 case SEEK_END:
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
273 vio->offset = vio->datasz - offset;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
274 break;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
275 default:
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
276 break;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
277 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
278
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
279 return vio->offset;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
280 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
281
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
282 static sf_count_t
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
283 vio_read(void *ptr, sf_count_t count, void *data)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
284 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
285 struct viodata *vio = data;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
286
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
287 memcpy(ptr, vio->data + vio->offset, count);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
288 vio->offset += count;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
289
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
290 return count;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
291 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
292
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
293 static sf_count_t
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
294 vio_tell(void *data)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
295 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
296 const struct viodata *vio = data;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
297
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
298 return vio->offset;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
299 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
300
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
301 struct audiostream *
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
302 audiostream_openmem(const void *data, size_t datasz)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
303 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
304 assert(data);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
305
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
306 SF_VIRTUAL_IO io = {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
307 .get_filelen = vio_get_filelen,
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
308 .seek = vio_seek,
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
309 .read = vio_read,
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
310 .tell = vio_tell
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
311 };
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
312 SF_INFO info;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
313 SNDFILE *file;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
314 struct viodata viodata = {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
315 .data = data,
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
316 .datasz = datasz,
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
317 };
411
d74f53299252 make: add basic GNU make support
David Demelier <markand@malikania.fr>
parents: 394
diff changeset
318
379
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
319 if (!(file = sf_open_virtual(&io, SFM_READ, &info, &viodata)))
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
320 return NULL;
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
321
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
322 return audiostream_create(file, &info);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
323 }
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
324
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
325 void
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
326 audiostream_finish(struct audiostream *s)
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
327 {
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
328 assert(s);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
329
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
330 alDeleteBuffers(1, &s->buffer);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
331 alSourcei(s->source, AL_BUFFER, 0);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
332 alDeleteSources(1, &s->source);
67c1c46af2c8 core: replace SDL2_mixer with OpenAL, closes #2528 @3h
David Demelier <markand@malikania.fr>
parents: 378
diff changeset
333 }