Giter Site home page Giter Site logo

sfml-template's Introduction

SFML Project Template

This is a simple template for starting SFML projects.

Guide

Installation

Compilation

Game Class

Installation

Simply run the following terminal commands:

git clone https://github.com/matthewflegg/sfml-template.git
cd sfml-template

You can also just create a template from the repository or clone this repo a different way. Regardless of how you do it, everything else will work the same way.

Compilation

Compilation is done via a shell script. Unfortunately, I haven't got round to writing a script for Windows yet. On Linux or macOS, run the following commands:

chmod +x build.sh
./build.sh

To run your game, run:

./bin/game

Adding Files (Optional)

You should add header files in the inc directory. Place their corresponding source files in src.

When you add a new source file open build.sh. Add a new line to the cpp string. Here's an example:

Before:

cpp="\
     $root/$src/game.cc"

After:

cpp="\
     $root/$src/game.cc \
     $root/$src/enemy.cc"

You shouldn't include any header files in the script. For more information, read the comments, these outline the steps to compile the project.

SFML Headers (Optional)

You can remove any SFML headers that you haven't included. For instance, let's remove SFML/Network.hpp:

Before:

lib="\
     -lsfml-audio \
     -lsfml-graphics \
     -lsfml-network \
     -lsfml-system \
     -lsfml-window"

After:

lib="\
     -lsfml-audio \
     -lsfml-graphics \
     -lsfml-system \
     -lsfml-window"

Changing Folder Names

If you change the names of any directories, make sure you change their corresponding variable in build.sh. For example, let's change src to source and inc to include:

Before:

src="src"
inc="inc"

After:

src="source"
inc="include"

Game Class

The game class is relatively straightforward to use. It includes update and render methods, and well as a sf::RenderWindow* and sf::VideoMode. You shouldn't need to edit main.cc.

Class Members

The game class includes three private members. These are:

sf::RenderWindow* window;  // Pointer to the render window used to draw to the screen
sf::VideoMode videoMode;   // Contains the resolution
sf::Event event;           // Used to store the value of events when `pollEvents` is called

Update and Draw

This is the same as most games you'll find. Just put your update logic in update(), and your drawing logic in draw().

void Game::update() {
    pollEvents();

    // TODO: Add update logic here
}

void Game::render() {
    window->clear(sf::Color::Black);

    // TODO: Add drawing code here

    window->display();
}

Init Methods

Here you'll find methods to set up the window and other variables. The usual event polling code you'd find in an SFML project is placed within pollEvents for organisation.

void Game::initVariables() {
    window = nullptr;  // It's always good to initialize your variables
}

void Game::initWindow() {
    // You can change the window settings here, such as the title and resolution
    videoMode = sf::VideoMode(800, 600);
    window = new sf::RenderWindow(videoMode, "Game", sf::Style::Titlebar | sf::Style::Close);
}

void Game::pollEvents() {
    // You can change or add events here, such as player input
    while (window->pollEvent(event)) {
        if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            window->close();
    }
}

Accessors

Game has an accessor that returns a bool indicating if the window is open. This is used in main.cc.

const bool Game::running() const {
    return window->isOpen();
}

Constructor and Destructor

Game also includes a constructor and destructor. Do not remove the delete window statement, as window is dynamically allocated.

Game::Game() {
    initVariables();
    initWindow();
}

Game::~Game() {
    delete window;
}

sfml-template's People

Contributors

v0idzdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.