Skip to content

Commit

Permalink
add simple water wave generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Forairaaaaa committed Mar 29, 2024
1 parent ef6bfb6 commit d186ead
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/misc/water_wave_generator/water_wave_generator.cpp
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);
}
48 changes: 48 additions & 0 deletions src/misc/water_wave_generator/water_wave_generator.h
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
2 changes: 2 additions & 0 deletions src/smooth_ui_toolkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
#include "utils/ring_buffer/ring_buffer.h"

#include "widgets/base/base.h"

#include "misc/water_wave_generator/water_wave_generator.h"

0 comments on commit d186ead

Please sign in to comment.