Feb 22 2013

Game Engine – Memory

Luthi

The point of the second assignment was to use our heap system instead of the default. Where we can create multiple heaps to speed up allocation and reduce fragmentation. For instance if you have 1 heap for everything that will stay around the entire game, that data is not in the way when searching for other data or space to put more data. We also created tracking information for each piece of data and a redundancy in case of corruption.

The structure:
We had a memory singleton (Mem) that controlled everything. It contained all the information like the amount of heaps, amount of allocated data, and total size of allocations in bytes. In it was a pointer to the heap list and data doubly linked lists. The heap class points to the next and previous heaps and starts the data list. It also holds the amount and size of allocations for that heap.

The tracking class completes the data linked lists for both heap and mem as well holding tracking information. This part was tricky as it sits above the data and we return the pointer to the actual data on return, so how do you find it again? You store the pointer to the tracking block in the 4bytes above the data.

Windows system programming:
This was my first go with direct system calls and I’d be lying if it didn’t scare the fuck out of me. Though it was a lot easier and less dangerous than I feared. HeapCreate(), HeapDestory(), HeapAllocate(), HeapFree(), HeapRealloc(), HeapSize(), and overloaded new and deletes.

Problems:
The second call to HeapAllocate, I get “Windows has triggered a breakpoint in PA2.exe.” Awesome. Thanks for the helpful information. I write print statements, I degbug it every way I know how. The time to leave for yoga comes and goes. I’m too tunnel visioned to care. I even watch the memory: create heap, allocate heap header, allocate first block, and I see it go red as it attempts to allocate the next. And boom.

I’m not certain how, but eventually I notice that I’m not seeing the tracking head pointer being added. I type in the address of where it’s going, the memory window jumps way down. Oops. My pointer math was written incorrectly for calculating the end of the tracking block. Why is it always the little things that trip you up?

Sometime after delete started working, I started to get random crashes. And I mean random. With the same break point it would crash sometimes before and sometime not. When it’s difficult to reproduce, it’s even more frustrating to find what’s causing it. I’d fix it, move on, and it’d come back. All I knew was sometimes a pointer had issues. When do they not?

First I added the destroyHeap in the mem initialize method (only if there are some allocated from before) to clean up from previous tests. I figure if we are zeroing out everything else anyways, might as well make it nice and clean. Plus there might be a heap pointer going awry somewhere. That really just made it crash more. So I’d go back and forth with commenting it out and then using it again.

Then I added freeing the blocks in the heap before destroying the heap in order to clean up the global linked list. So really it was a continue on and see if that fixes the problem sort of situation.

In the end, I was debugging something else when I realized that fixing the global pointers in the heapFree function was acting unusual. Fixed it and no more random crashes. Then it was only a short time before I fixed the remaining unit test failures.

Some code:

void *Heap::privHeapAlloc(Align align, size_t inSize, char *inName,  int lineNum)
{
   int alignment = this->privGetAlign(align);
   TrackingBlock *head = this->TrackingBlockHead;
   size_t totalSize = inSize + sizeof(TrackingBlock)+alignment+1;
 
   //allocation of space
   void *unAlignedAddr = HeapAlloc(winHeapHandle, 0, totalSize);
 
   //check allocation
   assert(0 != unAlignedAddr);
 
   TrackingBlock *block = new(unAlignedAddr) TrackingBlock(inName,lineNum);
 
   Mem::privAddAllocInfo(inSize, block);
 
   //attach heap pointers
   if(head != 0)
   {
      head->hPrev = block;
      block->hNext = head;
   }
   this->TrackingBlockHead = block;
 
   //update allocation info
   this->privAddAllocInfo(inSize);
 
   //update block information
   block->allocSize = inSize;
 
   //get the address at the end of the trackingblock
   void *temp = (void*)((unsigned int)block + sizeof(TrackingBlock));
 
   void * p = (void*)((unsigned int)temp +(alignment - 1) & ~(alignment - 1)); 
 
   //write the address to the top in the 4 bytes above
   *((unsigned int *)((char*)p-4)) = (unsigned int)block;
 
   return p;
}

Feb 22 2013

Game Engine – PCS Tree

Luthi

The first assignment this term was a parent/child/sibling tree. Each node had a pointers for a parent, child and sibling. That meant the siblings were a singly linked list which made things a bit tricky down the line. We were given a bare bones project with the PCS class and unit test. The point was to make all the functions actually do something (properly) so the tests would pass.

Setup:
While I knew the tests would fail, I honesty didn’t think it would blow up as it did:

The application has failed to start because its side-by-side configuration is incorrect.

That’s helpful! Thanks windows! Cheeky shit. So I go to the event log…

Activation context generation failed for “…PA1.exe”. Dependent Assembly Microsoft.VC90.DebugCRT, processorArchitecture=”x86″, publicKeyToken=”1fc8b3b9a1e18e3b”, type=”win32″, version=”9.0.21022.8″ could not be found.

My google-fu first tells me to install Microsoft Visual C++ 2005 Redistributable Package, which is odd since I already have it. Figured it couldn’t hurt so went ahead and tried that. Nope. Further digging, which isn’t a lot out there, I keep seeing references to Visual Studio 08. Odd I think since I’m using 10. Eventually come to an old posting that mentions installing 08. I grab a copy from MSDNAA, because I’m all out of ideas. The next day someone started a discussion and the best response was: You don’t need to install VS2008. Just open Project Properties and go to Linker > Manifest File, switch Generate Manifest to No.

Did I mention I’m too stubborn to ask (other than google) until I’ve tried everything? Something about proving that an old art nerd can in fact think with the other side of her brain again. Or maybe it’s a lady problem. I can do maths! Probably more likely though is that I remember more by fixing it myself.

Coding begins:
Most of it was pretty straight forward tree building stuff. It’d been a while since I’d dealt with trees and I’m pretty sure I’ve never actually written one.

Recursion and I have never been friendly. Spending most of my life in tangible ideas – photography and film – my brain gets stuck on the abstract concepts. For instance, it used to be that I wanted to punch whomever thought up pointers. That is until I took Computer Systems 2 where I figured out that sometimes you want to manipulate something while keeping the original in tact. All of a sudden, it clicked and I haven’t had a problem with them since.

I understand that recursion (if done on the right things) will speed up the run time. Divide and conquer and all that. But it takes me so long to see what it’s doing, tracing it out, and holy fuck the debugging nightmare it gives me.

Remove:
We were supposed to remove (not delete) the node and all of it’s children. This I spent quite a bit of my time on drawing, writing and rewriting.

//Remove a node and all of it's children. Remove the node.
 void PCSTree::remove(PCSNode * const inNode)
 {
    PCSNode *prevSibling = 0;
    PCSNode *child = inNode->getChild();
    PCSNode *sibling = inNode->getSibling();
 
    if(inNode != this->root)
    {
       PCSNode *parent = inNode->getParent();
 
       //find the previous sibling
       prevSibling = parent->getChild();
 
       if(prevSibling != inNode)
       {
          while(prevSibling->getSibling() != inNode)
          {
             prevSibling = prevSibling->getSibling();
          }
          prevSibling->setSibling(sibling);
       }
       else
       {
          //attach parent node to sibling
          parent->setChild(sibling);
       }
 
       //null pointers
       inNode->setParent(0);
       inNode->setSibling(0);
    }
 
    else
    {
       root = 0;
    }
 
    removeHelper(inNode);
    info.numLevels = this->getLevel(root);
 }
 
//remove all of the children.
 void PCSTree::removeHelper(PCSNode * const inNode)
 {  
    PCSNode *child = inNode->getChild();
    PCSNode *sibling = inNode->getSibling();
 
    while(child != 0)
    {
       remove(child);
       child = inNode->getChild();
    }
 
    while(sibling != 0)
    {
       remove(sibling);
       sibling = inNode->getSibling();
    }
 
    if(child == 0 && sibling == 0)
    {
       info.numNodes--;
    }
 
 }

Get Level:
The point here is to get the height of the tree – the amount of steps from root to the lowest node. It was another tricky one due to the singly linked list for siblings. Once I got it to not crash, I figured out that it wasn’t hitting all the siblings. Figuring it out really got me to learn how to debug a recursive function.

int PCSTree::getLevel(PCSNode * const inNode)
{
   int max = 0;
   int height = 0;
 
   if(inNode == NULL)
   {
      return 0;
   }
   else
   {
      PCSNode *child = inNode->getChild();
      PCSNode *sibling = inNode->getSibling();
      PCSNode *siblingChild;
 
      if(child == 0 && sibling == 0)
      {
         return 1;
      }   
      if(child != 0)
      {       
         height = this->getLevel(child);
         if (height > max)
         {
            max = height;
         }
      }
 
      while(sibling != 0)
      {
         siblingChild = sibling->getChild();
         if(siblingChild != 0)
         {
            height = this->getLevel(siblingChild);
            if (height > max)
            {
               max = height;
            }
         }
         sibling = sibling->getSibling();
      }
 
   }
      return max +1;
}

Jan 24 2013

fundamentals of game programing

Luthi

Last term was mental. I took this class that covered the game loop, collision basics, controls, cameras, OpenGL, etc. In all honesty, it really wasn’t all that difficult. I had a crazy amount of fun – especially with collisions. It really was the first time I made a game (mostly from scratch) from start to finish. I saw mostly because we were working with a simple game engine that was made for the class. It did however make the structure of a game make sense from the update->collision->draw basic loop to adding levels.

What was mental about the experience was the sheer amount of things we were doing in a short amount of time. I had a week to make pong. Another week to code asteroids. Luckily the final game (missile command) we had a bit more time. I still have the games and I may or may not post them.

We were also grouped up for the entire term so for the first time I had someone to ask when I got stuck. Though I did end up spending more time helping him than the reverse. It was still a good experience. We had actually met up for coffee and coding a couple of times.

I’m trying to post more going forward. I really am going to try this time. So maybe it’ll sound less like a quick rundown from memory and more of an actual code brain dump.


Jan 24 2013

#1reasonwhy

Luthi

At the end of November, twitter blew up with women in games sharing their experiences in the industry: #1ReasonWhy. In a response to the (albeit common) question of “Why are there so few lady game creators?” I even added my own: Because I was the only one heckled during my final game project presentation. #1reasonwhy

It was an amazing event to witness. The only shocking part of it all was the sheer amount of people who had the gumption to speak up. No matter how many tried to silence them. It makes me feel lucky that not only have I had it easy in comparison, but to know that I am in fact not alone.

This is my 3rd year in the game dev department at DePaul in Chicago. There were several prerequisites I had to take before the grad program actually started and I can only go mostly part-time, so it’s taking a while. All things considered, it’s far better than I had expected. Every professor I’ve come into contact with has been respectful from the beginning and I haven’t had a single instance of condescension. As well as no special treatment, at least as far as I can tell. In fact they have welcomed my class participation more than enough to make someone who is normally very quiet actually speak up. Last term I had to ask one to not put me on a team with a specific classmate (I will explain later). The email exchange was professional and respectful and he did so without question or making it an issue.

They do also have support for women in the computer department that sadly only caters to those without full-time jobs. While I could get to campus for a 4:30pm meeting, but the emails don’t always go out and rarely do I know more than a day in advance.

That’s not to say that things have been easy or that it’s been without incident.

Being the only or one of a handful of women in a class isn’t comfortable. It will make anyone question whether or not they belong. We as humans strive for situations that we fit in, and while I should feel at home with others who love games and coding, I just don’t. I feel uncomfortable most of the time. As if I really shouldn’t be there.

It doesn’t really help when the professor shows incredibly masculine videos to emphasize points. I’m pretty certain there is something better out there to signify now being advanced C++ coders than “welcome to the world of gentlemen, gentlemen.” Perhaps it would help to not bring up a study that women multitask better than men, especially when other studies say it’s actually men just prove that some people multitask better than other people. All that did was encourage the person sitting behind me to bring up the idea that “women are more emotional.” I turned around and asked if he was looking for a punch in the face and it made the class laugh. What I don’t understand is that if someone had said “all ____ (race) are _____ (stereotype)” shit would have gone down. It’s the same thing just instead of racist, it’s sexist.

For the most part my classmates aren’t exactly friendly to me and seem to just tolerate my existence. Conversations before/after class and during breaks aren’t what you’d call inclusive. While my interjections don’t fall on deaf ears, it’s not exactly welcoming either. Don’t get me wrong, there have been some that are like me and shy at first, but fine once they warm up to you. On the flipside, there have also been a handful that have been varying levels of terrible.

One that used the phrase “women shouldn’t talk like that,” to someone else. Luckily we both shot him down with looks and a response of “we can say whatever we want.” Strangely enough, he was one of the heckling offenders while I nervously gave a demo of my final game last term while trying not to throw up or pass out. Public speaking and I don’t really mix well. Perhaps I should have sat there invalidating myself through it like another woman had?

Last year there was one that fully believed I would commiserate with him on his failing grades no matter how many times I tried to get out the conversation. Every night during break he would follow me out to the cold of Chicago, stand too close and talked of failing classes, bad grades on projects and the state of our classmates geeky tee shirt attire. All I wanted to do was enjoy my smoke. Granted he never really paid attention to my responses of doing just fine nor the fact that I wear a different geek tee every week. While annoying, that would have been fine. It was the being 1 step behind me on the walk to the train – whether I took the elevator or ran down 10 flights of stairs. He would then get on the same train and watch me read from across the train car. That’s what set off my spidey sense enough ask not to work with him in the next class.

In all honesty, what’s worse is what happens in the outside world. Whenever I wear my xkcd tee “I’m not slacking off, my code is compiling,” without fail someone has to ask “are you really a programmer?” I don’t see why anyone would actually wear it if they weren’t. Though the attempted C++ syntax exams are rather unnecessary.

How I’ve chosen to deal with it
For the most part, I focus on the work. Doing as well as I am seriously helps the negative self-talk that comes from not feeling like I belong. I get A’s, that’s proof enough. I love coding and no one is going to take that away from me.

Comments that are sexist? It depends on the situation. Sometimes I jokingly threaten violence, but that’s only in very specific moments. Most of the time, it’s “Please explain how that is funny.” Then watch them try to justify the behaviour.

Sometimes, like the heckling, I don’t notice right away. I know it’s kind of shitty, but I’m too busy to focus on why. For the most part, it turns into jokes with friends. 1 that I’m turning into a game based on a conversation. Just something silly with Unity more for humour value.

Other than that, seriously taking a look at that mentoring list.


Jun 9 2012

summer.begin()

Luthi

I had a completely brutal final exam on Monday that left my brain mush for the rest of the week and oddly unable to sleep. 10 questions that I spent the full 3 hours on. I left at 5min left to go and most of the class was still there, so that made me feel better about it. I did better than I thought, but then again I always leave thinking I’d failed. Which is kind of absurd since I never get anything below a B. Final grade: A! Total GPA: 4.0! Yay!

I’m only partially disappointed that there are no classes I can take over the summer. I need a break, but I really don’t want to be pushing 40 when I graduate. I suppose now I have time to catch up on some things.

Coding Portability:
Last night I found a group of women coders who meet once a week to sit around and code together. They do have other meetups also. The problem is that my laptop is a 3 year old eee pc running linux. I want the ease of going back and forth from my desktop, so I just went ahead and reformatted and installed win7. It’s running fine, but there isn’t enough room on the main drive to install visual studio. So I guess I’ll just remote in or use notepad++ for now. At least until I can buy a new one. The bonus is that I can go to a coffee shop or sit outside and code anytime I please. Good thing since it’s getting hot and my apartment doesn’t have AC.

Gaming:
I need to play more. I have a stack of games that I want to play/need to finish but enough desire to at the moment to find the time. Today was my first full day of not having anything I needed to do and I played Diablo III for about 10min. Instead I logged out, walked to staples, and bought some random supplies. Perhaps I’ll fare better tomorrow.

Women in Computing Conference:
This week I received an email about a scholarship to Grace Hopper Celebration of Women in Computing 2012 conference. DePaul is sending 5 students this year – airfare, hotel and registration included. I don’t actually remember when the last time I wrote an essay, but I’m going to get a shot. Nothing to loose.

Women in Geekdom:
I mentioned in the comments for the C2E2 post that I’m thinking about doing something. Right now I’m working on a site that is a bit of a mix between a blog and a forum. A safe zone for us ladies to talk about the issues. Basically a blog in style, but anyone registered can post. I’m still playing around with ideas and trying to figure out if I can commit the time. We’ll see if anything comes of it.

Summer Learning List:
Right now I’m reading Effective C++. After that (or perhaps during for coding practice) I’m going to learn openGL. I also have a book on directx that I might dust off. Then there is C# and Unity3D.