-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef6bfb6
commit d186ead
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @file water_wave_generator.cpp | ||
* @author Forairaaaaa | ||
* @brief | ||
* @version 0.1 | ||
* @date 2024-03-30 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
#include "water_wave_generator.h" | ||
#include "../../utils/fpm/fixed.hpp" | ||
#include "../../utils/fpm/math.hpp" | ||
|
||
using namespace SmoothUIToolKit::Misc; | ||
|
||
void WaterWaveGenerator::init(size_t waveLenght) | ||
{ | ||
if (waveLenght == 0) | ||
return; | ||
|
||
// Resize | ||
_data.wave_a_buffer.reSize(waveLenght); | ||
_data.wave_b_buffer.reSize(waveLenght); | ||
|
||
// Fill it up | ||
for (int i = 0; i < waveLenght; i++) | ||
{ | ||
_data.wave_a_buffer.put(_wave_a_formula(i)); | ||
_data.wave_b_buffer.put(_wave_b_formula(i)); | ||
} | ||
_data.wave_x = waveLenght; | ||
} | ||
|
||
void WaterWaveGenerator::update() | ||
{ | ||
// Put wave b twice to make it faster | ||
_data.wave_a_buffer.put(_wave_a_formula(_data.wave_x)); | ||
_data.wave_b_buffer.put(_wave_b_formula(_data.wave_x)); | ||
_data.wave_b_buffer.put(_wave_b_formula(_data.wave_x)); | ||
_data.wave_x++; | ||
} | ||
|
||
// Just simple sine | ||
// https://www.desmos.com/calculator/dvzez1v6gr | ||
int WaterWaveGenerator::_wave_a_formula(const int& x) | ||
{ | ||
fpm::fixed_16_16 fx{x}; | ||
fx = fpm::sin(fx / 60) * 10; | ||
return static_cast<int>(fx); | ||
} | ||
|
||
int WaterWaveGenerator::_wave_b_formula(const int& x) | ||
{ | ||
fpm::fixed_16_16 fx{x - 20}; | ||
fx = fpm::sin(fx / 60) * 13 - 10; | ||
return static_cast<int>(fx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @file water_wave_generator.h | ||
* @author Forairaaaaa | ||
* @brief | ||
* @version 0.1 | ||
* @date 2024-03-30 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
#pragma once | ||
#include "../../core/types/types.h" | ||
#include "../../utils/ring_buffer/ring_buffer.h" | ||
#include <cstddef> | ||
#include <cstdint> | ||
|
||
namespace SmoothUIToolKit | ||
{ | ||
namespace Misc | ||
{ | ||
/** | ||
* @brief Provide two dynamic sine curves, to simulate waving water surface. | ||
* | ||
*/ | ||
class WaterWaveGenerator | ||
{ | ||
private: | ||
struct Data_t | ||
{ | ||
RingBuffer<int, 1> wave_a_buffer; | ||
RingBuffer<int, 1> wave_b_buffer; | ||
uint16_t wave_x = 0; | ||
}; | ||
Data_t _data; | ||
|
||
protected: | ||
virtual int _wave_a_formula(const int& x); | ||
virtual int _wave_b_formula(const int& x); | ||
|
||
public: | ||
void init(size_t waveLenght); | ||
virtual void update(); | ||
inline RingBuffer<int, 1>& getWaveA() { return _data.wave_a_buffer; } | ||
inline RingBuffer<int, 1>& getWaveB() { return _data.wave_b_buffer; } | ||
}; | ||
|
||
} // namespace Misc | ||
} // namespace SmoothUIToolKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters