KonsolScript:Tutorials:Buffering

From GPWiki
Jump to: navigation, search

Buffering

Buffering is a term when you do a buffer. ^_^ I don't care what's the real technical meaning of buffer. What I do understand is that when you do buffer, you are actually hiding all the messy preparation of whatever you need to prepare. Like when mom cooks a good-looking, special, yummy meal -- who cares what she has got into while cooking the meal (eww...). ^_^ All you care about is the food.

Same goes to game programming and the people who will play your games. So before lurking deeper inside the game programming world, get armed by the knowledge of buffering.

In KonsolScript, all types of buffers are handled thru a Number variable. Then further processing to the buffer is handled thru the Class of buffer it belongs. So every time you try to use any type of buffer, remember to declare a Number variable for it.

Var:Number myXYZbuffer;

Something like the one above should do the trick.

File Buffering

It's always good to make a game that will have a saving feature. In an RPG game, this is a must. When a programmer needs to manipulate a file, he needs to load it in a place to do a buffering. KonsolScript can buffer a text file with File class.

//how to buffer a file...
function main() {
  Var:Number myFileBuffer;
  File:Open("hello_file.txt", WRITE, myFileBuffer)
  File:Write("Test print on my file buffer", myFileBuffer)
  File:Close(myFileBuffer)
}

The code above demonstrates how to create a file buffer then writes on it.

  Var:Number myFileBuffer;
  File:Open("hello_file.txt", WRITE, myFileBuffer)

Once we have declared a Number variable to be our buffer, we can use File class' Open function and access "hello_file.txt" for writing.

  File:Write("Test print on my file buffer",myFileBuffer)

Next, using the file buffer that we opened, we can start writing a "Test print on file buffer" for demonstration purposes.

  File:Close(myFileBuffer)

After that, we can close the file buffer using File:Close.

Tip: See File class documentation for further reading.

Image buffering

In graphics programming, a programmer loads a resource file into the memory and do the necessary preparations. KonsolScript can buffer an image file with Image class.

//how to buffer an image file...
function main() {
  Var:Number myImageBuffer, hgt;
  Image:Load("hello_image.bmp", myImageBuffer)
  Image:GetHeight(myImageBuffer, hgt)
  Konsol:Log("height is " + hgt)
}

The code above demonstrates how to load an external BMP file resource into our image buffer. This will not do anything special. It will simply get the height of the loaded BMP image buffer. Let's have a closer look at it.

  Var:Number myImageBuffer, hgt;

First we need to declare a Number variable to handle our image buffer. We also declared another variable, hgt, to store the "hello_image.bmp"'s height.

  Image:Load("hello_image.bmp", myImageBuffer)

Provided that a "hello_image.bmp" is existing, this command won't have any error. This image file will be loaded into our image buffer.

  Image:GetHeight(myImageBuffer, hgt)

Using the image buffer, we can get the height using GetHeight function of Image class.

  Konsol:Log("height is " + hgt)

Finally, we log the retrieved height value.

Tip: See Image class documentation for further reading.

Sound Buffering

Even a simple game needs to have a background music or sound effects. That's where sound programming comes in. A sound buffer can be made with Sound class. A separate tutorial is also written for Sound class which can be read here too.