
sin = Math.sin;
random = Math.random;
floor = Math.floor;



function game(){
	SetFrameRate(60);

	var PPP = LoadImage("particle.png");


   var PSC = CreateParticleSystemChild(200); // create a child with a capacity of 300
   var TitleText = CreateSurface(100,50, CreateColor(0,0,0,0));
   
   TitleText.drawText(GetSystemFont(),1,1,"Sphere Demo!");
   //surface.drawZoomedText(font_obj,x,y,scale,text)
   TitleText = TitleText.createImage();
   
   var Gradient = LoadImage("gradient.png");
   PSC.body.x = 0;
   PSC.body.y = 100;

// our 'particle.png' features transparencies, but when using blend, we need these to be zero, so 
// they do not add to the background. So we blend our image on a black canvas, and use the result.
OnCanvas = function(img, color, mode){
	var canvas = CreateSurface(img.width, img.height, color);
	canvas.setBlendMode(mode || BLEND);
	if(img.toString() == "[object image]")
		canvas.blitSurface(img.createSurface(), 0, 0);
	else
		canvas.blitSurface(img, 0, 0);
	return canvas;
}


   var i_t = LoadImage("particle.png");
   var s_t = OnCanvas(i_t, CreateColor(0,0,0,255));

   PSC.swarm.renderer.texture = s_t.createImage();
   //PSC.swarm.renderer.texture = LoadImage("flara2.png");
   
   PSC.swarm.renderer.blend_mode = ADD
   PSC.initializer.setAgingParams({min:0.05,max:0.09});
   PSC.initializer.setPosParams({shape: PS_SHAPE_RECTANGLE,
                                     x: {min: 0,   max: 320 },
                                     y: {min: 0,   max: 30}});
   PSC.initializer.setVelParams({angle: {min: 0,   max: 2*Math.PI },
                                     a: {min:-0.3, max: 0.3},
                                     b: {min:-1,   max: 1}});
   PSC.callback.on_update = function()
   {
      if (this.swarm.size() < this.swarm.capacity() && !this.isDead())
        this.swarm.grow();
   }

	var c = 0;
	var BG = LoadSurface('ringt.jpg');
	BG.setBlendMode(ADD);
	var B = BG;
	var FLARE = LoadSurface('flara2.png');
	TEXT = "                                            Welcome Sphere user! "
		+ "This demo has different effects, most are rotation, blit, masking and using sin() to loop them. You can look at the code if you want to learn how this all is done."
		+ "                                                       "
	r=0;
	L = {
		A:{ x:0, y:0, vx:1.1, vy:0.6},
		B:{ x:GetScreenHeight(), y:GetScreenWidth(), vx:-0.9, vy:-2},
		update : function(P){
			P.x += P.vx; 
			if(P.x <0) P.vx = random(1);
			if(P.x >GetScreenWidth()) P.vx = -random(1);
			P.y += P.vy;
			if(P.y <0) P.vy = random(1);
			if(P.y >GetScreenHeight()) P.vy = -random(1);
		}
	
	}
	var bx1=by1=0;
	var bx2=by2=40;
	var nextcount=GetTime();
	var count = 0;
	var font = GetSystemFont();
	var rotateletters = true;
	var base = 0; //1st character of scroll

	while(!IsAnyKeyPressed()){
ApplyColorMask(CreateColor(1,1,1))
		c = 20*sin(GetTime()*0.001);
		c2 = 20*sin(GetTime()*0.002);

		r += c*c/10000;

	    	B = BG.clone();
		B.blitSurface(FLARE, L.A.x-(FLARE.width>>1)-c2-30, L.A.y-(FLARE.height>>1));
		B.blitSurface(FLARE, L.B.x-(FLARE.width>>1)-c2-30, L.B.y-(FLARE.height>>1));

	    	B.setBlendMode(BLEND);
		//B.blitSurface(TEXTURE.fence, 0,0);
		B.blit(c2+30,0);
		BezierCurve(CreateColor(50,255,255), 0.001, L.A.x, L.A.y, c2, 40-c, L.B.x, L.B.y);
		rand0 = floor(random()*100)+50;

		
		//Dosify the scrolltext's speed.
		if ((nextcount+10)<GetTime()) {
			++count;
			nextcount=GetTime();
		}
 
		if (count == 8)
		{
			++base;
			count = 0;
			if ( base > TEXT.length-40 )
				base=0;
		}

  
		var gt = GetTime()/200;
		rash = (GetTime()>>5) % 40;
		for(var i=0;i<40;i++)	//Waving text on the lower part of the screen
		{
			if (i == rash) 
				font.setColorMask(CreateColor(255,255,255,255));
			else if ( (i==rash-1) || (i==rash+1) )
				font.setColorMask(CreateColor(255,255,255,155));
			else
				font.setColorMask(CreateColor(255-(40-i)*6,255-i*6,40+i*6,255));
	
			if (rotateletters) {
				img = font.getCharacterImage( TEXT[i+base].charCodeAt(0) );
				img.rotateBlitMask(10+i*8-count,210+floor(Math.sin(gt+i*19)*18), -Math.sin( gt+i*19 - (3.14/4) )/2 , font.getColorMask() );
			} else
				font.drawText(10+i*8-count,210+floor(Math.sin(gt+i*19)*18) ,TEXT[i+base]);
  		}

		font.setColorMask(CreateColor(255,255,255,255));	//Restore color
 
      PSC.update(); 
      PSC.render();
//      Gradient.blit(0,0,SUBTRACT);
      TitleText.zoomBlit(120 - TitleText.width*(Math.sin(gt/4)/2),120,1+Math.sin(gt/4),SUBTRACT);


      FlipScreen();
		L.update(L.A);
		L.update(L.B);		

	}

}

