Skip to content

Instantly share code, notes, and snippets.

@JoshuaSmyth
JoshuaSmyth / helloworld.cpp
Created April 11, 2022 06:39
Emscripten Canvas putImageData example.
#include <emscripten/val.h>
#include <emscripten.h>
#include <stdio.h>
void Copy_ToCanvas(uint32_t* ptr, int w, int h)
{
EM_ASM_(
{
var data = Module.HEAPU8.slice($0, $0 + $1 * $2 * 4);
let context = Module['canvas'].getContext('2d');
@JoshuaSmyth
JoshuaSmyth / main.c
Created April 10, 2022 02:41
Example of a defer macro for use in C
#include <stdio.h>
#define macro_var(name) name##__LINE__
#define defer(start, end) for ( \
int macro_var(_i_) = (start, 0); \
!macro_var(_i_); \
(macro_var(_i_) += 1), end) \
#define profile defer(profile_begin(), profile_end())
@JoshuaSmyth
JoshuaSmyth / main.cpp
Last active April 3, 2022 01:17
Handmade window in CPP - Based of Handmade Hero.
#include <Windows.h>
#include <stdio.h>
#include "game.h" // Provides Render_Game() and Update_Game() functions
#define internal static;
#define global static;
global bool gIsRunning = true;
global bool gIsRendering = true;
@JoshuaSmyth
JoshuaSmyth / MapToJson.cs
Last active March 6, 2022 00:44
Converts a Duke3d build file format map to json.
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Map2json
{
internal class Program
{
// I changed some of the field names a litte
@JoshuaSmyth
JoshuaSmyth / Program.cs
Last active July 17, 2021 01:40
HTML <include> Tag
// Replaces a <include src="filename.html"/> tag with the html in the filename
using System;
using System.IO;
using System.Xml.Linq;
using System.Linq;
namespace HtmlBuild
{
class Program
@JoshuaSmyth
JoshuaSmyth / ObjLoader.cs
Last active June 26, 2021 22:09
Simple Obj Loader in CSharp
using Monorail.Mathlib;
using Monorail.Platform;
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
namespace Monorail.Graphics
{
// You will probably need to remap some math operations Vector, Cross-Product etc... into your existing framework.
@JoshuaSmyth
JoshuaSmyth / ScopedTimer
Created August 6, 2018 04:35
A simple timer that will call a function when going out of scope.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Proto.Structure
{
struct TimerRecord
{
public Stopwatch Stopwatch;
@JoshuaSmyth
JoshuaSmyth / ServiceRegistry.cs
Created August 6, 2018 04:34
A simple service registry for a service locator.
using System;
using System.Collections.Generic;
namespace Proto.Structure.Services
{
public interface IService { }
public static class ServiceRegistry
{
private static readonly Dictionary<Type, IService> InstantiatedServices = new Dictionary<Type, IService>();
@JoshuaSmyth
JoshuaSmyth / MessageSystem.cs
Last active October 17, 2019 21:14
A Message Pub/Sub System for C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Proto.Structure.Services
{
// Any message you want to pass should implement IMessage
public interface IMessage { }
// Any subscriber should implement IMessage Handler