Post Reply 
Xmas Spirit?
12-25-2023, 07:51 AM (This post was last modified: 12-25-2023 10:08 AM by komame.)
Post: #5
RE: Xmas Spirit?
(12-24-2023 10:28 PM)matalog Wrote:  Wow, that's a nice fire :-).

Thanks :-)

(12-24-2023 10:28 PM)matalog Wrote:  Did you experiment with making the fire pixels smaller, does it affect the overall speed of the program too much?

Yes, I experimented with increased resolution, but I encountered performance issues even on the G2 (not to mention the G1, where it was a disaster). Later, I noticed an error in your program that significantly affected performance, and after correcting it, I managed to more than double the number of pixels for the fire effect (exactly 240%) while maintaining the same FPS. Your mistake was that in the loop where you draw "baubs", you were also drawing the fireplace, so it was being drawn as many times as there were "baubs". It was a considerable waste of time in each frame of the animation. After correcting this, I achieved better performance in generating the fire. Not only did I increase the resolution, but I also improved the realism of the fire. Unfortunately, on the G1, I was unable to achieve much better effects, and it is necessary to stick with a resolution of 12x10 to keep the animation smooth. The program after my revisions is below (for G2), but before you paste it into Connectivity Kit, please read the "EDIT" comment at the bottom of the post.

Code:
CONST width=18, height=15;
LOCAL buffer:=MAKEMAT(0, height, width);

EXPORT Ctree()
BEGIN 
 DIMGROB_P(G1,320,240,1);
 DIMGROB_P(G2,320,240,1);
 DIMGROB_P(G3,320,240,1);
 DIMGROB_P(G4,320,240,RGB(255,0,0));
 DIMGROB_P(G5,320,240,RGB(255,0,0));
 DIMGROB_P(G6,320,240,RGB(255,0,0));
 DIMGROB_P(G7,width,height,0);
 LOCAL BAUBS;
 LOCAL BX={},BY={};
 LOCAL X,Y;
 LOCAL ox=-18;  //  X-AXIS OFFSET OF TREE
 LOCAL COL;
 LOCAL GO;
 LOCAL T;

 HAngle:=0;
 RECT_P(0);   //   BACKGROUND CLEAR
 LINE_P(200,160,140,240,RGB(180,60,0));  //   FIREPLACE LINE L
 LINE_P(290,160,320,210,RGB(180,60,0));  //  FIREPLACE LINE R
 TRIANGLE_P(120+ox,50,95+ox,92,145+ox,92,RGB(0,255,0));     //  TREE TOP
 TRIANGLE_P(120+ox,60,80+ox,130,160+ox,130,RGB(0,255,0));   //  TREE 2
 TRIANGLE_P(120+ox,90,70+ox,170,170+ox,170,RGB(0,255,0));   //  TREE 3
 TRIANGLE_P(120+ox,130,60+ox,200,180+ox,200,RGB(0,255,0));  //  TREE BOTTOM
 RECT_P(110+ox,201,130+ox,225,RGB(150,40,10)); //  TREE TRUNK
 RECT_P(200,80,290,160,RGB(255,0,0)); //   FIRE-CENTRE
 REPEAT
  X:=RANDINT(290,200);
  Y:=RANDINT(200,130);
  A:=X-245;
  B:=Y-160;
  IF SQRT(A^2+B^2)<34 AND Y<150 THEN
   ARC_P(G5,X,Y,2,0,2*π,{0,0});         //  COALS
   T:=T+1;
  END;
 UNTIL T=200;
 BLIT_P(G4,G5,RGB(255,0,0));
 BLIT_P(G5,RGB(255,0,0));
 BLIT_P(G1,G0);
 BLIT_P(G2,G0);

 REPEAT
  X:=RANDINT(50+ox,165+ox);
  Y:=RANDINT(30,220);
  GO:=1;

  IF BAUBS>0 THEN
   FOR T FROM 0 TO BAUBS DO
   A:=X-BX(T);
   B:=Y-BY(T);
   IF SQRT(A^2+B^2)<14 THEN
    GO:=0;
   END;
  END;
 END;

 IF BAUBS=0 THEN
  GO:=1;
 END;

 IF GO=1 AND GETPIX_P(X,Y)=RGB(0,255,0) THEN
  BAUBS:=BAUBS+1;
  BX(BAUBS):=X;
  BY(BAUBS):=Y;
  ARC_P(BX(BAUBS),BY(BAUBS),5,0,2*π,{0,0});
  //WAIT(0.3);
  END;
 UNTIL BAUBS=32;

 BLIT(G1,G2);

 T:=TICKS;
 REPEAT
  WHILE TICKS<T DO
  END;
  T:=T+80;
  RECT_P(G6,RGB(RANDINT(70,35),RANDINT(35,1),0));     //   FIRE-SHINE
  BLIT_P(G6,G5,RGB(255,0,0));
  FOR X FROM 0 to BAUBS-1 DO
   COL:=RGB(RANDINT(255,0),RANDINT(255,0),RANDINT(255,0));
   ARC_P(G1,BX(X),BY(X),5,0,2*π,{COL,COL});
  END;
  RECT_P(G1,200,80,290,160,RGB(RANDINT(200,255),RANDINT(0,200),RANDINT(0,20))​);
  BLIT_P(G1,G5,RGB(255,0,0));
  BLIT_P(G6,G1,0);
  FIRE();
  BLIT_P(G6);
 UNTIL ISKEYDOWN(4)=1;

END;

FIRE()
BEGIN
 LOCAL intensity;
 LOCAL x,y;
 buffer := REPLACE(buffer,{height,1},RANDMAT(1,width,255));
 FOR y FROM 2 TO height DO
  FOR x FROM 1 TO width DO
   intensity := IP((buffer(y, x) 
                 + buffer(y, IFTE(x=1, width, x-1)) 
                 + buffer(y, IFTE(x=width, 1, x+1))
                 + buffer(IFTE(y+1 > height, y, y+1), x)) / 4);
   intensity := MAX(0, intensity - RANDINT(0, 20));
   buffer(y-1, x) := intensity;
   CASE
    IF intensity > 160 THEN
     PIXON_P(G7,x, y, RGB(255, 255, intensity));
    END;
    IF intensity > 84 THEN
     PIXON_P(G7,x, y, RGB(255, intensity, 0));
    END;
    PIXON_P(G7,x, y, RGB(intensity*3, 20, 0));
   END;
  END;
 END;  
 BLIT_P(G6,210,90,280,152,G7,1,2,width,height,#FFFFFF,223);
END;

It looks quite nice, doesn't it?

I think that the performance can still be slightly improved because currently in your code, the function ARC_P (which is rather slow) is called 32 times in each iteration and it could be replaced with BLIT_P by referring to the buffered images that were drawn during the program initialization. This should provide an additional 15-20% gain in performance. Unfortunately, the biggest problem is that in calculations on matrices and lists, where you have to iterate over elements, PPL is very weak, and even such a small matrix as 12x10 calculated several times per second eats up all the power of this device. For such tasks, even much slower devices perform much better. This is a very annoying flaw of PPL. In Python, it looks much better. I can try to rewrite this fire effect-generating function in Python and call it from PPL. Then it will be possible to achieve an even better effect.

EDIT: When you paste this program into Connectivity Kit, there's a strange issue because Prime reports a syntax error on line 87:
RECT_P(G1,200,80,290,160,RGB(RANDINT(200,255),RANDINT(0,200),RANDINT(0,20))​);
You can see that after pasting, an additional space (blank character) appears before the last parenthesis on the Prime. This is probably a copy-paste bug in Connectivity Kit. To get the program to compile, you need to remove that space directly on the Prime. I've pasted this program many times into this post, but this error always occurs after copying it into Connectivity Kit, so remember to delete that blank character.

Best wishes,
Piotr
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Xmas Spirit? - matalog - 12-22-2023, 09:26 PM
RE: Xmas Spirit? - DéFCoM - 12-23-2023, 06:37 AM
RE: Xmas Spirit? - komame - 12-24-2023, 03:38 PM
RE: Xmas Spirit? - matalog - 12-24-2023, 10:28 PM
RE: Xmas Spirit? - komame - 12-25-2023 07:51 AM
RE: Xmas Spirit? - matalog - 12-25-2023, 03:13 PM
RE: Xmas Spirit? - komame - 12-26-2023, 07:39 AM
RE: Xmas Spirit? (fire effect) - komame - 12-30-2023, 04:04 PM
RE: Xmas Spirit? - komame - 12-31-2023, 10:59 AM
RE: Xmas Spirit? - tjurij - 01-02-2024, 05:46 AM
RE: Xmas Spirit? - komame - 01-02-2024, 06:39 AM
RE: Xmas Spirit? - StephenG1CMZ - 01-03-2024, 12:16 AM



User(s) browsing this thread: 1 Guest(s)