Index

Graphics Framework for C++

May 8, 2022
GitHub

About

SFML (Simple and Fast Multimedia Library) is an Open Source C++ library designed to make game development more accessible, exposing multimedia components to the user through a simple, yet powerful interface. It provides easy-to-use functions and features for handling graphics, audio, and user input, making it simpler to bring ideas to life. SFML is like a toolbox for developers, giving them the tools they need to build their own digital experiences, whether it's a game, a simulation, or a visually appealing application. It's a versatile and beginner-friendly framework that allows developers to unleash their creativity and create something truly unique.
Graphics Framework is a specialized toolkit built on top of SFML, designed to make simple games and simulations even more accessible and straightforward to develop in C++.

Template

You can find the full template here.
main.cpp

#include "Application.h"

int main()
{
    Application app;
    app.showFPS(false);
    app.setMaxFPS(120);
    app.run();

    return 0;
}
Application.h

#pragma once

#include "GraphicsFramework/main.h"

/*
    Returning false on boolean functions or closing the window quits the application and calls onDestroy().
 */
class Application : public GF::App
{
public:
    Application(std::string t = "Window");

    // called once before the application starts
    bool onCreate() override;

    // first thing to be called every frame
    bool onHandleEvent(GF::Event& event) override;

    // called every frame before draw
    virtual bool onUpdate(const float fElapsedTime, const float fTotalTime) override;

    // last thing to be called every frame
    bool onDraw() override;

    // called once before exiting the application
    void onDestroy() override;

    // called after another state switches to this one
    void onSwitch(std::string other) override;

private:

};
Application.cpp

#include "Application.h"

Application::Application(std::string t)
{
    title = t;
}

bool Application::onCreate()
{
    return true;
}

bool Application::onHandleEvent(GF::Event& event)
{
    // skip if no events have happened and this function was just called 
    // because of the order logic (handle events -> update -> draw) 
    if (event.isNothing()) return true;

    // handle waiting events, because events can stay in queue for a long time and that
    // is worse for user interaction. e.g maximum of 5 events per frame 
    int c = 0;
    while (window.pollEvent(event) && c++ < 5)
        onHandleEvent(event);

    return true;
}

// fElapsedTime - time since last frame
// fTotalTime - time since the beginning
bool Application::onUpdate(const float fElapsedTime, const float fTotalTime)
{
    return true;
}

bool Application::onDraw()
{
    return true;
}

void Application::onDestroy()
{

}

void Application::onSwitch(std::string other)
{

}

Examples

Mouse input


bool Application::onHandleEvent(GF::Event& event)
{
    if (GF::Mouse::Left.isPressed()) {
        if (GF::Mouse::isInsideWindow(window))
            // ...
    }

    if (GF::Mouse::Wheel.moved(event)) {
        int delta = GF::Mouse::Wheel.delta(event);
        sf::Vector2f mouse_position = GF::Mouse::getPosition(window);
        // ...
    }

    if (GF::Mouse::Left.doubleClicked(event))
        // ...

    // handle waiting events
    int c = 0;
    while (window.pollEvent(event) && c++ < 5)
        onHandleEvent(event);

    return true;
}
                

Keyboard input


bool Application::onHandleEvent(GF::Event& event)
{
    static GF::ToggleKey ESC(sf::Keyboard::Escape);

    event.showMessage();

    if (ESC.isKeyReleasedOnce(event))
        return false;

    return true;
}
                

Setting up the window


Application::Application(std::string t)
{
    title = t;
    
    // create a window of size (700, 500) on the center
    // of the screen with origin on top left corner
    setupWindow(
        700, 500,
        CENTER_SCREEN - sf::Vector2i(700 / 2, 500 / 2),
        sf::Style::Default
    );

    // will clear the screen every frame with the background color
    // (default black)
    setStaticScreen(false);
}
                

Drawing stuff


bool Application::onDraw()
{
    // draw a rectangle of size (500, 500) filled with white and 
    // its top left corner on the center of the screen
    window.draw(GF::Rectangle(sf::Vector2f(500, 500), CENTER, WHITE));

    // draw a white circle of radius 300 positioned at the center 
    // of the window with a blue outline of 3px
    window.draw(GF::Circle(300, CENTER, WHITE, BLUE, 3));
}
                

Running

1 - Run apt-get install g++ to install g++

2 - Download the SFML SDK from: https://www.sfml-dev.org/download/sfml/2.5.1/, unpack it and copy the files to your preferred location

3 - Create the following environment variable:

SFML_ROOT="<path-to-SFML-folder>"

4 - Update your library path with:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path-to-SFML-folder>/lib/

5 - Download/clone my SFML framework called GraphicsFramework from:

https://github.com/miguelfranca/SFML_GraphicsFramework.git

4 - To run simply execute the following command on the project folder

run

Screenshots/GIFs