Skip to content

Instantly share code, notes, and snippets.

View MarioLiebisch's full-sized avatar

Mario Liebisch MarioLiebisch

View GitHub Profile
@MarioLiebisch
MarioLiebisch / thr-race.cpp
Created June 27, 2017 06:51
Quick example of a threading race condition
#include <SFML/System.hpp>
#include <iostream>
int main()
{
int i = 0;
sf::Thread thread([](int *j) { ++*j; }, &i);
thread.launch();
std::cout << i; // This output is a race condition; it's either 0 or 1 depending on how fast the extra thread is executed
@MarioLiebisch
MarioLiebisch / rotate.cpp
Created August 25, 2017 08:30
Transforming a vector using SFML, e.g. by rotating it by 45 degree.
#include <SFML/Graphics/Transform.hpp>
#include <SFML/System/Vector2.hpp>
#include <iostream>
int main() {
// First we'll need a vector to tranform
sf::Vector2f myVector(2, 0);
std::cout << "myVector: " << myVector.x << ", " << myVector.y << "\n";
// Then the actual transform
@MarioLiebisch
MarioLiebisch / tetrominos.cpp
Created December 24, 2017 08:43
A simple example on how one could render concave shapes such as tetrominos using SFML's `sf::VertexArray`
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Tetrominos");
// The quads approach is easiest to understand
// (Note this is not supported on mobile targets and you'd have to define triangles instead)
sf::VertexArray quads(sf::Quads);
// Left piece
@MarioLiebisch
MarioLiebisch / circletext.cpp
Last active May 12, 2018 16:54
A minimalistic example to print text in a circle using SFML. (Example output: https://i.imgur.com/2M2OURz.png)
#include <SFML/Graphics.hpp>
int main(int argc, char **argv) {
sf::RenderWindow window(sf::VideoMode(800, 600), "Circle Text", sf::Style::Default);
window.setVerticalSyncEnabled(true);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut ultrices ante. Aliquam ornare eu enim eget accumsan.", font);
@MarioLiebisch
MarioLiebisch / observable.cpp
Created July 10, 2018 13:49
Simple implementation of an observable variable that will trigger once its value is changed (and which could be adjusted in the callback). This was really just a random idea, it's not perfect by any means.
#include <iostream>
#include <functional>
#include <string>
// Definee an `Observable` class.
// This works as a wrapper around a variable of any type.
// Once it's assigned a new value, a predefined callback is defined to react on this.
template<typename T>
class Observable {
public:
@MarioLiebisch
MarioLiebisch / fpsmeter.cpp
Last active July 16, 2018 08:09
A simple to use, self-containing FPS meter for SFML programs I use for testing
/*
Usage:
* Load a font, if you don't have one yet
* Create the `FpsMeter` object
* Optional: Set a position
* Draw the meter
sf::Font font;
font.loadFromFile("myfont.ttf");
@MarioLiebisch
MarioLiebisch / getself.cpp
Created July 22, 2018 16:18
Small utility function to get a running program's own executable path. This should work even if it's called using a symbolic link.
std::string getSelf()
{
char buf[1024] = { 0 };
#ifdef WIN32
DWORD ret = GetModuleFileNameA(NULL, buf, sizeof(buf));
if (ret && ret != sizeof(buf)) {
std::string res(buf);
for (auto &c : res)
if (c == '\\')
c = '/';
@MarioLiebisch
MarioLiebisch / statswindow.cpp
Created August 26, 2018 19:02
Small example how to add stats counting to a SFML `sf::RenderWindow`. Release builds could just use `sf::RenderWindow` directly instead. Note this isn't necessarily 100% perfect or correct; written from memory.
#include <SFML/Graphics.hpp>
#include <sstream>
class RenderWindowStats : public sf::RenderWindow {
public:
RenderWindowStats() : RenderWindow({640, 480}, "Test") {
}
void draw(sf::Drawable &dr, sf::RenderStates states = sf::RenderStates()) {
++mDrawCallsTemp;
@MarioLiebisch
MarioLiebisch / timer.cpp
Created May 6, 2019 18:11
Quick and simple timer example using SFML
#include <SFML/Graphics.hpp>
#include <sstream>
#include <iomanip>
int main(int argc, char **argv) {
sf::RenderWindow window(sf::VideoMode(300, 100), "Timer Test", sf::Style::Default);
window.setVerticalSyncEnabled(true);
sf::Font font;
font.loadFromFile("Arial.ttf");
@MarioLiebisch
MarioLiebisch / conext.hpp
Created July 14, 2019 12:42
An old simple wrapper I've used in a few old tools to get cross-platform coloring in console
/**
* @file conext.h
* @author Mario Liebisch <[email protected]>
* @section LICENSE
*
* © 2011 All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*