Thursday, July 7, 2011

Writing Objective-C Code on Windows

This is an ultra-compact guide to installing and testing the tools necessary to run Objective-C code on the Windows platform, based on my recent experience.

1.)  Navigate to http://www.gnustep.org/experience/Windows.html


2.) Install these packages in this order:
  





3.) Create a new Objective-C code file named hello.m in your home directory. You can use your favorite text editor for this. You will save this in your home directory, located inside your GNUstep installation location. This version of GNUstep places your home folder here (assuming default C:\ installation path) : 


C:\GNUstep\mysys\1.0\home\


4.) Open up your GNUstep shell prompt via Start -> All Programs -> GNUstep -> Shell. You should now be at the following screen. The default location that this screen is pointing to is the home folder specified below. Try using the command ' mkdir testdirectory ' and seeing if it shows up in your home directory. It should.




5.) Minimize your shell window and copy this test code into your hello.m file ( I use Notepad++):

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

        NSLog (@"hello world");
        [pool drain];
        return 0;
}


6.) Okay. Let's recap. At this point we have GNUstep installed, which has everything we need to use our shell environment and compile our code. We found the home directory that we will use to store our code files, and we have added a complete helloworld application file there - hello.m. Next we will compile this code into a runnable .exe file.


7.) Go back to your shell window and run the following compile command:

gcc `gnustep-config --objc-flags` -o hello hello.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base


8.) If everything went smoothly, you should get no output. You can double check your home folder to see if the .exe file now exists. If it does great! If it doesn't - not so great. Check out the tutorials below for more information. I'll try my best to answer any comments on the subject as well.


9.) Run your new exe by typing the command ' ./hello '.

Shell Commands


10.) That's it! If you see that output then you are in business. Everything is working as intended. Check out these other tutorials on the subject if you get stuck. Remember Google is your friend and I'll do my best to answer any direct questions. Good luck!


Thursday, June 9, 2011

Connecting to Active Directory with PHP through LDAP

Recently I had to grab some Active Directory data with PHP. A few quick searches led me to LDAP (Lightweight Access Directory Protocol). Note that LDAP is not a language or a framework, but a protocol that is used to communicate with directory servers (Microsoft's Active Directory in this case). There are plenty of great resources for learning about LDAP and directory servers, so I won't go into that. I just wanted to share my implementation of a PHP connection class that I hobbled together from various sources. I say hobbled because it is surprisingly difficult to find thorough PHP + LDAP + Active Directory samples.

<?php
 $domain = "YOURDOMAIN.local";
 $url = "YOURDOMAIN.local";
 $dn = "dc=YOURDOMAN,dc=local";
 
 //Connect to AD - must be bound before anything else can happen
 $rs = ldap_connect($url);
 
 //Set these because everyone says to
 ldap_set_option($rs, LDAP_OPT_PROTOCOL_VERSION, 3);
 ldap_set_option($rs, LDAP_OPT_REFERRALS, 0);
 
 $userId = "USERNAME";
 $password = "PASSWORD";
 
 //Attempt to login
 //one connection to rule them all, and in the code, bind them.
 $boundLogin = ldap_bind($rs, "$userId@$domain", $password);
 
 //Accounts must have a phone number, be of the object category 'person', not have an expiration date set. The givenname=Misc
 //clause is there because entries like "Teller Cordless" do not have an expiration date to check. All items like that in active directory
 //must have the first name of "Misc".
 $filter = "(&(objectCategory=person)(telephoneNumber=*)(|(accountExpires=9223372036854775807)(accountExpires=0)(givenname=Misc)))";
 $attributes = array("displayname", "mail", "givenName", "sn", "cn", "telephoneNumber", "department", "PhysicalDeliveryOfficeName", "directReports");
 
 //Populate attribute array with raw data
 $result = ldap_search($rs, $dn, $filter, $attributes);
 //Actually process the data and fill the object
 $entries = ldap_get_entries($rs, $result);

?>



That code will successfully pull the data from Active Directory and store the results in $entries. If you are unfamiliar with the logic in the $filter line, check out the image below.


Then to access the data, you can do something like this:

<?php
for($i=0; $i<($entries["count"]); $i++) 
{
 if(isset($entries[$i]["givenname"][0]))
 {
  $givenname = $entries[$i]["givenname"][0];
 }
 
 echo "Name is: " . $givenname;
}
?>


And there it is. Connecting to Active Directory with PHP through LDAP. Maybe I'll put up my Powershell to Active Directory scripts next!








Sunday, June 5, 2011

Casting Revive - Success!

Back at it with XNA after a long (long) break. All of Sunday produced this monstrosity:

Video:

Installer: http://www.cl.ly/2i2v3u33222c3y0O0X2t


Not much more needs to be said, really.

Friday, November 13, 2009

Linked Lists Simplified - Part One

Do you find it hard to understand linked lists? Well STOP RIGHT THERE! Chances are, you're probably thinking about it too hard. A linked list is, in its simplest form, a collection of nodes that point towards each other. However, that definition can also be confusing. Let the illustration below explain things more clearly:






Okay. Thats great and all, but where are these "nodes" coming from? Well as it turns out, nodes are simply user defined objects, like a class or a struct. Creating a node structure would look something like this:

struct listNode
{
string info;
listNode *link;
};

The most important thing to look out for here is the fact that listNode contains a listNode pointer named link. Using pointers is what gives us so much power with linked lists. Next up we will discuss the "last" or, "tail" pointer, along with basic list traversing.

Wednesday, October 7, 2009

Programming Wisdom

After a three hour battle with passing variables around in C++ (as I was encapsulating my previously global development variables), I came up with a pretty philosophical sounding summary of my experience:

"One does not truly understand passing by reference until he
first does truly not understand it."

Tiell, 2009 A.D.

Neat. Now, enough of that. What I really mean is, that during my development tonight I came across a situation where passing a variable by reference was the obvious course of action. However, I completely forgot to even attempt it. So I sat here thinking about it for nearly three hours. During those three hours, I basically came up with the basic underlying idea of passing a variable by reference. It wasn't until then that I realized, "Oh, that functionality is already built in." This is a reoccurring theme I think. You can spend a lot of time on a great idea, but then come to realize that it has already been developed. It's bittersweet, but much more sweet I think. You can just throw that functionality into your library and move on to the next big thing. Assuming that it too, hasn't already been developed.

C++, my dear old friend. It's good to be back.



Yeah. It was that simple. You don't want to see what I tried to make this work!

Thursday, July 2, 2009

Matrices, Rays, and Math - Oh My!

[EDIT 06/09/2011] Note that this is XNA 2.0, and has not been updated for newer versions. The code might not still stand, but the idea should!

After pulling from many resources online, (Remiers, Ziggyware, XNA Creators Club) I finally had heightmap collision working. What was missing, was the player rotating towards the position it was heading. When I first tried implementing the model rotation, I was using the same ray that was cast for my click-to-move code to calculate the direction. What I didn't realize was that because the camera is positioned behind the player, clicking behind the player was still clicking in front of the camera. I will let this illustration explain:



So, my direction vector was still saying "In front", even though I was clicking behind the player. The solution was to use a camera like camera 2 in the illustration for the players rotation, and to use view camera to, well, view. The code is as follows. Credit to Remiers for the majority of the processes.

Calculate the collision point here. Note the second camera used to get the directionCharacter vector:

void CalculateClickToMovePosition()
{
    if (currentMouseState.LeftButton == ButtonState.Pressed)
    {
        Vector3 nearScreenPoint = new Vector3(currentMouseState.X, currentMouseState.Y, 0);
        Vector3 farScreenPoint = new Vector3(currentMouseState.X, currentMouseState.Y, 1);
        Vector3 nearWorldPoint = GraphicsDevice.Viewport.Unproject(nearScreenPoint, viewCamera.cameraProjectionMatrix, viewCamera.cameraViewMatrix, Matrix.Identity);
        Vector3 farWorldPoint = GraphicsDevice.Viewport.Unproject(farScreenPoint, viewCamera.cameraProjectionMatrix, viewCamera.cameraViewMatrix, Matrix.Identity);

        //Direction for the Click to Move process
        direction = farWorldPoint - nearWorldPoint;
        direction.Normalize();

        Vector3 nearScreenPointCharacter = new Vector3(currentMouseState.X, currentMouseState.Y, 0);
        Vector3 farScreenPointCharacter = new Vector3(currentMouseState.X, currentMouseState.Y, 1);
        Vector3 nearWorldPointCharacter = GraphicsDevice.Viewport.Unproject(nearScreenPointCharacter, playerMovementCamera.cameraProjectionMatrix, playerMovementCamera.cameraViewMatrix, Matrix.Identity);
        Vector3 farWorldPointCharacter = GraphicsDevice.Viewport.Unproject(farScreenPointCharacter, playerMovementCamera.cameraProjectionMatrix, playerMovementCamera.cameraViewMatrix, Matrix.Identity);

        //Direction for player model rotation
        directionCharacter = farWorldPointCharacter - nearWorldPointCharacter;
        directionCharacter.Normalize();

        pointerRay = new Ray(nearWorldPoint, direction);
        lastRayPosition = new Vector3();
        height = heightMapInfo.GetHeight(pointerRay.Position);
        
        //Step the ray downwards until it no longer it intersecting
        //with the ground. At that point, we know the lastRayPosition
        //holds the exact collision point.
        while (pointerRay.Position.Y &amp;gt; height)
        {
            lastRayPosition = pointerRay.Position;
            pointerRay.Position += direction;
            height = heightMapInfo.GetHeight(pointerRay.Position);
        }
    }
}

Now we need to draw everything to the screen using those matrices we just calculated:

void DrawPlayer(Player player)
{
    foreach (ModelMesh mesh in player.model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.PreferPerPixelLighting = true;

            effect.World = Matrix.CreateWorld(new Vector3(player.position.X, player.position.Y + 12, player.position.Z), 
                                              new Vector3(directionCharacter.X, -90, directionCharacter.Z), 
                                              Vector3.Up) *Matrix.CreateScale(player.scale);

            effect.Projection = viewCamera.cameraProjectionMatrix;
            effect.View = viewCamera.cameraViewMatrix;
        }
        mesh.Draw();
    }
}

Notice how in Matrix.CreateWorld, we use the directionCharacter vector. That is the vector that we get from the playerMovementCamera.

I am not sure if I went about this the wrong way, but it seems to be working fine for my implementation. Hopefully someone will find this useful, and won't have to spend three days fighting with it.

Matrices! Woo!

Sunday, June 21, 2009

Sprites Sprites Sprites!

Finally got spritesheet management working pretty well. I owe most of that to the spritesheet XNA tutorial on the XNA creators site. I altered it to fit my own needs, so that was the fun part. I created a character class with a default constructor that was aware of the standard spritesheet dimensions (see below). That way, I literally have single line character creation. Neat.



You can't tell from the screenshot, of course, but its moving. It feels good to be working at the pixel data level, and actually understand what is going on. Being forced to program rudimentary things that you would rather not be programming really helps in the long run, I think. Although, now I will just add them to my library and never look back.




As always, source is available upon request.