misc/openalbridge/wrappers.c
author mbait
Thu, 06 May 2010 11:33:25 +0000
changeset 3433 566788d32e52
parent 3364 e5403e2bf02c
child 3487 b1d00f1950c8
permissions -rw-r--r--
Teleport AI: * calculate max height from target and check collision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
     1
/*
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     2
* OpenAL Bridge - a simple portable library for OpenAL interface
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     3
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     4
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     5
* This program is free software; you can redistribute it and/or modify
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     7
* the Free Software Foundation; version 2 of the License
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     8
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
     9
* This program is distributed in the hope that it will be useful,
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    12
* GNU Lesser General Public License for more details.
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    13
*
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    15
* along with this program; if not, write to the Free Software
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    17
*/
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    18
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    19
#include "wrappers.h"
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    20
#include "openalbridge_t.h"
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    21
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    22
extern ALint *Sources;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    23
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    24
void *Malloc (size_t nbytes) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    25
    void *aptr;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    26
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    27
    if ((aptr = malloc(nbytes)) == NULL) {
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    28
        fprintf(stderr,"(Bridge Fatal Error) - not enough memory");
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    29
        abort();
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    30
    }
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    31
    
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    32
    return aptr;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    33
}
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    34
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    35
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    36
void *Realloc (void *aptr, size_t nbytes) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    37
    aptr = realloc(aptr, nbytes);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    38
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    39
    if (aptr == NULL) {
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    40
        fprintf(stderr,"(Bridge Fatal Error) - not enough memory");
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    41
        abort();
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    42
    }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    43
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    44
    return aptr;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    45
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    46
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    47
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    48
FILE *Fopen (const char *fname, char *mode)	{
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    49
    FILE *fp;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    50
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    51
    fp = fopen(fname,mode);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    52
    if (fp == NULL)
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
    53
        fprintf(stderr,"(Bridge Error) - can't open file %s in mode '%s'", fname, mode);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    54
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    55
    return fp;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    56
}
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    57
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    58
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    59
void helper_fade(void *tmp) {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    60
    ALfloat gain;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    61
    ALfloat target_gain;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    62
    fade_t *fade;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    63
    uint32_t index;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    64
    uint16_t quantity;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    65
    al_fade_t type;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    66
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    67
    fade = tmp;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    68
    index = fade->index;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    69
    quantity = fade->quantity;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    70
    type = fade->type;
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    71
    free (fade);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    72
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    73
    if (type == AL_FADE_IN) {
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    74
#ifdef DEBUG
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    75
        err_msg("(%s) INFO - Fade-in in progress [index %d quantity %d]", prog, index, quantity);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    76
#endif
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    77
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    78
        // save the volume desired after the fade
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    79
        alGetSourcef(Sources[index], AL_GAIN, &target_gain);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    80
        if (target_gain > 1.0f || target_gain <= 0.0f)
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    81
            target_gain = 1.0f;
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    82
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    83
        alSourcePlay(Sources[index]);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    84
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    85
        for (gain = 0.0f ; gain <= target_gain; gain += (float) quantity/10000) {
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    86
#ifdef TRACE
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    87
            err_msg("(%s) DEBUG - Fade-in set gain to %f", gain);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    88
#endif
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    89
            alSourcef(Sources[index], AL_GAIN, gain);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    90
            usleep(10000);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    91
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    92
    } else {
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    93
        alGetSourcef(Sources[index], AL_GAIN, &target_gain);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    94
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
    95
        for (gain = target_gain; gain >= 0.00f; gain -= (float) quantity/10000) {
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    96
#ifdef TRACE
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    97
            err_msg("(%s) DEBUG - Fade-out set gain to %f", gain);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    98
#endif
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
    99
            alSourcef(Sources[index], AL_GAIN, gain);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   100
            usleep(10000);
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   101
        }
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   102
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   103
        if (AL_NO_ERROR != alGetError())
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   104
            fprintf(stderr,"(Bridge Warning) - Failed to set fade-out effect");
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   105
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   106
        // stop that sound and reset its volume
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   107
        alSourceStop (Sources[index]);
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   108
        alSourcef (Sources[index], AL_GAIN, target_gain);
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   109
    }
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   110
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   111
    if (AL_NO_ERROR != alGetError())
3364
e5403e2bf02c no more hardcoded teams, team selection is real \o/
koda
parents: 3362
diff changeset
   112
        fprintf(stderr,"(Bridge Warning) - Failed to set fade effect");
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   113
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   114
#ifndef _WIN32
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   115
    pthread_exit(NULL);
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   116
#else
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   117
    _endthread();
3353
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   118
#endif
a767dd3786b5 OpenAL makes a comeback! (might require some cleanup)
koda
parents:
diff changeset
   119
}
3362
8d3b4d19ce27 in reprocessing openalbridge, a lot of code cleanup and simplification
koda
parents: 3353
diff changeset
   120