snowing

Posted By: Pappenheimer

snowing - 11/24/10 11:37

Here is a simple snowing script.
It is a demo with a simple black box getting more and more covered with white dots.
snowing demo
The dots are very small (it depends on the size of the skin), so I want to add additional painting around the dots, but when I use the following part, it shows weird effects and eventually crashes. Maybe, someone has an idea to solve this issue?
Thanks in advance.
Code:
while(coords_cur_x <= (coords_x + coords_area))
			{
				pixel_to_bmap(canvas,coords_cur_x, coords_cur_y,pixel);
				coords_cur_y += 1;
				if(coords_cur_y > (coords_y + coords_area))
				{
					coords_cur_x += 1;
					coords_cur_y = coords_y - coords_area;
				}
			}



This is the whole script.
Code:
#include <litec.h>
#include <d3d9.h>

#include <acknex.h>
#include <default.c>

#define PRAGMA_ZERO

BMAP* canvas;

var format; 
var pixel;
var coords_x;
var coords_y;

function snowing()
{
	while(1)
	{
		c_trace(vector(random(30)-15,random(30)-15, 100), vector(random(30)-15,random(30)-15, -100), IGNORE_PASSABLE | IGNORE_ME | SCAN_TEXTURE | ACTIVATE_SHOOT);
		wait(1);				
	}	
}

function snowEvent()
{
	coords_x = hit.u1;
	coords_y = hit.v1;
}

action get_snow()//needs an own variable to prevent it from colliding with similar tarrain actions
{
	set(my, POLYGON);
 	my.emask |= ENABLE_SHOOT;
  	my.event = snowEvent;
	while(1)
	{
//		if(mouse_left)
//		{
//			while(mouse_left){wait(1);}
//			coords_x = random(10);
//			coords_y = random(10);
			var coords_area = 1;
			var coords_cur_x = coords_x - coords_area;
			var coords_cur_y = coords_y - coords_area;
			canvas = bmap_for_entity(my, 0);	//1 is the number of the skin
	
			format = bmap_lock(canvas, 0); // lock the canvas bitmap
			pixel = pixel_for_vec(vector(255,255,255), 100, format);
			pixel_to_bmap(canvas, coords_x, coords_y, pixel); // now write the pixel at the coords_x, coords_y position
//			while(coords_cur_x <= (coords_x + coords_area))
//			{
//				pixel_to_bmap(canvas,coords_cur_x, coords_cur_y,pixel);
//				coords_cur_y += 1;
//				if(coords_cur_y > (coords_y + coords_area))
//				{
//					coords_cur_x += 1;
//					coords_cur_y = coords_y - coords_area;
//				}
//			}
			bmap_unlock(canvas); // unlock the bitmap now
			bmap_to_mipmap(canvas);
//		}
		wait(1);
	}
}

function main()
{
	level_load(NULL);
	camera.z = 30;
	camera.tilt = -90;
	ent_create("cube_black.mdl", vector(0,0,0), get_snow);
	snowing();
}


Posted By: fogman

Re: snowing - 11/24/10 12:18

The only thing that comes to my mind:
coords_x & coords_y is not initialized.
So at gamestart they can theoretically hold any number.
Maybe try this:
var coords_x = 0;
var coords_y = 0;
Posted By: Pappenheimer

Re: snowing (pixel_to_bmap problem) - 11/24/10 19:09

The problems must relate to the while(coords.....)-loop..., because coords_x & coords_y immediately get their values.
Posted By: fogman

Re: snowing (pixel_to_bmap problem) - 11/24/10 19:16

Are you really sure?
The manual @ ent_create: "The given action will start immediately after creation"

But coords_x & coords_y get their values afterwards,
because you are calling "snowing()" after the creation.

But itīs really a raw guess, not more.


Posted By: flits

Re: snowing (pixel_to_bmap problem) - 11/24/10 19:23

the problem is i gues that you paint on a pixel that is lower then0 or higher then amount of pixel of the bmap that wil result into a crash
Posted By: Pappenheimer

Re: snowing (pixel_to_bmap problem) - 11/24/10 20:00

flits is right. I found that hint in this thread Pixelwerte???

Here is the working code:
Code:
#include <litec.h>
#include <d3d9.h>

#include <acknex.h>
#include <default.c>

#define PRAGMA_ZERO

BMAP* canvas;

var format; 
var pixel;
var coords_x = 0;
var coords_y = 0;

function snowing()
{
	while(1)
	{
		c_trace(vector(random(30)-15,random(30)-15, 100), vector(random(30)-15,random(30)-15, -100), IGNORE_PASSABLE | IGNORE_ME | SCAN_TEXTURE | ACTIVATE_SHOOT);
		wait(1);				
	}	
}

function snowEvent()
{
	coords_x = hit.u1;
	coords_y = hit.v1;
}


action get_snow()
{

	var coords_area = 4;
	var coords_cur_x = coords_x - coords_area;
	var coords_cur_y = coords_y - coords_area;
	set(my, POLYGON);
 	my.emask |= ENABLE_SHOOT;
  	my.event = snowEvent;
	while(1)
	{
			coords_cur_x = coords_x - coords_area*0.5;
			coords_cur_y = coords_y - coords_area*0.5;
			canvas = bmap_for_entity(my, 0);	//1 is the number of the skin
	
			format = bmap_lock(canvas, 0); // lock the canvas bitmap
			pixel = pixel_for_vec(vector(255,255,255), 100, format);
			pixel_to_bmap(canvas, coords_x, coords_y, pixel); // now write the pixel at the coords_x, coords_y position
			while(coords_cur_x <= (coords_x + coords_area))
			{
				if((coords_cur_x < canvas.width ) && (coords_cur_y < canvas.height))
				{
					if((coords_cur_x > 0) && (coords_cur_y > 0))
					{
						pixel_to_bmap(canvas,coords_cur_x, coords_cur_y,pixel);
					}
				}
				coords_cur_y += 1;
				if(coords_cur_y > (coords_y + coords_area))
				{
					coords_cur_x += 1;
					coords_cur_y = coords_y - coords_area;
				}
			}
			bmap_unlock(canvas); // unlock the bitmap now
			bmap_to_mipmap(canvas);
		wait(1);
	}
}

function main()
{
	level_load(NULL);
	camera.z = 30;
	camera.tilt = -90;
	ent_create("cube_black.mdl", vector(0,0,0), get_snow);
	snowing();
}



This was the required addition:
Code:
if((coords_cur_x < canvas.width ) && (coords_cur_y < canvas.height))
{
	if((coords_cur_x > 0) && (coords_cur_y > 0))
	{
[...]
	}
}


Posted By: Pappenheimer

resolved and result: snowing (pixel_to_bmap) - 11/24/10 23:21

I changed the code a bit.
Now a function cycles through all given entities of a level to add the polygon flag and the snow event.
Just in case that someone can use this, here is a small demo with the script included:
snowing demo
Thanks for your interest and help! laugh
© 2024 lite-C Forums