Burning fire effect

This is a discussion on Burning fire effect within the C Programming forums, part of the General Programming Boards category; My professor in our CSI02 (Computer Programming 1) told us that in our Final Laboratory Exam, we are going to ...

  1. #1
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    Burning fire effect

    My professor in our CSI02 (Computer Programming 1) told us that in our Final Laboratory Exam, we are going to make a burning effect program.
    Here's the position of flames:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <graphics.h>
    #include <dos.h>
    #include <stdlib.h>
    int main() {
    
    	int driver = DETECT, mode;
    	initgraph(&driver, &mode, "c:\\tc\\bgi");
    	int maxy = getmaxy(), midy = getmaxy() / 2;
    	int maxx = getmaxx();
    	int x;
    	setcolor(YELLOW);
    	while(!kbhit()) {
    		for(x = 0; x <= maxx; x++) {
    			outtextxy(10, midy - 20, "Supposed to be they are flames. My program is not animated but I just want to denote where the flames should set in.");
    			outtextxy(10, midy - 10, "indicate where the flames should set in.");
    			line(x, maxy, random(maxx), midy);
    			delay(10);
    		}
    		x = 0;
    		cleardevice();
    	}
    	getch();
    	closegraph();
    	return 0;
    }
    My professor showed us his demo and the said program has an approx. 420 lines of code and there are some asm codes embedded on it.

    Please anyone... If you have a burning effect program post it in here so that I can study it in advance before our finals.
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  2. #2
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    Sorry, I forgot to say hello...
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Your professor is teaching you how to program in an ancient compiler

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    Hi Alex,

    From your code i figure that your using "Turbo C/C++" under DOS and your "fire program" uses the standard Borland Graphics Interface(BGI) for 2d drawing.

    I also have that compiler/IDE and it happens to have some very nice documentation so i would suggest you should read that.

    In case you dont know, you can press ctrl F1 under any function to list help on it's definition and an example demonstrating the function in action.

    Some functions you might want to look up are, putpixel(), rand() and I dont think this program is as complicated as you think, you just need to "putpixels" and "clearpixels" at random co-ordinates which should form the upper end of your fire, while your lower end could remain static
    Thanks,
    Sahil

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Well, I dont have Your program.But I made a simple program two years back,which i think have same effects as your program needs.
    Attached Files Attached Files
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    @ cbastard
    Thank you. Anyway, your code for your revolution effect is quit difficult for me...
    Code:
    while(!kbhit())
    {  for(int i=10;i<=20;i++)
         { doggy();
           int tail[]={370-4*sin(i),215+4*cos(i),370+4*sin(i),215-4*cos(i),370+30*cos(i)+4*sin(i),215+30*sin(i)-4*cos(i),370+30*cos(i)-4*sin(i),215+30*sin(i)+4*cos(i)};
           sector(259,255,180,360,6,i);
           circle(259,255+3*i,3);
           fillpoly(4,tail);
           delay(50);
           cleardevice();
          }
    }
    As a beginner, I'm coding this way...
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <graphics.h>
    #include <dos.h>
    #include <math.h>
    
    /* A simple clock for cosine and sine representation */
    int main() {
    
    	int driver = DETECT, mode;
    	initgraph(&driver, &mode, "c:\\tc\\bgi");
    	int midy = getmaxy() / 2;
    	int midx = getmaxx() / 2;
    	int x, y, deg, r = 200;
    	float theta;
    
    	for(deg = -90; deg <= 270; deg += 6) {
    		theta = (deg * 3.1416) / 180;
    		x = r * cos(theta);
    		y = r * sin(theta);
    		outtextxy(midx - 8, midy - 210, "12");
    		outtextxy(midx + 210, midy - 5, "3");
    		outtextxy(midx - 3, midy + 210, "6");
    		outtextxy(midx - 210, midy - 5, "9");
    		circle(midx, midy, 220);
    		circle(midx, midy, 10);
    		line(midx, midy, midx + x, midy + y);
    		delay(1000);
    		if(deg == 270) deg = -90;
    		if(kbhit()) {
    			closegraph();
    			return 0;
    		}
    		cleardevice();
    	}
    	getch();
    	closegraph();
    	return 0;
    }
    I'm using that way for a more trigonometric function which is easy for me to understand... thank you cbastard for giving me sector function.
    In addition to my concern, my professor told me that the program: burning fire uses a color pallete through an array declaration. I dont think I have to learn assembly language anyway.
    Last edited by Alexthunder; 11-08-2005 at 09:26 PM.
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  7. #7
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    Unhappy

    Quote Originally Posted by sahil_m
    Hi Alex,

    From your code i figure that your using "Turbo C/C++" under DOS and your "fire program" uses the standard Borland Graphics Interface(BGI) for 2d drawing.

    I also have that compiler/IDE and it happens to have some very nice documentation so i would suggest you should read that.

    In case you dont know, you can press ctrl F1 under any function to list help on it's definition and an example demonstrating the function in action.

    Some functions you might want to look up are, putpixel(), rand() and I dont think this program is as complicated as you think, you just need to "putpixels" and "clearpixels" at random co-ordinates which should form the upper end of your fire, while your lower end could remain static
    Thank you. Yes I'm using Turbo C in DOS. I do ctr+f1 If i need help. I just want to know what are the functions I need to learn by posting it in here like your putpixel, rand() etc. and thank you for that. Please every body, if you have a burning effect fire program, help me...
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  8. #8
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > Yes I'm using Turbo C in DOS.
    Are you an archaeology student?
    Learning a bunch of stupid tricks for a fossil compiler and OS will do more harm than good when it comes to get a real programming job, using a real compiler running on a real OS.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    A fire effect can be achieved quite easily. This can be ported to Direct3D or OpenGL quite easily with pixel shaders and/or fixed function pipeline operations (although somewhat slower).
    The fire effect can be found in Special Effects Game Programming with DirectX by Mason McCuskey.

    I apologize in advance but I only have a BASIC demo of the technique at present. I altered the algo a bit to create more and more fires all burning in different places. This example is only 16-bit color. Expand to suit your needs.
    Code:
    DECLARE SUB AddSpecificHeat (x%, dist%, hvalue%)
    DECLARE SUB CoolRoot (value%)
    DECLARE SUB DrawFire (x%, y%, scale%)
    DECLARE SUB MoreFire ()
    DECLARE SUB UpdateFire (cooldown%)
    DECLARE SUB CreateNewFire ()
    DECLARE SUB SpawnFires (p%)
    DECLARE SUB anykey ()
    'Fire test
    
    DEFINT A-Z
    
    CONST firex% = 30
    CONST firey% = 30
    
    
    RANDOMIZE VAL(MID$(TIME$, 7, 2))
    DIM SHARED fire(1 TO firey%, 1 TO firex%)
    
    
    FOR i% = 1 TO firex%
      FOR j% = 1 TO firey%
        fire%(j%, i%) = 0
      NEXT
    NEXT
    
    SCREEN 7
    
    
    
    TYPE firespot
      x AS INTEGER
      y AS INTEGER
    END TYPE
    
    
    CONST maxfires% = 400
    
    DIM SHARED fires(maxfires%) AS firespot
    DIM SHARED numfires%
    
    
    CONST YELLOW = 1
    CONST ORANGE = 2
    CONST RED = 3
    CONST WHITE = 4
    
    
    
    
    CALL AddSpecificHeat(25, 20, 190)
    
    PALETTE 1, 4
    PALETTE 2, 12
    PALETTE 3, 14
    PALETTE 4, 15
    
    
    
    
    
    DIM SHARED curtim AS SINGLE
    curtim = TIMER
    DO
      'IF TIMER - curtim > .02 THEN
        SCREEN 7, 0, 1, 0: CLS
    
        LINE (0, 0)-(319, 100), 11, BF
        LINE (0, 100)-(319, 200), 10, BF
    
        FOR i% = 1 TO numfires% STEP 1
          x% = fires(i%).x
          y% = fires(i%).y
          CALL DrawFire(x%, y%, 1)
          'x% = x% + 4
          'IF x% > 320 THEN x% = x% - 320
          'fires(i%).x = x%
    
        NEXT
        'PCOPY 1, 0
        'END
        CALL UpdateFire(120)
        CALL CoolRoot(2)
        
        CALL MoreFire
    
        CALL SpawnFires(70)
    
        PCOPY 1, 0
        curtim = TIMER
      'END IF
      'CALL anykey
    LOOP
    
    DEFSNG A-Z
    SUB AddHeat (value)
    
    
    
    FOR i = 1 TO firex%
      fire%(20, i) = fire%(20, i) + INT(RND(1) * value) + 1
    NEXT
    
    END SUB
    
    SUB AddSpecificHeat (x%, dist%, hvalue%)
    
    
    
    
    
    
    sx% = x% - (dist% / 2)
    sx2% = sx% + dist%
    
    dx% = -(dist% / 2)
    
    
    IF sx% < 1 THEN sx% = sx% + (firex% - 1)
    
    DO
    
      
      coef# = ABS(dx% / dist%)
    
      heat% = hvalue% * (1 - coef#)
      curheat% = fire%(firey% - 1, sx%)
      curheat% = curheat% + heat%
      IF curheat% > 320 THEN curheat% = 320
      fire%(firey% - 1, sx%) = curheat%
    
      sx% = sx% + 1
      dx% = dx% + 1
      IF sx% > (firex% - 1) THEN sx% = sx% - (firex% - 1)
    
    LOOP UNTIL dx% >= (dist% / 2)
    
    
    
    
    
    END SUB
    
    SUB anykey
    
    DO
      a$ = INKEY$
    LOOP UNTIL LEN(a$) > 0
    
    END SUB
    
    SUB CoolRoot (value%)
    
    
    
    FOR i% = 1 TO firex%
      h% = 2.5
      h% = fire%(firey% - 1, i%)
      v% = INT(RND(1) * value%)
      h% = h% - v%
      IF h% < 0 THEN h% = 0
      fire%(firey% - 1, i%) = h%
    NEXT
    
    
    END SUB
    
    SUB CreateNewFire
    
    
    
    IF (numfires% < maxfires%) THEN
      numfires% = numfires% + 1
    
      fires(numfires%).x = INT(RND(1) * 320) + 1
      fires(numfires%).y = 100 + INT(RND(1) * 100) + 1
    
    END IF
    
    END SUB
    
    SUB DrawFire (x%, y%, scale%)
    
    sx% = x% - ((firex% / 2) * scale%)
    sy% = y%
    
    
    a% = firey% - 2
    b% = 1
    
    
    l% = 0
    DO
    
      FOR i% = sx% TO sx% + firex% - 1 STEP 1
    
        posa% = a% + 1
        posb% = b% + 1
        nega% = a% - 1
        negb% = b% - 1
    
        IF posa% >= firey% THEN posa% = firey%
        IF posb% >= firex% THEN posb% = posb% - (firex% - 1)
    
        IF nega% <= 1 THEN nega% = 1
        IF negb% <= 1 THEN negb% = negb% + (firex% - 1)
    
        h1% = fire%(nega%, negb%)
        h2% = fire%(nega%, b%)
        h4% = fire%(a%, negb%)
        h% = fire%(a%, b%)
        h6% = fire%(a%, posb%)
        h8% = fire%(posa%, b%)
    
    
        fh6% = (h1% + h4% + h6% + h2% + h8% + h%) / 6
        'fh4% = (h2% + h4% + h6% + h8%)
        fh% = fh6%
    
    
    
        SELECT CASE fh%
          CASE 0 TO 11
            c% = -1
          CASE 11 TO 80
            c% = 1
          CASE 71 TO 170
            c% = 2
          CASE 161 TO 260
            c% = 3
          CASE IS > 250
            c% = 4
        END SELECT
        IF c% <> -1 THEN PSET (i%, sy%), c%
        
        b% = b% + 1
      NEXT
      sy% = sy% - 1
      a% = a% - 1
      b% = 1
    LOOP UNTIL a% < 1
    
    
    
    END SUB
    
    SUB MoreFire
    
    
    
    
    
    mf% = INT(RND(1) * 100) + 1
    
    
    IF mf% > 90 THEN
    
      flx% = INT(RND(1) * firex%)
      dist% = INT(RND(1) * 10) + 1
      hv% = 100 + INT(RND(1) * 220) + 1
    
    
      CALL AddSpecificHeat(flx%, dist%, hv%)
    
    END IF
    
    
    END SUB
    
    DEFINT A-Z
    SUB SpawnFires (p%)
    
    n% = INT(RND(1) * 100) + 1
    
    IF n% > p% THEN CALL CreateNewFire
    
    
      
      
    
    
    
    
    
    
    END SUB
    
    DEFSNG A-Z
    SUB UpdateFire (cooldown%)
    
    t = -1
    belowheat% = 0
    
      FOR i% = 1 TO firey% - 2 STEP 1
    
          FOR j% = 1 TO firex% - 1
    
    
            d% = INT(RND(1) * 4)
            
            negj% = j% - d%
            posj% = j% + d%
    
    
            IF negj% <= 1 THEN negj% = negj% + (firex% - 1)
            IF posj% >= (firex%) THEN posj% = posj% - (firex% - 1)
    
            r% = INT(RND(1) * 100) + 1
            
            SELECT CASE r%
              CASE 0 TO 35
                belowheat% = fire%(i% + 1, negj%)
                p% = negj%
                t = 1
              CASE 36 TO 70
                belowheat% = fire%(i% + 1, j%)
                p% = j%
                t = 2
              CASE IS > 70
                'PRINT posj%: PCOPY 1, 0
                belowheat% = fire%(i% + 1, posj%)
                p% = posj%
                t = 3
            END SELECT
    
    
            IF belowheat% > 10 THEN
              v% = INT(RND(1) * cooldown%)
              belowheat% = belowheat% - v%
            ELSE
              cd% = INT(RND(1) * 50) + 1
              IF cd% > 35 THEN belowheat% = belowheat% - INT(RND(1) * 6)
            END IF
    
    
            IF belowheat% < 0 THEN belowheat% = 0
            IF p% = 0 THEN
              PRINT p%; t: PCOPY 1, 0: END
            END IF
            fire%(i%, j%) = belowheat%
            
            
          NEXT
      NEXT
    
    
    
    
    END SUB

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>your code for your revolution effect is quit difficult for me
    Tell me what you did'nt understand?search google for the arguments of function i used.

    Code:
    outtextxy(midx - 8, midy - 210, "12");
    		outtextxy(midx + 210, midy - 5, "3");
    		outtextxy(midx - 3, midy + 210, "6");
    		outtextxy(midx - 210, midy - 5, "9");
    		circle(midx, midy, 220);
    		circle(midx, midy, 10);
    This piece of code should be outside the for loop.So that CPU need not to execute that function again and again.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Type Writer Effect help please.
    By Fujitaka in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 10:11 AM
  2. operator has no effect ??
    By studentc in forum C Programming
    Replies: 3
    Last Post: 05-28-2004, 08:28 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  4. forest fire
    By Kohatian 3279 in forum C++ Programming
    Replies: 0
    Last Post: 04-18-2002, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21