Skip to content

Instantly share code, notes, and snippets.

@RonenNess
RonenNess / SimpleHTTPServer.cs
Last active July 22, 2022 21:23
A slightly modified version of SimpleHTTPServer.cs, see comments for details.
// MIT License - Copyright (c) 2016 Can Güney Aksakalli
// https://aksakalli.github.io/2014/02/24/simple-http-server-with-csparp.html
// This version is slightly modified from the original source stated above, see comments for details.
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
@RonenNess
RonenNess / csharp_forms_listbox_logs
Last active March 20, 2022 22:33
Snippet to turn a ListBox into a colored logs renderer
public Form1()
{
InitializeComponent();
// set custom draw method
List_EventsLog.DrawMode = DrawMode.OwnerDrawVariable;
List_EventsLog.DrawItem += List_EventsLog_DrawItem;
}
/// <summary>
@RonenNess
RonenNess / generate_toc_from_md.py
Created January 12, 2022 18:47
A very basic python script to generate ToC from markdown file. Only works for simple cases and 2 level of titles (# and ##).
def generate_link(title):
return "[" + title + "](#" + title.lower().replace(' ', '-') + ")"
print ("# Table Of Content")
with open("README.md", 'r') as infile:
for line in infile:
line = line.strip()
if line.startswith('## '):
@RonenNess
RonenNess / message_buffers.cs
Created December 19, 2021 22:38
Utility classes to pack and unpack data in bytes buffers in C#
/// <summary>
/// Utility to serialize data directly into a buffer.
/// </summary>
public class MessageSerializer
{
// the buffer containing the data.
private byte[] _buffer;
/// <summary>
/// Get the buffer itself that contains the data.
@RonenNess
RonenNess / string_utils.cs
Created December 19, 2021 22:34
Misc string related utils for C#, including deterministic hash method.
/// <summary>
/// String-related utils.
/// </summary>
public static class StringUtils
{
/// <summary>
/// Convert string to bytes[].
/// </summary>
/// <param name="str">String to convert.</param>
/// <returns>String as bytes[].</returns>
@RonenNess
RonenNess / random_string_gen.cs
Created December 19, 2021 22:33
C# random string generator.
// possible characters for the random strings generation
static string _charsForRandomness = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
/// <summary>
/// Generate a random string at a given length.
/// </summary>
public static string GenerateRandomString(int length = 8)
{
// generate random characters
var stringChars = new char[length];
@RonenNess
RonenNess / PackedColor.cs
Created December 19, 2021 22:26
A tiny utility to pack a color value in a single byte,
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
/// <summary>
/// Colors enum, can be packed in 4 bits.
/// </summary>
public enum ColorsEnum
{
White,
Red,
Green,
@RonenNess
RonenNess / AssetsLoader.cs
Created September 8, 2021 21:12
A utility class to load raw assets into MonoGame without building them as xnb first. To use this, add the MonoGame.Framework.Content.Pipeline and MonoGame.Framework.xxx NuGet packages.
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using MonoGame.Framework.Content.Pipeline.Builder;
using System.Reflection;
using System.IO;
@RonenNess
RonenNess / random_level_generator.js
Last active March 31, 2022 18:23
Random dungeon generator in JS - based on rectangle rooms and doors - MADE FOR 5mbg.com - feel free to use this
// generate a random level with rooms
// mapSize = level size {x, y}
// maxRooms = max number of rooms to generate (number). note: under some conditions the result will have less rooms than max rooms, for example if we run out of places to place rooms in.
// minRoomSize = min room size (number).
// maxRoomSize = max room size (number).
// base on the concepts found here: https://medium.com/@victorcatalintorac/dungeon-with-rooms-algorithm-for-javascript-ultimate-begginer-guide-ec1489e90314
// author: Ronen Ness.
// feel free to use this for any purpose.
// NOTE: made for a project on http://5mbg.com/.
function generateLevel(mapSize, maxRooms, minRoomSize, maxRoomSize) {
@RonenNess
RonenNess / gist:433eb904e695ba9026769122150ddecd
Created March 2, 2021 20:14
184 lines of code Snake game written in 5mbg.com
// general defs
const LEVEL_WIDTH = 40; // playable region width
const LEVEL_HEIGHT = 20; // playable region height
const SCALE = 4; // scale factor for rendering
const SCREEN_WIDTH = LEVEL_WIDTH * SCALE; // screen width
const SCREEN_HEIGHT = LEVEL_HEIGHT * SCALE; // screen height
// set engine init flags
initFlags.gfx.crispRendering = true; // make rendering pixelated / crisp
initFlags.gfx.fixedResolutionX = SCREEN_WIDTH; // lock x resolution