Wednesday, March 10, 2010

J1N Games

Jason Kim's Personal Journey Into Game Development

Archive for September, 2009

C++ Combinations Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial on how to find every combination of a given string in C++

A Combination of a string is if you are given string ABCD, then the combinations of the string are: A, AB, ABC, ABCD, B, BA, BAC, BACD… and so on.

C++ Combinations Example

C++ Permutation Examples

Posted by Jason On September - 29 - 2009

An Example/Tutorial of finding every permutation in a string in C++

A permutation is if given a string ABCD, then permutations of the string are: DACB, BCAD, DBCA, CBAD… and so on

C++ Permutation Example

C++ Sorting Examples

Posted by Jason On September - 29 - 2009

An Example/Tutorial of Sorting Methods in C++

In this example I show how to sort an array of numbers using a couple techniques.
Quicksort: A recursive sort done with a pivot point from the list and partitioning the list into left and right sub lists and arranging those sub lists to make every element in order.  Average Case O(n log n), with worst case O(n^2)
Bubblesort: Repeatedly stepping through the list to be sorted and comparing two items at a time and swapping them if they are in the wrong order.  Repeat until no swaps are needed. Best Case O(n), with worst case O(n^2)

C++ Sorting Examples

C++ Endian Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of using Big and Little Endian in C++

Endianness in Computer Science are talking about the byte/bit ordering used to represent data / words in a computer system.  Typical cases are order in which an integer value is stored as bytes in a computer memory.
Endian is talking about Byte Order.  Most computers today run on a 32 bit Word scheme.  32 bits is 4 Bytes, as 8 bits = 1 Byte.  Thus A 32 bit Integer is made up of 4 Bytes.

Big Endian: In Big Endian the higher order of the byte in the number stored in memory comes at the lower address in memory (Big End Comes First).  So imagine an Integer made up of Byte0,1,2,3  (4 bytes for a 32 bit integer).  Byte3 would come at the lowest memory address.  Big Endian ordering would look like this:
Base Address + 0 : Byte3
Base Address + 1 : Byte2
Base Address + 2 : Byte1
Base Address + 3 : Byte0

Little Endian: In Little Endian the low order byte of the number is stored in memory at the lowest address  (Little End Comes first).
Base Address + 0 : Byte0
Base Address + 1 : Byte1
Base Address + 2 : Byte2
Base Address + 3 : Byte3

x86 Processors (PC’s) use Little Endian
Power PC’s (macs) use Big Endian

In the example I will show how to test for Endian type on the computer, and how to swap between Big and Little.

Big/Little Endian comes from the story of Gulliver’s Travels, from the two political parties of the Lilliputians with some who fight over cracking their soft boiled eggs open from the little or the big side.

C++ Endian Example

C++ Singleton Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of the Singleton Design Pattern

A Singleton is a single instance of a class throughout the whole system.  Usually used for a global class in a system that can be accessed by everyone.  The life of a singleton class is the duration of the entire application.  It typically has uses for things like Logs, Audio, Application Sysetms things that generally need only 1 instance in the entire application.

Singletons are created by a static member function that returns a pointer to the single instance of the class.  Only this member function can call the constructor by making the constructor private and the copy constructor and = operator.

C++ Singleton Example

C++ Factory Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of the Factory Design Method

A factory method is a static method of a class that returns an object of that class’s type.  Unlike a constructor, the actual object it returns might be an instance of a subclass.  It can return existing instances multiple times.  The factory method is basically a “creator” design pattern in C++ for creating sub classes from a base type class.

C++ Factory Example

C++ Virtual Keyword Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of the Virtual Keyword.

The Virtual Keyword in C++ is used for inheritance and is an important concept in Object Orientated Programming and the Polymorphism buzzword.  The Virtual Keyword in a base class indicates a method can be used by a derived class or be defined or overridden with its own function.

The term “Pure Virtual” comes from when you declare a method in a class as such:  virtual void MyFunction() = 0;
By appending the “= 0″ part this means the virtual member function has no implementation.  This creates an abstract method by which makes the Class an Abstract Class.
An Abstract Class cannot create an instance of itself, only a pointer to itself can apply, however derived classes of the Abstract Class can.

The computer knows which virtual function to call from base and derived classes from the compiler setting up a virtual table, like the symbol table, the virtual table has pointers to each of the virtual functions and classes. The compiler sets up a hidden pointer *__vptr automatically for each class instance so that it points to the virtual table for that class.  It is different from the *this pointer which is actually a function parameter for self references.  the vptr is a real pointer and makes the classes bigger by one pointer.  vptr is also inherited by derived classes

The diamond problem, also comes up with multiple inheritance.  This is where if you have base class A, and derived class B and C, and class D derives from B and C, the compiler will find it ambiguous and does not know which path of derivation to take for class D, does class D derive A from class B or does class D derive from class C from A?  To solve this problem the virtual keyword can also be used here, by creating each derived class B and C like this:
class B : virtual public A
class C : virtual public A
The virtual keyword here will virtually inherit from A, thus solving the ambiguity of class D.

C++ Virtual Keyword Example

C++ Linked List Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial of a Linked List Structure.

Linked Lists are basically a type of data structure that houses in each node the data and a pointer to the next node.  A Singly-Linked List is your most basic Linked List which has each node from start to end with the end node pointing to a Null terminator.  A Doubly Linked List has nodes that point both to the next node and another node.  A Circularly Linked List is one where there is no Null terminator at the end, but instead the end node points to the first node.  The Idea is basically Head->Node->Node->End->NULL

C++ Linked List Example

C++ Overloading Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial on Function and Operator overloading in C++

Function Overloading is having functions of the same name but giving them different number of parameters or types.

Operator Overloading is having operator keywords like “+” or “-” perform another action or function.

C++ Overloading Example

C++ Template Example

Posted by Jason On September - 29 - 2009

An Example/Tutorial on how to use C++ Templates
Templates are special functions to be used with more than one type of class or typename indentifier.

A Template Function looks usually like this format:
template<class MyType>
MyType GetMax(MyType a, MyType b)
{
return (a>b ? a: b);
}
with a call that looks like this:
GetMax<int>(x,y);

Templates can also be used with classes.  See the example:
C++ Template Example/Tutorial