Re: Cracktro :)
Posted: November 1st, 2019, 4:37 pm
I've been kind of sad/lonely these past few days not being able to discuss this, hopefully this won't be the last of it .
Code: Select all
class Pattern {
public:
Pattern( unsigned nChannels,unsigned nRows, const std::vector<Note> data ) :
nChannels_( nChannels ),
nRows_( nRows )
{
size_ = nChannels * nRows;
assert( size_ > 0 );
data_ = data; // no pattern compression for now
}
private:
unsigned nChannels_;
unsigned nRows_;
unsigned size_;
std::vector<Note> data_;
};
Code: Select all
Pattern( unsigned nChannels,unsigned nRows, std::vector<Note> data ) :
nChannels_( nChannels ),
nRows_( nRows ),
size_( nChannels * nRows ),
data_( std::move( data ) )
{
assert( size_ > 0 );
}
Code: Select all
std::pair<std::unique_ptr<char[]>, size_t> readFileToBuffer( std::string filename )
{
// Open the file at the end in binary mode
auto file = std::ifstream( filename, std::ios::ate | std::ios::binary );
// Verify file is open
if( !file.is_open() )
throw std::runtime_error( "File not found, or whatever." );
// The end position is going to be the size of the file in bytes
auto const buffer_length = file.tellg();
// Reset position back to beginning
file.seekg( 0 );
// Create a temp buffer, just cleaner this way
auto temp_buffer = std::make_unique<char[]>( buffer_length );
// Read in the file contents
file.read( temp_buffer.get(), buffer_length );
// Create the std::pair by moving the buffer and length into it and return
return { std::move( temp_buffer ), buffer_length };
}
// Get the buffer from the function
auto safe_buffer = readFileToBuffer( "binfile.data" );
// With std::pair, you can use C++17's structured bindings to get references to the members of pair.
auto[buffer, size] = safe_buffer;
// You can check buffer for nullptr and size == 0
assert( buffer != nullptr && size != 0 );
// This way you can use the unique_ptr directly and you know it's count/size
int headerSize = 0;
memcpy( &headerSize, &buffer[ 0 ], sizeof( headerSize ) );
// yada, yada
Code: Select all
PatternHeader patternHeader;
SampleHeader sampleHeader;
Instrumentheader InstrumentHeader;
(...)
instrumentHeader.samples[sampleNr.length = sourceFile.InstrumentList[InstrumentNr].length;
Code: Select all
auto find_it = std::find_if(
begin( vec ),
end( vec ),
[]( auto const & element ){ return element.value() > 0; }
);
auto rem_it = std::remove_if(
begin( vec ),
end( vec ),
[]( auto const& element ){ return element.health() <= 0.f; }
);
for( auto it = begin( vec ); it != end( vec ); ++it )
{
*it = 42;
}
Code: Select all
void takeDamage( EntityVariant& entity_, float amount_ )noexcept;
Code: Select all
void stop( EntityVariant& entity_t )noexcept;
Code: Select all
void chooseMenuState( int selection_ )noexcept;
Code: Select all
void doMenuState( shooter::MainMenu& menu_, shooter::MenuVariant& menuVariant, int selection_ )noexcept
Code: Select all
template<class T>
constexpr void add( T const& lhs, T const& rhs )noexcept{ return lhs + rhs; }
Code: Select all
template<typename ContainerValueType>
Code: Select all
template<typename T> void move( T& entity_, float delta_time_ )noexcept;
template<typename EnemyType> void update( EnemyType& enemy_, float delta_time_ )noexcept
Code: Select all
auto cur = *this;
++( *this );
return cur;
Code: Select all
template<typename EnemyType> void update( EnemyType& enemy_, float delta_time_ )noexcept
{
if( isAtWaypoint( enemy_ ) )
{
advanceWaypoint( enemy_ );
if( pathFinished( enemy_ ) )
{
die( enemy_ );
stop( enemy_ );
}
else
{
changeDirection( enemy_, Vector2::normalize( enemy_.m_position - activeWaypoint( enemy_ ) ) );
}
}
move( enemy_, delta_time_ );
}
Code: Select all
template<typename EnemyType> void update( EnemyType& enemy_, float delta_time_ )noexcept
{
if( Vector2::length_sqr( enemy_.m_position - enemy_.waypoints[ enemy_.m_waypoint_index ] ) <
sqr( Constants::ship_radius ) )
{
++enemy_.m_waypoint_index;;
if( enemy_.m_waypoint_index >= enemy_.waypoints.size )
{
entity_.m_health = 0.f;
entity_.m_velocity = Vec2f{};
}
else
{
auto const direction = Vector2::normalize( enemy_.m_position - enemy_.waypoints[ enemy_.m_waypoint_index ] )
entity_.m_velocity = direction_ * entity_.m_max_speed;
}
}
entity_.m_position += entity_.m_velocity * delta_time_;
}
Code: Select all
void update( Boss1& boss_, float delta_time_ )noexcept;
void update( Boss2& boss_, float delta_time_ )noexcept;
void update( Projectile& ammo_, float delta_time_ )noexcept;
void update( EntityVariant& entityVariant_, float delta_time_ )noexcept;
Code: Select all
template<typename T> struct is_enemy
{
static constexpr bool value = std::disjunction_v<
std::is_same<T, Enemy1>,
std::is_same<T, Enemy2>,
std::is_same<T, Enemy3>,
std::is_same<T, Enemy4>,
std::is_same<T, Enemy5>
>;
};
template<typename T> constexpr bool is_enemy_v = is_enemy<T>::value;
template<
typename EnemyType,
std::enable_if_t<is_enemy_v<EnemyType>, int> = 0
> void update( EnemyType& enemy_, float delta_time_ )noexcept;
Code: Select all
// Common functions for all entities
void takeDamage( Hero& entity_, float amount_ )noexcept;
RectF boundingBox( Hero const& entity_ )noexcept;
void move( Hero& entity_, float delta_time_ )noexcept;
void stop( Hero& entity_t )noexcept;
void changeDirection( Hero& entity_t, Vec2f const& direction_ )noexcept;
void die( Hero& entity_t )noexcept;
void takeDamage( Enemy1& entity_, float amount_ )noexcept;
RectF boundingBox( Enemy1 const& entity_ )noexcept;
void move( Enemy1& entity_, float delta_time_ )noexcept;
void stop( Enemy1& entity_t )noexcept;
void changeDirection( Enemy1& entity_t, Vec2f const& direction_ )noexcept;
void die( Enemy1& entity_t )noexcept;
void takeDamage( Enemy2& entity_, float amount_ )noexcept;
RectF boundingBox( Enemy2 const& entity_ )noexcept;
void move( Enemy2& entity_, float delta_time_ )noexcept;
void stop( Enemy2& entity_t )noexcept;
void changeDirection( Enemy2& entity_t, Vec2f const& direction_ )noexcept;
void die( Enemy2& entity_t )noexcept;
void takeDamage( Enemy3& entity_, float amount_ )noexcept;
RectF boundingBox( Enemy3 const& entity_ )noexcept;
void move( Enemy3& entity_, float delta_time_ )noexcept;
void stop( Enemy3& entity_t )noexcept;
void changeDirection( Enemy3& entity_t, Vec2f const& direction_ )noexcept;
void die( Enemy3& entity_t )noexcept;
void takeDamage( Enemy4& entity_, float amount_ )noexcept;
RectF boundingBox( Enemy4 const& entity_ )noexcept;
void move( Enemy4& entity_, float delta_time_ )noexcept;
void stop( Enemy4& entity_t )noexcept;
void changeDirection( Enemy4& entity_t, Vec2f const& direction_ )noexcept;
void die( Enemy4& entity_t )noexcept;
void takeDamage( Enemy5& entity_, float amount_ )noexcept;
RectF boundingBox( Enemy5 const& entity_ )noexcept;
void move( Enemy5& entity_, float delta_time_ )noexcept;
void stop( Enemy5& entity_t )noexcept;
void changeDirection( Enemy5& entity_t, Vec2f const& direction_ )noexcept;
void die( Enemy5& entity_t )noexcept;
void takeDamage( Boss1& entity_, float amount_ )noexcept;
RectF boundingBox( Boss1 const& entity_ )noexcept;
void move( Boss1& entity_, float delta_time_ )noexcept;
void stop( Boss1& entity_t )noexcept;
void changeDirection( Boss1& entity_t, Vec2f const& direction_ )noexcept;
void die( Boss1& entity_t )noexcept;
void takeDamage( Boss2& entity_, float amount_ )noexcept;
RectF boundingBox( Boss2 const& entity_ )noexcept;
void move( Boss2& entity_, float delta_time_ )noexcept;
void stop( Boss2& entity_t )noexcept;
void changeDirection( Boss2& entity_t, Vec2f const& direction_ )noexcept;
void die( Boss2& entity_t )noexcept;
void takeDamage( EntityVariant& entity_, float amount_ )noexcept;
RectF boundingBox( EntityVariant const& entity_ )noexcept;
void move( EntityVariant& entity_, float delta_time_ )noexcept;
void stop( EntityVariant& entity_t )noexcept;
void changeDirection( EntityVariant& entity_t, Vec2f const& direction_ )noexcept;
void die( EntityVariant& entity_t )noexcept;
Code: Select all
template<typename T> void takeDamage( T& entity_, float amount_ )noexcept;
template<typename T> auto boundingBox( T const& entity_ )noexcept;
template<typename T> void move( T& entity_, float delta_time_ )noexcept;
template<typename T> void stop( T& entity_ )noexcept;
template<typename T> void changeDirection( T& entity_, Vec2f const& direction_ )noexcept;
template<typename T> void kill( T& entity_ )noexcept;
void takeDamage( EntityVariant& entity_, float amount_ )noexcept;
RectF boundingBox( EntityVariant const& entity_ )noexcept;
void move( EntityVariant& entity_, float delta_time_ )noexcept;
void stop( EntityVariant& entity_t )noexcept;
void changeDirection( EntityVariant& entity_t, Vec2f const& direction_ )noexcept;
void die( EntityVariant& entity_t )noexcept;