Merge remote-tracking branch 'Milek7/lua' into mover_in_c++

Merge remote-tracking branch 'Milek7/lua' into mover_in_c++
This commit is contained in:
firleju
2017-10-06 16:27:53 +02:00
parent c5097876e5
commit cd80c2ded6
12 changed files with 117 additions and 405 deletions

View File

@@ -80,7 +80,7 @@ if (WIN32)
add_definitions(-DHAVE_ROUND) # to make pymath to not redefine round
add_definitions(-DEU07_BUILD_STATIC) # to make pymath to not redefine round
set(SOURCES ${SOURCES} "windows.cpp" "Console.cpp" "Console/LPT.cpp" "Console/PoKeys55.cpp" "wavread.cpp")
set(SOURCES ${SOURCES} "windows.cpp" "Console.cpp" "Console/LPT.cpp" "Console/PoKeys55.cpp")
set(GLEW_INCLUDE_DIR ${GLEW_INCLUDE_DIR} "${DEPS_DIR}/glew/include/")
set(GLFW3_INCLUDE_DIR ${GLFW3_INCLUDE_DIR} "${DEPS_DIR}/glfw/include/")
set(GLUT_INCLUDE_DIR ${GLUT_INCLUDE_DIR} "${DEPS_DIR}/freeglut/include/")
@@ -93,25 +93,8 @@ if (WIN32)
set(PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR} "${DEPS_DIR}/python/include")
set(libserialport_INCLUDE_DIR ${LIBSERIALPORT_INCLUDE_DIR} "${DEPS_DIR}/libserialport/include")
if (${CMAKE_EXE_LINKER_FLAGS} STREQUAL "/machine:x86")
#set(GLEW_LIBRARY ${GLEW_LIBRARY} "${DEPS_DIR}/glew/lib/x86/glew32s.lib")
#set(GLFW3_LIBRARIES ${GLFW3_LIBRARIES} "${DEPS_DIR}/glfw/lib/x86/glfw3.lib")
#set(GLUT_glut_LIBRARY ${GLFW3_ROOT_PATH} "${DEPS_DIR}/freeglut/lib/x86/freeglut.lib")
#set(PNG_LIBRARY ${PNG_LIBRARY} "${DEPS_DIR}/libpng/lib/win32/libpng16.lib")
#set(OPENAL_LIBRARY ${OPENAL_LIBRARY} "${DEPS_DIR}/openal/lib/win32/OpenAL32.lib")
#set(LIBSNDFILE_LIBRARY ${LIBSNDFILE_LIBRARY} "${DEPS_DIR}/libsndfile/lib/win32/libsndfile-1.lib")
#set(LUAJIT_LIBRARIES ${LUAJIT_LIBRARIES} "${DEPS_DIR}/luajit/lib/win32/lua51.lib")
#set(PYTHON_LIBRARY ${PYTHON_LIBRARY} "${DEPS_DIR}/python/lib/x86/python27.lib")
elseif(${CMAKE_EXE_LINKER_FLAGS} STREQUAL "/machine:x64")
if(${CMAKE_EXE_LINKER_FLAGS} STREQUAL "/machine:x64")
set (ARCH "x64")
#set(GLEW_LIBRARY ${GLEW_LIBRARY} "${DEPS_DIR}/glew/lib/x64/glew32s.lib")
#set(GLFW3_LIBRARIES ${GLFW3_LIBRARIES} "${DEPS_DIR}/glfw/lib/x64/glfw3.lib")
#set(GLUT_glut_LIBRARY ${GLFW3_ROOT_PATH} "${DEPS_DIR}/freeglut/lib/x64/freeglut.lib")
#set(PNG_LIBRARY ${PNG_LIBRARY} "${DEPS_DIR}/libpng/lib/win64/libpng16.lib")
#set(OPENAL_LIBRARY ${OPENAL_LIBRARY} "${DEPS_DIR}/openal/lib/win64/OpenAL32.lib")
#set(LIBSNDFILE_LIBRARY ${LIBSNDFILE_LIBRARY} "${DEPS_DIR}/libsndfile/lib/win64/libsndfile-1.lib")
#set(LUAJIT_LIBRARIES ${LUAJIT_LIBRARIES} "${DEPS_DIR}/luajit/lib/win64/lua51.lib")
#set(PYTHON_LIBRARY ${PYTHON_LIBRARY} "${DEPS_DIR}/python/lib/x64/python27.lib")
endif()

View File

@@ -50,6 +50,10 @@ void TGauge::Init(TSubModel *NewSubModel, TGaugeType eNewType, double fNewScale,
}
else // a banan może być z optymalizacją?
NewSubModel->WillBeAnimated(); // wyłączenie ignowania jedynkowego transformu
float4x4 mat;
SubModel->ParentMatrix(&mat);
model_pos = *mat.TranslationGet();
}
};
@@ -318,7 +322,6 @@ void TGauge::UpdateValue()
};
// todo: ugly approach to getting train translation
// maybe cache gauge position
extern TWorld World;
@@ -331,16 +334,12 @@ void TGauge::play( sound *Sound )
if (SubModel && World.train())
{
float4x4 mat;
SubModel->ParentMatrix(&mat);
glm::vec3 pos = *mat.TranslationGet();
if (glm::length(pos) > 1.0f)
if (glm::length(model_pos) > 1.0f)
{
pos = glm::vec3(glm::vec4(pos, 1.0f) * glm::inverse((glm::mat4)World.train()->Dynamic()->mMatrix));
pos = pos + (glm::vec3)World.train()->Dynamic()->GetPosition();
auto pos = glm::vec3(glm::vec4(model_pos, 1.0f) * glm::inverse((glm::mat4)World.train()->Dynamic()->mMatrix));
pos += (glm::vec3)World.train()->Dynamic()->GetPosition();
Sound->set_mode(sound::anchored).dist(1.5f).position(pos);
Sound->set_mode(sound::anchored).dist(3.0f).position(pos);
}
}

View File

@@ -50,6 +50,8 @@ class TGauge {
void
play( sound* Sound );
glm::vec3 model_pos;
public:
TGauge() = default;
inline

View File

@@ -749,7 +749,6 @@ void Global::ConfigParse(cParser &Parser)
Global::uart_conf.enable = true;
Parser >> Global::uart_conf.port;
Parser >> Global::uart_conf.baud;
Parser >> Global::uart_conf.interval;
Parser >> Global::uart_conf.updatetime;
}
else if (token == "uarttune")
@@ -770,6 +769,19 @@ void Global::ConfigParse(cParser &Parser)
>> Global::uart_conf.currentmax
>> Global::uart_conf.currentuart;
}
else if (token == "uartfeature")
{
Parser.getTokens(4);
Parser >> Global::uart_conf.mainenable
>> Global::uart_conf.scndenable
>> Global::uart_conf.trainenable
>> Global::uart_conf.localenable;
}
else if (token == "uartdebug")
{
Parser.getTokens(1);
Parser >> Global::uart_conf.debug;
}
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
// na koniec trochę zależności
if (!bLoadTraction) // wczytywanie drutów i słupów

View File

@@ -298,7 +298,6 @@ class Global
bool enable = false;
std::string port;
int baud;
int interval;
float updatetime;
float mainbrakemin = 0.0f;
@@ -315,6 +314,13 @@ class Global
float hvuart = 65535.0f;
float currentmax = 10000.0f;
float currentuart = 65535.0f;
bool mainenable = true;
bool scndenable = true;
bool trainenable = true;
bool localenable = true;
bool debug = false;
};
static uart_conf_t uart_conf;

View File

@@ -1055,15 +1055,6 @@ bool TWorld::Update()
Ground.Update_Hidden();
Ground.Update_Lights();
{
glm::dmat4 cam_matrix;
Camera.SetMatrix(cam_matrix);
glm::vec3 pos(Camera.Pos.x, Camera.Pos.y, Camera.Pos.z);
sound_man->set_listener(pos, glm::mat3(cam_matrix));
sound_man->update(dt);
}
// render time routines follow:
dt = Timer::GetDeltaRenderTime(); // nie uwzględnia pauzowania ani mnożenia czasu
@@ -1094,6 +1085,16 @@ bool TWorld::Update()
Update_Camera( dt );
{
glm::dmat4 cam_matrix;
Camera.SetMatrix(cam_matrix);
glm::vec3 pos(Camera.Pos.x, Camera.Pos.y, Camera.Pos.z);
sound_man->set_listener(pos, glm::mat3(cam_matrix));
sound_man->update(Global::iPause ? 0.0f : dt);
}
GfxRenderer.Update( dt );
ResourceSweep();

View File

@@ -39,8 +39,8 @@ opengl_material::deserialize_mapping( cParser &Input, bool const Loadnow ) {
}
std::string path;
if( name.rfind( '\\' ) != std::string::npos ) {
path = name.substr( 0, name.rfind( '\\' ) + 1 );
if( name.rfind( '/' ) != std::string::npos ) {
path = name.substr( 0, name.rfind( '/' ) + 1 );
}
std::string key, value;

View File

@@ -348,9 +348,11 @@ sound& sound::position(Math3D::vector3 const &pos)
sound& sound::dist(float dist)
{
alSourcef(id, AL_MAX_DISTANCE, dist);
alSourcef(id, AL_REFERENCE_DISTANCE, dist / 3.82f);
max_dist = dist * 1.5f;
max_dist = dist * 2.0f;
float half_dist = dist / 3.82f;
alSourcef(id, AL_REFERENCE_DISTANCE, half_dist / 1.32f);
alSourcef(id, AL_ROLLOFF_FACTOR, 3.0f);
alSourcef(id, AL_MAX_DISTANCE, max_dist);
return *this;
}

View File

@@ -92,3 +92,4 @@
#define STRINGIZE_DETAIL(x) #x
#define STRINGIZE(x) STRINGIZE_DETAIL(x)
#define glDebug(x) if (GLEW_GREMEDY_string_marker) glStringMarkerGREMEDY(0, __FILE__ ":" STRINGIZE(__LINE__) ": " x);

116
uart.cpp
View File

@@ -15,11 +15,18 @@ uart_input::uart_input()
if (sp_open(port, SP_MODE_READ_WRITE) != SP_OK)
throw std::runtime_error("uart: cannot open port");
if (sp_set_baudrate(port, conf.baud) != SP_OK)
throw std::runtime_error("uart: cannot set baudrate");
sp_port_config *config;
if (sp_set_flowcontrol(port, SP_FLOWCONTROL_NONE) != SP_OK)
throw std::runtime_error("uart: cannot set flowcontrol");
if (sp_new_config(&config) != SP_OK ||
sp_set_config_baudrate(config, conf.baud) != SP_OK ||
sp_set_config_flowcontrol(config, SP_FLOWCONTROL_NONE) != SP_OK ||
sp_set_config_bits(config, 8) != SP_OK ||
sp_set_config_stopbits(config, 1) != SP_OK ||
sp_set_config_parity(config, SP_PARITY_NONE) != SP_OK ||
sp_set_config(port, config) != SP_OK)
throw std::runtime_error("uart: cannot set config");
sp_free_config(config);
if (sp_flush(port, SP_BUF_BOTH) != SP_OK)
throw std::runtime_error("uart: cannot flush");
@@ -30,6 +37,10 @@ uart_input::uart_input()
uart_input::~uart_input()
{
std::array<uint8_t, 31> buffer = { 0 };
sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0);
sp_drain(port);
sp_close(port);
sp_free_port(port);
}
@@ -44,17 +55,27 @@ void uart_input::poll()
last_update = now;
TTrain *t = Global::pWorld->train();
if (!t)
return;
sp_return ret;
if (sp_input_waiting(port) >= 16)
if ((ret = sp_input_waiting(port)) >= 16)
{
std::array<uint8_t, 16> buffer;
ret = sp_blocking_read(port, (void*)buffer.data(), buffer.size(), 0);
if (ret < 0)
throw std::runtime_error("uart: failed to read from port");
sp_drain(port);
if (conf.debug)
{
char buf[buffer.size() * 3 + 1];
size_t pos = 0;
for (uint8_t b : buffer)
pos += sprintf(&buf[pos], "%02X ", b);
WriteLog("uart: rx: " + std::string(buf));
}
data_pending = false;
for (auto entry : input_bits)
@@ -102,15 +123,14 @@ void uart_input::poll()
relay.post(std::get<1>(entry), 0, 0, action, 0, desired_state);
}
int mainctrl = buffer[6];
int scndctrl = buffer[7];
float trainbrake = (float)(((uint16_t)buffer[8] | ((uint16_t)buffer[9] << 8)) - conf.mainbrakemin) / (conf.mainbrakemax - conf.mainbrakemin);
float localbrake = (float)(((uint16_t)buffer[10] | ((uint16_t)buffer[11] << 8)) - conf.mainbrakemin) / (conf.localbrakemax - conf.localbrakemin);
t->set_mainctrl(mainctrl);
t->set_scndctrl(scndctrl);
t->set_trainbrake(trainbrake);
t->set_localbrake(localbrake);
if (conf.mainenable)
t->set_mainctrl(buffer[6]);
if (conf.scndenable)
t->set_scndctrl(buffer[7]);
if (conf.trainenable)
t->set_trainbrake((float)(((uint16_t)buffer[8] | ((uint16_t)buffer[9] << 8)) - conf.mainbrakemin) / (conf.mainbrakemax - conf.mainbrakemin));
if (conf.localenable)
t->set_localbrake((float)(((uint16_t)buffer[10] | ((uint16_t)buffer[11] << 8)) - conf.localbrakemin) / (conf.localbrakemax - conf.localbrakemin));
old_packet = buffer;
}
@@ -120,10 +140,10 @@ void uart_input::poll()
// TODO: ugly! move it into structure like input_bits
uint8_t buzzer = (uint8_t)t->get_alarm();
uint8_t tacho = (uint8_t)t->get_tacho();
uint16_t tank_press = (uint16_t)std::min(conf.tankuart, t->get_tank_pressure() / conf.tankmax * conf.tankuart);
uint16_t pipe_press = (uint16_t)std::min(conf.pipeuart, t->get_pipe_pressure() / conf.pipemax * conf.pipeuart);
uint16_t brake_press = (uint16_t)std::min(conf.brakeuart, t->get_brake_pressure() / conf.brakemax * conf.brakeuart);
uint8_t tacho = Global::iPause ? 0 : (uint8_t)t->get_tacho();
uint16_t tank_press = (uint16_t)std::min(conf.tankuart, t->get_tank_pressure() * 0.1f / conf.tankmax * conf.tankuart);
uint16_t pipe_press = (uint16_t)std::min(conf.pipeuart, t->get_pipe_pressure() * 0.1f / conf.pipemax * conf.pipeuart);
uint16_t brake_press = (uint16_t)std::min(conf.brakeuart, t->get_brake_pressure() * 0.1f / conf.brakemax * conf.brakeuart);
uint16_t hv_voltage = (uint16_t)std::min(conf.hvuart, t->get_hv_voltage() / conf.hvmax * conf.hvuart);
auto current = t->get_current();
uint16_t current1 = (uint16_t)std::min(conf.currentuart, current[0]) / conf.currentmax * conf.currentuart;
@@ -132,37 +152,35 @@ void uart_input::poll()
std::array<uint8_t, 31> buffer =
{
0, 0, //byte 0-1
tacho, //byte 2
0, 0, 0, 0, 0, 0, //byte 3-8
SPLIT_INT16(brake_press), //byte 9-10
SPLIT_INT16(pipe_press), //byte 11-12
SPLIT_INT16(tank_press), //byte 13-14
SPLIT_INT16(hv_voltage), //byte 15-16
SPLIT_INT16(current1), //byte 17-18
SPLIT_INT16(current2), //byte 19-20
SPLIT_INT16(current3), //byte 21-22
0, 0, 0, 0, 0, 0, 0, 0 //byte 23-30
tacho, //byte 0
0, //byte 1
(uint8_t)(t->btLampkaOpory.b() << 1 | t->btLampkaWysRozr.b() << 2), //byte 2
0, //byte 3
(uint8_t)(t->btLampkaOgrzewanieSkladu.b() << 0 | t->btLampkaOpory.b() << 1 |
t->btLampkaPoslizg.b() << 2 | t->btLampkaCzuwaka.b() << 6 |
t->btLampkaSHP.b() << 7), //byte 4
(uint8_t)(t->btLampkaStyczn.b() << 0 | t->btLampkaNadmPrzetw.b() << 2 |
t->btLampkaNadmSil.b() << 4 | t->btLampkaWylSzybki.b() << 5 |
t->btLampkaNadmSpr.b() << 6), //byte 5
(uint8_t)(buzzer << 7), //byte 6
SPLIT_INT16(brake_press), //byte 7-8
SPLIT_INT16(pipe_press), //byte 9-10
SPLIT_INT16(tank_press), //byte 11-12
SPLIT_INT16(hv_voltage), //byte 13-14
SPLIT_INT16(current1), //byte 15-16
SPLIT_INT16(current2), //byte 17-18
SPLIT_INT16(current3), //byte 19-20
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //byte 21-30
};
buffer[4] |= t->btLampkaOpory.b() << 1;
buffer[4] |= t->btLampkaWysRozr.b() << 2;
buffer[6] |= t->btLampkaOgrzewanieSkladu.b() << 0;
buffer[6] |= t->btLampkaOpory.b() << 1;
buffer[6] |= t->btLampkaPoslizg.b() << 2;
buffer[6] |= t->btLampkaCzuwaka.b() << 6;
buffer[6] |= t->btLampkaSHP.b() << 7;
buffer[7] |= t->btLampkaStyczn.b() << 0;
buffer[7] |= t->btLampkaNadmPrzetw.b() << 2;
buffer[7] |= t->btLampkaNadmSil.b() << 4;
buffer[7] |= t->btLampkaWylSzybki.b() << 5;
buffer[7] |= t->btLampkaNadmSpr.b() << 6;
buffer[8] |= buzzer << 7;
sp_flush(port, SP_BUF_INPUT); // flush input buffer in preparation for reply packet
if (conf.debug)
{
char buf[buffer.size() * 3 + 1];
size_t pos = 0;
for (uint8_t b : buffer)
pos += sprintf(&buf[pos], "%02X ", b);
WriteLog("uart: tx: " + std::string(buf));
}
ret = sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0);
if (ret != buffer.size())

View File

@@ -1,264 +0,0 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
//-----------------------------------------------------------------------------
// File: WavRead.cpp
//
// Desc: Wave file support for loading and playing Wave files using DirectSound
// buffers.
//
// Copyright (c) 1999 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "wavread.h"
#include "usefull.h"
//-----------------------------------------------------------------------------
// Name: ReadMMIO()
// Desc: Support function for reading from a multimedia I/O stream
//-----------------------------------------------------------------------------
HRESULT ReadMMIO(HMMIO hmmioIn, MMCKINFO *pckInRIFF, WAVEFORMATEX **ppwfxInfo)
{
MMCKINFO ckIn; // chunk info. for general use.
PCMWAVEFORMAT pcmWaveFormat; // Temp PCM structure to load in.
*ppwfxInfo = NULL;
if ((0 != mmioDescend(hmmioIn, pckInRIFF, NULL, 0)))
return E_FAIL;
if ((pckInRIFF->ckid != FOURCC_RIFF) || (pckInRIFF->fccType != mmioFOURCC('W', 'A', 'V', 'E')))
return E_FAIL;
// Search the input file for for the 'fmt ' chunk.
ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (0 != mmioDescend(hmmioIn, &ckIn, pckInRIFF, MMIO_FINDCHUNK))
return E_FAIL;
// Expect the 'fmt' chunk to be at least as large as <PCMWAVEFORMAT>;
// if there are extra parameters at the end, we'll ignore them
if (ckIn.cksize < (LONG)sizeof(PCMWAVEFORMAT))
return E_FAIL;
// Read the 'fmt ' chunk into <pcmWaveFormat>.
if (mmioRead(hmmioIn, (HPSTR)&pcmWaveFormat, sizeof(pcmWaveFormat)) != sizeof(pcmWaveFormat))
return E_FAIL;
// Allocate the waveformatex, but if its not pcm format, read the next
// word, and thats how many extra bytes to allocate.
if (pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM)
{
if (NULL == (*ppwfxInfo = new WAVEFORMATEX))
return E_FAIL;
// Copy the bytes from the pcm structure to the waveformatex structure
memcpy(*ppwfxInfo, &pcmWaveFormat, sizeof(pcmWaveFormat));
(*ppwfxInfo)->cbSize = 0;
}
else
{
// Read in length of extra bytes.
WORD cbExtraBytes = 0L;
if (mmioRead(hmmioIn, (CHAR *)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD))
return E_FAIL;
*ppwfxInfo = (WAVEFORMATEX *)new CHAR[sizeof(WAVEFORMATEX) + cbExtraBytes];
if (NULL == *ppwfxInfo)
return E_FAIL;
// Copy the bytes from the pcm structure to the waveformatex structure
memcpy(*ppwfxInfo, &pcmWaveFormat, sizeof(pcmWaveFormat));
(*ppwfxInfo)->cbSize = cbExtraBytes;
// Now, read those extra bytes into the structure, if cbExtraAlloc != 0.
if (mmioRead(hmmioIn, (CHAR *)(((BYTE *)&((*ppwfxInfo)->cbSize)) + sizeof(WORD)),
cbExtraBytes) != cbExtraBytes)
{
delete *ppwfxInfo;
*ppwfxInfo = NULL;
return E_FAIL;
}
}
// Ascend the input file out of the 'fmt ' chunk.
if (0 != mmioAscend(hmmioIn, &ckIn, 0))
{
delete *ppwfxInfo;
*ppwfxInfo = NULL;
return E_FAIL;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: WaveOpenFile()
// Desc: This function will open a wave input file and prepare it for reading,
// so the data can be easily read with WaveReadFile. Returns 0 if
// successful, the error code if not.
//-----------------------------------------------------------------------------
HRESULT WaveOpenFile( std::string const &Filename, HMMIO *phmmioIn, WAVEFORMATEX **ppwfxInfo,
MMCKINFO *pckInRIFF)
{
HRESULT hr;
HMMIO hmmioIn = NULL;
if (NULL == (hmmioIn = mmioOpen(const_cast<char*>(Filename.c_str()), NULL, MMIO_ALLOCBUF | MMIO_READ)))
return E_FAIL;
if (FAILED(hr = ReadMMIO(hmmioIn, pckInRIFF, ppwfxInfo)))
{
mmioClose(hmmioIn, 0);
return hr;
}
*phmmioIn = hmmioIn;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: WaveStartDataRead()
// Desc: Routine has to be called before WaveReadFile as it searches for the
// chunk to descend into for reading, that is, the 'data' chunk. For
// simplicity, this used to be in the open routine, but was taken out and
// moved to a separate routine so there was more control on the chunks
// that are before the data chunk, such as 'fact', etc...
//-----------------------------------------------------------------------------
HRESULT WaveStartDataRead(HMMIO *phmmioIn, MMCKINFO *pckIn, MMCKINFO *pckInRIFF)
{
// Seek to the data
if (-1 == mmioSeek(*phmmioIn, pckInRIFF->dwDataOffset + sizeof(FOURCC), SEEK_SET))
return E_FAIL;
// Search the input file for for the 'data' chunk.
pckIn->ckid = mmioFOURCC('d', 'a', 't', 'a');
if (0 != mmioDescend(*phmmioIn, pckIn, pckInRIFF, MMIO_FINDCHUNK))
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: WaveReadFile()
// Desc: Reads wave data from the wave file. Make sure we're descended into
// the data chunk before calling this function.
// hmmioIn - Handle to mmio.
// cbRead - # of bytes to read.
// pbDest - Destination buffer to put bytes.
// cbActualRead - # of bytes actually read.
//-----------------------------------------------------------------------------
HRESULT WaveReadFile(HMMIO hmmioIn, UINT cbRead, BYTE *pbDest, MMCKINFO *pckIn, UINT *cbActualRead)
{
MMIOINFO mmioinfoIn; // current status of <hmmioIn>
*cbActualRead = 0;
if (0 != mmioGetInfo(hmmioIn, &mmioinfoIn, 0))
return E_FAIL;
UINT cbDataIn = cbRead;
if (cbDataIn > pckIn->cksize)
cbDataIn = pckIn->cksize;
pckIn->cksize -= cbDataIn;
for (DWORD cT = 0; cT < cbDataIn; cT++)
{
// Copy the bytes from the io to the buffer.
if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
{
if (0 != mmioAdvance(hmmioIn, &mmioinfoIn, MMIO_READ))
return E_FAIL;
if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
return E_FAIL;
}
// Actual copy.
*((BYTE *)pbDest + cT) = *((BYTE *)mmioinfoIn.pchNext);
mmioinfoIn.pchNext++;
}
if (0 != mmioSetInfo(hmmioIn, &mmioinfoIn, 0))
return E_FAIL;
*cbActualRead = cbDataIn;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CWaveSoundRead()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CWaveSoundRead::CWaveSoundRead()
{
m_pwfx = NULL;
}
//-----------------------------------------------------------------------------
// Name: ~CWaveSoundRead()
// Desc: Destructs the class
//-----------------------------------------------------------------------------
CWaveSoundRead::~CWaveSoundRead()
{
Close();
SafeDelete(m_pwfx);
}
//-----------------------------------------------------------------------------
// Name: Open()
// Desc: Opens a wave file for reading
//-----------------------------------------------------------------------------
HRESULT CWaveSoundRead::Open(std::string const &Filename)
{
SafeDelete(m_pwfx);
HRESULT hr;
if (FAILED(hr = WaveOpenFile(Filename, &m_hmmioIn, &m_pwfx, &m_ckInRiff)))
return hr;
if (FAILED(hr = Reset()))
return hr;
return hr;
}
//-----------------------------------------------------------------------------
// Name: Reset()
// Desc: Resets the internal m_ckIn pointer so reading starts from the
// beginning of the file again
//-----------------------------------------------------------------------------
HRESULT CWaveSoundRead::Reset()
{
return WaveStartDataRead(&m_hmmioIn, &m_ckIn, &m_ckInRiff);
}
//-----------------------------------------------------------------------------
// Name: Read()
// Desc: Reads a wave file into a pointer and returns how much read
// using m_ckIn to determine where to start reading from
//-----------------------------------------------------------------------------
HRESULT CWaveSoundRead::Read(UINT nSizeToRead, BYTE *pbData, UINT *pnSizeRead)
{
return WaveReadFile(m_hmmioIn, nSizeToRead, pbData, &m_ckIn, pnSizeRead);
}
//-----------------------------------------------------------------------------
// Name: Close()
// Desc: Closes an open wave file
//-----------------------------------------------------------------------------
HRESULT CWaveSoundRead::Close()
{
if( m_hmmioIn != NULL ) {
mmioClose( m_hmmioIn, 0 );
}
return S_OK;
}

View File

@@ -1,48 +0,0 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
//-----------------------------------------------------------------------------
// File: WavRead.h
//
// Desc: Support for loading and playing Wave files using DirectSound sound
// buffers.
//
// Copyright (c) 1999 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#pragma once
#include <mmsystem.h>
#include <string>
HRESULT WaveOpenFile(std::string const &Filename, HMMIO *phmmioIn, WAVEFORMATEX **ppwfxInfo,
MMCKINFO *pckInRIFF);
HRESULT WaveStartDataRead(HMMIO *phmmioIn, MMCKINFO *pckIn, MMCKINFO *pckInRIFF);
HRESULT WaveReadFile(HMMIO hmmioIn, UINT cbRead, BYTE *pbDest, MMCKINFO *pckIn, UINT *cbActualRead);
//-----------------------------------------------------------------------------
// Name: class CWaveSoundRead
// Desc: A class to read in sound data from a Wave file
//-----------------------------------------------------------------------------
class CWaveSoundRead
{
public:
WAVEFORMATEX *m_pwfx; // Pointer to WAVEFORMATEX structure
HMMIO m_hmmioIn{ NULL }; // MM I/O handle for the WAVE
MMCKINFO m_ckIn; // Multimedia RIFF chunk
MMCKINFO m_ckInRiff; // Use in opening a WAVE file
public:
CWaveSoundRead();
~CWaveSoundRead();
HRESULT Open(std::string const &Filename);
HRESULT Reset();
HRESULT Read(UINT nSizeToRead, BYTE *pbData, UINT *pnSizeRead);
HRESULT Close();
};