openalbridge/openalwrap.c
author nemo
Sun, 28 Jun 2009 14:37:26 +0000
changeset 2200 8192be6e3aef
parent 2194 1597710c6118
child 2209 2573d4ff78f9
permissions -rw-r--r--
koda/Smaxx changes to openal for crossplatform building
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     1
/*
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     2
 * OpenAL Bridge - a simple portable library for OpenAL interface
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     4
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     8
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    13
 *
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    17
 */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    18
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    19
#include "openalwrap.h"
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    20
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    21
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    22
extern "C" {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    23
#endif 
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
    24
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
    25
	typedef struct _fade_t {
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
    26
		int index;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
    27
		unsigned int quantity;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
    28
	} fade_t;
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    29
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    30
	// Sources are points emitting sound.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    31
	ALuint *Sources;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    32
	// Buffers hold sound data.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    33
	ALuint *Buffers;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    34
	//index for Sources and Buffers
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    35
	ALuint globalindex, globalsize;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    36
	// Position of the source sound.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    37
	ALfloat **SourcePos;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    38
	// Velocity of the source sound.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    39
	ALfloat **SourceVel;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    40
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    41
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    42
	ALint openal_close(void) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    43
		/* This function stops all the sounds, deallocates all memory and closes OpenAL */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    44
		int i;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    45
		ALCcontext *context;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    46
		ALCdevice  *device;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    47
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    48
		alSourceStopv	(globalsize, Sources);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    49
		alDeleteSources (globalsize, Sources);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    50
		alDeleteBuffers (globalsize, Buffers);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    51
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    52
		for (i = 0; i < globalsize; i++) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    53
			free(SourcePos[i]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    54
			free(SourceVel[i]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    55
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    56
		free(SourcePos);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    57
		free(SourceVel);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    58
		free(Sources);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    59
		free(Buffers);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    60
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    61
		context = alcGetCurrentContext();
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    62
		device  = alcGetContextsDevice(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    63
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    64
		alcMakeContextCurrent(NULL);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    65
		alcDestroyContext(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    66
		alcCloseDevice(device);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    67
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    68
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    69
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    70
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    71
	ALint openal_init(int memorysize) {	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    72
		/* This function initializes an OpenAL contex, allocates memory space for data and prepares OpenAL buffers*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    73
		ALCcontext *context;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    74
		ALCdevice *device;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    75
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    76
		const ALCchar *default_device;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    77
		// Position of the listener.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    78
		ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    79
		// Velocity of the listener.
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    80
		ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    81
		// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    82
		ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    83
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    84
		default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    85
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    86
		fprintf(stderr, "Using default device: %s\n", default_device);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    87
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    88
		if ((device = alcOpenDevice(default_device)) == NULL) {
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
    89
			fprintf(stderr, "ERROR: Failed to open sound device\n");
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    90
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    91
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    92
		context = alcCreateContext(device, NULL);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    93
		alcMakeContextCurrent(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    94
		alcProcessContext(context);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    95
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    96
		if (AlGetError("ERROR %d: Creating a new contex\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    97
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    98
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
    99
		//allocate memory space for buffers and sources
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   100
		globalsize = memorysize;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   101
		Buffers   = (ALuint*)   Malloc(sizeof(ALuint  )*globalsize);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   102
		Sources   = (ALuint*)   Malloc(sizeof(ALuint  )*globalsize);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   103
		SourcePos = (ALfloat**) Malloc(sizeof(ALfloat*)*globalsize);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   104
		SourceVel = (ALfloat**) Malloc(sizeof(ALfloat*)*globalsize);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   105
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   106
		//set the listener gain, position (on xyz axes), velocity (one value for each axe) and orientation
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   107
		alListenerf (AL_GAIN,		 1.0f		);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   108
		alListenerfv(AL_POSITION,    ListenerPos);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   109
		alListenerfv(AL_VELOCITY,    ListenerVel);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   110
		alListenerfv(AL_ORIENTATION, ListenerOri);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   111
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   112
		if (AlGetError("ERROR %d: Setting Listener properties\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   113
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   114
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   115
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   116
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   117
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   118
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   119
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   120
	int openal_loadfile (const char *filename){
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   121
		/* This function opens a file, loads into memory and allocates the Source buffer for playing*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   122
		ALenum format;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   123
		ALsizei bitsize;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   124
		ALsizei freq;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   125
		uint8_t *data;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   126
		uint32_t fileformat;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   127
		int i, error;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   128
		FILE *fp;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   129
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   130
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   131
		/*detect the file format, as written in the first 4 bytes of the header*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   132
		fp = Fopen (filename, "rb");
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   133
		if (fp == NULL)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   134
			return -1;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   135
		error = fread (&fileformat, sizeof(uint32_t), 1, fp);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   136
		fclose (fp);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   137
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   138
		if (error < 0) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   139
			fprintf(stderr, "ERROR: file %s is too short \n", filename);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   140
			return -2;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   141
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   142
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   143
		//prepare the buffers to receive data
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   144
		alGenBuffers(1, &Buffers[globalindex]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   145
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   146
		if (AlGetError("ERROR %d: Allocating memory for buffers\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   147
			return -3;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   148
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   149
		//prepare the sources to emit sound
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   150
		alGenSources(1, &Sources[globalindex]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   151
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   152
		if (AlGetError("ERROR %d: Allocating memory for sources\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   153
			return -4;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   154
				
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   155
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   156
		if (fileformat == 0x5367674F) //check if ogg
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   157
			error = load_OggVorbis (filename, &format, &data, &bitsize, &freq);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   158
		else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   159
			if (fileformat == 0x46464952) //check if wav
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   160
				error = load_WavPcm (filename, &format, &data, &bitsize, &freq);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   161
			else {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   162
				fprintf(stderr, "ERROR: File format (%08X) not supported!\n", invert_endianness(fileformat));
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   163
				return -5;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   164
			}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   165
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   166
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   167
		//copy pcm data in one buffer
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   168
		alBufferData(Buffers[globalindex], format, data, bitsize, freq);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   169
		free(data);		//deallocate data to save memory
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   170
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   171
		if (AlGetError("ERROR %d: Writing data to buffer\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   172
			return -5;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   173
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   174
		//memory allocation for source position and velocity
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   175
		SourcePos[globalindex] = (ALfloat*) Malloc(sizeof(ALfloat)*3);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   176
		SourceVel[globalindex] = (ALfloat*) Malloc(sizeof(ALfloat)*3);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   177
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   178
		if (SourcePos[globalindex] == NULL || SourceVel[globalindex] == NULL)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   179
			return -6;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   180
			
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   181
		//source properties that it will use when it's in playback
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   182
		for (i = 0; i < 3; i++) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   183
			SourcePos[globalindex][i] = 0.0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   184
			SourceVel[globalindex][i] = 0.1;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   185
		}	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   186
		alSourcei (Sources[globalindex], AL_BUFFER,   Buffers[globalindex]  );
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   187
		alSourcef (Sources[globalindex], AL_PITCH,    1.0f					);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   188
		alSourcef (Sources[globalindex], AL_GAIN,     1.0f					);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   189
		alSourcefv(Sources[globalindex], AL_POSITION, SourcePos[globalindex]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   190
		alSourcefv(Sources[globalindex], AL_VELOCITY, SourceVel[globalindex]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   191
		alSourcei (Sources[globalindex], AL_LOOPING,  0						);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   192
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   193
		if (AlGetError("ERROR %d: Setting source properties\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   194
			return -7;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   195
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   196
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   197
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   198
		//returns the index of the source you just loaded, increments it and exits
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   199
		return globalindex++;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   200
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   201
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   202
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   203
	ALint openal_toggleloop (int index){
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   204
		/*Set or unset looping mode*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   205
		ALint loop;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   206
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   207
		if (index >= globalsize) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   208
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   209
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   210
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   211
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   212
		alGetSourcei (Sources[index], AL_LOOPING, &loop);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   213
		alSourcei (Sources[index], AL_LOOPING, !((uint8_t) loop) & 0x00000001);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   214
		if (AlGetError("ERROR %d: Getting or setting loop property\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   215
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   216
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   217
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   218
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   219
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   220
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   221
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   222
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   223
	ALint openal_setvolume (int index, unsigned char percentage) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   224
		/*Set volume for sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   225
		if (index >= globalindex) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   226
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   227
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   228
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   229
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   230
		if (percentage > 100)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   231
			percentage = 100;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   232
		alSourcef (Sources[index], AL_GAIN, (ALfloat) percentage/100.0f);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   233
		if (AlGetError("ERROR %d: Setting volume for last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   234
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   235
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   236
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   237
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   238
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   239
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   240
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   241
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   242
	ALint openal_setglobalvolume (unsigned char percentage) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   243
		/*Set volume for all sounds*/		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   244
		if (percentage > 100)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   245
			percentage = 100;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   246
		alListenerf (AL_GAIN, (ALfloat) percentage/100.0f);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   247
		if (AlGetError("ERROR %d: Setting global volume\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   248
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   249
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   250
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   251
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   252
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   253
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   254
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   255
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   256
	ALint openal_togglemute () {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   257
		/*Mute or unmute sound*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   258
		ALfloat mute;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   259
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   260
		alGetListenerf (AL_GAIN, &mute);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   261
		if (mute > 0) 
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   262
			mute = 0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   263
		else
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   264
			mute = 1.0;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   265
		alListenerf (AL_GAIN, mute);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   266
		if (AlGetError("ERROR %d: Setting mute property\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   267
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   268
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   269
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   270
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   271
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   272
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   273
	
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   274
#ifndef _WIN32
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   275
	void *helper_fadeout(void *tmp) {
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   276
#else
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   277
	void WINAPI helper_fadeout(void *tmp) {	
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   278
#endif
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   279
		ALfloat gain;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   280
		fade_t *fade;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   281
		int index; 
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   282
		unsigned int quantity; 
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   283
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   284
		fade = tmp;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   285
		index = fade->index;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   286
		quantity = fade->quantity;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   287
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   288
		alGetSourcef(Sources[index], AL_GAIN, &gain);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   289
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   290
		for ( ; gain >= 0.00f; gain -= (float) quantity/10000){
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   291
#ifdef DEBUG
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   292
			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   293
#endif
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   294
			alSourcef(Sources[index], AL_GAIN, gain);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   295
			usleep(10000);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   296
		}
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   297
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   298
		AlGetError("ERROR %d: Setting fade out volume\n");
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   299
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   300
		//stop that sound and reset its gain
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   301
		alSourceStop (Sources[index]);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   302
		alSourcef (Sources[index], AL_GAIN, 1.0f);	
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   303
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   304
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   305
		pthread_exit(NULL);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   306
#else
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   307
		free(fade);
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   308
		_endthread();
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   309
#endif
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   310
	}
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   311
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   312
	ALint openal_fadeout(int index, unsigned int quantity) {
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   313
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   314
		pthread_t thread;
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   315
		fade_t fade;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   316
		fade.index = index;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   317
		fade.quantity = quantity;
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   318
#else
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   319
		HANDLE Thread;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   320
		DWORD threadID;
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   321
		fade_t *fade = malloc(sizeof(fade_t));
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   322
		fade->index = index;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   323
		fade->quantity = quantity;
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   324
#endif
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   325
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   326
		if (index >= globalindex) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   327
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   328
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   329
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   330
		
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   331
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   332
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   333
		pthread_create(&thread, NULL, helper_fadeout, (void*) &fade);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   334
		pthread_detach(thread);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   335
#else
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   336
		Thread = _beginthread(&helper_fadeout, 0, (void*) fade);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   337
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   338
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   339
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   340
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   341
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   342
	}
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   343
		
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   344
#ifndef _WIN32
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   345
		void *helper_fadein(void *tmp) 
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   346
#else
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   347
		void WINAPI helper_fadein(void *tmp) 
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   348
#endif
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   349
		{
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   350
			ALfloat gain;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   351
			fade_t *fade;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   352
			int index; 
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   353
			unsigned int quantity; 
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   354
			
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   355
			fade = tmp;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   356
			index = fade->index;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   357
			quantity = fade->quantity;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   358
			
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   359
			gain = 0.0f;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   360
			alSourcef(Sources[index], AL_GAIN, gain);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   361
			alSourcePlay(Sources[index]);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   362
			
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   363
			for ( ; gain <= 1.00f; gain += (float) quantity/10000){
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   364
#ifdef DEBUG
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   365
				fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   366
#endif
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   367
				alSourcef(Sources[index], AL_GAIN, gain);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   368
				usleep(10000);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   369
			}
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   370
			
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   371
			if (AlGetError("ERROR %d: Setting fade in volume\n") != AL_TRUE)
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   372
				return AL_FALSE;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   373
			
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   374
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   375
			pthread_exit(NULL);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   376
#else
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   377
			free(fade);
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   378
			_endthread();
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   379
#endif
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   380
		}
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   381
			
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   382
		
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   383
	ALint openal_fadein(int index, unsigned int quantity) {
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   384
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   385
		pthread_t thread;
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   386
		fade_t fade;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   387
		fade.index = index;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   388
		fade.quantity = quantity;
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   389
#else
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   390
		HANDLE Thread;
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   391
		DWORD threadID;
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   392
		fade_t *fade = malloc(sizeof(fade_t));
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   393
		fade->index = index;
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   394
		fade->quantity = quantity;
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   395
#endif
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   396
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   397
		if (index >= globalindex) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   398
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   399
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   400
		}
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   401
				
2194
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   402
#ifndef _WIN32
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   403
		pthread_create(&thread, NULL, helper_fadein, (void*) &fade);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   404
		pthread_detach(thread);
1597710c6118 koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
nemo
parents: 2191
diff changeset
   405
#else
2200
8192be6e3aef koda/Smaxx changes to openal for crossplatform building
nemo
parents: 2194
diff changeset
   406
		Thread = _beginthread(&helper_fadein, 0, (void*) fade);
2191
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   407
#endif
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   408
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   409
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   410
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   411
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   412
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   413
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   414
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   415
	ALint openal_playsound(int index){
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   416
		/*Play sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   417
		if (index >= globalindex) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   418
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   419
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   420
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   421
		alSourcePlay(Sources[index]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   422
		if (AlGetError("ERROR %d: Playing last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   423
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   424
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   425
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   426
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   427
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   428
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   429
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   430
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   431
	ALint openal_pausesound(int index){
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   432
		/*Pause sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   433
		if (index >= globalindex) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   434
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   435
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   436
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   437
		alSourcePause(Sources[index]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   438
		if (AlGetError("ERROR %d: Pausing last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   439
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   440
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   441
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   442
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   443
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   444
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   445
	ALint openal_stopsound(int index){
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   446
		/*Stop sound number index*/
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   447
		if (index >= globalindex) {
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   448
			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   449
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   450
		}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   451
		alSourceStop(Sources[index]);
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   452
		if (AlGetError("ERROR %d: Stopping last sound\n") != AL_TRUE)
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   453
			return AL_FALSE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   454
		
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   455
		alGetError();  /* clear any AL errors beforehand */
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   456
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   457
		return AL_TRUE;
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   458
	}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   459
	
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   460
#ifdef __CPLUSPLUS
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   461
}
20c62f787a4d koda's OpenAL conversion:
unc0rr
parents:
diff changeset
   462
#endif