Skip to content

Instantly share code, notes, and snippets.

View MarioLiebisch's full-sized avatar

Mario Liebisch MarioLiebisch

View GitHub Profile
@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 / 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 / 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 / run.cmd
Created November 30, 2016 11:32
A quick and simple batch file to run small tests using SFML. Just execute `run <name>` where `<name>´ is the base name of your cpp file.
@echo off
cls
g++ -o %1.exe %1.cpp -std=c++11 -static-libstdc++ -static-libgcc -DSFML_STATIC -lsfml-network-s -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lwinmm -lopengl32 -lgdi32 -ljpeg -lws2_32 && %1
@MarioLiebisch
MarioLiebisch / parallaxShaderF.cpp
Created July 18, 2016 08:47
Drawing a parallax background using a fragment shader.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768), "Parallax Example",
sf::Style::Titlebar | sf::Style::Close | sf::Style::Fullscreen);
window.setVerticalSyncEnabled(true);
sf::Texture texture;
// This is a texture 1024x1024 with three backgrounds stacked one over the other from front to back
@MarioLiebisch
MarioLiebisch / parallaxShader.cpp
Created July 17, 2016 19:17
SFML Example to render a repeated parallax background using a vertex shader for movement.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Parallax Example",
sf::Style::Titlebar | sf::Style::Close);
window.setVerticalSyncEnabled(true);
sf::Texture texture;
if (!texture.loadFromFile("resources/background.jpg"))
@MarioLiebisch
MarioLiebisch / main.cpp
Created May 21, 2016 11:07
Simple SFML Game class
#include <SFML/Graphics.hpp>
class Game {
private:
// Add all your variables you need, your "globals".
sf::RenderWindow window;
sf::Texture texture;
sf::Sprite sprite;
unsigned int lives;
// ...
@MarioLiebisch
MarioLiebisch / getnumbered.cmd
Created November 24, 2015 18:52
Windows batch: Get the next free numbered file name
@echo off
REM Initialize the first/lowest number
set NUMBER=0
REM Jump point: return here if the current file exists
:LOOP
REM Leave the loop in case the file doesn't exist
if not exist backup-%NUMBER%.zip goto DONE
REM Increment the counter
set /a NUMBER+=1
@MarioLiebisch
MarioLiebisch / .htaccess
Created November 10, 2015 18:49
Redirect all http traffic to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/https/
RewriteCond %{SCRIPT_NAME} !^/https/
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
@MarioLiebisch
MarioLiebisch / gist:e7b5202db7387b3a9698
Created June 5, 2015 09:58
Basic Procedural Dungeon Generation (e.g. for Rougelikes)

#Basic Procedural Random Dungeon Generation

  • Create one set holding used rooms.
  • Create one set holding open ends.
  • Place a random room, add it two both sets and flag it as the entrance.
  • Repeat as long as you don't have your intended number of rooms:
    • Randomly pick a used room ("current room").
    • Pick a random direction.
    • If the chosen direction is the border of the map, continue the loop.
  • Remove the current room from the open ends.