From 5add2c5f1ce91caada08d20f0884c696a9b0ab0c Mon Sep 17 00:00:00 2001 From: milek7 Date: Mon, 25 Sep 2017 18:08:54 +0200 Subject: [PATCH 01/14] changes --- uart.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/uart.cpp b/uart.cpp index e3c70c23..0aaab746 100644 --- a/uart.cpp +++ b/uart.cpp @@ -47,13 +47,14 @@ void uart_input::poll() sp_return ret; - if (sp_input_waiting(port) >= 16) + if ((ret = sp_input_waiting(port)) >= 16) { std::array 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"); - + + WriteLog("uart: recv"); sp_drain(port); data_pending = false; @@ -115,6 +116,8 @@ void uart_input::poll() old_packet = buffer; } + WriteLog("uart, available input: " + std::to_string(ret)); + if (!data_pending && sp_output_waiting(port) == 0) { // TODO: ugly! move it into structure like input_bits @@ -162,8 +165,9 @@ void uart_input::poll() buffer[8] |= buzzer << 7; - sp_flush(port, SP_BUF_INPUT); // flush input buffer in preparation for reply packet +// sp_flush(port, SP_BUF_INPUT); // flush input buffer in preparation for reply packet + WriteLog("uart: send"); ret = sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0); if (ret != buffer.size()) throw std::runtime_error("uart: failed to write to port"); From 8dd119066f8d00fd72af62ca3d914095c16807b4 Mon Sep 17 00:00:00 2001 From: milek7 Date: Tue, 26 Sep 2017 15:38:08 +0200 Subject: [PATCH 02/14] git changes --- Globals.cpp | 14 ++++++++++++- Globals.h | 8 +++++++- uart.cpp | 57 +++++++++++++++++++++++++++++++++++------------------ 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/Globals.cpp b/Globals.cpp index 9621a627..eb6c9e93 100644 --- a/Globals.cpp +++ b/Globals.cpp @@ -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 diff --git a/Globals.h b/Globals.h index 8848ffc6..820d0d3f 100644 --- a/Globals.h +++ b/Globals.h @@ -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; diff --git a/uart.cpp b/uart.cpp index 0aaab746..0bf3f938 100644 --- a/uart.cpp +++ b/uart.cpp @@ -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"); @@ -44,6 +51,8 @@ void uart_input::poll() last_update = now; TTrain *t = Global::pWorld->train(); + if (!t) + return; sp_return ret; @@ -54,8 +63,15 @@ void uart_input::poll() if (ret < 0) throw std::runtime_error("uart: failed to read from port"); - WriteLog("uart: recv"); - 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) @@ -103,21 +119,18 @@ 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.mainbrakemin) / (conf.localbrakemax - conf.localbrakemin)); old_packet = buffer; } - WriteLog("uart, available input: " + std::to_string(ret)); - if (!data_pending && sp_output_waiting(port) == 0) { // TODO: ugly! move it into structure like input_bits @@ -165,9 +178,15 @@ void uart_input::poll() 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)); + } - WriteLog("uart: send"); ret = sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0); if (ret != buffer.size()) throw std::runtime_error("uart: failed to write to port"); From a55aed29b42ddbfd6c0fe1bb50d9fa232b3bf657 Mon Sep 17 00:00:00 2001 From: milek7 Date: Tue, 26 Sep 2017 16:27:13 +0200 Subject: [PATCH 03/14] uart pressure changes --- uart.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/uart.cpp b/uart.cpp index 0bf3f938..8c4dcc1b 100644 --- a/uart.cpp +++ b/uart.cpp @@ -137,9 +137,9 @@ void uart_input::poll() 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); + 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; From 63e8b285eccfeb238d648d64daeec532b804005a Mon Sep 17 00:00:00 2001 From: milek7 Date: Tue, 26 Sep 2017 16:59:51 +0200 Subject: [PATCH 04/14] uart changes --- uart.cpp | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/uart.cpp b/uart.cpp index 8c4dcc1b..8362eaeb 100644 --- a/uart.cpp +++ b/uart.cpp @@ -148,36 +148,27 @@ void uart_input::poll() std::array 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; - if (conf.debug) { char buf[buffer.size() * 3 + 1]; From 47dde0c68522d9a0dee425bcbe673486468d6f06 Mon Sep 17 00:00:00 2001 From: maciek001 Date: Wed, 27 Sep 2017 18:45:16 +0200 Subject: [PATCH 05/14] Update uart.cpp change 129 line. --- uart.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uart.cpp b/uart.cpp index 8362eaeb..d5db2953 100644 --- a/uart.cpp +++ b/uart.cpp @@ -126,7 +126,7 @@ void uart_input::poll() 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.mainbrakemin) / (conf.localbrakemax - conf.localbrakemin)); + t->set_localbrake((float)(((uint16_t)buffer[10] | ((uint16_t)buffer[11] << 8)) - conf.localbrakemin) / (conf.localbrakemax - conf.localbrakemin)); old_packet = buffer; } From cc61b8c00ce8f8d8ad3a036833e0c12a8fdab0f4 Mon Sep 17 00:00:00 2001 From: milek7 Date: Thu, 28 Sep 2017 21:42:17 +0200 Subject: [PATCH 06/14] uart changes --- uart.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/uart.cpp b/uart.cpp index d5db2953..1497998e 100644 --- a/uart.cpp +++ b/uart.cpp @@ -37,6 +37,10 @@ uart_input::uart_input() uart_input::~uart_input() { + std::array buffer = { 0 }; + sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0); + sp_drain(port); + sp_close(port); sp_free_port(port); } @@ -135,7 +139,7 @@ void uart_input::poll() { // TODO: ugly! move it into structure like input_bits - uint8_t buzzer = (uint8_t)t->get_alarm(); + uint8_t buzzer = Global::iPause ? 0 : (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() * 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); From 7723e69ed31d87b6358d19d9db35160a52537db3 Mon Sep 17 00:00:00 2001 From: milek7 Date: Sat, 30 Sep 2017 00:30:45 +0200 Subject: [PATCH 07/14] cache gauge sound position --- Gauge.cpp | 15 +++++++-------- Gauge.h | 2 ++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Gauge.cpp b/Gauge.cpp index 74f255e0..97327aa9 100644 --- a/Gauge.cpp +++ b/Gauge.cpp @@ -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,14 +334,10 @@ 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); } diff --git a/Gauge.h b/Gauge.h index 8dd61959..12e84494 100644 --- a/Gauge.h +++ b/Gauge.h @@ -50,6 +50,8 @@ class TGauge { void play( sound* Sound ); + glm::vec3 model_pos; + public: TGauge() = default; inline From 93a929002592fd6f7aba6138c1e4160bfc8108bb Mon Sep 17 00:00:00 2001 From: milek7 Date: Sat, 30 Sep 2017 11:50:27 +0200 Subject: [PATCH 08/14] sound files cleanup --- CMakeLists.txt | 2 +- DummySound.cpp | 50 ---------- stdafx.h | 1 + wavread.cpp | 264 ------------------------------------------------- wavread.h | 48 --------- 5 files changed, 2 insertions(+), 363 deletions(-) delete mode 100644 DummySound.cpp delete mode 100644 wavread.cpp delete mode 100644 wavread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 77d13adc..6b437952 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,7 @@ set(SOURCES if (WIN32) add_definitions(-DHAVE_ROUND) # 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") endif() if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) diff --git a/DummySound.cpp b/DummySound.cpp deleted file mode 100644 index 805a1167..00000000 --- a/DummySound.cpp +++ /dev/null @@ -1,50 +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/. -*/ - -#include -#include "Sound.h" - -TSoundContainer::TSoundContainer(LPDIRECTSOUND pDS, std::string const &Directory, std::string const &Filename, int NConcurrent) -{ -}; - -TSoundContainer::~TSoundContainer() -{ -}; - -LPDIRECTSOUNDBUFFER TSoundContainer::GetUnique(LPDIRECTSOUND pDS) -{ - return new dummysb(); -}; - -TSoundsManager::~TSoundsManager() -{ -}; - -void TSoundsManager::Free() -{ -}; - -TSoundContainer * TSoundsManager::LoadFromFile( std::string const &Dir, std::string const &Filename, int Concurrent) -{ - return new TSoundContainer(0, Dir, Filename, Concurrent); -}; - -LPDIRECTSOUNDBUFFER TSoundsManager::GetFromName(std::string const &Name, bool Dynamic, float *fSamplingRate) -{ - return new dummysb(); -}; - -void TSoundsManager::RestoreAll() -{ -}; - -void TSoundsManager::Init(HWND hWnd) -{ -}; diff --git a/stdafx.h b/stdafx.h index 2d45a0fd..23a8d555 100644 --- a/stdafx.h +++ b/stdafx.h @@ -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); + diff --git a/wavread.cpp b/wavread.cpp deleted file mode 100644 index 1e41938a..00000000 --- a/wavread.cpp +++ /dev/null @@ -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 ; - // 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 . - 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(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 - - *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; -} diff --git a/wavread.h b/wavread.h deleted file mode 100644 index f0bc4357..00000000 --- a/wavread.h +++ /dev/null @@ -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 -#include - -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(); -}; From 0603937ee40b9ac6025029f2dc1bf84f16f351d9 Mon Sep 17 00:00:00 2001 From: milek7 Date: Sat, 30 Sep 2017 20:36:04 +0200 Subject: [PATCH 09/14] linux slash changes --- material.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/material.cpp b/material.cpp index dcd6a784..1ff157b8 100644 --- a/material.cpp +++ b/material.cpp @@ -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; From 93ccd6802b2a5e9ef6018e83037416a055c10378 Mon Sep 17 00:00:00 2001 From: milek7 Date: Wed, 4 Oct 2017 18:56:26 +0200 Subject: [PATCH 10/14] oops --- uart.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uart.cpp b/uart.cpp index 1497998e..156c4c1b 100644 --- a/uart.cpp +++ b/uart.cpp @@ -139,8 +139,8 @@ void uart_input::poll() { // TODO: ugly! move it into structure like input_bits - uint8_t buzzer = Global::iPause ? 0 : (uint8_t)t->get_alarm(); - uint8_t tacho = (uint8_t)t->get_tacho(); + uint8_t buzzer = (uint8_t)t->get_alarm(); + 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); From 50eaf092093a292941d5b3c816d514c217157125 Mon Sep 17 00:00:00 2001 From: milek7 Date: Thu, 5 Oct 2017 12:07:51 +0200 Subject: [PATCH 11/14] sound changes --- Gauge.cpp | 2 +- World.cpp | 19 ++++++++++--------- sound.cpp | 8 +++++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Gauge.cpp b/Gauge.cpp index 97327aa9..3339e581 100644 --- a/Gauge.cpp +++ b/Gauge.cpp @@ -339,7 +339,7 @@ void TGauge::play( sound *Sound ) 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); } } diff --git a/World.cpp b/World.cpp index 015db2ae..20617628 100644 --- a/World.cpp +++ b/World.cpp @@ -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(); diff --git a/sound.cpp b/sound.cpp index 95f8c26b..652ecf3d 100644 --- a/sound.cpp +++ b/sound.cpp @@ -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; } From 3b2915fdff68f388d4c78b6dcc737801b07ff217 Mon Sep 17 00:00:00 2001 From: milek7 Date: Wed, 11 Oct 2017 19:33:14 +0200 Subject: [PATCH 12/14] tranmission sound changes sound changes sound changes --- DynObj.cpp | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/DynObj.cpp b/DynObj.cpp index ecfefdcc..b3afb979 100644 --- a/DynObj.cpp +++ b/DynObj.cpp @@ -3690,15 +3690,11 @@ void TDynamicObject::RenderSounds() rsWentylator->stop(); } } - if (MoverParameters->TrainType == dt_ET40 && rsPrzekladnia) + if (rsPrzekladnia) { if (MoverParameters->Vel > 0.1) - { - freq = MoverParameters->Vel; - rsPrzekladnia->pitch(freq); - vol = MoverParameters->Vel; - rsPrzekladnia->loop().position(GetPosition()).gain(vol).play(); - } + rsPrzekladnia->pitch(MoverParameters->Vel).gain(MoverParameters->Vel) + .position(GetPosition()).play(); else rsPrzekladnia->stop(); } @@ -4713,22 +4709,32 @@ void TDynamicObject::LoadMMediaFile(std::string BaseDir, std::string TypeName, } } - else if( ( token == "transmission:" ) - && ( MoverParameters->EngineType == ElectricSeriesMotor ) ) { + else if(token == "transmission:") { // plik z dzwiekiem, mnozniki i ofsety amp. i czest. - double attenuation; - parser.getTokens( 2, false ); - parser - >> token - >> attenuation; - rsPrzekladnia = sound_man->create_sound(token); + std::string name; + float attenuation, gain_mul = 0.029f, gain_off = 0.1f, pitch_mul = 0.005f, pitch_off = 1.0f; + parser.getTokens(1, false); + if (parser.peek() != "{") + { + parser >> name; + parser.getTokens(); + parser >> attenuation; + } + else + { + cParser extp(parser.getToken(false, "}")); + extp.getTokens(6, false); + extp >> name >> attenuation >> gain_mul >> gain_off >> pitch_mul >> pitch_off; + } + + rsPrzekladnia = sound_man->create_sound(name); if (rsPrzekladnia) { rsPrzekladnia->dist(attenuation); - rsPrzekladnia->gain_mul = 0.029; - rsPrzekladnia->gain_off = 0.1; - rsPrzekladnia->pitch_mul = 0.005; - rsPrzekladnia->pitch_off = 1.0; + rsPrzekladnia->gain_mul = gain_mul; + rsPrzekladnia->gain_off = gain_off; + rsPrzekladnia->pitch_mul = pitch_mul; + rsPrzekladnia->pitch_off = pitch_off; } } From 730280a2e2bae8ce9e0c9fda1be94027a172fc7d Mon Sep 17 00:00:00 2001 From: milek7 Date: Wed, 11 Oct 2017 19:35:06 +0200 Subject: [PATCH 13/14] sound changes --- DynObj.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DynObj.cpp b/DynObj.cpp index b3afb979..479c2c85 100644 --- a/DynObj.cpp +++ b/DynObj.cpp @@ -4659,10 +4659,10 @@ void TDynamicObject::LoadMMediaFile(std::string BaseDir, std::string TypeName, >> token >> attenuation; rsSilnik = sound_man->create_sound(token); + parser.getTokens( 4, false ); if (rsSilnik) { rsSilnik->dist(attenuation); - parser.getTokens( 4, false ); parser >> rsSilnik->gain_mul; if( MoverParameters->EngineType == DieselEngine ) { @@ -4695,10 +4695,10 @@ void TDynamicObject::LoadMMediaFile(std::string BaseDir, std::string TypeName, >> token >> attenuation; rsWentylator = sound_man->create_sound(token); + parser.getTokens( 4, false ); if (rsWentylator) { rsWentylator->dist(attenuation); - parser.getTokens( 4, false ); parser >> rsWentylator->gain_mul >> rsWentylator->gain_off From c5716a21581e36d24f11706f7e6c28cce3115dfb Mon Sep 17 00:00:00 2001 From: milek7 Date: Thu, 12 Oct 2017 21:57:15 +0200 Subject: [PATCH 14/14] sound crash fix --- sound.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sound.cpp b/sound.cpp index 652ecf3d..be956d70 100644 --- a/sound.cpp +++ b/sound.cpp @@ -71,7 +71,10 @@ sound_manager::~sound_manager() sound_buffer* sound_manager::find_buffer(std::string name) { - name.erase(name.rfind('.')); + size_t pos = name.rfind('.'); + if (pos != name.npos) + name.erase(pos); + std::replace(name.begin(), name.end(), '\\', '/'); auto search = buffers.find(name); @@ -86,7 +89,10 @@ std::string sound_manager::find_file(std::string name) if (FileExists(name)) return name; - name.erase(name.rfind('.')); + size_t pos = name.rfind('.'); + if (pos != name.npos) + name.erase(pos); + std::vector exts { ".wav", ".WAV", ".flac", ".ogg" }; for (auto const &ext : exts) if (FileExists(name + ext)) @@ -118,7 +124,9 @@ sound_buffer* sound_manager::get_buffer(std::string const &name) buf = new sound_buffer(file); - file.erase(file.rfind('.')); + size_t pos = file.rfind('.'); + if (pos != file.npos) + file.erase(pos); buffers.emplace(file, buf); return buf;