Skip to content

Instantly share code, notes, and snippets.

@xenobrain
xenobrain / CMakeLists.txt
Last active September 1, 2023 18:59
Raylib with Web Target
cmake_minimum_required(VERSION 3.24)
# Install CPM.cmake
set(CPM_VERSION 0.38.1)
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/ext)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_VERSION}.cmake")
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
file(DOWNLOAD https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_VERSION}/CPM.cmake
@xenobrain
xenobrain / CMakeLists.txt
Last active June 28, 2023 14:07
Cocoa Window with Metal layer in pure C++
cmake_minimum_required(VERSION 3.25)
project(CocoaWindow)
set(CMAKE_CXX_STANDARD 20)
# Install CPM ##########################################################################################################
set(CPM_VERSION 0.38.1)
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/libraries)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
@xenobrain
xenobrain / delegate.h
Last active May 5, 2023 16:26
delegate
template <typename T> class delegate;
template <typename R, typename... Args> class delegate<R(Args...)> {
public:
template <R(*function)(Args...)> auto bind(void) -> void {
_instance = nullptr;
_proxy = &function_proxy<function>;
}
template <class C, R(C::* function)(Args...)> auto bind(C* instance) -> void {
@xenobrain
xenobrain / boingy2.rb
Last active April 18, 2023 10:13
Boingy2
RAD2DEG = 180.0 / Math::PI
TIME_STEP = 0.2 / 60 # delta time isn't required in DragonRuby but it really handy for tuning and debugging physics
COLLISION_BIAS = 0.05 # adds some energy into the collision to get objects to separate. tune this in steps of 0.01
COLLISION_SLOP = 0.1 # amount shapes are allowed to overlap without triggering correction. helps avoid position jitter
COLLISION_ITERATIONS = 10 # how many times to run the solver. a good range is between 5 and 15
def tick args
args.gtk.reset if args.inputs.keyboard.r
# Create some circles
@xenobrain
xenobrain / simple_boingy.rb
Last active April 14, 2023 13:08
Simple Bouncing
def tick args
# Create some circles
args.state.circles ||= 10.map_with_ys 2 do |x, y|
radius = 32
diameter = radius * 2
# for static objects use mass = Float::INFINITY
mass = Math::PI * radius * radius
path = 'sprites/circle/blue.png'
offset_x = (1280 - ((diameter + radius) * 10)) / 2
@xenobrain
xenobrain / circles.rb
Last active October 12, 2023 14:42
circles for dragonruby
module GTK
class Runtime
def draw_circle c
radius = c.radius.to_i || 0
xc = c.x.to_i + radius
yc = c.y.to_i + radius
t = c.thickness || 1
r = c.r || 0
g = c.g || 0
b = c.b || 0
@xenobrain
xenobrain / sat.rb
Last active September 25, 2023 16:09
SAT
def create_vertices rect
x = rect.x; y = rect.y
w = rect.w; h = rect.h
cx = x + w * 0.5; cy = y + h * 0.5
sin = Math.sin (rect.angle || 0.0).to_radians; cos = Math.cos (rect.angle || 0.0).to_radians
[
[(x - cx) * cos - (y + h - cy) * sin + cx, (x - cx) * sin + (y + h - cy) * cos + cy], # Top Left
[(x + w - cx) * cos - (y + h - cy) * sin + cx, (x + w - cx) * sin + (y + h - cy) * cos + cy], # Top Right
[(x + w - cx) * cos - (y - cy) * sin + cx, (x + w - cx) * sin + (y - cy) * cos + cy], # Bottom Right
[(x - cx) * cos - (y - cy) * sin + cx, (x - cx) * sin + (y - cy) * cos + cy] # Bottom Left
SCALE = 175.0
LOD = 0.005
MAX_Z_FAR = 600.0
MIN_Z_FAR = 175.0
MIN_SCREEN_W = 70
MIN_SCREEN_H = 60
MAX_SCREEN_W = 360
MAX_SCREEN_H = 120
DRS_FRAME_WINDOW = 10
Z_FAR_ADJUST_SPEED = 1
@xenobrain
xenobrain / hex_game.rb
Last active January 5, 2023 16:07
starting point for a hex based game
include MatrixFunctions
# Math constants
K_INV_6 = 1.0 / 6.0
K_2_OVER_3 = 2.0 / 3.0
K_2_PI = Math::PI * 2.0
K_SQRT_3 = Math.sqrt 3.0
K_NEG_1_OVER_3 = -1.0 / 3.0
K_DEG2RAD = Math::PI / 180.0
K_RAD2DEG = 360.0 / (2.0 * Math::PI)
args.gtk.method(:write_file).parameters.map(&:last).map { |p| p.to_s }