~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/freealut/src/alutInit.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "alutInternal.h"
 
2
 
 
3
static enum
 
4
{
 
5
  Unintialized,                 /* ALUT has not been initialized yet or has been de-initialised */
 
6
  ALUTDeviceAndContext,         /* alutInit has been called successfully */
 
7
  ExternalDeviceAndContext      /* alutInitWithoutContext has been called */
 
8
} initialisationState = Unintialized;
 
9
 
 
10
/*
 
11
 * Note: alutContext contains something valid only when initialisationState
 
12
 * contains ALUTDeviceAndContext.
 
13
 */
 
14
static ALCcontext *alutContext;
 
15
 
 
16
ALboolean _alutSanityCheck(void)
 
17
{
 
18
  ALCcontext *context;
 
19
 
 
20
  if (initialisationState == Unintialized)
 
21
  {
 
22
    _alutSetError(ALUT_ERROR_INVALID_OPERATION);
 
23
    return AL_FALSE;
 
24
  }
 
25
 
 
26
  context = alcGetCurrentContext();
 
27
  if (context == NULL)
 
28
  {
 
29
    _alutSetError(ALUT_ERROR_NO_CURRENT_CONTEXT);
 
30
    return AL_FALSE;
 
31
  }
 
32
 
 
33
  if (alGetError() != AL_NO_ERROR)
 
34
  {
 
35
    _alutSetError(ALUT_ERROR_AL_ERROR_ON_ENTRY);
 
36
    return AL_FALSE;
 
37
  }
 
38
 
 
39
  if (alcGetError(alcGetContextsDevice(context)) != ALC_NO_ERROR)
 
40
  {
 
41
    _alutSetError(ALUT_ERROR_ALC_ERROR_ON_ENTRY);
 
42
    return AL_FALSE;
 
43
  }
 
44
 
 
45
  return AL_TRUE;
 
46
}
 
47
 
 
48
ALboolean alutInit(int *argcp, char **argv)
 
49
{
 
50
  ALCdevice *device;
 
51
  ALCcontext *context;
 
52
 
 
53
  if (initialisationState != Unintialized)
 
54
  {
 
55
    _alutSetError(ALUT_ERROR_INVALID_OPERATION);
 
56
    return AL_FALSE;
 
57
  }
 
58
 
 
59
  if ((argcp == NULL) != (argv == NULL))
 
60
  {
 
61
    _alutSetError(ALUT_ERROR_INVALID_VALUE);
 
62
    return AL_FALSE;
 
63
  }
 
64
 
 
65
  device = alcOpenDevice(NULL);
 
66
  if (device == NULL)
 
67
  {
 
68
    _alutSetError(ALUT_ERROR_OPEN_DEVICE);
 
69
    return AL_FALSE;
 
70
  }
 
71
 
 
72
  context = alcCreateContext(device, NULL);
 
73
  if (context == NULL)
 
74
  {
 
75
    alcCloseDevice(device);
 
76
    _alutSetError(ALUT_ERROR_CREATE_CONTEXT);
 
77
    return AL_FALSE;
 
78
  }
 
79
 
 
80
  if (!alcMakeContextCurrent(context))
 
81
  {
 
82
    alcDestroyContext(context);
 
83
    alcCloseDevice(device);
 
84
    _alutSetError(ALUT_ERROR_MAKE_CONTEXT_CURRENT);
 
85
    return AL_FALSE;
 
86
  }
 
87
 
 
88
  initialisationState = ALUTDeviceAndContext;
 
89
  alutContext = context;
 
90
  return AL_TRUE;
 
91
}
 
92
 
 
93
ALboolean alutInitWithoutContext(int *argcp, char **argv)
 
94
{
 
95
  if (initialisationState != Unintialized)
 
96
  {
 
97
    _alutSetError(ALUT_ERROR_INVALID_OPERATION);
 
98
    return AL_FALSE;
 
99
  }
 
100
 
 
101
  if ((argcp == NULL) != (argv == NULL))
 
102
  {
 
103
    _alutSetError(ALUT_ERROR_INVALID_VALUE);
 
104
    return AL_FALSE;
 
105
  }
 
106
 
 
107
  initialisationState = ExternalDeviceAndContext;
 
108
  return AL_TRUE;
 
109
}
 
110
 
 
111
ALboolean alutExit(void)
 
112
{
 
113
  ALCdevice *device;
 
114
 
 
115
  if (initialisationState == Unintialized)
 
116
  {
 
117
    _alutSetError(ALUT_ERROR_INVALID_OPERATION);
 
118
    return AL_FALSE;
 
119
  }
 
120
 
 
121
  if (initialisationState == ExternalDeviceAndContext)
 
122
  {
 
123
    initialisationState = Unintialized;
 
124
    return AL_TRUE;
 
125
  }
 
126
 
 
127
  if (!_alutSanityCheck())
 
128
  {
 
129
    return AL_FALSE;
 
130
  }
 
131
 
 
132
  if (!alcMakeContextCurrent(NULL))
 
133
  {
 
134
    _alutSetError(ALUT_ERROR_MAKE_CONTEXT_CURRENT);
 
135
    return AL_FALSE;
 
136
  }
 
137
 
 
138
  device = alcGetContextsDevice(alutContext);
 
139
  alcDestroyContext(alutContext);
 
140
  if (alcGetError(device) != ALC_NO_ERROR)
 
141
  {
 
142
    _alutSetError(ALUT_ERROR_DESTROY_CONTEXT);
 
143
    return AL_FALSE;
 
144
  }
 
145
 
 
146
  if (!alcCloseDevice(device))
 
147
  {
 
148
    _alutSetError(ALUT_ERROR_CLOSE_DEVICE);
 
149
    return AL_FALSE;
 
150
  }
 
151
 
 
152
  initialisationState = Unintialized;
 
153
  return AL_TRUE;
 
154
}