#include <acknex.h>
#include <default.c>
//bitmap to be generated
BMAP* map;
//tile bmaps
BMAP* tile1 = "plain001.tga";
BMAP* tile2 = "plain002.tga";
//display for our bitmap
PANEL* viewer =
{
pos_x = 5;
pos_y = 5;
layer = 10;
alpha = 100;
flags = SHOW | TRANSLUCENT;
}
//small utility function to ensure vector components are smaller than a certain value
void vec_trunc(var *vec, var limit)
{
vec[0] = minv(vec[0], limit);
vec[1] = minv(vec[1], limit);
vec[2] = minv(vec[2], limit);
}
//adds feature bitmap to the map at the (X,Y) position. you have to manually lock and unlock bitmaps
void add_bmap(var X, var Y, BMAP *map, var map_format, BMAP *feature, var feature_format)
{
var temp_map_pixel[3];
var temp_feature_pixel[3];
var temp_map_alpha;
var temp_feature_alpha;
var i, j;
for(i = 0; i < bmap_width(feature); i++)
{
for(j = 0; j < bmap_height(feature); j++)
{
//ensure we stay within main bitmap bounds to prevent engine crashing
if(X+i >= bmap_width(map) || Y+j >= bmap_height(map)) continue;
//read map pixel
pixel_to_vec(temp_map_pixel, temp_map_alpha, map_format, pixel_for_bmap(map, X+i, Y+j));
//read feature pixel
pixel_to_vec(temp_feature_pixel, temp_feature_alpha, feature_format, pixel_for_bmap(feature, i, j));
//color mixing in 3 steps
//1.multiply feature pixel's color with normalized alpha
vec_scale(temp_feature_pixel, temp_feature_alpha/100);
//2.add resulting color to the map pixel's color
vec_add(temp_map_pixel, temp_feature_pixel);
//3.check that resulting vector components stay within 0-255 range
vec_trunc(temp_map_pixel, 255);
//write the resulting color to the map
pixel_to_bmap(map, X+i, Y+j, pixel_for_vec(temp_map_pixel, temp_map_alpha, map_format));
}
}
}
void main()
{
level_load(NULL);
wait(3);
//create a blank map
map = bmap_createblack(512, 512, 32);
bmap_fill(map, vector(1,1,1), 100);
viewer.bmap = map;
var i;//our index var
//lock main bitmap
var map_format = bmap_lock(map, 0);
//lock first feature bitmap - called only once instead of locking\unlocking bitmap every time
var feature_format = bmap_lock(tile1, 0);
//add 20 features at random positions
for(i = 0; i < 20; i++)
{
add_bmap(random(bmap_width(map)), random(bmap_height(map)), map, map_format, tile1, feature_format);
}
//we're done with first feature - can unlock it's bitmap
bmap_unlock(tile1);
//do the same routine for the second feature type
var feature_format = bmap_lock(tile2, 0);
for(i = 0; i < 20; i++)
{
add_bmap(random(bmap_width(map)), random(bmap_height(map)), map, map_format, tile2, feature_format);
}
bmap_unlock(tile2);
//unlock main bitmap
bmap_unlock(map);
}