caudio/Source/cMemorySource.cpp
Joshua Jones 28a22c7f73 Fixed problem with the msvc project not outputting a .lib file.
Updated tutorials to fix minor bugs and pathing issues.  Also fixed crash bug on failure to create the audio object.
Added msvc projects for all tutorials.
Updated the listener class to be self contained and only handle stuff related to the OpenAL listener.  It does NOT init OpenAL anymore, that has been moved to cAudioManager.
Extended the listener class to support all settings that native OpenAL supports.
Cleaned up cFileSource and fixed a crash bug on NULL file handle
Fixed returning bad audio buffer chunk sizes from the cOggDecoder on errors in the ogg stream
Seeking can now be down in fractions of a second now, changed the seconds field from int to float
Fixed various odd formatting.
Fixed potential crash bug in cMemorySource if memory could not be allocated.
cMemorySource will no longer clear the buffer you give to it before filling it with data.  This prevents an overwrite from happening in case of error but the user should provide a zeroed buffer to cMemorySource anyway for safety.
Relative seeking is now supported by cOggDecoder.
2009-08-08 05:51:32 +00:00

98 lines
1.7 KiB
C++

#include "../Headers/cMemorySource.h"
#include <cstring>
#include <iostream>
namespace cAudio
{
cMemorySource::cMemorySource(const void* data, int size, bool copy) : Data(NULL), Size(0), Valid(false), Pos(0)
{
if(data && size > 0)
{
Size = size;
if(copy)
{
Data = new char[Size];
if(Data)
memcpy(Data, data, Size);
}
else
{
Data = (char*)data;
}
if(Data)
Valid = true;
}
}
cMemorySource::~cMemorySource()
{
delete[] Data;
}
//!Returns true if the DataStream is valid
bool cMemorySource::isValid()
{
return Valid;
}
//!Returns the current position of the data stream.
int cMemorySource::getCurrentPos()
{
return Pos;
}
//!Returns the data stream size
int cMemorySource::getSize()
{
return Size;
}
//!Read current Data Stream Data
int cMemorySource::read(void* output, int size)
{
//memset(output, 0, size);
if(Pos+size <= Size)
{
memcpy(output, Data+Pos, size);
Pos += size;
return size;
}
else
{
int extra = (Pos+size) - Size;
int copied = size - extra;
memcpy(output, Data+Pos, copied);
Pos = Size;
return copied;
}
}
//!Seek the data stream
bool cMemorySource::seek(int amount, bool relative)
{
if(relative)
{
Pos += amount;
if(Pos > Size)
{
Pos = Size;
return false;
}
}
else
{
Pos = amount;
if(Pos > Size)
{
Pos = Size;
return false;
}
}
return true;
}
};