Wednesday, March 10, 2010

J1N Games

Jason Kim's Personal Journey Into Game Development

Archive for the ‘Code Work’ Category

C++ Lua Script Integration

Posted by Jason On October - 6 - 2009

Game developers and Software Developers often make use of a scripting language to integrate with their C++ applications.  One common one is a free scripting package known as Lua.
Lua was developed for C, but is fully functional with C++.  This example shows how to effect variables,call functions,and create classes in both C++ and Lua, and how they pass data back and forth between each other.
Scripting is a very important process in software development, it allows the developer to quickly change values in a script file and see immediate changes in their application without having to completely rebuild the app.
An example would be lets say in a video game, you have a Player, and his values need to be balanced for the amount of damage he does, or his speed.  Going into source engine and changing this value would take up a lot of time as you would have to rebuild the entire application/engine, instead with script you can just change a few values in a script file and launch the application to see your changes.  This allows for quick-iteration between builds and faster progression testing/application tweaks.

For this code example you will need Lua, available at http://www.lua.org/
I also make use of luna.h an addon for Lua for C++ binding (included in source files) also available from lua.org

C++ Lua Script Integration

C++ Fmod Sound API Integration

Posted by Jason On October - 6 - 2009

This is an example of how to integrate the FMOD Sound API with C++ into a class that can be used in any c++ application.
To compile the user needs to have the FMOD Sound API Includes and Libraries and DLL’s, found at: http://www.fmod.org/

This example also makes use of a singleton design pattern.

C++ FMOD Sound API Integration

C++ Boggle Board

Posted by Jason On October - 6 - 2009

This c++ app builds a given boggle board and finds all the words on the boggle board based upon a given dictionary file.  The way I did this is simply by taking all the combinations of all of the letters on the boggle board
and compared them against a dictionary file.  In this implementation you will notice that it does not find “every” word because there are missing words from the “dictionary.txt” file I downloaded from the Internet

C++ Boggle Board

C++ Memory Manager

Posted by Jason On October - 6 - 2009

This is a C++ example of our own versions of Malloc and Free.  In Embedded Systems, often memory is not readily available and small.  Thus static allocations of memory are often good.
This Memory Manager works with a static allocation of a memory pool.  This way the user knows exactly how much memory they are working with, as opposed to dynamic memory in heaps.
We have a memory pool of something like char[10000] where our malloc returns a pointer in memory in the pool and allocates bytes from the pool, and our free frees up the pointer memory.

C++ Memory Manager

C++ Memory Queue

Posted by Jason On October - 6 - 2009

A FIFO Queue that stores bytes in a static allocated memory pool.  Embedded Systems often have a small amount of memory to work with.  So often times people use a static allocation of memory so they know exactly how much memory they are working with as opposed to dynamic memory on a heap.  This program is an example of how to construct a queue with a static allocation of memory and store values by byte in the memory pool.
The basic idea is you have something like char pool[10000] and pool becomes your static allocation of memory.  Then you can store unsigned char bytes into the char array as a queue, popping off bytes as you need etc.

C++ FIFO Memory Queue

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