Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example program #215

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:

- name: Configure CSFML CMake
shell: bash
run: cmake -S $GITHUB_WORKSPACE/CSFML -B $GITHUB_WORKSPACE/CSFML/build -DCMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/CSFML/install -DBUILD_SHARED_LIBS=TRUE -DCSFML_LINK_SFML_STATICALLY=FALSE -DSFML_DIR=$GITHUB_WORKSPACE/SFML/install/lib/cmake/SFML -DCMAKE_VERBOSE_MAKEFILE=ON -DWARNINGS_AS_ERRORS=TRUE ${{matrix.platform.flags}}
run: cmake -S $GITHUB_WORKSPACE/CSFML -B $GITHUB_WORKSPACE/CSFML/build -DCMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/CSFML/install -DBUILD_SHARED_LIBS=TRUE -DCSFML_BUILD_EXAMPLES=TRUE -DCSFML_LINK_SFML_STATICALLY=FALSE -DSFML_DIR=$GITHUB_WORKSPACE/SFML/install/lib/cmake/SFML -DCMAKE_VERBOSE_MAKEFILE=ON -DWARNINGS_AS_ERRORS=TRUE ${{matrix.platform.flags}}

- name: Build CSFML
shell: bash
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ install(DIRECTORY include
PATTERN ".svn" EXCLUDE)
install(FILES license.md DESTINATION ${INSTALL_MISC_DIR})
install(FILES readme.md DESTINATION ${INSTALL_MISC_DIR})

# add an option for building the examples
csfml_set_option(CSFML_BUILD_EXAMPLES FALSE BOOL "TRUE to build the CSFML examples, FALSE to ignore them")
if(CSFML_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
5 changes: 5 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(SRC example.c)

add_executable(example ${SRC})
target_link_libraries(example PRIVATE csfml-graphics csfml-audio)
set_file_warnings(${SRC})
Binary file added examples/doodle_pop.ogg
Binary file not shown.
89 changes: 89 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <SFML/Audio.h>
#include <SFML/Graphics.h>

#include <stdlib.h>

int main(void)
{
// Create the main window
const sfVideoMode mode = {800, 600, 32};
sfRenderWindow* window = sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, NULL);
if (!window)
return EXIT_FAILURE;

// Load a sprite to display
sfTexture* texture = sfTexture_createFromFile("sfml_logo.png", NULL);
if (!texture)
{
sfRenderWindow_destroy(window);
return EXIT_FAILURE;
}
sfSprite* sprite = sfSprite_create();
sfSprite_setTexture(sprite, texture, sfTrue);
const sfVector2f spritePosition = {200, 200};
sfSprite_setPosition(sprite, spritePosition);

// Create a graphical text to display
sfFont* font = sfFont_createFromFile("tuffy.ttf");
if (!font)
{
sfSprite_destroy(sprite);
sfTexture_destroy(texture);
sfRenderWindow_destroy(window);
return EXIT_FAILURE;
}
sfText* text = sfText_create();
sfText_setString(text, "Hello, SFML!");
sfText_setFont(text, font);
sfText_setCharacterSize(text, 50);

// Load a music to play
sfMusic* music = sfMusic_createFromFile("doodle_pop.ogg");
if (!music)
{
sfText_destroy(text);
sfFont_destroy(font);
sfSprite_destroy(sprite);
sfTexture_destroy(texture);
sfRenderWindow_destroy(window);
return EXIT_FAILURE;
}

// Play the music
sfMusic_play(music);

// Start the game loop
sfEvent event;
while (sfRenderWindow_isOpen(window))
{
// Process events
while (sfRenderWindow_pollEvent(window, &event))
{
// Close window : exit
if (event.type == sfEvtClosed)
sfRenderWindow_close(window);
}

// Clear the screen
sfRenderWindow_clear(window, sfBlack);

// Draw the sprite
sfRenderWindow_drawSprite(window, sprite, NULL);

// Draw the text
sfRenderWindow_drawText(window, text, NULL);

// Update the window
sfRenderWindow_display(window);
}

// Cleanup resources
sfMusic_destroy(music);
sfText_destroy(text);
sfFont_destroy(font);
sfSprite_destroy(sprite);
sfTexture_destroy(texture);
sfRenderWindow_destroy(window);

return EXIT_SUCCESS;
}
Binary file added examples/sfml_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/tuffy.ttf
Binary file not shown.
6 changes: 3 additions & 3 deletions include/SFML/Audio/Listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ CSFML_AUDIO_API void sfListener_setPosition(sfVector3f position);
/// \return The listener's position
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API sfVector3f sfListener_getPosition();
CSFML_AUDIO_API sfVector3f sfListener_getPosition(void);

////////////////////////////////////////////////////////////
/// \brief Set the orientation of the forward vector in the scene
Expand All @@ -91,7 +91,7 @@ CSFML_AUDIO_API void sfListener_setDirection(sfVector3f direction);
/// \return Listener's forward vector (not normalized)
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API sfVector3f sfListener_getDirection();
CSFML_AUDIO_API sfVector3f sfListener_getDirection(void);

////////////////////////////////////////////////////////////
/// \brief Set the upward vector of the listener in the scene
Expand All @@ -114,7 +114,7 @@ CSFML_AUDIO_API void sfListener_setUpVector(sfVector3f upVector);
/// \return Listener's upward vector (not normalized)
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API sfVector3f sfListener_getUpVector();
CSFML_AUDIO_API sfVector3f sfListener_getUpVector(void);


#endif // SFML_LISTENER_H
2 changes: 1 addition & 1 deletion include/SFML/Audio/SoundRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ CSFML_AUDIO_API const char** sfSoundRecorder_getAvailableDevices(size_t* count);
/// \return The name of the default audio capture device (null terminated)
///
////////////////////////////////////////////////////////////
CSFML_AUDIO_API const char* sfSoundRecorder_getDefaultDevice();
CSFML_AUDIO_API const char* sfSoundRecorder_getDefaultDevice(void);

////////////////////////////////////////////////////////////
/// \brief Set the audio capture device
Expand Down
2 changes: 1 addition & 1 deletion include/SFML/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
// ...
// };
//
// CSFML_DEPRECATED void globalFunc();
// CSFML_DEPRECATED void globalFunc(void);
////////////////////////////////////////////////////////////
#if defined(CSFML_NO_DEPRECATED_WARNINGS)

Expand Down
2 changes: 1 addition & 1 deletion include/SFML/Graphics/RenderStates.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ typedef struct
/// \brief Define the default values for a RenderState
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfRenderStates sfRenderStates_default();
CSFML_GRAPHICS_API sfRenderStates sfRenderStates_default(void);

#endif // SFML_RENDERSTATES_H
2 changes: 1 addition & 1 deletion include/SFML/Graphics/RenderTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ CSFML_GRAPHICS_API const sfTexture* sfRenderTexture_getTexture(const sfRenderTex
/// \return The maximum anti-aliasing level supported by the system
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API unsigned int sfRenderTexture_getMaximumAntialiasingLevel();
CSFML_GRAPHICS_API unsigned int sfRenderTexture_getMaximumAntialiasingLevel(void);

////////////////////////////////////////////////////////////
/// \brief Enable or disable the smooth filter on a render texture
Expand Down
2 changes: 1 addition & 1 deletion include/SFML/Graphics/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ CSFML_GRAPHICS_API void sfTexture_bind(const sfTexture* texture, sfTextureCoordi
/// \return Maximum size allowed for textures, in pixels
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API unsigned int sfTexture_getMaximumSize();
CSFML_GRAPHICS_API unsigned int sfTexture_getMaximumSize(void);


#endif // SFML_TEXTURE_H
2 changes: 1 addition & 1 deletion include/SFML/Graphics/VertexBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ CSFML_GRAPHICS_API void sfVertexBuffer_bind(const sfVertexBuffer* vertexBuffer);
/// \return True if vertex buffers are supported, false otherwise
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfBool sfVertexBuffer_isAvailable();
CSFML_GRAPHICS_API sfBool sfVertexBuffer_isAvailable(void);


#endif // SFML_VERTEXBUFFER_H
2 changes: 1 addition & 1 deletion include/SFML/Network/UdpSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ CSFML_NETWORK_API sfSocketStatus sfUdpSocket_receivePacket(sfUdpSocket* socket,
/// \return The maximum size of a UDP datagram (message)
///
////////////////////////////////////////////////////////////
CSFML_NETWORK_API unsigned int sfUdpSocket_maxDatagramSize();
CSFML_NETWORK_API unsigned int sfUdpSocket_maxDatagramSize(void);


#endif // SFML_UDPSOCKET_H
4 changes: 2 additions & 2 deletions include/SFML/Window/Clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/// \return Clipboard contents as a locale-dependent ANSI string
///
////////////////////////////////////////////////////////////
CSFML_WINDOW_API const char* sfClipboard_getString();
CSFML_WINDOW_API const char* sfClipboard_getString(void);


////////////////////////////////////////////////////////////
Expand All @@ -55,7 +55,7 @@ CSFML_WINDOW_API const char* sfClipboard_getString();
/// \return Clipboard contents as UTF-32
///
////////////////////////////////////////////////////////////
CSFML_WINDOW_API const sfUint32* sfClipboard_getUnicodeString();
CSFML_WINDOW_API const sfUint32* sfClipboard_getUnicodeString(void);

////////////////////////////////////////////////////////////
/// \brief Set the content of the clipboard as ANSI string data
Expand Down
4 changes: 2 additions & 2 deletions include/SFML/Window/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <SFML/Window/Types.h>
#include <SFML/Window/Window.h>

typedef void (*GlFunctionPointer)();
typedef void (*GlFunctionPointer)(void);

////////////////////////////////////////////////////////////
/// \brief Create a new context
Expand Down Expand Up @@ -104,6 +104,6 @@ CSFML_WINDOW_API sfContextSettings sfContext_getSettings(const sfContext* contex
/// \return The active context's ID or 0 if no context is currently active
///
////////////////////////////////////////////////////////////
CSFML_WINDOW_API sfUint64 sfContext_getActiveContextId();
CSFML_WINDOW_API sfUint64 sfContext_getActiveContextId(void);

#endif // SFML_CONTEXT_H
4 changes: 2 additions & 2 deletions include/SFML/Window/Vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef uint64_t VkSurfaceKHR;
typedef struct VkAllocationCallbacks VkAllocationCallbacks;


typedef void (*sfVulkanFunctionPointer)();
typedef void (*sfVulkanFunctionPointer)(void);

////////////////////////////////////////////////////////////
/// \brief Tell whether or not the system supports Vulkan
Expand Down Expand Up @@ -79,4 +79,4 @@ CSFML_WINDOW_API sfVulkanFunctionPointer sfVulkan_getFunction(const char* name);
/// \return Vulkan instance extensions required for graphics
///
////////////////////////////////////////////////////////////
CSFML_WINDOW_API const char* const* sfVulkan_getGraphicsRequiredInstanceExtensions();
CSFML_WINDOW_API const char* const* sfVulkan_getGraphicsRequiredInstanceExtensions(void);