Lite-C Libraries

Top  Previous  Next

This month's plug and play snippet is different; it shows several examples that make heavy use of the existing lite-C libraries. In fact, these libraries are perfect for the "plug and play" section of the magazine, because you only have to include them, and then you have access to a lot of pre-written code that will greatly simplify your job.


Copy the \pnplibs folder inside your Gamestudio folder, and then open and run pnplibs.c. I wanted to keep the code as simple as possible, so I didn't include a player action; this means that you will have to press the zero key once, and then use the arrow keys and Page Up / Page Down to move and look around.


Let's start with function main:


function main()


       camera.ambient = 70;

       fps_max = 70;

       video_mode = 7; // run in 800x600 pixels

       video_depth = 32; // 32 bit mode

       video_screen = 1; // start in full screen mode

       level_load (pnplibs_wmb);

       wait (2);

       ent_createlayer("skycube+6.tga", SKY | CUBE | SHOW, 1);        


       // splits an image file horizontally and vertically into several parts and stores them as separate files under the name "splitnm.bmp"

       bmap_split("map.bmp", 2, 3); // split map.bmp into 2x3 = 6 smaller pieces, naming them split00.bmp... split21.bmp



This looks like a standard main( ) function, but take a look at the last line: it calls a bmap_split function which is located inside the bmap.c library and splits a bitmap into the desired number of pieces.


aum102_pnp2 aum102_pnp3 aum102_pnp4

aum102_pnp5 aum102_pnp6 aum102_pnp7


Don't forget to include bmap.c in your main script; otherwise, you won't have access to the functions inside it. Remember to do this for all the libraries that you plan to use.


Let's move on to a new library: entmove.c, a library that contains many entity-related functions. I chose to use a path patrolling function from within the library; take a look at the code and you'll see how easy it is to use.


action patroller()


       var walk_percentage;

       // let an entity move along the specified path, with a speed = 4, looking ahead

       ent_movepath(my, "path_000", 4, 2);

       while (1)


               walk_percentage += 6 * time_step;

               ent_animate(my, "walk", walk_percentage, ANM_CYCLE); // play the "walk" animation in a loop

               wait (1);




Actually, the only line of code that is needed is this one:


ent_movepath(my, "path_000", 4, 2);


Yes, this line alone will move the entity on a path; the rest of the code was added by me because I wanted the guard model to be animated as well.




Moving on to a fresh library named mtlFX.c; it contains lots of shaders that can be tested by simply changing the name of the chosen material - here's a quick example.


action test_material()


       my.material = mtl_specBump;

       while (1)


               my.pan += 2 * time_step;

               wait (1);        






The library comes with a great variety of materials: environment mapping, bump mapping, parallax mapping, chrome, glass, etc. Let's check out the particles.c library, which will speed up the process of creating particle effects significantly.


function snow_startup()


       var number_of_flakes = 150; // sets the number of flakes

       wait (-10); // wait for 10 seconds before starting the snow



               effect(p_snow, number_of_flakes * time_frame, vector(camera.x + 400 - random(800), camera.y + 400 - random(800), camera.z + 100), vector(-5, 2, 0));





I hope that you like my simple, and yet fully functional snow effect; it uses the p_snow library function that is included inside particles.c




The last example uses the strio.c library, which contains string and file-related functions.


STRING* temp_str = "This is a great way to replace words in a way that is way beyond imagination!";


function replace_startup()


       while (!key_r) {wait (1);}

       str_replaceall(temp_str, "way", "mode");



TEXT* string_txt =


       pos_x = 20;

       pos_y = 20;


       flags = SHOW;



This time I am using the str_replaceall function, which replaces all the occurrences of the word "way" inside temp_str with "mode". Here's how the text looks like initially:




And here's the same string after a "R" key press which has run str_replaceall( ):




As you can see, all the code I've added is only used to make the things more user-friendly; the actual word replacement code consists of a single line. And I am sure that many of you would have been able to write functions like these ones, but why reinvent the wheel when there's no need to do that?


I couldn't cover all the functions from all the libraries here, but you are encouraged to do so: you can find them all inside Gamestudio's \include folder. I hope that by now you have realized the power that is hidden behind the existing lite-C libraries; I have to admit that I didn't use them too much until now, but this will definitely change from now on - I hope you'll do the same.