mirror of
https://github.com/SFML/CSFML.git
synced 2026-10-24 13:53:36 +02:00
Add example program
This commit is contained in:
1 parent
843deb2f6b
commit
bf29112d56
7 files changed
+101
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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()
|
||||
@@ -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 not shown.
@@ -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 not shown.
|
After Width: | Height: | Size: 13 KiB |
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user