Skip to content

Instantly share code, notes, and snippets.

View benloong's full-sized avatar

benloong benloong

View GitHub Profile
@benloong
benloong / RandomExtension.swift
Created June 28, 2016 09:20
Array random take extension
import Foundation
extension Array {
func random2(_ lower: Int, _ upper: Int) -> Int {
return random() % (upper - lower) + lower
}
func randomTake<T>(_ n: Int) -> [T] {
public bool Raycast(Ray r, out float distance)
{
Vector3 min = this.min;
Vector3 max = this.max;
float tmin = float.NegativeInfinity, tmax = float.PositiveInfinity;
Vector3 dir_inv = new Vector3();
@benloong
benloong / ExportSprites.cs
Created July 11, 2016 03:52 — forked from tsubaki/ExportSprites.cs
Load Multiple Sprite with scriptableObject
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ExportSprites {
[MenuItem("Assets/Create/SpriteScriptableObject")]
static void Export()
{
foreach (var sprite in Selection.objects) {
@benloong
benloong / UIDissolve.shader
Last active February 11, 2025 15:39 — forked from nukeop/dissolve.shader
Dissolve shader for Unity
Shader "UI/Dissolve"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
@benloong
benloong / delegate.cpp
Last active December 20, 2016 06:36
c++ delegate implementation
#pragma once
#include<type_traits>
template <typename T> class Delegate;
template<class R, class ...Args>
class Delegate <R(Args...)> final
{
@benloong
benloong / PageViewController.cs
Created November 3, 2016 08:59
PageViewController for Unity engine.
using UnityEngine;
using UnityEngine.EventSystems;
public class PageViewController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler
{
public enum Direction
{
Horizontal,
Vertical
}
template<typename L1, typename L2>
struct S : L1, L2
{
S(L1 l1, L2 l2) :L1( std::move(l1) ), L2( std::move(l2) )
{
}
using L1::operator();
using L2::operator();
@benloong
benloong / Semaphore.cpp
Created December 8, 2016 05:17
Semaphore using c++11 condition_variable and mutex, SpinLock using atomic_flag
#include <future>
struct SpinLock {
std::atomic_flag spin_flag = ATOMIC_FLAG_INIT;
void lock() {
for (size_t k = 0; spin_flag.test_and_set(std::memory_order_acquire); k++)
{
if (k < 4)
{
@benloong
benloong / aligned_allocator.h
Created December 19, 2016 09:17
aligned allocator
#pragma once
#include<memory>
template<int alignment>
struct aligned_allocator
{
static_assert((alignment&(alignment - 1)) == 0, "alignment must be power of 2.");
void* allocate(size_t size) {
@benloong
benloong / pool_allocator.h
Created December 19, 2016 13:00
simple pool allocator
#pragma once
#include<memory>
template<typename T, size_t ChunkSize = 1024>
struct pool_allocator
{
using alloc_type = T;
using pointer_type = alloc_type*;
pool_allocator()