Skip to content

Commit

Permalink
Use inheritance to remove This data member
Browse files Browse the repository at this point in the history
It's reasonable to say that, for example, sfImage _is_ an sf::Image.
This "is-a" relationship is what inheritance does a very good job
modeling. By using inheritance to implement CSFML types in terms of
their SFML counterpart, we get code that is simpler and easier to
read. We also avoid some of the extra "dancing" required to construct
a CSFML type from an SFML type. This improves the encapsulation of
our CSFML implementation.
  • Loading branch information
ChrisThrasher committed Sep 20, 2024
1 parent 4ef89bb commit a22b884
Show file tree
Hide file tree
Showing 52 changed files with 441 additions and 488 deletions.
4 changes: 2 additions & 2 deletions src/CSFML/Audio/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
sfSound* sfSound_create(const sfSoundBuffer* buffer)
{
assert(buffer);
return new sfSound{sf::Sound(buffer->This), buffer};
return new sfSound{sf::Sound(*buffer), buffer};
}


Expand Down Expand Up @@ -83,7 +83,7 @@ void sfSound_setBuffer(sfSound* sound, const sfSoundBuffer* buffer)
if (buffer)
{
assert(sound);
sound->This.setBuffer(buffer->This);
sound->This.setBuffer(*buffer);
sound->Buffer = buffer;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/CSFML/Audio/SoundBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,45 +109,45 @@ bool sfSoundBuffer_saveToFile(const sfSoundBuffer* soundBuffer, const char* file
{
assert(soundBuffer);
assert(filename);
return soundBuffer->This.saveToFile(filename);
return soundBuffer->saveToFile(filename);
}


////////////////////////////////////////////////////////////
const int16_t* sfSoundBuffer_getSamples(const sfSoundBuffer* soundBuffer)
{
assert(soundBuffer);
return soundBuffer->This.getSamples();
return soundBuffer->getSamples();
}


////////////////////////////////////////////////////////////
uint64_t sfSoundBuffer_getSampleCount(const sfSoundBuffer* soundBuffer)
{
assert(soundBuffer);
return soundBuffer->This.getSampleCount();
return soundBuffer->getSampleCount();
}


////////////////////////////////////////////////////////////
unsigned int sfSoundBuffer_getSampleRate(const sfSoundBuffer* soundBuffer)
{
assert(soundBuffer);
return soundBuffer->This.getSampleRate();
return soundBuffer->getSampleRate();
}


////////////////////////////////////////////////////////////
unsigned int sfSoundBuffer_getChannelCount(const sfSoundBuffer* soundBuffer)
{
assert(soundBuffer);
return soundBuffer->This.getChannelCount();
return soundBuffer->getChannelCount();
}


////////////////////////////////////////////////////////////
sfTime sfSoundBuffer_getDuration(const sfSoundBuffer* soundBuffer)
{
assert(soundBuffer);
return {soundBuffer->This.getDuration().asMicroseconds()};
return {soundBuffer->getDuration().asMicroseconds()};
}
2 changes: 1 addition & 1 deletion src/CSFML/Audio/SoundBufferRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const sfSoundBuffer* sfSoundBufferRecorder_getBuffer(const sfSoundBufferRecorder
{
assert(soundBufferRecorder);

soundBufferRecorder->SoundBuffer.This = soundBufferRecorder->This.getBuffer();
soundBufferRecorder->SoundBuffer = sfSoundBuffer{soundBufferRecorder->This.getBuffer()};

return &soundBufferRecorder->SoundBuffer;
}
Expand Down
3 changes: 1 addition & 2 deletions src/CSFML/Audio/SoundBufferStruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
////////////////////////////////////////////////////////////
// Internal structure of sfSoundBuffer
////////////////////////////////////////////////////////////
struct sfSoundBuffer
struct sfSoundBuffer : sf::SoundBuffer
{
sf::SoundBuffer This;
};
58 changes: 29 additions & 29 deletions src/CSFML/Graphics/CircleShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,95 +64,95 @@ void sfCircleShape_destroy(const sfCircleShape* shape)
void sfCircleShape_setPosition(sfCircleShape* shape, sfVector2f position)
{
assert(shape);
shape->This.setPosition(convertVector2(position));
shape->setPosition(convertVector2(position));
}


////////////////////////////////////////////////////////////
void sfCircleShape_setRotation(sfCircleShape* shape, float angle)
{
assert(shape);
shape->This.setRotation(sf::degrees(angle));
shape->setRotation(sf::degrees(angle));
}


////////////////////////////////////////////////////////////
void sfCircleShape_setScale(sfCircleShape* shape, sfVector2f scale)
{
assert(shape);
shape->This.setScale(convertVector2(scale));
shape->setScale(convertVector2(scale));
}


////////////////////////////////////////////////////////////
void sfCircleShape_setOrigin(sfCircleShape* shape, sfVector2f origin)
{
assert(shape);
shape->This.setOrigin(convertVector2(origin));
shape->setOrigin(convertVector2(origin));
}


////////////////////////////////////////////////////////////
sfVector2f sfCircleShape_getPosition(const sfCircleShape* shape)
{
assert(shape);
return convertVector2(shape->This.getPosition());
return convertVector2(shape->getPosition());
}


////////////////////////////////////////////////////////////
float sfCircleShape_getRotation(const sfCircleShape* shape)
{
assert(shape);
return shape->This.getRotation().asDegrees();
return shape->getRotation().asDegrees();
}


////////////////////////////////////////////////////////////
sfVector2f sfCircleShape_getScale(const sfCircleShape* shape)
{
assert(shape);
return convertVector2(shape->This.getScale());
return convertVector2(shape->getScale());
}


////////////////////////////////////////////////////////////
sfVector2f sfCircleShape_getOrigin(const sfCircleShape* shape)
{
assert(shape);
return convertVector2(shape->This.getOrigin());
return convertVector2(shape->getOrigin());
}


////////////////////////////////////////////////////////////
void sfCircleShape_move(sfCircleShape* shape, sfVector2f offset)
{
assert(shape);
shape->This.move(convertVector2(offset));
shape->move(convertVector2(offset));
}


////////////////////////////////////////////////////////////
void sfCircleShape_rotate(sfCircleShape* shape, float angle)
{
assert(shape);
shape->This.rotate(sf::degrees(angle));
shape->rotate(sf::degrees(angle));
}


////////////////////////////////////////////////////////////
void sfCircleShape_scale(sfCircleShape* shape, sfVector2f factors)
{
assert(shape);
shape->This.scale(convertVector2(factors));
shape->scale(convertVector2(factors));
}


////////////////////////////////////////////////////////////
sfTransform sfCircleShape_getTransform(const sfCircleShape* shape)
{
assert(shape);
shape->Transform = convertTransform(shape->This.getTransform());
shape->Transform = convertTransform(shape->getTransform());
return shape->Transform;
}

Expand All @@ -161,7 +161,7 @@ sfTransform sfCircleShape_getTransform(const sfCircleShape* shape)
sfTransform sfCircleShape_getInverseTransform(const sfCircleShape* shape)
{
assert(shape);
shape->InverseTransform = convertTransform(shape->This.getInverseTransform());
shape->InverseTransform = convertTransform(shape->getInverseTransform());
return shape->InverseTransform;
}

Expand All @@ -170,7 +170,7 @@ sfTransform sfCircleShape_getInverseTransform(const sfCircleShape* shape)
void sfCircleShape_setTexture(sfCircleShape* shape, const sfTexture* texture, bool resetRect)
{
assert(shape);
shape->This.setTexture(texture ? texture->This : nullptr, resetRect);
shape->setTexture(texture ? texture->This : nullptr, resetRect);
shape->Texture = texture;
}

Expand All @@ -179,31 +179,31 @@ void sfCircleShape_setTexture(sfCircleShape* shape, const sfTexture* texture, bo
void sfCircleShape_setTextureRect(sfCircleShape* shape, sfIntRect rect)
{
assert(shape);
shape->This.setTextureRect(convertRect(rect));
shape->setTextureRect(convertRect(rect));
}


////////////////////////////////////////////////////////////
void sfCircleShape_setFillColor(sfCircleShape* shape, sfColor color)
{
assert(shape);
shape->This.setFillColor(convertColor(color));
shape->setFillColor(convertColor(color));
}


////////////////////////////////////////////////////////////
void sfCircleShape_setOutlineColor(sfCircleShape* shape, sfColor color)
{
assert(shape);
shape->This.setOutlineColor(convertColor(color));
shape->setOutlineColor(convertColor(color));
}


////////////////////////////////////////////////////////////
void sfCircleShape_setOutlineThickness(sfCircleShape* shape, float thickness)
{
assert(shape);
shape->This.setOutlineThickness(thickness);
shape->setOutlineThickness(thickness);
}


Expand All @@ -219,47 +219,47 @@ const sfTexture* sfCircleShape_getTexture(const sfCircleShape* shape)
sfIntRect sfCircleShape_getTextureRect(const sfCircleShape* shape)
{
assert(shape);
return convertRect(shape->This.getTextureRect());
return convertRect(shape->getTextureRect());
}


////////////////////////////////////////////////////////////
sfColor sfCircleShape_getFillColor(const sfCircleShape* shape)
{
assert(shape);
return convertColor(shape->This.getFillColor());
return convertColor(shape->getFillColor());
}


////////////////////////////////////////////////////////////
sfColor sfCircleShape_getOutlineColor(const sfCircleShape* shape)
{
assert(shape);
return convertColor(shape->This.getOutlineColor());
return convertColor(shape->getOutlineColor());
}


////////////////////////////////////////////////////////////
float sfCircleShape_getOutlineThickness(const sfCircleShape* shape)
{
assert(shape);
return shape->This.getOutlineThickness();
return shape->getOutlineThickness();
}


////////////////////////////////////////////////////////////
size_t sfCircleShape_getPointCount(const sfCircleShape* shape)
{
assert(shape);
return shape->This.getPointCount();
return shape->getPointCount();
}


////////////////////////////////////////////////////////////
sfVector2f sfCircleShape_getPoint(const sfCircleShape* shape, size_t index)
{
assert(shape);
return convertVector2(shape->This.getPoint(index));
return convertVector2(shape->getPoint(index));
}


Expand All @@ -275,37 +275,37 @@ sfVector2f sfCircleShape_getGeometricCenter(const sfCircleShape* shape)
void sfCircleShape_setRadius(sfCircleShape* shape, float radius)
{
assert(shape);
shape->This.setRadius(radius);
shape->setRadius(radius);
}


////////////////////////////////////////////////////////////
float sfCircleShape_getRadius(const sfCircleShape* shape)
{
assert(shape);
return shape->This.getRadius();
return shape->getRadius();
}


////////////////////////////////////////////////////////////
void sfCircleShape_setPointCount(sfCircleShape* shape, size_t count)
{
assert(shape);
shape->This.setPointCount(count);
shape->setPointCount(count);
}


////////////////////////////////////////////////////////////
sfFloatRect sfCircleShape_getLocalBounds(const sfCircleShape* shape)
{
assert(shape);
return convertRect(shape->This.getLocalBounds());
return convertRect(shape->getLocalBounds());
}


////////////////////////////////////////////////////////////
sfFloatRect sfCircleShape_getGlobalBounds(const sfCircleShape* shape)
{
assert(shape);
return convertRect(shape->This.getGlobalBounds());
return convertRect(shape->getGlobalBounds());
}
3 changes: 1 addition & 2 deletions src/CSFML/Graphics/CircleShapeStruct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
////////////////////////////////////////////////////////////
// Internal structure of sfCircleShape
////////////////////////////////////////////////////////////
struct sfCircleShape
struct sfCircleShape : sf::CircleShape
{
sf::CircleShape This;
const sfTexture* Texture{};
mutable sfTransform Transform{};
mutable sfTransform InverseTransform{};
Expand Down
Loading

0 comments on commit a22b884

Please sign in to comment.