Skip to content

Instantly share code, notes, and snippets.

View gapspt's full-sized avatar

Jorge Galvao gapspt

  • Duck Games
  • Lisbon
View GitHub Profile
@gapspt
gapspt / ObjectPool.hpp
Created September 14, 2025 19:30
Generic object pool implementation in C++ ( Draft only, untested, might contain errors!)
// Draft only, untested, might contain errors!!!
// Note: spin-lock implementation taken from https://en.cppreference.com/w/cpp/atomic/atomic_flag.html
#include <atomic>
#include <vector>
template <class T> class ObjectPool {
public:
std::function<T*()> factory;
@gapspt
gapspt / FpsCounter.cs
Last active February 24, 2025 11:00
Quick and dirty way to count frames per second (FPS) on Unity
using UnityEngine;
public class FpsCounter : MonoBehaviour
{
float prev = -1;
float fps = 0;
void Update()
{
var now = Time.realtimeSinceStartup;
@gapspt
gapspt / PolyPartition.cs
Created January 12, 2024 10:11
Port of https://github.com/ivanfratric/polypartition to C# (Partial but almost complete port - lacking monotone partitioning)
// Origin: https://github.com/ivanfratric/polypartition
// Commit: 5dcc93c9c023a2296610ec98539e2377f4285d30
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using tppl_float = System.Double;
using TPPLPolyList = System.Collections.Generic.LinkedList<TPPLPoly>;
@gapspt
gapspt / addOverlayImage.js
Last active August 14, 2022 20:49
Dinamically add an image as an overlay when pressing a specific sequence of keys on the keyboard. The image will scale with the browser window (responsive) while keeping its original ratio.
function addOverlayImage(url, scale) {
let e = document.createElement('img');
e.style.visibility = 'hidden';
e.src = url;
let w, h, r = 0;
function setSize() {
if (r) {
let winR = window.innerWidth / window.innerHeight;
let s = scale * (r > winR ? window.innerWidth / w : window.innerHeight / h);
e.width = w * s;
@gapspt
gapspt / promise_debug.js
Last active March 29, 2022 17:20
JavaScript pending promises debug
// Adapted from https://makandracards.com/makandra/46681-javascript-how-to-query-the-state-of-a-promise
function promiseState(promise, callback) {
const uniqueValue = /unique/;
Promise.race([promise, Promise.resolve(uniqueValue)])
.then(
(value) => callback(value === uniqueValue ? 'pending' : 'fulfilled'),
(reason) => callback(`rejected (${reason})`));
}
function debugPromiseState(promise) {
promiseState(promise, (state) => {