summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/GameDraw.cpp3
-rw-r--r--src/GameInitDispose.cpp166
-rw-r--r--src/GameLoop.cpp3
-rw-r--r--src/GameTick.cpp3
-rw-r--r--src/Globals.cpp4
-rw-r--r--src/Person.cpp3
-rw-r--r--src/Skeleton.cpp3
-rw-r--r--src/Support.cpp69
-rw-r--r--src/Support.h4
-rw-r--r--src/main.zig6
-rw-r--r--src/misc.h2
-rw-r--r--src/misc.zig16
12 files changed, 111 insertions, 171 deletions
diff --git a/src/GameDraw.cpp b/src/GameDraw.cpp
index 471786d..abb0919 100644
--- a/src/GameDraw.cpp
+++ b/src/GameDraw.cpp
@@ -25,8 +25,7 @@ extern int thirdperson;
 extern float multiplier;
 extern int nocolors;
 extern int visions;
-extern unsigned int gSourceID[100];
-extern unsigned int gSampleSet[100];
+extern unsigned int gSourceID[37];
 extern Camera camera;
 extern Sprites sprites;
 extern float camerashake;
diff --git a/src/GameInitDispose.cpp b/src/GameInitDispose.cpp
index 94ad884..ed767fd 100644
--- a/src/GameInitDispose.cpp
+++ b/src/GameInitDispose.cpp
@@ -26,8 +26,8 @@
 #include "misc.h"
 #include "Game.h"
 
-extern unsigned int gSourceID[100];
-extern unsigned int gSampleSet[100];
+extern unsigned int gSourceID[37];
+extern unsigned int gSampleSet[37];
 extern Camera camera;
 extern Skeleton testskeleton;
 extern Sprites sprites;
@@ -109,87 +109,88 @@ Game* makeGame(Config config)
 
 void LoadSounds(bool musictoggle)
 {
-	// generate ten OpenAL sample sets and two sources
-	alGenBuffers(37, gSampleSet);
-	alGenSources(37, gSourceID);
-
-	// load up some audio data...
-	loadOgg((char*) ":Data:Sounds:underwater.ogg",
-		gSampleSet[visionsound], gSourceID[visionsound]);
-	loadOgg((char*) ":Data:Sounds:soulin.ogg",
-		gSampleSet[soulinsound], gSourceID[soulinsound]);
-	loadOgg((char*) ":Data:Sounds:soulout.ogg",
-		gSampleSet[souloutsound], gSourceID[souloutsound]);
-	loadOgg((char*) ":Data:Sounds:footstep1.ogg",
-		gSampleSet[footstepsound], gSourceID[footstepsound]);
-	loadOgg((char*) ":Data:Sounds:footstep2.ogg",
-		gSampleSet[footstepsound+1], gSourceID[footstepsound+1]);
-	loadOgg((char*) ":Data:Sounds:footstep3.ogg",
-		gSampleSet[footstepsound+2], gSourceID[footstepsound+2]);
-	loadOgg((char*) ":Data:Sounds:footstep4.ogg",
-		gSampleSet[footstepsound+3], gSourceID[footstepsound+3]);
-	loadOgg((char*) ":Data:Sounds:footstep5.ogg",
-		gSampleSet[footstepsound+4], gSourceID[footstepsound+4]);
-	loadOgg((char*) ":Data:Sounds:bodyland.ogg",
-		gSampleSet[bodylandsound], gSourceID[bodylandsound]);
-	loadOgg((char*) ":Data:Sounds:headland.ogg",
-		gSampleSet[headlandsound], gSourceID[headlandsound]);
-	loadOgg((char*) ":Data:Sounds:sniperrifle.ogg",
-		gSampleSet[riflesound], gSourceID[riflesound]);
-	loadOgg((char*) ":Data:Sounds:BodyHit.ogg",
-		gSampleSet[bodyhitsound], gSourceID[bodyhitsound]);
-	loadOgg((char*) ":Data:Sounds:WallHit.ogg",
-		gSampleSet[wallhitsound], gSourceID[wallhitsound]);
-	loadOgg((char*) ":Data:Sounds:machinegun.ogg",
-		gSampleSet[machinegunsound], gSourceID[machinegunsound]);
-	loadOgg((char*) ":Data:Sounds:Nearbullet.ogg",
-		gSampleSet[nearbulletsound], gSourceID[nearbulletsound]);
-	loadOgg((char*) ":Data:Sounds:riflewhack.ogg",
-		gSampleSet[headwhacksound], gSourceID[headwhacksound]);
-	loadOgg((char*) ":Data:Sounds:headshot.ogg",
-		gSampleSet[headshotsound], gSourceID[headshotsound]);
-	loadOgg((char*) ":Data:Sounds:reload.ogg",
-		gSampleSet[reloadsound], gSourceID[reloadsound]);
-	loadOgg((char*) ":Data:Sounds:click.ogg",
-		gSampleSet[clicksound], gSourceID[clicksound]);
-	loadOgg((char*) ":Data:Sounds:SW.ogg",
-		gSampleSet[pistol1sound], gSourceID[pistol1sound]);
-	loadOgg((char*) ":Data:Sounds:glock.ogg",
-		gSampleSet[pistol2sound], gSourceID[pistol2sound]);
-	loadOgg((char*) ":Data:Sounds:pinpull.ogg",
-		gSampleSet[pinpullsound], gSourceID[pinpullsound]);
-	loadOgg((char*) ":Data:Sounds:pinreplace.ogg",
-		gSampleSet[pinreplacesound], gSourceID[pinreplacesound]);
-	loadOgg((char*) ":Data:Sounds:handlerelease.ogg",
-		gSampleSet[grenadethrowsound], gSourceID[grenadethrowsound]);
-	loadOgg((char*) ":Data:Sounds:bounce.ogg",
-		gSampleSet[bouncesound], gSourceID[bouncesound]);
-	loadOgg((char*) ":Data:Sounds:bounce2.ogg",
-		gSampleSet[bounce2sound], gSourceID[bounce2sound]);
-	loadOgg((char*) ":Data:Sounds:explosion.ogg",
-		gSampleSet[explosionsound], gSourceID[explosionsound]);
-	loadOgg((char*) ":Data:Sounds:headland.ogg",
-		gSampleSet[bodywhacksound], gSourceID[bodywhacksound]);
-	loadOgg((char*) ":Data:Sounds:rain.ogg",
-		gSampleSet[rainsound], gSourceID[rainsound]);
-	loadOgg((char*) ":Data:Sounds:Lose.ogg",
-		gSampleSet[losesound], gSourceID[losesound]);
-	loadOgg((char*) ":Data:Sounds:Disguisekill.ogg",
-		gSampleSet[disguisekillsound], gSourceID[disguisekillsound]);
-	loadOgg((char*) ":Data:Sounds:knifeslash.ogg",
-		gSampleSet[knifeslashsound], gSourceID[knifeslashsound]);
-	loadOgg((char*) ":Data:Sounds:shotgun.ogg",
-		gSampleSet[shotgunsound], gSourceID[shotgunsound]);
+	gSampleSet[visionsound] = loadSound("underwater.ogg");
+	gSampleSet[soulinsound] = loadSound("soul-in.ogg");
+	gSampleSet[souloutsound] = loadSound("soul-out.ogg");
+	gSampleSet[footstepsound + 0] = loadSound("footstep1.ogg");
+	gSampleSet[footstepsound + 1] = loadSound("footstep2.ogg");
+	gSampleSet[footstepsound + 2] = loadSound("footstep3.ogg");
+	gSampleSet[footstepsound + 3] = loadSound("footstep4.ogg");
+	gSampleSet[footstepsound + 4] = loadSound("footstep5.ogg");
+	gSampleSet[bodylandsound] = loadSound("bodyland.ogg");
+	gSampleSet[headlandsound] = loadSound("headland.ogg");
+	gSampleSet[riflesound] = loadSound("sniperrifle.ogg");
+	gSampleSet[machinegunsound] = loadSound("machinegun.ogg");
+	gSampleSet[bodyhitsound] = loadSound("BodyHit.ogg");
+	gSampleSet[wallhitsound] = loadSound("WallHit.ogg");
+	gSampleSet[machinegunsound] = loadSound("machinegun.ogg");
+	gSampleSet[nearbulletsound] = loadSound("Nearbullet.ogg");
+	gSampleSet[headwhacksound] = loadSound("riflewhack.ogg");
+	gSampleSet[headshotsound] = loadSound("headshot.ogg");
+	gSampleSet[reloadsound] = loadSound("reload.ogg");
+	gSampleSet[clicksound] = loadSound("click.ogg");
+	gSampleSet[pistol1sound] = loadSound("SW.ogg");
+	gSampleSet[pistol2sound] = loadSound("glock.ogg");
+	gSampleSet[pinpullsound] = loadSound("pinpull.ogg");
+	gSampleSet[pinreplacesound] = loadSound("pinreplace.ogg");
+	gSampleSet[grenadethrowsound] = loadSound("handlerelease.ogg");
+	gSampleSet[bouncesound] = loadSound("bounce.ogg");
+	gSampleSet[bounce2sound] = loadSound("bounce2.ogg");
+	gSampleSet[explosionsound] = loadSound("explosion.ogg");
+	gSampleSet[headlandsound] = loadSound("headland.ogg");
+	gSampleSet[rainsound] = loadSound("rain.ogg");
+	gSampleSet[losesound] = loadSound("Lose.ogg");
+	gSampleSet[disguisekillsound] = loadSound("disguise-kill.ogg");
+	gSampleSet[knifeslashsound] = loadSound("knifeslash.ogg");
+	gSampleSet[shotgunsound] = loadSound("shotgun.ogg");
+	if (musictoggle) {
+		gSampleSet[mainmenusong] = loadSound("mainmenusong.ogg");
+		gSampleSet[shootsong] = loadSound("shootsong.ogg");
+		gSampleSet[zombiesong] = loadSound("zombiesong.ogg");
+		gSampleSet[knifesong] = loadSound("knifesong.ogg");
+	}
+
+	alGenSources(33, gSourceID);
+	alSourcei(gSourceID[visionsound], AL_BUFFER, gSampleSet[visionsound]);
+	alSourcei(gSourceID[soulinsound], AL_BUFFER, gSampleSet[soulinsound]);
+	alSourcei(gSourceID[souloutsound], AL_BUFFER, gSampleSet[souloutsound]);
+	alSourcei(gSourceID[footstepsound + 0], AL_BUFFER, gSampleSet[footstepsound + 0]);
+	alSourcei(gSourceID[footstepsound + 1], AL_BUFFER, gSampleSet[footstepsound + 1]);
+	alSourcei(gSourceID[footstepsound + 2], AL_BUFFER, gSampleSet[footstepsound + 2]);
+	alSourcei(gSourceID[footstepsound + 3], AL_BUFFER, gSampleSet[footstepsound + 3]);
+	alSourcei(gSourceID[footstepsound + 4], AL_BUFFER, gSampleSet[footstepsound + 4]);
+	alSourcei(gSourceID[bodylandsound], AL_BUFFER, gSampleSet[bodylandsound]);
+	alSourcei(gSourceID[headlandsound], AL_BUFFER, gSampleSet[headlandsound]);
+	alSourcei(gSourceID[riflesound], AL_BUFFER, gSampleSet[riflesound]);
+	alSourcei(gSourceID[bodyhitsound], AL_BUFFER, gSampleSet[bodyhitsound]);
+	alSourcei(gSourceID[wallhitsound], AL_BUFFER, gSampleSet[wallhitsound]);
+	alSourcei(gSourceID[machinegunsound], AL_BUFFER, gSampleSet[machinegunsound]);
+	alSourcei(gSourceID[nearbulletsound], AL_BUFFER, gSampleSet[nearbulletsound]);
+	alSourcei(gSourceID[headwhacksound], AL_BUFFER, gSampleSet[headwhacksound]);
+	alSourcei(gSourceID[headshotsound], AL_BUFFER, gSampleSet[headshotsound]);
+	alSourcei(gSourceID[reloadsound], AL_BUFFER, gSampleSet[reloadsound]);
+	alSourcei(gSourceID[clicksound], AL_BUFFER, gSampleSet[clicksound]);
+	alSourcei(gSourceID[pistol1sound], AL_BUFFER, gSampleSet[pistol1sound]);
+	alSourcei(gSourceID[pistol2sound], AL_BUFFER, gSampleSet[pistol2sound]);
+	alSourcei(gSourceID[pinpullsound], AL_BUFFER, gSampleSet[pinpullsound]);
+	alSourcei(gSourceID[pinreplacesound], AL_BUFFER, gSampleSet[pinreplacesound]);
+	alSourcei(gSourceID[grenadethrowsound], AL_BUFFER, gSampleSet[grenadethrowsound]);
+	alSourcei(gSourceID[bouncesound], AL_BUFFER, gSampleSet[bouncesound]);
+	alSourcei(gSourceID[bounce2sound], AL_BUFFER, gSampleSet[bounce2sound]);
+	alSourcei(gSourceID[explosionsound], AL_BUFFER, gSampleSet[explosionsound]);
+	alSourcei(gSourceID[bodywhacksound], AL_BUFFER, gSampleSet[bodywhacksound]);
+	alSourcei(gSourceID[rainsound], AL_BUFFER, gSampleSet[rainsound]);
+	alSourcei(gSourceID[losesound], AL_BUFFER, gSampleSet[losesound]);
+	alSourcei(gSourceID[disguisekillsound], AL_BUFFER, gSampleSet[disguisekillsound]);
+	alSourcei(gSourceID[knifeslashsound], AL_BUFFER, gSampleSet[knifeslashsound]);
+	alSourcei(gSourceID[shotgunsound], AL_BUFFER, gSampleSet[shotgunsound]);
 
 	if (musictoggle) {
-		loadOgg((char*) ":Data:Sounds:mainmenusong.ogg",
-			gSampleSet[mainmenusong], gSourceID[mainmenusong]);
-		loadOgg((char*) ":Data:Sounds:shootsong.ogg",
-			gSampleSet[shootsong], gSourceID[shootsong]);
-		loadOgg((char*) ":Data:Sounds:zombiesong.ogg",
-			gSampleSet[zombiesong], gSourceID[zombiesong]);
-		loadOgg((char*) ":Data:Sounds:knifesong.ogg",
-			gSampleSet[knifesong], gSourceID[knifesong]);
+		alGenSources(4, gSourceID + 33);
+		alSourcei(gSourceID[mainmenusong], AL_BUFFER, gSampleSet[mainmenusong]);
+		alSourcei(gSourceID[shootsong], AL_BUFFER, gSampleSet[shootsong]);
+		alSourcei(gSourceID[zombiesong], AL_BUFFER, gSampleSet[zombiesong]);
+		alSourcei(gSourceID[knifesong], AL_BUFFER, gSampleSet[knifesong]);
 	}
 
 	alListenerfv(AL_POSITION, {});
@@ -1677,5 +1678,6 @@ void closeGame(Game* game)
 		game->flaretextureptr,
 	};
 	glDeleteTextures(4, textures);
-	alDeleteSources(100, gSourceID); // delete sound sources
+	alDeleteSources(33 + game->musictoggle * 4, gSourceID);
+	alDeleteBuffers(33 + game->musictoggle * 4, gSampleSet);
 }
diff --git a/src/GameLoop.cpp b/src/GameLoop.cpp
index 77ec774..b3810c6 100644
--- a/src/GameLoop.cpp
+++ b/src/GameLoop.cpp
@@ -25,8 +25,7 @@ extern float multiplier;
 extern int thirdperson;
 extern int visions;
 extern float soundscalefactor;
-extern unsigned int gSourceID[100];
-extern unsigned int gSampleSet[100];
+extern unsigned int gSourceID[37];
 extern Camera camera;
 extern float rad2deg;
 extern Fog fog;
diff --git a/src/GameTick.cpp b/src/GameTick.cpp
index efc473f..57b2efe 100644
--- a/src/GameTick.cpp
+++ b/src/GameTick.cpp
@@ -28,8 +28,7 @@ extern float multiplier;
 extern int thirdperson;
 extern int visions;
 extern Sprites sprites;
-extern unsigned int gSourceID[100];
-extern unsigned int gSampleSet[100];
+extern unsigned int gSourceID[37];
 extern Camera camera;
 extern float camerashake;
 extern Fog fog;
diff --git a/src/Globals.cpp b/src/Globals.cpp
index 6fdad88..d1ae639 100644
--- a/src/Globals.cpp
+++ b/src/Globals.cpp
@@ -9,8 +9,8 @@ float sinefluctprog;
 
 float multiplier = 0.0f;
 
-unsigned int gSourceID[100]; // hundred source IDs
-unsigned int gSampleSet[100]; // hundred sample set ID numbers
+unsigned int gSourceID[37];
+unsigned int gSampleSet[37];
 
 float precipitationhorz;
 float precipitationvert;
diff --git a/src/Person.cpp b/src/Person.cpp
index d529143..ea2280c 100644
--- a/src/Person.cpp
+++ b/src/Person.cpp
@@ -22,8 +22,7 @@
 #include "Person.h"
 
 extern float multiplier;
-extern unsigned int gSourceID[100];
-extern unsigned int gSampleSet[100];
+extern unsigned int gSourceID[37];
 extern Animation animation[30];
 extern int thirdperson;
 extern int visions;
diff --git a/src/Skeleton.cpp b/src/Skeleton.cpp
index f1f3ff5..4f2a2a7 100644
--- a/src/Skeleton.cpp
+++ b/src/Skeleton.cpp
@@ -4,8 +4,7 @@
 #include "Serialize.h"
 
 extern float multiplier;
-extern unsigned int gSourceID[100];
-extern unsigned int gSampleSet[100];
+extern unsigned int gSourceID[37];
 extern int visions;
 extern float rad2deg;
 extern Camera camera;
diff --git a/src/Support.cpp b/src/Support.cpp
index f7e726a..d070faa 100644
--- a/src/Support.cpp
+++ b/src/Support.cpp
@@ -7,7 +7,6 @@
 #include <unistd.h>
 
 #include <GLFW/glfw3.h>
-#include <vorbis/vorbisfile.h>
 
 #include "Support.h"
 #include "Files.h"
@@ -195,71 +194,3 @@ void Files::EndLoad()
 
 	sFile = -1;
 }
-
-/* Read the requested Ogg file and load it into OpenAL */
-void loadOgg(char *filename, ALuint buffer, ALuint source)
-{
-	/* Try to find the real file (and place it in filename1) */
-	char filename1[MAX_PATH];
-	fix_filename(filename, filename1);
-
-	FILE* fp = fopen(filename1, "rb");
-	if (fp == NULL) {
-		fprintf(stderr, "ERROR: unable to open %s\n", filename1);
-		exit(EXIT_FAILURE);
-	}
-
-	OggVorbis_File vf;
-	int error = -ov_open(fp, &vf, NULL, 0);
-	if (error > 0) {
-		fprintf(stderr, "ERROR: vorbis error %d opening %s\n",
-			error, filename1);
-		exit(EXIT_FAILURE);
-	}
-
-	vorbis_info* vi = ov_info(&vf, -1);
-	if (vi == NULL) {
-		fprintf(stderr,
-			"ERROR: vorbis error opening %s (ov_info failed)\n",
-			filename1);
-		exit(EXIT_FAILURE);
-	}
-
-	/* Hack around some possible ogg vorbis weirdness */
-	ALsizei size = vi->channels * 2 * ov_pcm_total(&vf, -1);
-	ALvoid* data = malloc(((size + 2047) / 2048 + 1) * 2048);
-	if (data == NULL) {
-		fprintf(stderr,
-			"ERROR: could not allocate %d bytes while loading %s\n",
-			size, filename1);
-		exit(EXIT_FAILURE);
-	}
-
-	char* i = (char*) data;
-	int section;
-	long ret;
-#if BYTE_ORDER == BIG_ENDIAN
-	while (ret = ov_read(&vf, i, 1024, 1, 2, 1, &section))
-#else
-	while (ret = ov_read(&vf, i, 1024, 0, 2, 1, &section))
-#endif
-		if (ret > 0) /* XXX: How about negative ret? */
-			i += ret;
-
-	switch (vi->channels) {
-	case 1:
-		alBufferData(buffer, AL_FORMAT_MONO16, data, size, vi->rate);
-		break;
-	case 2:
-		alBufferData(buffer, AL_FORMAT_STEREO16, data, size, vi->rate);
-		break;
-	default:
-		fprintf(stderr, "ERROR: ogg %s has %d channels\n",
-			filename1, vi->channels);
-		exit(EXIT_FAILURE);
-	}
-
-	free(data);
-	ov_clear(&vf);
-	alSourcei(source, AL_BUFFER, buffer);
-}
diff --git a/src/Support.h b/src/Support.h
index b033272..cd71fc0 100644
--- a/src/Support.h
+++ b/src/Support.h
@@ -4,8 +4,6 @@
 #include <stdio.h>
 #include <unistd.h>
 
-#include <AL/al.h>
-
 #define fsFromStart SEEK_SET
 
 typedef char* Str255;
@@ -28,8 +26,6 @@ typedef struct UnsignedWide
 int Random();
 bool IsKeyDown(int key);
 
-void loadOgg(char* filename, ALuint buffer, ALuint source);
-
 FILE *cfh_fopen(const char *filename, const char *mode);
 
 #endif
diff --git a/src/main.zig b/src/main.zig
index e6eea7b..4b116c8 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -23,10 +23,12 @@ const configuration = @import("config.zig");
 const gf = @import("gfz");
 const gl = @import("zgl");
 const legacy = @cImport(@cInclude("Game.h"));
+const misc = @import("misc.zig");
 
 comptime {
     // Work around lazy compilation.
-    @export(@import("misc.zig").loadTexture, .{ .name = "loadTexture" });
+    @export(misc.loadSound, .{ .name = "loadSound" });
+    @export(misc.loadTexture, .{ .name = "loadTexture" });
 }
 
 var game: *legacy.Game = undefined;
@@ -78,7 +80,7 @@ pub fn main() !void {
     defer device.deinit() catch unreachable;
     const context = try al.Context.init(device, &.{});
     defer context.deinit() catch unreachable;
-    try al.useContext(context);
+    try context.makeCurrent();
 
     legacy.initGame(game);
     defer legacy.closeGame(game);
diff --git a/src/misc.h b/src/misc.h
index b3a7f43..e3bef7d 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -1,8 +1,10 @@
 #ifndef BLACKSHADES_MISC_H
 #define BLACKSHADES_MISC_H
 
+#include <AL/al.h>
 #include <GL/gl.h>
 
+extern "C" ALuint loadSound(const char*);
 extern "C" GLuint loadTexture(const char*);
 
 #endif // BLACKSHADES_MISC_H
diff --git a/src/misc.zig b/src/misc.zig
index 9015a02..9ad5933 100644
--- a/src/misc.zig
+++ b/src/misc.zig
@@ -22,14 +22,26 @@ usingnamespace @cImport({
     @cInclude("lodepng.h");
 });
 
+const al = @import("zeal");
 const allocator = std.heap.c_allocator;
 const cwd = std.fs.cwd;
 const data_dir = @import("build_options").data_dir ++ [_]u8{ sep };
 const free = std.c.free;
-const sep = std.fs.path.sep;
+const join = std.fs.path.joinZ;
 const maxInt = std.math.maxInt;
-const std = @import("std");
+const sep = std.fs.path.sep;
 const span = std.mem.span;
+const std = @import("std");
+
+pub fn loadSound(filename: [*:0]const u8) callconv(.C) u32 {
+    const path = join(allocator, &.{ data_dir ++ "sounds", span(filename) })
+        catch unreachable;
+    defer allocator.free(path);
+    const audio = al.Audio.read(allocator, span(path)) catch unreachable;
+    defer audio.free();
+    const buffer = al.Buffer.init(audio) catch unreachable;
+    return buffer.reference;
+}
 
 fn check(errorString: fn (c_uint) callconv(.C) [*c]const u8,
          status: anytype) void {