Skip to content

Commit

Permalink
Update to latest SFML 3 API
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed Jun 25, 2024
1 parent 78c225b commit e76574d
Show file tree
Hide file tree
Showing 25 changed files with 256 additions and 382 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: SFML/SFML
ref: b583eaf1606e54e5f443b094b3b2a6e3af72a5a4
ref: 7034e40cccda3cf227a5f707cdff7589b9539538
path: SFML

- name: Configure SFML CMake
Expand Down
34 changes: 4 additions & 30 deletions include/SFML/Graphics/Rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@
////////////////////////////////////////////////////////////
typedef struct
{
float left;
float top;
float width;
float height;
sfVector2f position;
sfVector2f size;
} sfFloatRect;

typedef struct
{
int left;
int top;
int width;
int height;
sfVector2i position;
sfVector2i size;
} sfIntRect;

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -79,27 +75,5 @@ CSFML_GRAPHICS_API bool sfIntRect_contains(const sfIntRect* rect, int x, int y);
CSFML_GRAPHICS_API bool sfFloatRect_intersects(const sfFloatRect* rect1, const sfFloatRect* rect2, sfFloatRect* intersection);
CSFML_GRAPHICS_API bool sfIntRect_intersects(const sfIntRect* rect1, const sfIntRect* rect2, sfIntRect* intersection);

////////////////////////////////////////////////////////////
/// \brief Get the position of the rectangle's top-left corner
///
/// \return Position of rectangle
///
/// \see getSize
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfVector2f sfFloatRect_getPosition(const sfFloatRect* rect);
CSFML_GRAPHICS_API sfVector2i sfIntRect_getPosition(const sfIntRect* rect);

////////////////////////////////////////////////////////////
/// \brief Get the size of the rectangle
///
/// \return Size of rectangle
///
/// \see getPosition
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfVector2f sfFloatRect_getSize(const sfFloatRect* rect);
CSFML_GRAPHICS_API sfVector2i sfIntRect_getSize(const sfIntRect* rect);


#endif // SFML_RECT_H
11 changes: 0 additions & 11 deletions include/SFML/Graphics/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,6 @@ CSFML_GRAPHICS_API void sfView_setRotation(sfView* view, float angle);
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API void sfView_setViewport(sfView* view, sfFloatRect viewport);

////////////////////////////////////////////////////////////
/// \brief Reset a view to the given rectangle
///
/// Note that this function resets the rotation angle to 0.
///
/// \param view View object
/// \param rectangle Rectangle defining the zone to display
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API void sfView_reset(sfView* view, sfFloatRect rectangle);

////////////////////////////////////////////////////////////
/// \brief Get the center of a view
///
Expand Down
5 changes: 3 additions & 2 deletions include/SFML/System/InputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Export.h>
#include <stdlib.h>


typedef int64_t (*sfInputStreamReadFunc)(void* data, int64_t size, void* userData);
typedef int64_t (*sfInputStreamSeekFunc)(int64_t position, void* userData);
typedef int64_t (*sfInputStreamReadFunc)(void* data, size_t size, void* userData);
typedef int64_t (*sfInputStreamSeekFunc)(size_t position, void* userData);
typedef int64_t (*sfInputStreamTellFunc)(void* userData);
typedef int64_t (*sfInputStreamGetSizeFunc)(void* userData);

Expand Down
39 changes: 15 additions & 24 deletions src/SFML/Audio/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,22 @@
////////////////////////////////////////////////////////////
sfMusic* sfMusic_createFromFile(const char* filename)
{
sfMusic* music = new sfMusic;
if (!music->This.openFromFile(filename))
{
delete music;
music = nullptr;
}

return music;
auto music = sf::Music::openFromFile(filename);
if (!music)
return nullptr;

return new sfMusic{{}, std::move(*music)};
}


////////////////////////////////////////////////////////////
sfMusic* sfMusic_createFromMemory(const void* data, size_t sizeInBytes)
{
sfMusic* music = new sfMusic;
if (!music->This.openFromMemory(data, sizeInBytes))
{
delete music;
music = nullptr;
}

return music;
auto music = sf::Music::openFromMemory(data, sizeInBytes);
if (!music)
return nullptr;

return new sfMusic{{}, std::move(*music)};
}


Expand All @@ -63,15 +57,12 @@ sfMusic* sfMusic_createFromStream(sfInputStream* stream)
{
CSFML_CHECK_RETURN(stream, nullptr);

sfMusic* music = new sfMusic;
music->Stream = CallbackStream(stream);
if (!music->This.openFromStream(music->Stream))
{
delete music;
music = nullptr;
}
auto sfmlStream = std::make_unique<CallbackStream>(stream);
auto music = sf::Music::openFromStream(*sfmlStream);
if (!music)
return nullptr;

return music;
return new sfMusic{std::move(sfmlStream), std::move(*music)};
}


Expand Down
4 changes: 3 additions & 1 deletion src/SFML/Audio/MusicStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
#include <SFML/Audio/Music.hpp>
#include <SFML/CallbackStream.h>

#include <memory>


////////////////////////////////////////////////////////////
// Internal structure of sfMusic
////////////////////////////////////////////////////////////
struct sfMusic
{
CallbackStream Stream;
std::unique_ptr<CallbackStream> Stream;
sf::Music This;
};

Expand Down
50 changes: 39 additions & 11 deletions src/SFML/CallbackStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,57 @@ class CallbackStream : public sf::InputStream
/// \param data Buffer where to copy the read data
/// \param size Desired number of bytes to read
///
/// \return The number of bytes actually read
/// \return The number of bytes actually read, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
std::int64_t read(void* data, std::int64_t size) override
std::optional<std::size_t> read(void* data, std::size_t size) override
{
return myStream.read ? myStream.read(data, size, myStream.userData) : -1;
if (!myStream.read)
return std::nullopt;

const auto bytesRead = myStream.read(data, size, myStream.userData);
if (bytesRead != -1)
return static_cast<std::size_t>(bytesRead);

return std::nullopt;
}

////////////////////////////////////////////////////////////
/// \brief Change the current reading position
///
/// \param position The position to seek to, from the beginning
///
/// \return The position actually seeked to, or -1 on error
/// \return The position actually seeked to, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
std::int64_t seek(std::int64_t position) override
std::optional<std::size_t> seek(std::size_t position) override
{
return myStream.seek ? myStream.seek(position, myStream.userData) : -1;
if (!myStream.seek)
return std::nullopt;

const auto actualPosition = myStream.seek(position, myStream.userData);
if (actualPosition != -1)
return actualPosition;

return std::nullopt;
}

////////////////////////////////////////////////////////////
/// \brief Return the current reading position in the stream
///
/// \return The current position, or -1 on error.
/// \return The current position, or `std::nullopt` on error
///
////////////////////////////////////////////////////////////
std::int64_t tell() override
std::optional<std::size_t> tell() override
{
return myStream.tell ? myStream.tell(myStream.userData) : -1;
if (!myStream.tell)
return std::nullopt;

const auto position = myStream.tell(myStream.userData);
if (position != -1)
return position;

return std::nullopt;
}

////////////////////////////////////////////////////////////
Expand All @@ -101,9 +122,16 @@ class CallbackStream : public sf::InputStream
/// \return The total number of bytes available in the stream, or -1 on error
///
////////////////////////////////////////////////////////////
std::int64_t getSize() override
std::optional<std::size_t> getSize() override
{
return myStream.getSize ? myStream.getSize(myStream.userData) : -1;
if (!myStream.getSize)
return std::nullopt;

const auto bytesAvailable = myStream.getSize(myStream.userData);
if (bytesAvailable != -1)
return bytesAvailable;

return std::nullopt;
}

private:
Expand Down
32 changes: 16 additions & 16 deletions src/SFML/Graphics/CircleShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void sfCircleShape_setTexture(sfCircleShape* shape, const sfTexture* texture, bo
////////////////////////////////////////////////////////////
void sfCircleShape_setTextureRect(sfCircleShape* shape, sfIntRect rect)
{
CSFML_CALL(shape, setTextureRect(sf::IntRect({ rect.left, rect.top }, { rect.width, rect.height })));
CSFML_CALL(shape, setTextureRect(sf::IntRect({ rect.position.x, rect.position.y }, { rect.size.x, rect.size.y })));
}


Expand Down Expand Up @@ -224,14 +224,14 @@ const sfTexture* sfCircleShape_getTexture(const sfCircleShape* shape)
////////////////////////////////////////////////////////////
sfIntRect sfCircleShape_getTextureRect(const sfCircleShape* shape)
{
sfIntRect rect = {0, 0, 0, 0};
sfIntRect rect = {{0, 0}, {0, 0}};
CSFML_CHECK_RETURN(shape, rect);

sf::IntRect sfmlRect = shape->This.getTextureRect();
rect.left = sfmlRect.left;
rect.top = sfmlRect.top;
rect.width = sfmlRect.width;
rect.height = sfmlRect.height;
rect.position.x = sfmlRect.position.x;
rect.position.y = sfmlRect.position.y;
rect.size.x = sfmlRect.size.x;
rect.size.y = sfmlRect.size.y;

return rect;
}
Expand Down Expand Up @@ -321,14 +321,14 @@ void sfCircleShape_setPointCount(sfCircleShape* shape, size_t count)
////////////////////////////////////////////////////////////
sfFloatRect sfCircleShape_getLocalBounds(const sfCircleShape* shape)
{
sfFloatRect rect = {0, 0, 0, 0};
sfFloatRect rect = {{0, 0}, {0, 0}};
CSFML_CHECK_RETURN(shape, rect);

sf::FloatRect sfmlRect = shape->This.getLocalBounds();
rect.left = sfmlRect.left;
rect.top = sfmlRect.top;
rect.width = sfmlRect.width;
rect.height = sfmlRect.height;
rect.position.x = sfmlRect.position.x;
rect.position.y = sfmlRect.position.y;
rect.size.x = sfmlRect.size.x;
rect.size.y = sfmlRect.size.y;

return rect;
}
Expand All @@ -337,14 +337,14 @@ sfFloatRect sfCircleShape_getLocalBounds(const sfCircleShape* shape)
////////////////////////////////////////////////////////////
sfFloatRect sfCircleShape_getGlobalBounds(const sfCircleShape* shape)
{
sfFloatRect rect = {0, 0, 0, 0};
sfFloatRect rect = {{0, 0}, {0, 0}};
CSFML_CHECK_RETURN(shape, rect);

sf::FloatRect sfmlRect = shape->This.getGlobalBounds();
rect.left = sfmlRect.left;
rect.top = sfmlRect.top;
rect.width = sfmlRect.width;
rect.height = sfmlRect.height;
rect.position.x = sfmlRect.position.x;
rect.position.y = sfmlRect.position.y;
rect.size.x = sfmlRect.size.x;
rect.size.y = sfmlRect.size.y;

return rect;
}
32 changes: 16 additions & 16 deletions src/SFML/Graphics/ConvexShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void sfConvexShape_setTexture(sfConvexShape* shape, const sfTexture* texture, bo
////////////////////////////////////////////////////////////
void sfConvexShape_setTextureRect(sfConvexShape* shape, sfIntRect rect)
{
CSFML_CALL(shape, setTextureRect(sf::IntRect({ rect.left, rect.top }, { rect.width, rect.height })));
CSFML_CALL(shape, setTextureRect(sf::IntRect({ rect.position.x, rect.position.y }, { rect.size.x, rect.size.y })));
}


Expand Down Expand Up @@ -221,14 +221,14 @@ const sfTexture* sfConvexShape_getTexture(const sfConvexShape* shape)
////////////////////////////////////////////////////////////
sfIntRect sfConvexShape_getTextureRect(const sfConvexShape* shape)
{
sfIntRect rect = {0, 0, 0, 0};
sfIntRect rect = {{0, 0}, {0, 0}};
CSFML_CHECK_RETURN(shape, rect);

sf::IntRect sfmlRect = shape->This.getTextureRect();
rect.left = sfmlRect.left;
rect.top = sfmlRect.top;
rect.width = sfmlRect.width;
rect.height = sfmlRect.height;
rect.position.x = sfmlRect.position.x;
rect.position.y = sfmlRect.position.y;
rect.size.x = sfmlRect.size.x;
rect.size.y = sfmlRect.size.y;

return rect;
}
Expand Down Expand Up @@ -311,14 +311,14 @@ void sfConvexShape_setPoint(sfConvexShape* shape, size_t index, sfVector2f point
////////////////////////////////////////////////////////////
sfFloatRect sfConvexShape_getLocalBounds(const sfConvexShape* shape)
{
sfFloatRect rect = {0, 0, 0, 0};
sfFloatRect rect = {{0, 0}, {0, 0}};
CSFML_CHECK_RETURN(shape, rect);

sf::FloatRect sfmlRect = shape->This.getLocalBounds();
rect.left = sfmlRect.left;
rect.top = sfmlRect.top;
rect.width = sfmlRect.width;
rect.height = sfmlRect.height;
rect.position.x = sfmlRect.position.x;
rect.position.y = sfmlRect.position.y;
rect.size.x = sfmlRect.size.x;
rect.size.y = sfmlRect.size.y;

return rect;
}
Expand All @@ -327,14 +327,14 @@ sfFloatRect sfConvexShape_getLocalBounds(const sfConvexShape* shape)
////////////////////////////////////////////////////////////
sfFloatRect sfConvexShape_getGlobalBounds(const sfConvexShape* shape)
{
sfFloatRect rect = {0, 0, 0, 0};
sfFloatRect rect = {{0, 0}, {0, 0}};
CSFML_CHECK_RETURN(shape, rect);

sf::FloatRect sfmlRect = shape->This.getGlobalBounds();
rect.left = sfmlRect.left;
rect.top = sfmlRect.top;
rect.width = sfmlRect.width;
rect.height = sfmlRect.height;
rect.position.x = sfmlRect.position.x;
rect.position.y = sfmlRect.position.y;
rect.size.x = sfmlRect.size.x;
rect.size.y = sfmlRect.size.y;

return rect;
}
Loading

0 comments on commit e76574d

Please sign in to comment.