Memory Management Tutorial

I was going through my data structure notes and was surfing the net to find out more about memory management. I chanced upon this website which has a pretty clear application that simulate the allocation and freeing of memory. Feel free to check it out!
http://research.cs.vt.edu/AVresearch/MMtutorial/index.php

 

Basic Memory Management
http://www.memorymanagement.org/articles/

Program Design Methodology

Hey everyone! I am taking data structure this semester. I will be sharing some of the things that I learned over here and hopefully you can picked up one or two useful things.

Why Plan?

  • Group projects without a plan will not work
  • If you have not figure out the design before you begin coding, you may paint yourself into a corner and have to start over.
  • You save time by planning.
  • You can verify that the algorithm works before coding.
  • The plan is your :blueorint: for software construction.

So How do I design an Algorithm?

  • Good Question. There is no real answer to this question that works in all cases.
  • Algorithm design is the creative part of programming– a very human endeavor.
  • Computer Programming is  an art.
  • Elegant solutions are the simplest but not all problems can be solved elegantly.
  • Experience helps patterns of problems and solutions emerge over time.

Some Help

  • Make sure that specifications are complete and make sense. Do you understand the problem?
  • Determine what problem is supposed to output.
  • What errors might occur while the program is running?
  • How should the program end?
  • Get answers to questions about the specifications before any code.
  • Break the problem into sub problems
  • For each sub problem
    - Write a concise problem statement to be sure you understand
    -find an existing algorithm (if there is one). or create a new one
    -design the data  structures needed
    - Write out the steps (algorithm) for solving the sub problem
  • Create the code to implement your solution
    -code and debug each function separately
    -this is called procedural abstraction
    - test all functions together

Implementation

  •  Using the plan you developed, begin writing code
  • Document as you code, preferably with automatic documentation generators (e.g JavaDoc or Doxygen).
  • Place detailed comments at the beginning of the program/module explaining the purpose of the program/module
  • Make comments for each function and use comments for unusual logic

Testing

  •  Testing can use about half the time for development of a system.
  • Test modules independently
    - drivers to test modules
    - stubs to test modules for which functions have not been developed
    - test cases that exercise all logical possibilities. Test normal and borderline cases.

Robustness

  • How will the program react to invalid data?
  • Should the program crash?
  • User friendly error messages. Are compiler messages user friendly?
  • How robust a program needs to be depends on the specifications and the user.

System Testing

  • Testing modules aloe is not enough.
  • The whole system with all modules included needs to be tested.
  • Testing by end users should be done since they are the ones who must ultimately accept the system. (Beta testers)
  • Systems that do not the the acceptance of the end users will fail even if well written.

Debugging

  • When encounter errors:
    - Think!
    - Random changes only make the problem worse.
    - Use debugging tools.
    - Fixing one error may cause another error ( one more) Retest with the same tests as before.
    - Keep the test cases for future use.

Particle Engine

I am currently working on the particle engine of my game. My team mate forwarded  me this link (http://rbwhitaker.wikidot.com/2d-particle-engine-1) that had a tutorial on how to make a particle engine. The tutorial consist of 4 whole page of texts. I think that is great but too long when you want to refer to it again and again.

I will give a summary of what the tutorial is about. Reason number #1 is that I learned better when I summed up things. Reason number #2 is that this allowed me to have a quick recap if I want to refer to it again. I do not want to waste time reading through all the 4 pages again.

What is a Particles?
A particles is a small point in your games that will be drawn with an image.

Anatomy of a Particle Engine
A particle engine consist of three major components:
1. Particles
2. A Particles Emitter
3. Engine itself

Particles often has:
1.  A position in a game world
2. Velocity
3.  Angle that it is rotated at
4. Angular Velocity (Indicates how quickly the particle is spinning)
5. Color and texture for drawing.
6. Life span

Particles Emitter is:
1. The location that the particles are coming from
2. Responsible for determining how many particles will be created at any given time.

Particle Engine:
Manages the state of the previous two components and is responsible for removing dead particles from the system.

Creating a Particle Class
I have rewrote the code in C++ so it’s abit different from what is shown in the website.

Particle.hpp
class Particle
{
public:
particle(Texture2D texture, Vector2 position, Vector2 velocity,float angle, float angularVelocity, Color color, float size, int ttl);  //Constructor
~particle();   //Destructor
void Update();
void Draw();

private:
Texture2D Texture;  //Texture that will be drawn to represent the particles
Vector2 Position;    //Current Position of the particle
Vector2 Velocity;  // Speed of the particle at the current instance
float Angle;   //Current angle of rotation
float AngularVelocity;  //Speed that angle is changing
Color Color;  //Color of the particle
float Size;   //Size of the particle
int TTL;   //Time to live
}

Particle.cpp
particle::particle(Texture2D texture, Vector2 position, Vector2 velocity,float angle, float angularVelocity, Color color, float size, int ttl)
{
Texture= texture;
Position = position;
Velocity = velocity;
Angle = angle;
AngularVelcoity = angularVelocity;
Color = color;
Size = size;
TTL = ttl;
}

void particle::Update()
{
TTL–;
position += Velocity;
Angle += AngularVelocity;
}

void particle::Draw()
{
DrawParticles(); //The drawing will be done using the draw particles function I program in the graphic engine. More will be disclose about the engine in later stage.
}

ParticleEngine Class
ParticleEngine.hpp

class ParticleEngine
{

public:
ParticleEngine( List<Texture2D> textures, Vector2 location); //Constructor
Particle GenerateNewParticle();
void Update();
void  Draw();

private:
Vector2 EmitterLocation;
Random random;
List<Particle> particles;
List<Texture2D> textures;

}

ParticleEngine.cpp
ParticleEngine::ParticleEngine( List<Texture2D>, Vector2 location)
{
EmitterLocation = location;
this.textures = textures;
this.particles = new List<Particle>();
random = new Random();
}

Particles ParticleEngine::GenerateNewParticle()
{
Texture2D texture = textures[random.Next(textures.count)]; //Assign a random texture to the generated texture.
Vector2 position = EmitterLocation;
Vector2 Velocity = new Vector2( 1f * (float)(random.NextDouble() * 2-1), 1f * (float)(random.NextDouble() * 2-1));
float angle = 0;
float angularVelocity = 0.1f * (float)(random.NextDouble() * 2-1 );
Color color = new Color( (float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
float size = (float)random.NextDouble();
int ttl = 2- + random.Next(40);

return new Particle(texture, position, velocity, angle , angularVelocity, color, size , ttl);
}

void ParticlesEngine::Update()
{
int total = 10;

for(int i = 0; i<total; i++)
{
particles.Add(GenerateNewParticle());
}

for(int particle = 0; particle < particles.count; ++particle)
{
particle[particle].update();
if(particle[particle].TTL <=0)
{
particles.removeAt(particle);
–Particle;
}

}

void ParticleEngine::Draw(spriteBatch spriteBatch)
{
spriteBatch.Begin();
for()
{
particle[index].Draw(spriteBatch);
}

spriteBatch.End();

}

Using the particle Engine
Create an instance of the particle engine.

ParticleEngine particleEngine;

At Load()
List<Texture2D> texture = new List<Texture2D>();
texture.Add(content.Load<Texture2D>(“circle”));
texture.Add(content.Load<Texture2D>(“star”));
texture.Add(content.Load<Texture2D>(“diamond”));
particleEngine = new ParticleEngine(texture, new Vector2(400,200));

update()
particleEngine.EmitterLocation = new Vector2( Mouse.GetState().x, Mouse.GetState().y);
particleEngine.Update();

Draw()
particleEngine.Draw(spriteBatch);

My workstation

I am currently using a Dell Laptop which I got it from an IT sale at a pretty good rate. It’s not the most powerful machine but it does the job for me. The 17 inch LCD monitor was reused from my family’s spoiled desktop. Again It’s not the most beautiful LCD but it does help me a lot especially when I need to refer to multiple windows.

The lamp was given to me from my boss when he closed down his company operation in Singapore. It was bought from Ikea at around s$50. I like how the lamp creates a studio-like environment but it can get the room slightly warm.

I have two board facing me. A white board and a magnetic board below. I will break down projects into smaller components and write them down onto a post it notes and paste it on the boards. This will give me a better overview of what has been done and what’s not. Once done I will shift them.  Seeing the post it going from one side to another give me a great sense of accomplishment.

On my table, under the glass, I have pictures and notes that I will occasionally read them(especially when I am bored) to motivate me to work hard.

Tagged , , , , ,

Simple Game

I am having my vacation now. Other than my school game project, I am working on a mini game with a friend who is studying computer science in Nanyang Technological University (NTU). He’s not train in game programming, so we have a bit to work on. Using my previous experience with game engine development we are going to make a new game engine. As I spent my whole of last semester working mainly on the graphic engine,  I will take this opportunity to work on other parts of the engine. Game Design will be simple.

In this project we will be using google code for storing our code,  tortoise SVN, visual studio 2010 and directX 9.

 

 

Tagged , , , ,
Follow

Get every new post delivered to your Inbox.