User Tools

Site Tools


didattici:esempio2sfml

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
didattici:esempio2sfml [2019/12/31 10:43]
profpro
didattici:esempio2sfml [2020/06/08 22:20] (current)
Line 1: Line 1:
 +Esempio 2
  
 +Prima potrebbe essere necessario installare tutti i pacchetti elencati nella pagina [[didattici:sfml]]
 +
 +Poi per compilare questo file si deve usare il comando
 +
 +  gcc main.c -lcsfml-graphics -lcsfml-system -lcsfml-window 
 +
 +
 +<file c main.c>
 +
 +#include <stdio.h>
 +#include <stdbool.h>
 +#include <stdlib.h>
 +
 +#include <SFML/Audio.h>
 +#include <SFML/Graphics.h>  //contiene anche sprite//
 +#include <SFML/Graphics/Texture.h> // /usr/include/SFML/Graphics/Texture.h
 +#include <SFML/Window/Keyboard.h>
 +
 +int main()
 +{
 +    sfVideoMode mode = {400, 200, 32};
 +    sfRenderWindow* window=NULL;
 +
 +
 +    sfTexture* texture=NULL; 
 +    sfEvent event;
 +    /* Create the main window */
 +    window = sfRenderWindow_create(mode, "SFML Titolone", sfResize | sfClose, NULL);
 +    if (!window)
 +        return EXIT_FAILURE;
 +        
 +    /* Create a sprite to display */
 +        
 + sfSprite* sprite=sfSprite_create();  
 + texture = sfTexture_createFromFile("./immag/omino.png", NULL); // spiegato in Texture.h (su windows doppio shash //)
 + if (!texture)
 +        return EXIT_FAILURE;
 +    sfSprite_setTexture(sprite,texture,false); //Sprite.h
 +
 +    
 +    /* Start the game loop */
 +    while (sfRenderWindow_isOpen(window))
 +    {
 +        /* Process events */
 +        while (sfRenderWindow_pollEvent(window, &event))
 +        {
 +            /* Close window : exit */
 +            if (event.type == sfEvtClosed)
 +                sfRenderWindow_close(window);
 +                
 +            else if (event.type == sfEvtKeyPressed ) 
 + if(sfKeyboard_isKeyPressed(sfKeyA)==true) // simboli in SFML/Window/Keyboard.h
 + {
 + printf("evento\n"); 
 + sfVector2f vec = {10.0f, 0.0f};// Shader.h:/// sfVector2f vec = {5.2f, 6.0f};
 + sfSprite_move(sprite,vec);
 + }
 + }
 +        /* Clear the screen */
 +        sfRenderWindow_clear(window, sfGreen);
 +
 + /* draw, draw, ...   TRA CLEAR E DISPLAY . Vedi RenderWindow.h*/
 +       sfRenderWindow_drawText(window, text, NULL);
 +       sfRenderWindow_drawSprite(window,sprite,NULL); 
 +       sfRenderWindow_drawText(window, text, NULL);
 +
 +        /* Update the window */
 +        sfRenderWindow_display(window);
 +    }
 +    /* Cleanup resources */
 +
 +    sfText_destroy(text);
 +
 +    sfRenderWindow_destroy(window);
 +    return EXIT_SUCCESS;
 +}
 +</file>