Skip to content

Instantly share code, notes, and snippets.

View chunkyguy's full-sized avatar

Sidharth Juyal chunkyguy

View GitHub Profile
@chunkyguy
chunkyguy / Exception.cpp
Created October 13, 2012 08:16
Exception Handling C vs C++
/*
What: Exception using throw and jmp/longjmp
Author: Sid
Note: Test run with 3 cases
1: x(10), y(2) : Returns 2
2: x(10), y(0) : Catch exception DIVIDE_EXCP, prints error and quit
3: x(10), y(-2) : Catch exception NEG_EXCP, does nothing
*/
#include <iostream>
@chunkyguy
chunkyguy / lpf.cpp
Created November 6, 2012 00:07
largest prime factor - unoptimized
#include <iostream>
#include <cmath>
#include <cstring>
bool debug;
bool isFactor(long double n, long double f){
long double i = 1.0;
long double m = f * i;
while(m <= n){
@chunkyguy
chunkyguy / gist:5082285
Created March 4, 2013 13:37
Line segment intersection test
// Test for two line segments intersection
// sid@whackylabs.com
// Inspired from:
// theory: http://www.bryceboe.com/2006/10/23/line-segment-intersection-algorithm/
// source: http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
#include <iostream>
template<typename T>
/*
sid@whackylabs.com
Tool to test collision detection using Grapher.app
Create formatted data.
Input: cd.in
0.000000 320.000000 194.204971 -60.418026 244.204971 -10.418026
0.000000 320.000000 194.249619 -60.418026 244.249619 -10.418026
0.000000 320.000000 194.294266 -60.418026 244.294266 -10.418026
@chunkyguy
chunkyguy / gist:5206358
Created March 20, 2013 17:01
C++ pointer to member function
// Using std::function for callback
#include <iostream>
#include <utility>
class Client;
// Calculator server
class Server{
public:
typedef void (Client::*CB)(int result);
@chunkyguy
chunkyguy / gist:5253727
Created March 27, 2013 12:08
This code demonstrates a way to broadcast messages to many listeners. I'm scrapping this code I worked on for few hours. Why? Because for now I'm going forward with one global event dispatcher. Right now on iDevices it doesn't makes any sense to buffer the events, as the OS already buffers them. Maybe someday I'll return to something similar.
//
// EventLoop.h
// HideousGameEngine
//
// Created by Sid on 27/03/13.
// Copyright (c) 2013 whackylabs. All rights reserved.
//
#ifndef __HideousGameEngine__EventLoop__
#define __HideousGameEngine__EventLoop__
@chunkyguy
chunkyguy / gist:5908490
Created July 2, 2013 11:15
texture coordinates
<html>
<head>
<title>Texture Coordinates</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="webgl-utils.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
// Thank you std::unique_ptr!
// Compile with
// clang++ unique_ptr.cpp -o unique_ptr -std=c++11 -stdlib=libc++ && ./unique_ptr
/* OUTPUT:
--- START ---
Game
--- MAIN ---
Screen
MainScreen
@chunkyguy
chunkyguy / gist:5986168
Last active July 19, 2018 23:08
std::unique_ptr: Creating unique_ptr to hold array of pointers to something.
// unique_ptr
// Case 1: Array of type
// Case 2: Array of pointer to type
#include <iostream>
#include <memory>
class Bar {
public:
Bar(float b) : bb(b) {
@chunkyguy
chunkyguy / theory.cpp
Created August 14, 2013 21:00
Theory behind C++ I/O.
#include<iostream>
#include<fstream>
using namespace std;
struct test
{
short sss;
char ccc;
int iii;