Skip navigation

Category Archives: misc

I keep coming across code like

for( int deviceIndex=0; deviceIndex < devices.size(); deviceIndex++ )
{
    devices[deviceIndex].something();
    devices[deviceIndex].param += value;
}

what’s wrong with this code? wouldn’t it be a lot better like this?

for( int i=0; i < devices.size(); i++ )
{
    devices[i].something();
    devices[i].param += value;
}

i is a dummy variable. It’s name doesn’t matter – it just needs to be short. devices[deviceIndex] is redundant – we already know what we are accessing by the array’s name, devices[deviceIndex] is just longer and noisier than devices[i] – without adding additional clarity.

Just came across this in a C code apple example:

do
{
  // stuff..
} while (false);

The code breaks the loop when an error occurs inside it. This is kind of like try/catch/finally, where you have a simple catch(…) all block that you jump to if you “break” the loop. It seems like a poor substitute for a function call however.

Some artifacts may arise. For example, say you inherit from std::string. You want to write a function that ends up calling a method from std::string that returns a std::string: (like .substr()). Now you have an extra ctor call to construct your derived type object –

I’m testing polldaddy polls. Here’s the link.

The weirdest shit. You have to ask OpenGL to normalize byte colors. So the correct call to glVertexAttribPointer would be:

glVertexAttribPointer( attribNo, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, squareColors);

I spent like 20 minutes looking for what the problem could be with my code when I tried to switch float colors to byte colors, all my colors were white. And it was just this `normalized` flag.

I effectively had this:

void fun( const char* msg )
{
  function = [msg](){
    // do something with msg
  }
}

Then call:

fun( string( "oh hi hello" ).c_str() ) ;

See what’s wrong? The string object whose char* ptr I passed to fun _dies_ immediately after the line executing call to fun runs. So the captured const char* ptr [msg] is invalid if function() runs at a later time (after return of call to fun())

nov 20 2013 4:22p

The item that an enemy would go for was flip flopping. It would continually flip between chasing after the 2 nearest items. I detected this bug in 2 minutes on the 1st launch, because I had inserted printf statements.

- Detection: printf statements
Enemy squadron Fighter thinks item B1 is reachable in 2.703664 seconds
Enemy squadron Fighter thinks item Bandit is reachable in 2.884945 seconds
Enemy squadron Fighter thinks item B1 is reachable in 2.703305 seconds
Enemy squadron Fighter thinks item Bandit is reachable in 2.884617 seconds

Reason: units were programmed not to chase after items that units on their same team were chasing after. Unfortunately overlooked the fact that I would be seen on as on my same team.

Solutions:
– If I’m already gathering an itembox, stick with it until i get it OR
– If the person gathering the itembox is ME, keep going after it.


Using integer indices into arrays of objects (instead of iterators, or pointers to the objects themselves) has saved my ass many times.

Say you have a vector<Object>. Well, you wouldn’t store pointers of type Object* to an element of this array, ever, because if the vector gets resized, all objects get deallocated/reallocated and so all your previous references will be invalid. But if you store a vector<Object*> and save an external reference to an Object* within that vector, then that’s no problem.

But anyway, storing integer arrays is much better than storing copies of pointers. For one, if the pointer gets deleted, you’re not left with an invalid / dangling pointer somewhere. Checking if a reference into the array is valid because you just have to check the integer index is between 0 and (.size()-1).

Simple example


f := (x) -> x^2

Is the function f(x)=x^2. You call it by writing


f(2)

To animate the function, just add another parameter and give it a range in a plot command.


plot( a*f(x), x=-2..2, a=0.1..5 )

C++’s RTTI is expensive. Very expensive. Notoriously and verifiably expensive.

So how can we efficiently get information of the concrete subclass, given an abstract base class pointer, without the great expense of using RTTI?

Simple. Use an enum.

Consider the example in my stackoverflow post. What I did there is just keep a single bitfield (an int) that has concrete types bitwise OR’d in, in each derived constructor.

So, in the example posted, an object of Type BigCat would have 0111 for its typeTag field, indicating that it is an Animal, a Cat, and a BigCat. Concrete type checking is cheap as a bitwise AND.

eta is easy to compute if you have distance between 2 objects, their relative accelerations, and relative velocities.

By _relative_ accelerations and velocities, i mean the rate at which they are coming together. So you have to project both objects’ velocity components onto the line joining the two objects, and use that scalar component in these computations.

Without further ado, eta:

eta = -v1 +/- sqrt( v12 + 2a(dist) ) / a

In image:

I ran into this error before, it is described here. Basically preface whatever the offending line is with this->.

Did you know if you work in “High Quality Rendering” mode (click the red box) the split polygon tool is _much more helpful_?

What’s wrong with this code?

class Object{
  int x ;
  Object() {
    defaults() ;
  }
  Object( int ix ) : x(ix) {
    defaults() ;
  }
  
  defaults() { 
    x=0;
  }
} ;

Well, obviously x has the value 0, instead of the initializer-list desired default value sent `ix`.

So, what happens when you have 10-15 members that all have default value initialization in defaults(), and possible assignment in the initializer list, is defaults() clobbers whatever was sent to the initializer list, since the initializer list runs first.

I puzzled over this for a few minutes.

Warcraft 1,2 & 3 used swords for attack, shields for stop. It works because the entire theme was medieval. So, swords mean attack, shields mean “stop”. Regardless of unit type (even air units — there’s no cognitive dissonance there).

Starcraft 1 & 2 cleverly abstracted this away though. Swords clearly wouldn’t fit in Starcraft. Different units had different means of attacking. Zerg never use guns. They use spit or claws. Protoss had some “sword-like” units that did hand-to-hand combat (zealots), but other units fired projectiles (dragoons). They used a _crosshair_ instead for the attack symbol.

So, in my own game I had to think, what symbolizes _attack_ in my game? It’s a space combat game, and attack is done primarily via missiles. So I subbed in a picture of a missile for attack. And voila. The right symbol for attack is _a picture of whatever attacking is done with_ in your game.

If it were a streetfighting game, you’d use an icon of a fist.

I couldn’t find a way to do this through the GUI, but I’m positive there was one.

Anyway, the MEL command to show normals for the selected mesh:

polyOptions -dn 1;

And to hide them again

polyOptions -dn 0;

Taken from here

Maya rotate snap hotkey is j

j means rotate snap, because you know, in original english rotation by discrete amounts of 15 degrees was called “jotation”.

Anyway, double clicking the ROTATE TOOL gives you a box @ right, you can modify the size of the discrete step there.

If you notice, holding j “temporarily” clicks on the “discrete rotate” setting.

Maya vertex snap hotkey is v

If you hold v it will let you snap a vertex to another one while dragging.

Maya is a visual editor, so they actually hid the information of the numerical coordinates of the vertices pretty well.

You can usually get the results you want by snapping. But when you can’t, there’s the Component Editor.

You go to Window / General Editors / Component Editor to get it. I learned this from a SimplyMaya thread.

I almost forgot the simple formula for checkerboarding,

if( (i % 2) == (j % 2) )  WHITE;
else BLACK ;

If both i and j are EVEN, OR both i and j are ODD, you get one color. If they differ in “oddness” then you get the other color.

There is a really shitty post on stackoverflow about this, filled with nonsense.

I’m working with old OpenGL code. Specifically, code based off GLES2Sample.

There was a slight change around iOS 5.0 or so, requiring you to have a root view controller. If your code is based off older sample code, such as GLES2Sample, then no root view controller was created in those code samples.

To fix GLES2Sample, I know the following fix works. Right in `applicationDidFinishLaunching`, I create a root view controller and attach my glView to it.

    - (void) applicationDidFinishLaunching:(UIApplication *)application
    {
      // To make the 'Application windows are expected
      // to have a root view controller
      // at the end of application launch' warning go away,
      // you should have a rootviewcontroller,
      // but this app doesn't have one at all.
      window.rootViewController = [[UIViewController alloc] init];  // MAKE ONE
      window.rootViewController.view = glView; // MUST SET THIS UP OTHERWISE
      // THE ROOTVIEWCONTROLLER SEEMS TO INTERCEPT TOUCH EVENTS
    }

That makes the warning go away, and doesn’t really affect your app otherwise.

Developer journals are my answer to dead code. Word Documents, blog entries, whatever. Source control history is harder to wade through than dated log entries about how a function used to work, and how it works now.

Logging with thoughts on what changed and why it changed is essential, even if you’ll never refer to it, the peace of mind you get while simultaneously retaining a clean code base is worth it. Delete with confidence – and a backup. Plus, you’ll never get the clarity why you deleted something as when the moment you deleted it.

Here’s an example of a journal entry I just made for today:

This is part of a Word document. I just downsized the font so it is quickly scrollable. I doubt I will need the comments or dead code I am about to delete, or that I will be unable to regenerate them easily, but just in case, the info is all there in this dated entry.

This article confirmed my suspicion.

Besides a wide variety of flavors and roast selections (with French Vanilla available as a light roast), Target’s coffee is indeed my cup of.. coffee.

So far I’ve only began the French Vanilla bag, but it tastes delicious. It actually tastes a lot like coffee shop coffee. It’s very fresh, very smooth, and the vanilla flavoring is just right — not overpowering, but pleasantly tasty.

I also got the plain light roast. It costs about $3.50 less than a bag of Starbuck’s blonde, if it’s good, it will probably be my new main.

There’s this point in Woo’s paper that confused me before, and it just confused me again now.

The paper says to always pick the largest t.

So the situations in red and pink confused me. It would seem at first glance that “further t” (in the pink diagram, the one intersecting min.y) would be the “greater t” but the _wrong one_ to pick.

But it turns out when you cull the back faces _based on ray direction_, the smaller t would be at the max.y plane, even though the ray starts AFTER the max.y plane.

Note Woo’s method as presented _doesn’t work for when the ray starts totally inside the box_.

If the ray starts inside the box, if you only need the boolean result then RETURN TRUE immediately. But if you need the intersection point, you need to turn around some of the rules of the test:

From “Geometric Tools for Computer Graphics”, the definition of convex given on pg 265 sec 7.7 is:

A set S is convex if given any two points P and Q in S,the line segment ( 1 − t ) P + t Q for t ∈ [0, 1] is also in S.

Said more plainly, if every line you can draw that connects two vertices a shape is completely contained inside the shape (tracing edges is allowed), then that shape is convex.

Basic program showing how to use GLSL and GLUT together

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <map>
using namespace std ;

#define _USE_MATH_DEFINES
#include <math.h>

// REMEMBER TO INSTALL GLEW! http://glew.sourceforge.net/install.html
// 
// 2 WAYS TO COMPILE GLEW:
// 1)  AS A STATIC LIB.
//     #defining GLEW_STATIC means to compile the glew library as a static lib.
//     This means your prog WILL NOT require the glew32.dll file to accompany your executable.
//
// THIS IS HOW YOU COMPILE GLEW AS A STATIC LIB:
#define GLEW_STATIC
#include <GL/glew.h>
#pragma comment( lib, "glew32s.lib" )   // the 's' is for 'static'

// 2)  AS A DYNAMIC LIB:
//     THIS REQUIRES YOU INCLUDE glew32.dll WITH YOUR FINAL EXECUTABLE/HAVE IT INSTALLED.
//#include <GL/glew.h>
//#pragma comment( lib, "glew32.lib" )

// I'm using the STATIC LIB way above, because I prefer it.

#include <GL/glut.h>

GLuint glslProgId ;     // GLSL shader PROGRAM identifier
GLuint vshId, pshId ;   // GLSL vertex shader id, pixel shader id

// These 2 are "handles" that connect the POSITION attribute in
// the GLSL shader with __AN ACTUAL SET OF VERTICES__ from the C++ source code.
// In this program each VERTEX has a position and a color (only).
GLuint positionAttribute, colorAttribute;

// This is the connector between the uniform variable modelViewMatrix,
// used in the glsl shader code.  We need this variable to LOAD
// a specific modelview matrix INTO the GLSL shader before rendering the verts.
GLuint uniformModelViewMatrix;

// The vertex array object identifier.  This single number
// identifies EVERYTHING about the vertex buffer we are 
// about to create and render, from its attribute set (positions, colors)
// to where to fetch the data from when it's time to draw.
GLuint vao ;

// window width and height.
float w=512.f, h=512.f;

struct VertexPC
{
  float x,y,z, r,g,b,a ;
  VertexPC( float ix, float iy, float iz, float ir, float ig, float ib, float ia ):
    x(ix),y(iy),z(iz), r(ir),g(ig),b(ib),a(ia)
  {}
} ;

// The data for the vertex and index buffers.
// I'm just using index buffers to show how they are used.
static VertexPC verts[] = { 
  VertexPC(0,0,0, 1,0,0,1),
  VertexPC(1,0,0, 0,1,0,1),
  VertexPC(0,1,0, 0,0,1,1)
} ;
static int indices[] = {0,1,2};

map<int,const char*> glErrName ;
// GL ERR
map<int,const char*> createErrMap()
{
  map<int,const char*> errmap ;
  errmap.insert( make_pair( 0x0000, "GL_NO_ERROR" ) ) ;
  errmap.insert( make_pair( 0x0500, "GL_INVALID_ENUM" ) ) ;
  errmap.insert( make_pair( 0x0501, "GL_INVALID_VALUE" ) ) ;
  errmap.insert( make_pair( 0x0502, "GL_INVALID_OPERATION" ) ) ;
  errmap.insert( make_pair( 0x0503, "GL_STACKOVERFLOW" ) ) ;
  errmap.insert( make_pair( 0x0504, "GL_STACK_UNDERFLOW" ) ) ;
  errmap.insert( make_pair( 0x0505, "GL_OUTOFMEMORY" ) ) ;
  
  errmap.insert( make_pair( 0x8CD5, "GL_FRAMEBUFFER_COMPLETE" ) ) ;
  errmap.insert( make_pair( 0x8CD6, "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT" ) ) ;
  errmap.insert( make_pair( 0x8CD7, "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT" ) ) ;
  errmap.insert( make_pair( 0x8CD9, "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS" ) ) ;
  errmap.insert( make_pair( 0x8CDD, "GL_FRAMEBUFFER_UNSUPPORTED" ) ) ;                    
  return errmap ;
} 

inline bool GL_OK()
{
  GLenum err = glGetError() ;
  if( err != GL_NO_ERROR )
    printf( "GLERROR %d %s\n", err, glErrName[ err ] ) ;
  return err == GL_NO_ERROR ;
}

inline bool GL_OK( int line, const char* file )
{
  GLenum err = glGetError() ;
  if( err != GL_NO_ERROR )
    printf( "GLERROR %d %s, line=%d of file=%s\n", err, glErrName[ err ], line, file ) ;
  return err == GL_NO_ERROR ;
}

inline bool CHECK( bool cond, const char* errMsg )
{
  if( !cond )  puts( errMsg ) ;
  return cond ;
}

// WITH LINE NUMBERS
#define CHECK_GL GL_OK( __LINE__, __FILE__ ) 

// WITHOUT LINE #s
//#define CHECK_GL GL_OK()

void printShaderInfoLog(GLuint obj)
{
  int infologLength = 0;
  
  glGetShaderiv( obj, GL_INFO_LOG_LENGTH, &infologLength ) ;
  if( infologLength )
  {
    char *infoLog = (char *)malloc(infologLength);
    int charsWritten ;
    glGetShaderInfoLog( obj, infologLength, &charsWritten, infoLog ) ;
    puts( infoLog ) ;
    free( infoLog ) ;
  }
}

void printProgramInfoLog( GLuint obj )
{
  int infologLength = 0;
  
  glGetProgramiv( obj, GL_INFO_LOG_LENGTH, &infologLength ) ;
  if( infologLength )
  {
    char *infoLog = (char *)malloc( infologLength ) ;
    int charsWritten ;
    glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
    puts( infoLog ) ;
    free( infoLog ) ;
  }
}

void setupShaders()
{
  // The text of the shaders.  I put it as an inline string
  // just so I could post this easily w/o external files,
  // but you could easily load the shader from an external file.
  // The point is, you must pass your shader to OpenGL as
  // just a NULL terminated string.
  const char *vs = 
    "uniform mat4 modelViewMatrix ;                         "
    "                                                       "
    "attribute vec3 position;                               "
    "attribute vec4 color;                                  "
    "                                                       "
    "varying vec4 Color;                                    "
    "                                                       "
    "void main()                                            "
    "{                                                      "
    "  Color = color;                                       "
    "  gl_Position = modelViewMatrix * vec4(position,1.0) ; "
    "}                                                      " ;

  const char *fs =
    "in vec4 Color;            "
    "                          "
    "void main()               "
    "{                         "
    "  gl_FragColor = Color ;  "
    "}                         " ;
  
  vshId = glCreateShader(GL_VERTEX_SHADER) ;  CHECK_GL ;
  glShaderSource( vshId, 1, &vs, NULL ) ;  CHECK_GL ;
  glCompileShader( vshId ) ;  CHECK_GL ;
  printShaderInfoLog( vshId );
  
  pshId = glCreateShader(GL_FRAGMENT_SHADER) ;  CHECK_GL ;
  glShaderSource( pshId, 1, &fs, NULL ) ;  CHECK_GL ;
  glCompileShader( pshId ) ;  CHECK_GL ;
  printShaderInfoLog( pshId );

  glslProgId = glCreateProgram();
  glAttachShader( glslProgId, vshId );
  glAttachShader( glslProgId, pshId );

  glLinkProgram( glslProgId );
  printProgramInfoLog( glslProgId );

  positionAttribute = glGetAttribLocation( glslProgId, "position" ) ;
  colorAttribute = glGetAttribLocation( glslProgId, "color" ) ; 

  uniformModelViewMatrix = glGetUniformLocation( glslProgId, "modelViewMatrix" ) ;

  
  
  // Make a "vertex buffer"
  GLuint vertexBuffer ;
  glGenBuffers( 1, &vertexBuffer ) ;

  // LOAD THE DATA INTO IT:
  glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer ) ;
  glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW ) ;

  // Make an "index buffer"
  GLuint indexBuffer ;
  glGenBuffers( 1, &indexBuffer ) ;
  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer ) ;
  glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( indices ), indices, GL_STATIC_DRAW ) ;



  //// SET UP THE VAO
  // You know, this is weird, but a VERTEX ARRAY OBJECT
  // DOESN'T ACTUALLY STORE DATA.
  // Vertex BUFFERS store the data.
  // A VAO stores the SETUP of how a group of
  // vertices is to be rendered.
  // That is, it stores:
  // 1)  The DATA (in vertex BUFFERS) to be drawn
  // 2)  THE SEMANTICS of that data (vertex format)
  // 3)  THE LINKAGE TO THE SHADER THAT WILL BE USED TO DRAW THIS DATA.

  // So you can see, it's all very intermingled and messy.
  // TO draw vertex data with GLSL, you have to:
  //   1) Load the raw vertex data into a vertex buffer
  //   2) set up vertex attributes (via glVertexAttribPointer)
  //      WHICH does 2 things:
  //      a)  It tells OpenGL the VERTEX FORMAT (floats? 3 elements? 4 elements? stride?)
  //      b)  It tells OpenGL THE SHADER VARIABLE THAT THIS VERTEX ATTRIBUTE LINKS INTO.
  //   3) Call glDrawArrays or glDrawElements( ..., 0 ) 
  //      
  // INIT THE VERTEX ARRAY (stuff to draw)
  // The vertex array OBJECT remembers info about
  // WHAT VERTEX ATTRIBS ARE TO BE BOUND, to what
  // indices, and the actual vertex buffer object
  // from which to pull data when rendering.
  glGenVertexArrays( 1, &vao ) ;
  glBindVertexArray( vao ) ;

  glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer ) ;
  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer ) ;
  
  glEnableVertexAttribArray( positionAttribute ) ;
  glVertexAttribPointer(
    positionAttribute,  // index. NOTE THIS HAS BEEN CONNECTED UP EARLIER
    // WITH THE VERTEX SHADER THAT WILL BE USED TO RENDER THIS VERTEX ARRAY
    // IN THE LINE:
    //   > positionAttribute = glGetAttribLocation( glslProgId, "position" ) ; // C++ code line
    // and "position" is used in the GLSL vertex shader as:
    //   > attribute vec3 position; // GLSL shader line
    // SO. Don't be confused, the `positionAttribute` variable is simply connected up
    // to the vertex shader's INCOMING "position" attribute.  Naked girls.
    3,          // size. 3 floats/vertex
    GL_FLOAT,   // type. floats!
    0,          // normalized? No.
    sizeof(VertexPC),          // stride
    0           // pointer.  0 says to use the CURRENTLY BOUND
    // VERTEX BUFFER (WHATEVER THAT IS AT THE TIME OF THE DRAW CALL).
    // If you want to draw from CLIENT MEMORY you would pass AN ACTUAL ADDRESS
    // here (BUT YOU MUST MAKE SURE TO HAVE NO VERTEX BUFFER BOUND BECAUSE THEN
    // THE MEMORY ADDRESS YOU PASS HERE WOULD BE INTERPRETTED AS AN __OFFSET__
    // INTO THAT VERTEX BUFFER, TRIGGERING A SEGFAULT/OUT OF BOUNDS EXCEPTION TYPE THING.)
  ) ;

  glEnableVertexAttribArray( colorAttribute ) ;

  int offset = 3*sizeof(float) ;
  glVertexAttribPointer( colorAttribute, 4, GL_FLOAT, 0, sizeof( VertexPC ),
  
    (void*)offset  // RE: THE LAST PARAMETER: I know it looks ridiculous!! to pass a NUMBER as cast to VOID* here,
    // but that's C api's for you.  "Wait! Shouldn't it be &offset?"
    // NO. Your eyes kid you not.  You take the INTEGER NUMBER (which amounts to 12 here)
    // AND JUST PASS THAT AS IF IT WERE A "VOID*" POINTER. Even though you and I know it is not.
  ) ;

}

void keyboard(unsigned char key, int x, int y)
{
  switch(key)
  {
    case 27: //esc
      glDeleteVertexArrays( 1,&vao ) ;
      glDeleteProgram( glslProgId );
      glDeleteShader( vshId );
      glDeleteShader( pshId );
      exit(0);
      break ;
  }
}

void resizeWindow(int width, int height)
{
  w=width,h=height;
}

void draw()
{
  glViewport( 0, 0, w, h ) ;
  
  glClearColor( 0.1f, 0.1f, 0.1f, 0.1f ) ;
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;

  // Bind the shader I created earlier
  glUseProgram( glslProgId );
  
  // Update the uniform variable.  YOU CAN
  // ONLY UPDATE THE UNIFORMS OF __BOUND SHADERS__ --
  // IE glUniformMatrix4fv must be called AFTER glUseProgram
  float m[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 } ;
  glUniformMatrix4fv(uniformModelViewMatrix, 1, false, m);

  // Here we bind the vertex array "object".  This is EQUIVALENT
  // to "replaying" 
  glBindVertexArray( vao );

  // Now we could draw in one of 2 ways:

  // DRAW USING THE VERTEX BUFFER (ONLY):
  //glDrawArrays( GL_TRIANGLES, 0, 3 ) ; // draw as vertex buffer (with no index buffer)

  // DRAW USING INDEX BUFFER + VERTEX BUFFER
  glDrawElements( 
    GL_TRIANGLES,
    3, //# INDICES in the index buffer
    GL_UNSIGNED_INT, // I used 4-byte ints. Some other people use shorts, but I never do.
    0  // 0 means, use the index buffer bound
  ) ;

  glutSwapBuffers();
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowPosition(100,100);
  glutInitWindowSize( w,h ) ;
  glutCreateWindow("GLSL with GLUT");

  glutDisplayFunc(draw);
  glutIdleFunc(draw);
  glutReshapeFunc(resizeWindow);
  glutKeyboardFunc(keyboard);

  glewInit();
  //if( glewIsSupported("GL_VERSION_3_0") )

  glEnable(GL_DEPTH_TEST);
  glClearColor(1.0,1.0,1.0,1.0);

  setupShaders() ;

  glutMainLoop() ;

  return 0 ; 
}

If you’re using numerical indexing to access the xyz components of a vector, (with 0=x, 1=y, 2=z) a good tip is to remember that

axis = 0 ; // for example.
otherAxis1 = ( axis + 1 ) % 3 ; // =1
otherAxis2 = ( axis + 2 ) % 3 ; // =2

This works for all values of axis to get you “the other 2″ axes

The description on Wikipedia is ok, but here’s more.

Marching cubes is all about filling the space with cubes, and “cutting the cubes” by triangles if the isosurface cuts through the cube.

This talk by Shewchuk is great to help understand what an isosurface is, etc.

Now, there’s all this talk about 256 possible cube configurations. What’s that about.

Well the cube has 8 vertices. Pretend being inside the isosurface means the vertex is “wet”. Assuming the cube could dip in and out of the isosurface on any of those 8 vertices, means there are 2^8=256 possible configurations for the vertices to be “wet” (inside the isosurface).

The case where the cube is “completely dry” is when no vertices are inside the isosurface. If I put one corner in the isosurface, then one vertex is “wet”. But wait! There are 8 ways that “one corner” can be wet, because there are 8 corners on a cube. So each of the 8 corners can be in one of 2 states (“wet” or “dry”), means there are 2^8 possible configurations for the corners to be wet and dry. This actually boils down to just 14 cases if you exploit symmetry (ie consider the case of “one vertex being wet” as one case, not 8 cases).

This is easier to understand if you look at a square.

So there, we have 4 vertices, so 2^4=16 cases. case 0 is “dry”. case 1 has 1 vertex wet, and there are 4 symmetric cases. Case 2 has 2 adjacent corners.. etc. Just look at the diagram.

I can’t count the number of times extremely difficult mathematese was made understandable via a quick diagram.

All academics should take COMIC DRAWING as a course in university. The reason is, to be able to express themselves via diagrams and symbols, actors and examples, rather than just relying on words and the jargon of their field.

Well, a surprising result, never use your extern variables inside a function that runs before main.

Here’s an example:

In the header:

//extern test.  the externs are giving 0's.
struct Vector4f
{
  float x,y,z,w;
  Vector4f():x(0),y(0),z(0),w(0){}
  Vector4f( float ix, float iy, float iz, float iw ) :x(ix),y(iy),z(iz),w(iw){}
} ;

extern Vector4f Red ; // This is the weird variable..

In the main.cpp file:


static map<char, Vector4f> initMapping()
{
  map<char, Vector4f> mp ;
  mp.insert( make_pair( 'r', Red ) ) ;
  
  for( auto &p : mp )
    printf( "%c: %f %f %f %f\n", p.first, p.second.x, p.second.y, p.second.z, p.second.w );
  return mp ;
}

//Vector4f Red(1,0,0,1) ; // if this is here, Red has (1,0,0,1)
map<char, Vector4f> colorMapping = initMapping() ;
Vector4f Red(1,0,0,1) ; // if Red is only given its value HERE, when the static ctor runs, Red will be (0,0,0,0)

int main(int argc, const char * argv[])
{
  for( auto &p : colorMapping )
    printf( "%c: %f %f %f %f\n", p.first, p.second.x, p.second.y, p.second.z, p.second.w );
}

So those 2 declarations of Vector4f Red (without extern qualifier) in the cpp are the issue here. If the compiler runs into the Vector4f Red(1,0,0,1) ; BEFORE running SomeStruct::initMapping(), then the mapping has the correct value for Red in it. If the compiler first runs into Vector4f Red(1,0,0,1) only __after__ running SomeStruct::initMapping(), then the value of Red inside the map would be all 0′s.

I was browsing my spam and I came across this “spammer template”: use it to develop spam protection software!

{
{I have|I’ve} been {surfing|browsing} online more than {three|3|2|4} hours today, yet I never found any interesting article like yours. {It’s|It
is} pretty worth enough for me. {In my opinion|Personally|In my view}, if all {webmasters|site
owners|website owners|web owners} and bloggers made good content as you did, the
{internet|net|web} will be {much more|a lot more} useful than ever before.
|
I {couldn’t|could not} {resist|refrain from} commenting. {Very well|Perfectly|Well|Exceptionally well} written!|
{I will|I’ll} {right away|immediately} {take hold of|grab|clutch|grasp|seize|snatch} your {rss|rss feed} as I {can not|can’t} {in finding|find|to find} your {email|e-mail} subscription {link|hyperlink} or {newsletter|e-newsletter} service. Do {you have|you’ve} any?

{Please|Kindly} {allow|permit|let} me {realize|recognize|understand|recognise|know} {so that|in order that} I
{may just|may|could} subscribe. Thanks.|
{It is|It’s} {appropriate|perfect|the best} time to make some plans for the future and {it is|it’s} time to be happy.
{I have|I’ve} read this post and if I could I {want to|wish to|desire to} suggest you {few|some} interesting things or {advice|suggestions|tips}. {Perhaps|Maybe} you {could|can} write next articles referring to this article. I {want to|wish to|desire to} read {more|even more} things about it!|
{It is|It’s} {appropriate|perfect|the best} time to make {a few|some} plans for {the future|the longer term|the long run}
and {it is|it’s} time to be happy. {I have|I’ve} {read|learn} this {post|submit|publish|put up} and if I
{may just|may|could} I {want to|wish to|desire to} {suggest|recommend|counsel} you
{few|some} {interesting|fascinating|attention-grabbing} {things|issues}
or {advice|suggestions|tips}. {Perhaps|Maybe} you {could|can} write {next|subsequent} articles {relating to|referring to|regarding}
this article. I {want to|wish to|desire to} {read|learn} {more|even more} {things|issues} {approximately|about}
it!|
{I have|I’ve} been {surfing|browsing} {online|on-line} {more than|greater than} {three|3} hours {these days|nowadays|today|lately|as of late}, {yet|but} I {never|by no means} {found|discovered} any {interesting|fascinating|attention-grabbing} article like yours. {It’s|It is} {lovely|pretty|beautiful}
{worth|value|price} {enough|sufficient} for me.
{In my opinion|Personally|In my view}, if all {webmasters|site owners|website
owners|web owners} and bloggers made {just right|good|excellent} {content|content material} as
{you did|you probably did}, the {internet|net|web} {will be|shall be|might be|will probably be|can be|will
likely be} {much more|a lot more} {useful|helpful} than ever before.
|
Ahaa, its {nice|pleasant|good|fastidious} {discussion|conversation|dialogue} {regarding|concerning|about|on the topic of} this {article|post|piece of writing|paragraph} {here|at this place} at this {blog|weblog|webpage|website|web
site}, I have read all that, so {now|at this time} me also commenting {here|at this place}.

|
I am sure this {article|post|piece of writing|paragraph} has touched
all the internet {users|people|viewers|visitors}, its really really
{nice|pleasant|good|fastidious} {article|post|piece
of writing|paragraph} on building up new {blog|weblog|webpage|website|web site}.

|
Wow, this {article|post|piece of writing|paragraph} is {nice|pleasant|good|fastidious}, my {sister|younger sister} is
analyzing {such|these|these kinds of} things, {so|thus|therefore} I am going to {tell|inform|let know|convey} her.
|
{Saved as a favorite|bookmarked!!}, {I really like|I like|I love}
{your blog|your site|your web site|your website}!
|
Way cool! Some {very|extremely} valid points!
I appreciate you {writing this|penning this} {article|post|write-up}
{and the|and also the|plus the} rest of the {site is|website is} {also very|extremely|very|also really|really} good.
|
Hi, {I do believe|I do think} {this is an excellent|this is a great} {blog|website|web
site|site}. I stumbledupon it ;) {I will|I am going to|I’m going to|I may} {come back|return|revisit} {once again|yet again} {since I|since i have} {bookmarked|book marked|book-marked|saved as a favorite} it. Money and freedom {is the best|is the greatest} way to change, may you be rich and continue to {help|guide} {other people|others}.|
Woah! I’m really {loving|enjoying|digging} the template/theme of this {site|website|blog}.
It’s simple, yet effective. A lot of times it’s {very hard|very difficult|challenging|tough|difficult|hard}
to get that “perfect balance” between {superb usability|user friendliness|usability} and {visual appearance|visual appeal|appearance}.
I must say {that you’ve|you have|you’ve} done a {awesome|amazing|very good|superb|fantastic|excellent|great} job with this.
{In addition|Additionally|Also}, the blog loads {very|extremely|super} {fast|quick}
for me on {Safari|Internet explorer|Chrome|Opera|Firefox}.
{Superb|Exceptional|Outstanding|Excellent} Blog!|
These are {really|actually|in fact|truly|genuinely} {great|enormous|impressive|wonderful|fantastic} ideas in {regarding|concerning|about|on the topic of} blogging.
You have touched some {nice|pleasant|good|fastidious} {points|factors|things} here.
Any way keep up wrinting.|
{I love|I really like|I enjoy|I like|Everyone loves} what you guys
{are|are usually|tend to be} up too. {This sort of|This type of|Such|This kind of} clever work
and {exposure|coverage|reporting}! Keep up the {superb|terrific|very good|great|good|awesome|fantastic|excellent|amazing|wonderful} works guys I’ve {incorporated||added|included} you guys to {|my|our||my personal|my own} blogroll.|
{Howdy|Hi there|Hey there|Hi|Hello|Hey}! Someone in my {Myspace|Facebook} group shared this {site|website} with us so I came to {give it a look|look it over|take a look|check it out}. I’m definitely
{enjoying|loving} the information. I’m {book-marking|bookmarking} and will be tweeting this to my followers! {Terrific|Wonderful|Great|Fantastic|Outstanding|Exceptional|Superb|Excellent} blog and {wonderful|terrific|brilliant|amazing|great|excellent|fantastic|outstanding|superb} {style and design|design and style|design}.|
{I love|I really like|I enjoy|I like|Everyone loves} what you guys {are|are usually|tend to be} up too. {This sort of|This type of|Such|This kind of} clever work and {exposure|coverage|reporting}! Keep up the {superb|terrific|very good|great|good|awesome|fantastic|excellent|amazing|wonderful} works guys I’ve {incorporated|added|included} you guys to {|my|our|my
personal|my own} blogroll.|
{Howdy|Hi there|Hey there|Hi|Hello|Hey} would you
mind {stating|sharing} which blog platform you’re {working with|using}? I’m {looking|planning|going}
to start my own blog {in the near future|soon} but
I’m having a {tough|difficult|hard} time {making a decision|selecting|choosing|deciding} between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your {design and style|design|layout} seems different then most blogs and I’m looking for something {completely unique|unique}.
P.S {My apologies|Apologies|Sorry} for {getting|being} off-topic but I
had to ask!|
{Howdy|Hi there|Hi|Hey there|Hello|Hey} would you mind letting me know which {webhost|hosting company|web host} you’re {utilizing|working with|using}? I’ve
loaded your blog in 3 {completely different|different} {internet browsers|web browsers|browsers} and I must say
this blog loads a lot {quicker|faster} then most. Can you {suggest|recommend} a good {internet
hosting|web hosting|hosting} provider at a {honest|reasonable|fair} price?
{Thanks a lot|Kudos|Cheers|Thank you|Many thanks|Thanks}, I appreciate it!
|
{I love|I really like|I like|Everyone loves} it {when
people|when individuals|when folks|whenever people} {come together|get together} and share {opinions|thoughts|views|ideas}.

Great {blog|website|site}, {keep it up|continue the good work|stick with it}!
|
Thank you for the {auspicious|good} writeup. It in fact
was a amusement account it. Look advanced to {far|more} added
agreeable from you! {By the way|However}, how {can|could} we communicate?
|
{Howdy|Hi there|Hey there|Hello|Hey} just wanted to give you
a quick heads up. The {text|words} in your {content|post|article} seem to be running off the
screen in {Ie|Internet explorer|Chrome|Firefox|Safari|Opera}.
I’m not sure if this is a {format|formatting} issue or something to do with {web browser|internet browser|browser} compatibility but I {thought|figured} I’d post to let you know.
The {style and design|design and style|layout|design} look great though!
Hope you get the {problem|issue} {solved|resolved|fixed} soon.
{Kudos|Cheers|Many thanks|Thanks}|
This is a topic {that is|that’s|which is} {close to|near to} my heart… {Cheers|Many thanks|Best wishes|Take care|Thank you}! {Where|Exactly where} are your contact details though?|
It’s very {easy|simple|trouble-free|straightforward|effortless} to find out any {topic|matter} on {net|web} as compared to {books|textbooks}, as I found this {article|post|piece of writing|paragraph} at this {website|web site|site|web page}.
|
Does your {site|website|blog} have a contact page?

I’m having {a tough time|problems|trouble} locating it but, I’d like to {send|shoot} you an {e-mail|email}.
I’ve got some {creative ideas|recommendations|suggestions|ideas} for your blog you might be interested in hearing. Either way, great {site|website|blog} and I look forward to seeing it {develop|improve|expand|grow} over time.|
{Hola|Hey there|Hi|Hello|Greetings}! I’ve been {following|reading} your {site|web site|website|weblog|blog} for {a long time|a while|some time}
now and finally got the {bravery|courage} to go ahead and give you a shout out from {New Caney|Kingwood|Huffman|Porter|Houston|Dallas|Austin|Lubbock|Humble|Atascocita} {Tx|Texas}!
Just wanted to {tell you|mention|say} keep up the {fantastic|excellent|great|good} {job|work}!
|
Greetings from {Idaho|Carolina|Ohio|Colorado|Florida|Los angeles|California}!
I’m {bored to tears|bored to death|bored} at work so I decided to {check out|browse} your {site|website|blog} on my iphone during lunch break. I {enjoy|really like|love} the {knowledge|info|information} you {present|provide} here and can’t wait to take a look when
I get home. I’m {shocked|amazed|surprised} at how {quick|fast} your blog loaded on my {mobile|cell phone|phone} .. I’m not even using WIFI, just
3G .. {Anyhow|Anyways}, {awesome|amazing|very good|superb|good|wonderful|fantastic|excellent|great} {site|blog}!
|
Its {like you|such as you} {read|learn} my {mind|thoughts}!

You {seem|appear} {to understand|to know|to grasp} {so much|a lot} {approximately|about} this, {like you|such as you}
wrote the {book|e-book|guide|ebook|e book} in it or something.
{I think|I feel|I believe} {that you|that you
simply|that you just} {could|can} do with {some|a few} {%|p.
c.|percent} to {force|pressure|drive|power} the message {house|home} {a
bit|a little bit}, {however|but} {other than|instead of} that, {this
is|that is} {great|wonderful|fantastic|magnificent|excellent} blog.
{A great|An excellent|A fantastic} read. {I’ll|I will} {definitely|certainly} be back.|
I visited {multiple|many|several|various} {websites|sites|web sites|web pages|blogs} {but|except|however} the audio {quality|feature} for audio songs {current|present|existing} at this {website|web site|site|web page} is {really|actually|in fact|truly|genuinely} {marvelous|wonderful|excellent|fabulous|superb}.|
{Howdy|Hi there|Hi|Hello}, i read your blog {occasionally|from time to time} and i own a similar one and i was just {wondering|curious} if you get a lot of spam {comments|responses|feedback|remarks}? If so how do you {prevent|reduce|stop|protect against} it, any plugin or anything you can {advise|suggest|recommend}? I get so much lately it’s driving me {mad|insane|crazy} so any {assistance|help|support} is very much appreciated.
|
Greetings! {Very helpful|Very useful} advice {within this|in this particular} {article|post}!
{It is the|It’s the} little changes {that make|which will make|that produce|that will make} {the biggest|the largest|the greatest|the most important|the most significant} changes. {Thanks a lot|Thanks|Many thanks} for sharing!|
{I really|I truly|I seriously|I absolutely} love {your blog|your site|your website}.. {Very nice|Excellent|Pleasant|Great} colors & theme. Did you {create|develop|make|build} {this website|this site|this web site|this amazing site} yourself? Please reply back as I’m {looking to|trying
to|planning to|wanting to|hoping to|attempting to} create {my own|my very own|my own personal} {blog|website|site} and {would like to|want
to|would love to} {know|learn|find out} where you got
this from or {what the|exactly what the|just what the} theme {is called|is
named}. {Thanks|Many thanks|Thank you|Cheers|Appreciate
it|Kudos}!|
{Hi there|Hello there|Howdy}! This {post|article|blog post} {couldn’t|could not} be written {any better|much better}! {Reading through|Looking at|Going through|Looking through} this {post|article} reminds me of my previous roommate! He {always|constantly|continually} kept {talking about|preaching about} this. {I will|I’ll|I am going to|I most certainly will} {forward|send} {this article|this information|this post} to him.
{Pretty sure|Fairly certain} {he will|he’ll|he’s
going to} {have a good|have a very good|have a great} read.
{Thank you for|Thanks for|Many thanks for|I appreciate you for} sharing!
|
{Wow|Whoa|Incredible|Amazing}! This blog looks {exactly|just} like my old one!
It’s on a {completely|entirely|totally} different {topic|subject} but it has pretty much the same {layout|page layout} and design. {Excellent|Wonderful|Great|Outstanding|Superb} choice of colors!|
{There is|There’s} {definately|certainly} {a lot to|a great deal to} {know about|learn about|find
out about} this {subject|topic|issue}. {I like|I love|I really like} {all the|all
of the} points {you made|you’ve made|you have made}.|
{You made|You’ve made|You have made} some {decent|good|really
good} points there. I {looked|checked} {on the internet|on
the web|on the net} {for more info|for more information|to find out more|to learn more|for additional information} about the issue
and found {most individuals|most people} will go along with your views on
{this website|this site|this web site}.|
{Hi|Hello|Hi there|What’s up}, I {log on to|check|read} your {new stuff|blogs|blog} {regularly|like every week|daily|on a regular basis}. Your {story-telling|writing|humoristic} style is {awesome|witty}, keep {doing what you’re doing|up
the good work|it up}!|
I {simply|just} {could not|couldn’t} {leave|depart|go away} your {site|web site|website} {prior to|before} suggesting that I {really|extremely|actually} {enjoyed|loved} {the standard|the usual} {information|info} {a person|an individual} {supply|provide} {for your|on your|in your|to your} {visitors|guests}? Is {going to|gonna} be {back|again} {frequently|regularly|incessantly|steadily|ceaselessly|often|continuously} {in order to|to} {check up on|check out|inspect|investigate cross-check} new posts|
{I wanted|I needed|I want to|I need to} to thank you for this {great|excellent|fantastic|wonderful|good|very good} read!! I {definitely|certainly|absolutely} {enjoyed|loved} every {little bit of|bit of} it. {I have|I’ve got|I have got} you {bookmarked|book marked|book-marked|saved as
a favorite} {to check out|to look at} new {stuff you|things you}
post…|
{Hi|Hello|Hi there|What’s up}, just wanted to {mention|say|tell you}, I {enjoyed|liked|loved} this {article|post|blog post}. It was {inspiring|funny|practical|helpful}. Keep on posting!|
I {{leave|drop|{write|create}} a {comment|leave a response}|drop a {comment|leave a response}|{comment|leave a response}} {each time|when|whenever} I {appreciate|like|especially enjoy} a {post|article} on a {site|{blog|website}|site|website} or {I have|if I have} something to {add|contribute|valuable to contribute} {to the discussion|to the conversation}. {It is|Usually it is|Usually it’s|It’s} {a result of|triggered by|caused by} the {passion|fire|sincerness} {communicated|displayed} in the {post|article} I {read|looked at|browsed}. And {on|after} this {post|article} Bobobobo’s Weblog | technology and the internets.

I {{was|was actually} moved|{was|was actually} excited} enough to
{drop|{leave|drop|{write|create}}|post} a {thought|{comment|{comment|leave a response}a
response}} {:-P|:)|;)|;-)|:-)} I {do have|actually do have} {{some|a few} questions|a couple of questions|2 questions} for
you {if you {don’t|do not|usually do not|tend not to} mind|if it’s
{allright|okay}}. {Is it|Could it be} {just|only|simply} me or {do|does
it {seem|appear|give the impression|look|look as if|look
like} like} {some|a few} of {the|these} {comments|responses|remarks} {look|appear|come across} {like they are|as if
they are|like} {coming from|written by|left by} brain
dead {people|visitors|folks|individuals}? :-P
And, if you are {posting|writing} {on|at} {other|additional} {sites|social sites|online sites|online
social sites|places}, {I’d|I would} like to {follow|keep up with} {you|{anything|everything} {new|fresh} you have to post}. {Could|Would} you {list|make a list} {all|every one|the complete urls} of {your|all your} {social|communal|community|public|shared} {pages|sites} like your {twitter feed, Facebook page or linkedin profile|linkedin profile, Facebook page or twitter feed|Facebook page, twitter feed, or linkedin profile}?|
{Hi there|Hello}, I enjoy reading {all of|through} your {article|post|article post}. I {like|wanted} to write a little comment to support you.|
I {always|constantly|every time} spent my half an hour to read this {blog|weblog|webpage|website|web site}’s
{articles|posts|articles or reviews|content} {everyday|daily|every day|all the time} along with a {cup|mug} of coffee.
|
I {always|for all time|all the time|constantly|every time} emailed this {blog|weblog|webpage|website|web site}
post page to all my {friends|associates|contacts}, {because|since|as|for the reason that} if like to read it {then|after that|next|afterward} my {friends|links|contacts} will too.
|
My {coder|programmer|developer} is trying to {persuade|convince} me to
move to .net from PHP. I have always disliked the idea because of the {expenses|costs}.
But he’s tryiong none the less. I’ve been using {Movable-type|WordPress} on {a number of|a variety of|numerous|several|various} websites for about a year and am {nervous|anxious|worried|concerned} about switching to
another platform. I have heard {fantastic|very good|excellent|great|good} things about blogengine.
net. Is there a way I can {transfer|import} all my wordpress {content|posts}
into it? {Any kind of|Any} help would be {really|greatly} appreciated!
|
{Hello|Hi|Hello there|Hi there|Howdy|Good day}! I could
have sworn I’ve {been to|visited} {this blog|this web site|this website|this site|your blog} before but after {browsing through|going through|looking at} {some of the|a few of the|many of the} {posts|articles} I realized it’s new to me.
{Anyways|Anyhow|Nonetheless|Regardless}, I’m {definitely|certainly} {happy|pleased|delighted} {I found|I discovered|I came across|I stumbled upon} it and I’ll
be {bookmarking|book-marking} it and checking back {frequently|regularly|often}!
|
{Terrific|Great|Wonderful} {article|work}! {This is|That is} {the type of|the kind of} {information|info} {that are
meant to|that are supposed to|that should} be shared {around the|across the} {web|internet|net}.
{Disgrace|Shame} on {the {seek|search} engines|Google} for {now not|not|no longer} positioning this {post|submit|publish|put up} {upper|higher}!
Come on over and {talk over with|discuss with|seek advice from|visit|consult with} my {site|web site|website} .
{Thank you|Thanks} =)|
Heya {i’m|i am} for the first time here. I {came across|found} this board and I find It {truly|really} useful & it helped me out {a lot|much}. I hope to give something back and {help|aid} others like you {helped|aided} me.|
{Hi|Hello|Hi there|Hello there|Howdy|Greetings}, {I think|I believe|I do believe|I do think|There’s no
doubt that} {your site|your website|your web site|your
blog} {might be|may be|could be|could possibly be} having {browser|internet browser|web
browser} compatibility {issues|problems}. {When I|Whenever I}
{look at your|take a look at your} {website|web site|site|blog} in
Safari, it looks fine {but when|however when|however, if|however,
when} opening in {Internet Explorer|IE|I.E.}, {it has|it’s got} some overlapping issues. {I just|I simply|I merely} wanted to {give you a|provide you with a} quick heads up! {Other than that|Apart from that|Besides that|Aside from that}, {fantastic|wonderful|great|excellent} {blog|website|site}!|
{A person|Someone|Somebody} {necessarily|essentially} {lend a hand|help|assist} to make {seriously|critically|significantly|severely} {articles|posts} {I would|I might|I’d} state.
{This is|That is} the {first|very first} time I frequented your {web page|website page} and
{to this point|so far|thus far|up to now}? I {amazed|surprised} with the {research|analysis} you made to {create|make} {this
actual|this particular} {post|submit|publish|put up} {incredible|amazing|extraordinary}.
{Great|Wonderful|Fantastic|Magnificent|Excellent} {task|process|activity|job}!
|
Heya {i’m|i am} for {the primary|the first} time here. I {came across|found} this board and I {in finding|find|to find} It {truly|really} {useful|helpful} & it helped me out {a lot|much}. {I am hoping|I hope|I’m hoping} {to give|to
offer|to provide|to present} {something|one thing} {back|again} and {help|aid} others {like you|such as you} {helped|aided} me.
|
{Hello|Hi|Hello there|Hi there|Howdy|Good day|Hey there}!
{I just|I simply} {would like to|want to|wish to} {give you a|offer you a} {huge|big} thumbs up {for the|for
your} {great|excellent} {info|information} {you have|you’ve got|you have got} {here|right here} on this post. {I will be|I’ll be|I am} {coming back to|returning to} {your blog|your site|your website|your web site} for more soon.
|
I {always|all the time|every time} used to {read|study} {article|post|piece of writing|paragraph}
in news papers but now as I am a user of {internet|web|net} {so|thus|therefore} from
now I am using net for {articles|posts|articles or reviews|content}, thanks to web.
|
Your {way|method|means|mode} of {describing|explaining|telling} {everything|all|the whole
thing} in this {article|post|piece of writing|paragraph} is
{really|actually|in fact|truly|genuinely} {nice|pleasant|good|fastidious}, {all|every
one} {can|be able to|be capable of} {easily|without difficulty|effortlessly|simply}
{understand|know|be aware of} it, Thanks a lot.
|
{Hi|Hello} there, {I found|I discovered} your {blog|website|web site|site} {by means of|via|by the use of|by way of} Google {at the same time as|whilst|even as|while} {searching for|looking for} a {similar|comparable|related} {topic|matter|subject}, your {site|web site|website} {got here|came} up, it {looks|appears|seems|seems to
be|appears to be like} {good|great}. {I have|I’ve} bookmarked it in my google bookmarks.
{Hello|Hi} there, {simply|just} {turned into|became|was|become|changed into} {aware of|alert to} your {blog|weblog} {thru|through|via} Google, {and found|and located} that {it is|it’s}
{really|truly} informative. {I’m|I am} {gonna|going to} {watch out|be careful} for brussels. {I will|I’ll} {appreciate|be grateful}
{if you|should you|when you|in the event you|in case
you|for those who|if you happen to} {continue|proceed} this {in future}.
{A lot of|Lots of|Many|Numerous} {other folks|folks|other people|people} {will be|shall be|might be|will probably be|can be|will likely
be} benefited {from your|out of your} writing.

Cheers!|
{I am|I’m} curious to find out what blog {system|platform} {you have been|you happen to be|you are|you’re} {working with|utilizing|using}?

I’m {experiencing|having} some {minor|small} security {problems|issues} with my latest {site|website|blog} and {I would|I’d} like to find something more {safe|risk-free|safeguarded|secure}.
Do you have any {solutions|suggestions|recommendations}?|
{I am|I’m} {extremely|really} impressed with your writing skills {and also|as well as} with the layout on your {blog|weblog}. Is this a paid theme or did you {customize|modify} it yourself? {Either way|Anyway} keep up the {nice|excellent} quality writing, {it’s|it is} rare
to see a {nice|great} blog like this one {these days|nowadays|today}.
|
{I am|I’m} {extremely|really} {inspired|impressed} {with your|together with your|along with your} writing {talents|skills|abilities} {and also|as {smartly|well|neatly} as} with the {layout|format|structure} {for your|on your|in your|to your} {blog|weblog}. {Is this|Is that this} a paid {subject|topic|subject matter|theme} or did you {customize|modify} it {yourself|your self}? {Either way|Anyway} {stay|keep} up the {nice|excellent} {quality|high quality} writing, {it’s|it is}
{rare|uncommon} {to peer|to see|to look} a {nice|great} {blog|weblog} like this
one {these days|nowadays|today}..|
{Hi|Hello}, Neat post. {There is|There’s} {a problem|an issue} {with your|together with your|along with your} {site|web site|website} in {internet|web} explorer, {may|might|could|would} {check|test} this? IE {still|nonetheless} is the {marketplace|market} {leader|chief} and {a large|a good|a big|a huge} {part of|section of|component to|portion of|component of|element of} {other folks|folks|other people|people} will {leave out|omit|miss|pass over} your {great|wonderful|fantastic|magnificent|excellent} writing {due to|because of} this problem.|
{I’m|I am} not sure where {you are|you’re} getting your {info|information}, but {good|great} topic. I needs to spend some time learning {more|much more} or understanding more. Thanks for {great|wonderful|fantastic|magnificent|excellent} {information|info} I was looking for this {information|info} for my mission.|
{Hi|Hello}, i think that i saw you visited my {blog|weblog|website|web site|site} {so|thus} i came to “return the favor”.{I am|I’m}
{trying to|attempting to} find things to {improve|enhance} my {website|site|web site}!
I suppose its ok to use {some of|a few of} your ideas!

\

GKMatch is weird. You don’t ever instantiate GKMatch directly. Instead you use GKMatchMaker as a type of “factory” to instantiate GKMatch objects.

There are basically 2 ways to get a GKMatch up and running.

1) BOOTY CALLS: I call out people I want to have sex with have a match with BY NAME.

2) RANDOM ENCOUNTER: All players initiated THEIR OWN random partner search, and GameCenter “hooked them up” with a random partner.

First we’ll cover booty calls, then random encounters.

1. Booty call

BOOTY CALL works as follows. ONE OF THE PLAYERS (the “horny dude”) INVITES ALL THE OTHER PLAYERS BY NAME. He can invite as many players as he wishes (mm hmm). ALL PLAYERS WHO WANT TO ACCEPT INVITES BY NAME __MUST__ HAVE REGISTERED TO RECEIVE “BOOTY CALLS” BY SETTING [GKMatchmaker sharedMatchmaker].inviteHandler. We’ll call those players “the booty”.

// ALL PLAYERS OFFERING UP THEIR BOOTIES FOR TAPPING MUST
// SET THEIR INVITEHANDLER:
[GKMatchmaker sharedMatchmaker].inviteHandler = ^( GKInvite *acceptedInvite, NSArray *playersToInvite )
{
  // THIS RUNS FOR PLAYER WHO GOT A BYNAME INVITE ONLY
  printf( "You got an invitation to join a match started by `%s`\n", [acceptedInvite.inviter UTF8String] );
  
  // ACCEPTS THE MATCH:
  [ [GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite completionHandler:^( GKMatch *theMatch, NSError *nsError ) {
    if( nsError ) {
      error( nsError, "matchForInvite:acceptedInvite" ) ; // prints the error (using my `error` function)
      return ;
    }

    // KEEP A COPY OF THE GKMATCH OBJECT.
    Game->match = theMatch ; /////////////// YOU MUST DO THIS.
    // IF YOU DON'T "RETAIN" THE MATCH OBJECT THEN IT GOES AWAY AND THE MATCH NEVER STARTS.
      
    // ATTACH YOUR GKMatchDelegate.  __YOU MUST DO THIS__
    // otherwise you have no chance in hell of making a GKMatch work.
    // I instantiated myGameMatchDelegate OUTSIDE this function.
    // instantiating it here gives an ARC error. 
    theMatch.delegate = myGameMatchDelegate ; // myGameMatchDelegate implements GKMatchDelegate protocol

    // Now after this, your GKMatchDelegate match:player:didChangeState: event will trigger
    // automatically.  You can continue to do network setup there.  From that point forward
    // the machines are "having sex" (network connected).
 } ] ;
} ;

The “horny dude” who initiates the booty call does so like this:

GKMatchRequest *request = [[GKMatchRequest alloc] init] ; 
request.minPlayers = 2 ; // INCLUDES YOU.  "The minimum number of players must be at least 2."
request.maxPlayers = 2 ;
request.defaultNumberOfPlayers = 2 ;
request.playerAttributes = 0 ; // NO SPECIAL ATTRIBS
request.playerGroup = 0 ;

// Only for when you have another user to play with BYNAME.
request.playersToInvite = [[NSArray alloc] initWithObjects:@"G:1928347891235", nil] ; //G:1928347891235 is
// the ID of the player I want to booty call.

// A block to be called when a response from an invited BY NAME player is returned to your game.
request.inviteeResponseHandler = ^( NSString *playerID, GKInviteeResponse response )
{
  printf( "\n*** `%s` RESPONDED TO YOUR INVITE WITH `%s`\n", 
  [playerID UTF8String], GKInviteeResponseName[response] ) ;
} ;

request.inviteMessage = @"COME PLAY WITH ME" ; // This appears in a game center notification notice.
  
// This is like a GKMatch constructor for the caller.
// It usually completes almost IMMEDIATELY.
[ [GKMatchmaker sharedMatchmaker] findMatchForRequest:request
   withCompletionHandler:^( GKMatch *newMatch, NSError *nsError )
  {
    // THIS RUNS FOR THE INVITER IMMEDIATELY AFTER SENDING ANY findMatchForRequest
    if( nsError ) {
      error( nsError, "[MM findMatchForRequest] failed" ) ;
      return ;
    }
      
    // This is the chance to finish setting up the GKMatch object.
    newMatch.delegate = myGameDelegate ; // YOU HAVE TO SET THE DELEGATE.
    Game->match = newMatch ; // YOU MUST DO THIS.
    // IF YOU DON'T RETAIN THE MATCH OBJECT THEN IT GOES AWAY AND THE MATCH NEVER STARTS.
  }
] ;

So after the booty call goes out, OTHER PLAYERS RECEIVE THE INVITATIONS AND ACCEPT IT (Note: requires VIA GC OVERLAY).

2. RANDOM ENCOUNTERS

BOTH PLAYERS ARE LOOKING FOR “RANDOM ENCOUNTERS”. They put on makeup/shave as appropriate. They then EACH INVOKE findMatchForRequest, BUT WITHOUT SPECIFYING OTHER PLAYER NAMES TO HOOK UP WITH. In other words, they’re looking to hook up with just anybody. GAMECENTER WILL “HOOK THEM UP” WITH RANDOM PARTNERS (TO HAVE SEX WITH).

The code that both players call then, to say to GameCenter they are DTF with just about anyone, is very similar to the code used “make a booty call” above. You just don’t fill out the playerIDs field (leave it nil).

GKMatchRequest *request = [[GKMatchRequest alloc] init] ; 
request.minPlayers = 2 ; // INCLUDES YOU.  "The minimum number of players must be at least 2."
request.maxPlayers = 2 ;
request.defaultNumberOfPlayers = 2 ;
request.playerAttributes = 0 ; // NO SPECIAL ATTRIBS
request.playerGroup = 0 ;

request.playersToInvite = nil ; // NIL THAT
  
// This is like a GKMatch constructor for the caller.
// It completes IMMEDIATELY.
[ [GKMatchmaker sharedMatchmaker] findMatchForRequest:request
   withCompletionHandler:^( GKMatch *newMatch, NSError *nsError )
  {
    // THIS RUNS FOR THE INVITER IMMEDIATELY AFTER SENDING ANY findMatchForRequest
    if( nsError ) {
      error( nsError, "[MM findMatchForRequest] failed" ) ;
      return ;
    }
      
    // This is the chance to finish setting up the GKMatch object.
    newMatch.delegate = myGameDelegate ; // YOU HAVE TO SET THE DELEGATE.
    Game->match = newMatch ; // YOU MUST DO THIS.
    // IF YOU DON'T RETAIN THE MATCH OBJECT THEN IT GOES AWAY AND THE MATCH NEVER STARTS.
  }
] ;

I don’t have any real technical reason for hating exceptions. I simply hate exceptions in a stylistic sense.

Exceptions are a retroactive way of handling error conditions. They kind of say “Yeah try this, if that doesn’t work, then do that.”

try
{
  to parse the value out of the string
}
catch( ValueParsingException ex )
{
  not a valid value..?
}

Seems like a manager with a bad attitude. Like “Yeah try it, whatever, and if that doesn’t work, then do this other thing”. Sure, technically it’s very clear what the code is doing. But to me, (mostly because I’ve seen too many 200 line try/catch blocks), try/catch will always be a code structure very readily abused.

I generally hate people that say they will do one thing, then partway through they say “No, go back” and start doing something else. But that is exactly the language of a try/catch block.

Exceptions are like walking across the street without looking:

try
{
  to cross the street
}
catch( BusHitException busException )
{
  go to hospital?
}

I much prefer a series of rigorous if statements to try/catch blocks where possible. I’d much rather have code like this:

if( the string is valid )
{
  parse the value out of the string
}
else
{
  not a valid value.
}

So in other words, look before crossing:

if( there are no cars )
{
  cross the street
}
else
{
  wait
}

That would be more like a while loop, but you get the idea.

If statements say “I’m watching what is going on, this code knows what its doing.”

Sure, you can achieve the exact same ends with try/catch as with if/else. But try/catch all too often is used as sloppy code. I hate when exceptions are used in the generic “If something goes wrong then just throw that general error “Uh there was an exception” and printstacktrace.”

Have you ever seen one of those 200 line try blocks? Disgusting. People use them because they don’t know/they don’t care what is going wrong.

Try/catch can too easily be abused. That’s why I hate it as a programming construct.

Funky Inheritance

Or functor-based inheritance

Funky inheritance is my name for when you allow attachment of functors to specify behavior instead of always just subclassing.

This came up recently in some UI event handling code that I was writing. Buttons come and go, dialogs appear and get dismissed. Each dialog dismissal button (okButton) needs something specific to happen, but that same functionality is extremely unlikely to be reused in another dialog.

Normally, to write an object type that has customizable behavior (by a subclass), you would write a base class like this:

class Dialog
{
  virtual void onDismiss() = 0 ;
};

So, when you subclass Dialog, you override onDismiss to provide custom behavior that occurs when the dialog is dismissed (assume onDismiss would be invoked by a window manager).

class FileOpenDialog : public Dialog
{
  virtual void onDismiss()
  {
    // stuff to do when the FileOpenDialog is dismissed..
  }
};

So, that’s the regular (classical) inheritance pattern. Every time you want different functionality onDismiss(), you’d write a new subclass of Dialog. Another way to get custom code to run onDismiss is to allow onDismiss() to accept a functor that can be invoked …

class FileOpenDialog : public Dialog
{
  virtual void onDismiss( function <void ()> runThisAfter )
  {
    // stuff to do when the FileOpenDialog is dismissed..
    // then..
    runThisAfter() ;
  }
};

OR

You could write onDismiss as a functor in base class Dialog instead of using classical inheritance here:

class Dialog
{
  function< void (Dialog* that) > onDismiss ;
};

Now onDismiss is a functor (that may or may not be set).

So then customized behavior is by instance, not by class. Creating a FileOpenDialog object would then be like:

Dialog fileOpenDialog ;
fileOpenDialog.onDismiss = [](Dialog* that) {
  // Do something with `that`, which acts as `this`
  // if this were a member function ;)
} ;

When the window object handler finds out a dialog has been dismissed, it invokes the onDismiss handler if it has been set.

// It is detected that fileOpenDialog needs to be closed:
if( fileOpenDialog.onDismiss )
  fileOpenDialog.onDismiss( fileOpenDialog ) ;

// Above line manually passes `this`.
// If I am not mistaken, this is considered "Pythonic" (just kidding,
// but Python does require passing of self in a similarly visible fashion)

This may come across to you as very JavaScripty- or follow a prototypal inheritance pattern. But it actually isn’t prototypal inheritance, because objects do not inherit from other objects. Each instance of Dialog has its own behavior, that is not repeated by any other instance of Dialog.

Of course, this would need some more detail and fleshing out to really work. But this is the jist of funky inheritance. Regular inheritance is class-wide customization. Funky inheritance is instance-wide customization. Funky inheritance is great for when you need to define custom behavior (often one-off, for a button or a widget) without having to define an entire subclass.

Drawbacks of Funky Inheritance

Functors are unable to persist their own internal running state except as static variables. However statics inside a functor are inaccessible from outside the functor. This means modification of a functor’s internal values is difficult/impossible from outside the functor. So, that kind of sucks sometimes.

One way you can modify a functor’s internal state variables is to capture a pointer before entering the functor:

Object * someObject = new Object() ;
functor = [ someObject ]() {
  // now if someObject is modified outside the functor,
  // I will use "the most up to date" version of someObject.
} ;

Functors seem to be best used to send global state messages or somehow modify global state. For example, if a FileOpenDialog is modal, it may flip off the `modal` flag inside your window manager when it is dismissed. Functors are also useful to modify the object running the functor’s state as well.

GameCenter isn’t easy to work with. For one it’s networking, and networking errors are hard to debug because there’s no natural UI. debug print messages is all you’ve got. It doesn’t help that GC is none too detailed on what you did wrong, and sometimes weird errors can occur if you use the API incorrectly.

1. A match request by name can take up to 2 minutes to reach the remote device. Unprepared for this I’d just sit there trying the request again and again, wondering why it wasn’t working.

2. The iOS simulator doesn’t seem to work for GKMatchmaker requests. I don’t know why this is, but the iOS simulator cannot even startBrowsingForNearbyPlayersWithReachableHandler.

3.

I created an xml file with my newest XCode C++ theme.

It looks like this:

So, gray background, white text, all numeric constants and defines are kinda blue, classes are purple, and C preprocessor macros are red.

The factory pattern is most useful when an object type cannot and should not be used independently of some master managing class.

An analogy would be if you have a Theater as the "Master class" (factory) and the objects that are created in the factory are Tickets. So you have to ask the Master Theater class for a new Ticket() whenever you want one. The Master class can now remember that it has sold you a Ticket and that there is one additional Ticket in the system. Tickets aren’t useful at all if the ticket issuer disappeared. Notice this is much better than having a system where Tickets are created with no reference to a Theater, but then are registered to a theater after creation. That would be like "counterfeit your ticket, then call us and tell us you have a ticket to our Theater".

And so the factory pattern makes perfect sense here.

Objects make the most sense in the context of encapsulating code that:

  • Has some mandatory startup that must run (constructors), otherwise the related functions won’t work
  • Has a family of related functions, but not all of them should be called by the class user (private)
  • Has some cleanup routines that should be called when the user is done (destructor)

An example of this is my SecKeyHelper.h file I wrote today. That file is just C functions, but there is some code that must be called before any of the related functions work. On top of that, only 2 functions should really be called by the class user.

Public key loading, private key loading, it’s all very easy with OpenSSL on Linux. You must link the libcrypto.dylib library in XCode for this to work.

XCode 4.6 project download here

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/pem.h>
 
// I'm not using BIO for base64 encoding/decoding.  It is difficult to use.
// Using superwills' Nibble And A Half instead 
// https://github.com/superwills/NibbleAndAHalf/blob/master/NibbleAndAHalf/base64.h
#include "base64.h"
 
// The PADDING parameter means RSA will pad your data for you
// if it is not exactly the right size
//#define PADDING RSA_PKCS1_OAEP_PADDING
#define PADDING RSA_PKCS1_PADDING
//#define PADDING RSA_NO_PADDING
 
RSA* loadPUBLICKeyFromString( const char* publicKeyStr )
{
  // A BIO is an I/O abstraction (Byte I/O?)
  
  // BIO_new_mem_buf: Create a read-only bio buf with data
  // in string passed. -1 means string is null terminated,
  // so BIO_new_mem_buf can find the dataLen itself.
  // Since BIO_new_mem_buf will be READ ONLY, it's fine that publicKeyStr is const.
  BIO* bio = BIO_new_mem_buf( (void*)publicKeyStr, -1 ) ; // -1: assume string is null terminated
  
  BIO_set_flags( bio, BIO_FLAGS_BASE64_NO_NL ) ; // NO NL
  
  // Load the RSA key from the BIO
  RSA* rsaPubKey = PEM_read_bio_RSA_PUBKEY( bio, NULL, NULL, NULL ) ;
  if( !rsaPubKey )
    printf( "ERROR: Could not load PUBLIC KEY!  PEM_read_bio_RSA_PUBKEY FAILED: %s\n", ERR_error_string( ERR_get_error(), NULL ) ) ;
  
  BIO_free( bio ) ;
  return rsaPubKey ;
}
 
RSA* loadPRIVATEKeyFromString( const char* privateKeyStr )
{
  BIO *bio = BIO_new_mem_buf( (void*)privateKeyStr, -1 );
  //BIO_set_flags( bio, BIO_FLAGS_BASE64_NO_NL ) ; // NO NL
  RSA* rsaPrivKey = PEM_read_bio_RSAPrivateKey( bio, NULL, NULL, NULL ) ;
  
  if ( !rsaPrivKey )
    printf("ERROR: Could not load PRIVATE KEY!  PEM_read_bio_RSAPrivateKey FAILED: %s\n", ERR_error_string(ERR_get_error(), NULL));
  
  BIO_free( bio ) ;
  return rsaPrivKey ;
}
 
unsigned char* rsaEncrypt( RSA *pubKey, const unsigned char* str, int dataSize, int *resultLen )
{
  int rsaLen = RSA_size( pubKey ) ;
  unsigned char* ed = (unsigned char*)malloc( rsaLen ) ;
  
  // RSA_public_encrypt() returns the size of the encrypted data
  // (i.e., RSA_size(rsa)). RSA_private_decrypt() 
  // returns the size of the recovered plaintext.
  *resultLen = RSA_public_encrypt( dataSize, (const unsigned char*)str, ed, pubKey, PADDING ) ; 
  if( *resultLen == -1 )
    printf("ERROR: RSA_public_encrypt: %s\n", ERR_error_string(ERR_get_error(), NULL));
 
  return ed ;
}
 
unsigned char* rsaDecrypt( RSA *privKey, const unsigned char* encryptedData, int *resultLen )
{
  int rsaLen = RSA_size( privKey ) ; // That's how many bytes the decrypted data would be
  
  unsigned char *decryptedBin = (unsigned char*)malloc( rsaLen ) ;
  *resultLen = RSA_private_decrypt( RSA_size(privKey), encryptedData, decryptedBin, privKey, PADDING ) ;
  if( *resultLen == -1 )
    printf( "ERROR: RSA_private_decrypt: %s\n", ERR_error_string(ERR_get_error(), NULL) ) ;
    
  return decryptedBin ;
}
 
unsigned char* makeAlphaString( int dataSize )
{
  unsigned char* s = (unsigned char*) malloc( dataSize ) ;
  
  int i;
  for( i = 0 ; i < dataSize ; i++ )
    s[i] = 65 + i ;
  s[i-1]=0;//NULL TERMINATOR ;)
  
  return s ;
}
 
// You may need to encrypt several blocks of binary data (each has a maximum size
// limited by pubKey).  You shoudn't try to encrypt more than
// RSA_LEN( pubKey ) bytes into some packet.
// returns base64( rsa encrypt( <<binary data>> ) )
// base64OfRsaEncrypted()
// base64StringOfRSAEncrypted
// rsaEncryptThenBase64
char* rsaEncryptThenBase64( RSA *pubKey, unsigned char* binaryData, int binaryDataLen, int *outLen )
{
  int encryptedDataLen ;
  
  // RSA encryption with public key
  unsigned char* encrypted = rsaEncrypt( pubKey, binaryData, binaryDataLen, &encryptedDataLen ) ;
  
  // To base 64
  int asciiBase64EncLen ;
  char* asciiBase64Enc = base64( encrypted, encryptedDataLen, &asciiBase64EncLen ) ;
  
  // Destroy the encrypted data (we are using the base64 version of it)
  free( encrypted ) ;
  
  // Return the base64 version of the encrypted data
  return asciiBase64Enc ;
}
 
// rsaDecryptOfUnbase64()
// rsaDecryptBase64String()
// unbase64ThenRSADecrypt()
// rsaDecryptThisBase64()
unsigned char* rsaDecryptThisBase64( RSA *privKey, char* base64String, int *outLen )
{
  int encBinLen ;
  unsigned char* encBin = unbase64( base64String, (int)strlen( base64String ), &encBinLen ) ;
  
  // rsaDecrypt assumes length of encBin based on privKey
  unsigned char *decryptedBin = rsaDecrypt( privKey, encBin, outLen ) ;
  free( encBin ) ;
  
  return decryptedBin ;
}
  
 
 
int main( int argc, const char* argv[] )
{
  ERR_load_crypto_strings();  
  
  puts( "We are going to: rsa_decrypt( unbase64( base64( rsa_encrypt( <<binary data>> ) ) ) )" );
  // public key
  // http://srdevspot.blogspot.ca/2011/08/openssl-error0906d064pem.html
  //1. The file must contain:
  //-----BEGIN CERTIFICATE-----
  //on a separate line (i.e. it must be terminated with a newline).
  //2. Each line of "gibberish" must be 64 characters wide.
  //3. The file must end with:
  //-----END CERTIFICATE-----
  // YOUR PUBLIC KEY MUST CONTAIN NEWLINES.  If it doesn't (ie if you generated it with
  // something like
  // ssh-keygen -t rsa -C "you@example.com"
  // ) THEN YOU MUST INSERT NEWLINES EVERY 64 CHRS (just line it up with how I have it here
  // or with how the ssh-keygen private key is formatted by default)
  const char *b64_pKey = "-----BEGIN PUBLIC KEY-----\n"
  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp2w+8HUdECo8V5yuKYrWJmUbL\n"
  "tD6nSyVifN543axXvNSFzQfWNOGVkMsCo6W4hpl5eHv1p9Hqdcf/ZYQDWCK726u6\n"
  "hsZA81AblAOOXKaUaxvFC+ZKRJf+MtUGnv0v7CrGoblm1mMC/OQI1JfSsYi68Epn\n"
  "aOLepTZw+GLTnusQgwIDAQAB\n"
  "-----END PUBLIC KEY-----\n";
  
  // private key
  const char *b64priv_key = "-----BEGIN RSA PRIVATE KEY-----\n"
  "MIICXAIBAAKBgQCp2w+8HUdECo8V5yuKYrWJmUbLtD6nSyVifN543axXvNSFzQfW\n"
  "NOGVkMsCo6W4hpl5eHv1p9Hqdcf/ZYQDWCK726u6hsZA81AblAOOXKaUaxvFC+ZK\n"
  "RJf+MtUGnv0v7CrGoblm1mMC/OQI1JfSsYi68EpnaOLepTZw+GLTnusQgwIDAQAB\n"
  "AoGBAKDuq3PikblH/9YS11AgwjwC++7ZcltzeZJdGTSPY1El2n6Dip9ML0hUjeSM\n"
  "ROIWtac/nsNcJCnvOnUjK/c3NIAaGJcfRPiH/S0Ga6ROiDfFj2UXAmk/v4wRRUzr\n"
  "5lsA0jgEt5qcq2Xr/JPQVGB4wUgL/yQK0dDhW0EdrJ707e3BAkEA1aIHbmcVfCP8\n"
  "Y/uWuK0lvWxrIWfR5MlHhI8tD9lvkot2kyXiV+jB6/gktwk1QaFsy7dCXn7w03+k\n"
  "xrjEGGN+kQJBAMuKf55lDtU9K2Js3YSStTZAXP+Hz7XpoLxmbWFyGvBx806WjgAD\n"
  "624irwS+0tBxkERbRcisfb2cXmAx8earT9MCQDZuVCpjBWxd1t66qYpgQ29iAmG+\n"
  "jBIY3qn9uOOC6RSTiCCx1FvFqDMxRFmGdRVFxeyZwsVE3qNksF0Zko0MPKECQCEe\n"
  "oDV97DP2iCCz5je0R5hUUM2jo8DOC0GcyR+aGZgWcqjPBrwp5x08t43mHxeb4wW8\n"
  "dFZ6+trnntO4TMxkA9ECQB+yCPgO1zisJWYuD46KISoesYhwHe5C1BQElQgi9bio\n"
  "U39fFo88w1pok23a2CZBEXguSvCvexeB68OggdDXvy0=\n"
  "-----END RSA PRIVATE KEY-----\n";
 
  // String to encrypt, INCLUDING NULL TERMINATOR:
  int dataSize=37 ; // 128 for NO PADDING, __ANY SIZE UNDER 128 B__ for RSA_PKCS1_PADDING
  unsigned char *str = makeAlphaString( dataSize ) ;
  printf( "\nThe original data is:\n%s\n\n", (char*)str ) ;
  
  // LOAD PUBLIC KEY
  RSA *pubKey = loadPUBLICKeyFromString( b64_pKey ) ;
  
  int asciiB64ELen ;
  char* asciiB64E = rsaEncryptThenBase64( pubKey, str, dataSize, &asciiB64ELen ) ;
   
  RSA_free( pubKey ) ; // free the public key when you are done all your encryption
  
  printf( "Sending base64_encoded ( rsa_encrypted ( <<binary data>> ) ):\n%s\n", asciiB64E ) ;
  puts( "<<----------------  SENDING DATA ACROSS INTERWEBS  ---------------->>" ) ;
  
  char* rxOverHTTP = asciiB64E ; // Simulate Internet connection by a pointer reference
  printf( "\nRECEIVED some base64 string:\n%s\n", rxOverHTTP ) ;
  puts( "\n * * * What could it be?" ) ;
  
  // Now decrypt this very string with the private key
  RSA *privKey = loadPRIVATEKeyFromString( b64priv_key ) ;
  
  // Now we got the data at the server.  Time to decrypt it.
  int rBinLen ;
  unsigned char* rBin = rsaDecryptThisBase64( privKey, rxOverHTTP, &rBinLen ) ;
  printf("Decrypted %d bytes, the recovered data is:\n%.*s\n\n", rBinLen, rBinLen, rBin ) ; // rBin is not necessarily NULL
  // terminated, so we only print rBinLen chrs
  
  RSA_free(privKey) ;
  
  bool allEq = true ;
  for( int i = 0 ; i < dataSize ; i++ )
    allEq &= (str[i] == rBin[i]) ;
  
  if( allEq ) puts( "DATA TRANSFERRED INTACT!" ) ;
  else puts( "ERROR, recovered binary does not match sent binary" ) ;
  free( str ) ; 
  free( asciiB64E ) ; // rxOverHTTP  
  free( rBin ) ;
  ERR_free_strings();
}

Rae Johnston posts about an experience in a Starbucks’ involving a Bioshock Infinite t-shirt.

I am not sticking up for that guy. He sounds like a loser. But I want to explain what I think is behind that behavior.

First of all reading her top tweets you can see she gets a lot of flack for being a girl gamer. That brings us to the first point:

Any guys that give girl gamers flack for being a girl are sexist guys period. They’re losers. The girl is on “their turf” so they subject her to bad treatment. Only losers do that and there’s no excuse for it.

Second is the encounter with that guy that got almost 10k retweets. The thing there is, there is already a bunch of internet memes about gamer girls who pretend to like games, only to get guys’ attention.

Also you have to keep in mind the shock value of hot girl gamers. Although this might not be true nerdy guys immediately think they have a shot at the nerdy hot girl. Because! Finally! A hot chick who digs the things I do. We’re on the same page! Blah blah blah.

He did a stupid thing calling you out rudely for the shirt. He was a moron. But these are just thoughts on what is going through nerds’ heads.

I can never find this when I’m looking for it, so I dedicated a whole blog post to it.

Here.

The easiest way I’ve found to generate a random alphanumeric string involves RAND(), SHA(), and TO_BASE64()

select LEFT( TO_BASE64( SHA(rand()) ), 6 ) ;

You need MySQL 5.6 to execute.

Alternatively some MYSQL Procedures magic

DELIMITER //

drop function if exists randChr //
create function randChr()
returns char
BEGIN
  IF RAND() <= 0.5 THEN -- Lowercase
	return CHAR( 97 + 25*rand() ) ;
  ELSE -- uc
    return CHAR( 65 + 25*rand() ) ;
  END IF;
END //

drop function if exists randString //
create function randString( len int )
returns varchar(255)
BEGIN
  SET @n = 0;
  SET @res = '' ;
  REPEAT
    SET @res = concat( @res, randChr() ) ;
    set @n = @n + 1 ;
  UNTIL @n >= len END REPEAT;
  return @res ;
END //

DELIMITER ;

select randString( 5 );

Starting from my previous program (Windows), connecting in Mac OS X is even EASIER, only there are a couple’a gotchas.

To code with MYSQL in Mac OS X, just install MySQL normally. In fact it comes with the `/include` and `/lib` folders when you installed it. They are in `/usr/local/mysql` (which is a link to the specific version you have).

Ok, now that’s done, here’s your C++ program:

#include <stdio.h>
#include <stdlib.h>
#include "/usr/local/mysql/include/mysql.h"

MYSQL mysql;    // MYSQL global
MYSQL * conn ;  // represents connection to database

int main()
{
  mysql_init( &mysql ) ;
  
  //   http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html
  conn = mysql_real_connect(  &mysql,
                              "localhost",// synonymous with 127.0.0.1
                              "root",     // connect as user="root".  Uh, bad security here..
                              "",         // my root password is blank.  REALLY bad security :)
                              "mysql",    // connect to the 'mysql' _database_ within MySQL itself.
                               0, 0, 0 ) ;
  
  // Check if connection succeeded.
  if( !conn )
  {
    printf( "Couldn't connect to MySQL database server!\n" ) ;
    printf( "Error: %s\n", mysql_error( &mysql ) ) ;
    return 1 ;
  }
  //else  puts( "Connect success" ) ;
  
  // Here, we are connected.
  // form a sql string to execute.
  if( mysql_query( conn, "select * from user" ) )
  {
    printf("Whoops!  The query failed.  Error:  %s\n", mysql_error( conn ) );
    return 1 ;
  }

  // here, query succeeded, so we can proceed to pull out results.
  MYSQL_RES * resultset ;
  MYSQL_ROW row;  // MYSQL_ROW is #defined as (char **)
  // Data ALWAYS comes back from MySQL as
  // an array of strings.  To convert it
  // to ints or whatever is YOUR JOB as the programmer.

  // mysql_store_result basically fetches
  // the entire array of results and dumps them
  // into our local program memory space (all
  // in the resultset variable.
  resultset = mysql_store_result( conn );

  // How many rows will there be?
  my_ulonglong numRows = mysql_num_rows( resultset ) ;
  printf( "There are %llu ROWS (records) of data\n", numRows ) ;
  
  // Now tell me what columns there are
  // in the result set.
  int numFields = mysql_num_fields( resultset ) ;
  printf( "There are %d FIELDS (columns) of data\n", numFields ) ;

  // Print all those column by name
  MYSQL_FIELD * fields = mysql_fetch_fields( resultset ) ;
  for( int i = 0 ; i < numFields ; i++ )
  {
    printf( "%25s", fields[i].name ) ;
  }

  printf( "\n" ) ;
  // print all results
  while( (row = mysql_fetch_row( resultset )) )
  {
    // row is 2d array of char
    // underlying type is char **
    for ( int i = 0; i < numFields ; i++ )
      printf( "%5s", row[ i ] ) ;
    
    puts( "" ) ;    // next row
  }
  
  mysql_free_result( resultset );  // Now free the result
  mysql_close( conn ) ;  // And close down.
  return 0;
}


GOTCHA #1: An easy one, but you need to link up your TARGET with the libmysqlclient.a library file. So goto your TARGETS / BUILD PHASES tab, under “Link Binary With Libraries” go select “libmysqlclient.a” from /usr/local/mysql/lib.
GOTCHA #2: This is the stupid one, COPY /usr/local/mysql/lib/libmysqlclient.18.dylib to /usr/lib/. ISN’T THAT DUMB??? Well it’s the eq of copying a DLL to C:\Windows\System32 on Windows. Just something you have to do.

If you don’t copy the .dylib you’ll get Library not loaded: libmysqlclient.16.dylib reason: image not found.

The easiest.

1. Mac OS X

sudo apachectl start

Make sure cgi is enabled in your Apache settings

sudo vi /etc/apache2/httpd.conf

Create and build the following:

#include <stdio.h>

int main(int argc, const char * argv[])
{
  //puts( "HTTP/1.1 200 OK" ) ;
  puts( "Content-Type: text/plain; charset=UTF-8" ) ;
  puts( "Status: 200 OK" ) ;
  puts( "" ) ;
  puts( "HELLO INTERNET" ) ;
}

The default folder Xcode 4.6 dumps the exec in is

/Users/YOURUSERNAME/Library/Developer/Xcode/DerivedData/PROJECTNAME-andabunchofrandomcharacters/Build/Products/Debug/PROJECTNAME

Take the exec (called PROJECTNAME) and drop it into:

/Library/WebServer/CGI-Executables/

hit http://localhost/cgi-bin/YOUREXECNAME

It should say HELLO INTERNET.

You are on your way to C++ internet freedom

I simulated some game results. In the sim, Player 2 was so bad, he was 0 for 680. The ranking algorithm ranked him first though, because he sucked so bad, he kept on losing increasingly large quantities of negative points for each match he lost.

It is possible! This table shows it as a sort of nested table.

All you have to do to achieve glow in pixelmator:

1) Take an image

2) Duplicate the layer (Make sure View/Show Layers is on, then Right click/Duplicate)

3) View / Show Effects must be on. From the little Effects window, choose Gaussian Blur. Make it something like 10-15px of blur radius. Your duplicate layer should look like that:

4) Under Blending in your Layers dialog select LINEAR DODGE, then drag the opacity to get your desired level of glow. Make sure the blurred texture appears ON TOP of the original.

5) The result should look something like that

You could compute them using factorials, but this is far more efficient.

// Computes n CHOOSE k
// ( n )
// ( k )
int binomial( int n, int k )
{
  // must accumulate nums/dens separately
  // to avoid roundoff error
  int nums = 1;
  int dens = 1;
  for( int i = 1 ; i <= k ; i++ )
  {
    nums *= ( n - k + i ) ;
    dens *= i ;
  }
  return nums/dens ;
}

//test
int p=7;
for( int i = 0  ; i <= p ; i++ )
  printf( "%dx^%d +", binomial(p, i), p-i ) ;
puts("");

Delegate: A person sent or authorized to represent others
Delegation

First, DELEGATE is problematic. It’s not a common word, and most people don’t know what it means, and beyond that it’s NOT CLEAR what it means in a software context. It’s a bad metaphor.

The use of the word DELEGATE is different than in C#. A delegate in C# is just a function pointer. A DELEGATE in Objective-C is an OBJECT that implements a bunch of functions (very similar to overriding methods in inheritance), BUT THESE FUNCTIONS AREN’T OVERRIDING FUNCTIONS IN A BASE CLASS. Instead, they are implementing A “PROTOCOL” (not a C++ programming concept, its an objective-c concept). OC PROTOCOLS are like C++ INTERFACES (abstract base classes), only weirder. Delegate classes contain just a bunch of stubs of functionality that would get called by the default Apple UIApplication object, if that object respondsToSelector, for example.

Some Foundation Framework classes are like big assholes, like UIApplication for example. They go about their business in a VERY routine way, and WHEN THEY FEEL LIKE IT, they “delegate” little tasks out to YOUR DELEGATE OBJECT. When you program with Foundation classes, you provide that itty-bitty, gopher-boy, go-do-what-I-said, shoe-shine, lackey-boy, I-have-no-money-please-help-me-sir object (called a DELEGATE). The big king UIApplication Foundation class WILL CALL YOUR DELEGATE WHEN THE TIME COMES.

So your little shitty delegate just implements willDoStep3 here, and arguments may be passed by Framework object. UIApplication has a bunch of points at which it “needs” a delegate to call: applicationDidFinishLaunching, applicationWillResignActive, etc.

So app delegation is like fake inheritance. In normal inheritance, you would OVERRIDE applicationDidFinishLaunching, and provide your own method stub. In Objective C, you “provide a delegate object that implements a protocol”. It is conceptually almost the same thing.

I don’t understand why Apple decided to invent this new language and concepts of protocols and delegates, when C++’s concepts of interfaces and inheritance do just fine.


As an example of implementing a delegate protocol, let’s implement GKMatchDelegate. All the information you need is in the documentation there.

// SFMatchDelegate : inherits from NSObject< Implements GKMatchDelegate protocol >
@interface SFMatchDelegate : NSObject< GKMatchDelegate >
{
}

// any @property here

@end



// Usually in another file:
@implementation SFMatchDelegate

//any @synthesize statements

// "override"/implement protocol "Tasks"

//Receiving Data from Other Players
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
}

//Receiving State Notifications About Other Players
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state
{
}

//Handling Errors
- (void)match:(GKMatch *)match didFailWithError:(NSError *)error
{
}

//Reinviting a Player
- (BOOL)match:(GKMatch *)match shouldReinvitePlayer:(NSString *)playerID
{
  return YES ;
}

@end

They play a game of odds.  “If we sell lots of shitty product at a cheap price, with 1/5 dead, then we make money b/c 1/5 will buy the dead product and customers won’t want to pay $15 to return a $20 item.”

 

 

Follow

Get every new post delivered to your Inbox.

Join 37 other followers