Skip to content

Commit

Permalink
Fix API changes in SFML 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThrasher committed May 24, 2024
1 parent 1b53960 commit 489b95f
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 166 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: c89c32d7baa15927a39a161a4ead867c11d5ea21
ref: b583eaf1606e54e5f443b094b3b2a6e3af72a5a4
path: SFML

- name: Configure SFML CMake
Expand Down
48 changes: 17 additions & 31 deletions src/SFML/Audio/SoundBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,22 @@
////////////////////////////////////////////////////////////
sfSoundBuffer* sfSoundBuffer_createFromFile(const char* filename)
{
sfSoundBuffer* buffer = new sfSoundBuffer;
auto soundBuffer = sf::SoundBuffer::loadFromFile(filename);
if (!soundBuffer)
return nullptr;

if (!buffer->This.loadFromFile(filename))
{
delete buffer;
buffer = nullptr;
}

return buffer;
return new sfSoundBuffer{std::move(*soundBuffer)};
}


////////////////////////////////////////////////////////////
sfSoundBuffer* sfSoundBuffer_createFromMemory(const void* data, size_t sizeInBytes)
{
sfSoundBuffer* buffer = new sfSoundBuffer;

if (!buffer->This.loadFromMemory(data, sizeInBytes))
{
delete buffer;
buffer = nullptr;
}
auto soundBuffer = sf::SoundBuffer::loadFromMemory(data, sizeInBytes);
if (!soundBuffer)
return nullptr;

return buffer;
return new sfSoundBuffer{std::move(*soundBuffer)};
}


Expand All @@ -67,15 +59,12 @@ sfSoundBuffer* sfSoundBuffer_createFromStream(sfInputStream* stream)
{
CSFML_CHECK_RETURN(stream, nullptr);

sfSoundBuffer* buffer = new sfSoundBuffer;
CallbackStream sfmlStream(stream);
if (!buffer->This.loadFromStream(sfmlStream))
{
delete buffer;
buffer = nullptr;
}
auto soundBuffer = sf::SoundBuffer::loadFromStream(sfmlStream);
if (!soundBuffer)
return nullptr;

return buffer;
return new sfSoundBuffer{std::move(*soundBuffer)};
}


Expand All @@ -87,18 +76,15 @@ sfSoundBuffer* sfSoundBuffer_createFromSamples(const int16_t* samples,
sfSoundChannel* channelMapData,
size_t channelMapSize)
{
sfSoundBuffer* buffer = new sfSoundBuffer;

std::vector<sf::SoundChannel> channelMap(channelMapSize);
for (std::size_t i = 0; i < channelMap.size(); ++i)
channelMap[i] = static_cast<sf::SoundChannel>(channelMapData[i]);
if (!buffer->This.loadFromSamples(samples, sampleCount, channelCount, sampleRate, channelMap))
{
delete buffer;
buffer = nullptr;
}

return buffer;
auto soundBuffer = sf::SoundBuffer::loadFromSamples(samples, sampleCount, channelCount, sampleRate, channelMap);
if (!soundBuffer)
return nullptr;

return new sfSoundBuffer{std::move(*soundBuffer)};
}


Expand Down
4 changes: 2 additions & 2 deletions src/SFML/Audio/SoundBufferRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const sfSoundBuffer* sfSoundBufferRecorder_getBuffer(const sfSoundBufferRecorder
{
CSFML_CHECK_RETURN(soundBufferRecorder, nullptr);

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

return &soundBufferRecorder->SoundBuffer;
return &*soundBufferRecorder->SoundBuffer;
}

////////////////////////////////////////////////////////////
Expand Down
8 changes: 5 additions & 3 deletions src/SFML/Audio/SoundBufferRecorderStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@
#include <SFML/Audio/SoundBufferRecorder.hpp>
#include <SFML/Audio/SoundBufferStruct.h>

#include <optional>


////////////////////////////////////////////////////////////
// Internal structure of sfSoundBufferRecorder
////////////////////////////////////////////////////////////
struct sfSoundBufferRecorder
{
sf::SoundBufferRecorder This;
mutable sfSoundBuffer SoundBuffer;
std::string DeviceName;
sf::SoundBufferRecorder This;
mutable std::optional<sfSoundBuffer> SoundBuffer;
std::string DeviceName;
};


Expand Down
35 changes: 13 additions & 22 deletions src/SFML/Graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,22 @@
////////////////////////////////////////////////////////////
sfFont* sfFont_createFromFile(const char* filename)
{
sfFont* font = new sfFont;
if (!font->This.loadFromFile(filename))
{
delete font;
font = nullptr;
}
auto font = sf::Font::loadFromFile(filename);
if (!font)
return nullptr;

return font;
return new sfFont{std::move(*font), {}, {}};
}


////////////////////////////////////////////////////////////
sfFont* sfFont_createFromMemory(const void* data, size_t sizeInBytes)
{
sfFont* font = new sfFont;
if (!font->This.loadFromMemory(data, sizeInBytes))
{
delete font;
font = nullptr;
}
auto font = sf::Font::loadFromMemory(data, sizeInBytes);
if (!font)
return nullptr;

return font;
return new sfFont{std::move(*font), {}, {}};
}


Expand All @@ -64,15 +58,12 @@ sfFont* sfFont_createFromStream(sfInputStream* stream)
{
CSFML_CHECK_RETURN(stream, nullptr);

sfFont* font = new sfFont;
font->Stream = CallbackStream(stream);
if (!font->This.loadFromStream(font->Stream))
{
delete font;
font = nullptr;
}
auto sfmlStream = std::make_shared<CallbackStream>(stream);
auto font = sf::Font::loadFromStream(*sfmlStream);
if (!font)
return nullptr;

return font;
return new sfFont{std::move(*font), {}, std::move(sfmlStream)};
}


Expand Down
3 changes: 2 additions & 1 deletion src/SFML/Graphics/FontStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <SFML/Graphics/TextureStruct.h>
#include <SFML/CallbackStream.h>
#include <map>
#include <memory>


////////////////////////////////////////////////////////////
Expand All @@ -41,7 +42,7 @@ struct sfFont
{
sf::Font This;
std::map<unsigned int, sfTexture> Textures;
CallbackStream Stream;
std::shared_ptr<CallbackStream> Stream;
};


Expand Down
51 changes: 15 additions & 36 deletions src/SFML/Graphics/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,60 +35,43 @@
////////////////////////////////////////////////////////////
sfImage* sfImage_create(unsigned int width, unsigned int height)
{
sfImage* image = new sfImage;
image->This.create({ width, height });

return image;
return new sfImage{sf::Image({width, height})};
}


////////////////////////////////////////////////////////////
sfImage* sfImage_createFromColor(unsigned int width, unsigned int height, sfColor color)
{
sfImage* image = new sfImage;
image->This.create({ width, height }, sf::Color(color.r, color.g, color.b, color.a));

return image;
return new sfImage{sf::Image({ width, height }, sf::Color(color.r, color.g, color.b, color.a))};
}


////////////////////////////////////////////////////////////
sfImage* sfImage_createFromPixels(unsigned int width, unsigned int height, const uint8_t* data)
{
sfImage* image = new sfImage;
image->This.create({ width, height }, data);

return image;
return new sfImage{sf::Image({ width, height }, data)};
}


////////////////////////////////////////////////////////////
sfImage* sfImage_createFromFile(const char* filename)
{
sfImage* image = new sfImage;

if (!image->This.loadFromFile(filename))
{
delete image;
image = nullptr;
}
auto image = sf::Image::loadFromFile(filename);
if (!image)
return nullptr;

return image;
return new sfImage{std::move(*image)};
}


////////////////////////////////////////////////////////////
sfImage* sfImage_createFromMemory(const void* data, size_t sizeInBytes)
{
sfImage* image = new sfImage;
auto image = sf::Image::loadFromMemory(data, sizeInBytes);
if (!image)
return nullptr;

if (!image->This.loadFromMemory(data, sizeInBytes))
{
delete image;
image = nullptr;
}

return image;
return new sfImage{std::move(*image)};
}


Expand All @@ -97,16 +80,12 @@ sfImage* sfImage_createFromStream(sfInputStream* stream)
{
CSFML_CHECK_RETURN(stream, nullptr);

sfImage* image = new sfImage;

CallbackStream sfmlStream(stream);
if (!image->This.loadFromStream(sfmlStream))
{
delete image;
image = nullptr;
}
auto image = sf::Image::loadFromStream(sfmlStream);
if (!image)
return nullptr;

return image;
return new sfImage{std::move(*image)};
}


Expand Down
17 changes: 6 additions & 11 deletions src/SFML/Graphics/RenderTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@ sfRenderTexture* sfRenderTexture_create(unsigned int width, unsigned int height,
}

// Create the render texture
sfRenderTexture* renderTexture = new sfRenderTexture;

if (!renderTexture->This.create({ width, height }, params))
{
delete renderTexture;
auto renderTexture = sf::RenderTexture::create({ width, height }, params);
if (!renderTexture)
return nullptr;
}

renderTexture->Target = new sfTexture(const_cast<sf::Texture*>(&renderTexture->This.getTexture()));
renderTexture->DefaultView.This = renderTexture->This.getDefaultView();
renderTexture->CurrentView.This = renderTexture->This.getView();

return renderTexture;
return new sfRenderTexture{std::move(*renderTexture),
new sfTexture(const_cast<sf::Texture*>(&renderTexture->getTexture())),
{renderTexture->getDefaultView()},
{renderTexture->getView()}};
}


Expand Down
Loading

0 comments on commit 489b95f

Please sign in to comment.