1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-05 20:57:46 +02:00

#9 tab to space

This commit is contained in:
XProger
2016-10-04 04:12:48 +03:00
parent be5dae6e2d
commit 65917262bb
2 changed files with 80 additions and 80 deletions

View File

@@ -6,14 +6,14 @@
#define STB_VORBIS_HEADER_ONLY #define STB_VORBIS_HEADER_ONLY
#include "libs/stb_vorbis/stb_vorbis.c" #include "libs/stb_vorbis/stb_vorbis.c"
#define SND_CHANNELS_MAX 32 #define SND_CHANNELS_MAX 32
#define BUFFER_SIZE_MP3 8192 #define BUFFER_SIZE_MP3 8192
namespace Sound { namespace Sound {
struct Frame { struct Frame {
short L, R; short L, R;
}; };
struct Decoder { struct Decoder {
Stream *stream; Stream *stream;
@@ -77,12 +77,12 @@ namespace Sound {
sample = clamp(sample, -32768, 32767); sample = clamp(sample, -32768, 32767);
sample2 = sample1; sample2 = sample1;
sample1 = sample; sample1 = sample;
delta = max(table[nibble] * delta / 256, 16); delta = max(table[nibble] * delta / 256, 16);
return sample; return sample;
} }
} channel[2]; } channel[2];
virtual int decode(Frame *frames, int count) { virtual int decode(Frame *frames, int count) {
static const int coeff1[] = { 256, 512, 0, 192, 240, 460, 392 }; static const int coeff1[] = { 256, 512, 0, 192, 240, 460, 392 };
static const int coeff2[] = { 0, -256, 0, 64, 0, -208, -232 }; static const int coeff2[] = { 0, -256, 0, 64, 0, -208, -232 };
@@ -110,7 +110,7 @@ namespace Sound {
frames[1].R = channel[1].sample1; frames[1].R = channel[1].sample1;
} }
return 2; return 2;
} else { } else {
uint8 value; uint8 value;
stream->read(value); stream->read(value);
uint8 n1 = value >> 4, n2 = value & 0xF; uint8 n1 = value >> 4, n2 = value & 0xF;
@@ -125,7 +125,7 @@ namespace Sound {
return 1; return 1;
} }
} }
} }
}; };
struct MP3 : Decoder { struct MP3 : Decoder {
@@ -149,9 +149,9 @@ namespace Sound {
int i = 0; int i = 0;
char *ptr = (char*)frames; char *ptr = (char*)frames;
while (ptr < (char*)&frames[count]) { while (ptr < (char*)&frames[count]) {
int res = mp3_decode(mp3, buffer + pos, size - pos, (short*)ptr, &info); int res = mp3_decode(mp3, buffer + pos, size - pos, (short*)ptr, &info);
if (res) { if (res) {
pos += res; pos += res;
ptr += info.audio_bytes; ptr += info.audio_bytes;
i += info.audio_bytes; i += info.audio_bytes;
} else } else
@@ -191,7 +191,7 @@ namespace Sound {
int res = stb_vorbis_get_samples_short_interleaved(ogg, channels, (short*)frames + i, (count - i) * 2); int res = stb_vorbis_get_samples_short_interleaved(ogg, channels, (short*)frames + i, (count - i) * 2);
if (!res) break; if (!res) break;
i += res; i += res;
} }
return i; return i;
} }
}; };
@@ -201,19 +201,19 @@ namespace Sound {
vec3 velocity; vec3 velocity;
} listener; } listener;
enum Flags { enum Flags {
LOOP = 1, LOOP = 1,
PAN = 2, PAN = 2,
REVERB_NEAR = 4, REVERB_NEAR = 4,
REVERB_MIDDLE = 8, REVERB_MIDDLE = 8,
REVERB_FAR = 16, REVERB_FAR = 16,
}; };
struct Sample { struct Sample {
Decoder *decoder; Decoder *decoder;
float volume; float volume;
float pitch; float pitch;
int flags; int flags;
bool isPlaying; bool isPlaying;
Sample(Stream *stream, float volume, float pitch, int flags) : decoder(NULL), volume(volume), pitch(pitch), flags(flags), isPlaying(true) { Sample(Stream *stream, float volume, float pitch, int flags) : decoder(NULL), volume(volume), pitch(pitch), flags(flags), isPlaying(true) {
@@ -250,10 +250,10 @@ namespace Sound {
decoder = new OGG(stream, 2); decoder = new OGG(stream, 2);
} else if (fourcc == FOURCC("ID3\3")) { // mp3 } else if (fourcc == FOURCC("ID3\3")) { // mp3
decoder = new MP3(stream, 2); decoder = new MP3(stream, 2);
} }
ASSERT(decoder != NULL); ASSERT(decoder != NULL);
} }
~Sample() { ~Sample() {
delete decoder; delete decoder;
@@ -272,7 +272,7 @@ namespace Sound {
return true; return true;
} }
} *channels[SND_CHANNELS_MAX]; } *channels[SND_CHANNELS_MAX];
int channelsCount; int channelsCount;
void init() { void init() {
@@ -286,7 +286,7 @@ namespace Sound {
mp3_decode_free(); mp3_decode_free();
} }
void fill(Frame *frames, int count) { void fill(Frame *frames, int count) {
struct FrameHI { struct FrameHI {
int L, R; int L, R;
}; };
@@ -296,24 +296,24 @@ namespace Sound {
Frame *buffer = new Frame[count]; Frame *buffer = new Frame[count];
for (int i = 0; i < channelsCount; i++) { for (int i = 0; i < channelsCount; i++) {
memset(buffer, 0, sizeof(Frame) * count); memset(buffer, 0, sizeof(Frame) * count);
channels[i]->render(buffer, count); channels[i]->render(buffer, count);
for (int j = 0; j < count; j++) { for (int j = 0; j < count; j++) {
result[j].L += buffer[j].L; result[j].L += buffer[j].L;
result[j].R += buffer[j].R; result[j].R += buffer[j].R;
} }
} }
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
frames[i].L = clamp(result[i].L, -32768, 32767); frames[i].L = clamp(result[i].L, -32768, 32767);
frames[i].R = clamp(result[i].R, -32768, 32767); frames[i].R = clamp(result[i].R, -32768, 32767);
} }
delete[] buffer; delete[] buffer;
delete[] result; delete[] result;
for (int i = 0; i < channelsCount; i++) for (int i = 0; i < channelsCount; i++)
if (!channels[i]->isPlaying) { if (!channels[i]->isPlaying) {
@@ -321,7 +321,7 @@ namespace Sound {
channels[i] = channels[--channelsCount]; channels[i] = channels[--channelsCount];
i--; i--;
} }
} }
Stream *openWAD(const char *name) { Stream *openWAD(const char *name) {
Stream *stream = new Stream("cdaudio.wad"); Stream *stream = new Stream("cdaudio.wad");
@@ -344,13 +344,13 @@ namespace Sound {
return NULL; return NULL;
} }
void play(Stream *stream, float volume, float pitch, int flags) { void play(Stream *stream, float volume, float pitch, int flags) {
if (!stream) return; if (!stream) return;
if (channelsCount < SND_CHANNELS_MAX) if (channelsCount < SND_CHANNELS_MAX)
channels[channelsCount++] = new Sample(stream, volume, pitch, flags); channels[channelsCount++] = new Sample(stream, volume, pitch, flags);
else else
LOG("! no free channels\n"); LOG("! no free channels\n");
} }
} }
#endif #endif

View File

@@ -5,7 +5,7 @@
#include <stdint.h> #include <stdint.h>
void __cdecl operator delete(void *ptr, unsigned int size) { void __cdecl operator delete(void *ptr, unsigned int size) {
// //
} }
*/ */
#include "game.h" #include "game.h"
@@ -120,14 +120,14 @@ WAVEHDR waveBuf[2];
void soundFree() { void soundFree() {
if (!sndReady) return; if (!sndReady) return;
sndReady = false; sndReady = false;
EnterCriticalSection(&sndCS); EnterCriticalSection(&sndCS);
waveOutUnprepareHeader(waveOut, &waveBuf[0], sizeof(WAVEHDR)); waveOutUnprepareHeader(waveOut, &waveBuf[0], sizeof(WAVEHDR));
waveOutUnprepareHeader(waveOut, &waveBuf[1], sizeof(WAVEHDR)); waveOutUnprepareHeader(waveOut, &waveBuf[1], sizeof(WAVEHDR));
waveOutReset(waveOut); waveOutReset(waveOut);
waveOutClose(waveOut); waveOutClose(waveOut);
delete[] sndData; delete[] sndData;
LeaveCriticalSection(&sndCS); LeaveCriticalSection(&sndCS);
DeleteCriticalSection(&sndCS); DeleteCriticalSection(&sndCS);
} }
void CALLBACK sndFill(HWAVEOUT waveOut, UINT uMsg, DWORD_PTR dwInstance, LPWAVEHDR waveBuf, DWORD dwParam2) { void CALLBACK sndFill(HWAVEOUT waveOut, UINT uMsg, DWORD_PTR dwInstance, LPWAVEHDR waveBuf, DWORD dwParam2) {
@@ -137,45 +137,45 @@ void CALLBACK sndFill(HWAVEOUT waveOut, UINT uMsg, DWORD_PTR dwInstance, LPWAVEH
return; return;
} }
EnterCriticalSection(&sndCS); EnterCriticalSection(&sndCS);
waveOutUnprepareHeader(waveOut, waveBuf, sizeof(WAVEHDR)); waveOutUnprepareHeader(waveOut, waveBuf, sizeof(WAVEHDR));
Sound::fill((Sound::Frame*)waveBuf->lpData, SND_SIZE / 4); Sound::fill((Sound::Frame*)waveBuf->lpData, SND_SIZE / 4);
waveOutPrepareHeader(waveOut, waveBuf, sizeof(WAVEHDR)); waveOutPrepareHeader(waveOut, waveBuf, sizeof(WAVEHDR));
waveOutWrite(waveOut, waveBuf, sizeof(WAVEHDR)); waveOutWrite(waveOut, waveBuf, sizeof(WAVEHDR));
LeaveCriticalSection(&sndCS); LeaveCriticalSection(&sndCS);
} }
void soundInit(HWND hwnd) { void soundInit(HWND hwnd) {
InitializeCriticalSection(&sndCS); InitializeCriticalSection(&sndCS);
if (waveOutOpen(&waveOut, WAVE_MAPPER, &waveFmt, (INT_PTR)sndFill, 0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR) { if (waveOutOpen(&waveOut, WAVE_MAPPER, &waveFmt, (INT_PTR)sndFill, 0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR) {
sndReady = true; sndReady = true;
sndData = new char[SND_SIZE * 2]; sndData = new char[SND_SIZE * 2];
memset(&waveBuf, 0, sizeof(waveBuf)); memset(&waveBuf, 0, sizeof(waveBuf));
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
waveBuf[i].dwBufferLength = SND_SIZE; waveBuf[i].dwBufferLength = SND_SIZE;
waveBuf[i].lpData = sndData + SND_SIZE * i; waveBuf[i].lpData = sndData + SND_SIZE * i;
sndFill(waveOut, 0, 0, &waveBuf[i], 0); sndFill(waveOut, 0, 0, &waveBuf[i], 0);
} }
} else { } else {
sndReady = false; sndReady = false;
sndData = NULL; sndData = NULL;
} }
} }
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) { switch (msg) {
// window // window
case WM_ACTIVATE : case WM_ACTIVATE :
Input::reset(); Input::reset();
break; break;
case WM_SIZE: case WM_SIZE:
Core::width = LOWORD(lParam); Core::width = LOWORD(lParam);
Core::height = HIWORD(lParam); Core::height = HIWORD(lParam);
break; break;
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage(0); PostQuitMessage(0);
break; break;
// keyboard // keyboard
case WM_KEYDOWN : case WM_KEYDOWN :
case WM_KEYUP : case WM_KEYUP :
@@ -212,7 +212,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPara
return 1; return 1;
// touch // touch
// TODO // TODO
// sound // sound
default : default :
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
} }
@@ -253,12 +253,12 @@ int main() {
HWND hWnd = CreateWindow("static", "OpenLara", WS_OVERLAPPEDWINDOW, 0, 0, r.right - r.left, r.bottom - r.top, 0, 0, 0, 0); HWND hWnd = CreateWindow("static", "OpenLara", WS_OVERLAPPEDWINDOW, 0, 0, r.right - r.left, r.bottom - r.top, 0, 0, 0, 0);
HDC hDC = GetDC(hWnd); HDC hDC = GetDC(hWnd);
HGLRC hRC = initGL(hDC); HGLRC hRC = initGL(hDC);
joyInit(); joyInit();
soundInit(hWnd); soundInit(hWnd);
Game::init(); Game::init();
SetWindowLong(hWnd, GWL_WNDPROC, (LONG)&WndProc); SetWindowLong(hWnd, GWL_WNDPROC, (LONG)&WndProc);
ShowWindow(hWnd, SW_SHOWDEFAULT); ShowWindow(hWnd, SW_SHOWDEFAULT);
@@ -301,7 +301,7 @@ int main() {
} }
} while (msg.message != WM_QUIT); } while (msg.message != WM_QUIT);
soundFree(); soundFree();
Game::free(); Game::free();
freeGL(hRC); freeGL(hRC);