Advertisement
jalih

Flames in FTN95

Aug 16th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module flames
  2.   use mswin
  3.   implicit none
  4.  
  5.   integer, parameter :: width = 800, height = 200
  6.  
  7.   type rgb_type
  8.     integer R
  9.     integer G
  10.     integer B
  11.   end type rgb_type
  12.  
  13.   type hsv_type
  14.     integer H
  15.     integer S
  16.     integer V
  17.   end type hsv_type
  18.  
  19.   ! our 256 color fire palette
  20.   type(rgb_type) :: palette(0:255)
  21.   ! we make buffer a little bit bigger than our graphics surface
  22.   integer :: buffer(0:(width-1+2)*(height-1+3))
  23.  
  24.   integer :: addr
  25.  
  26.   contains
  27.  
  28.     subroutine HSV2RGB(HSV, RGB)
  29.       type(hsv_type), intent(in)  :: HSV
  30.       type(rgb_type), intent(out) :: RGB
  31.       real :: H, S, V
  32.       real :: f, p, q, t
  33.       integer i
  34.  
  35.       H = anint(real(HSV%H)*255/360)
  36.       S = anint(real(HSV%S)/255)
  37.       v = anint(real(HSV%V)/255)
  38.  
  39.       if(nint(S).eq.0) then
  40.         RGB%R = nint(V*255)
  41.         RGB%G = nint(V*255)
  42.         RGB%B = nint(V*255)
  43.        
  44.         return
  45.       endif
  46.  
  47.       H = H/60
  48.       i = floor(H)
  49.       f = H-i
  50.       p = V*(1-S)
  51.       q = V*(1-S*f)
  52.       t = V*(1-S*(1-f))
  53.  
  54.       select case(i)
  55.         case(0)
  56.           RGB%R = nint(V*255)
  57.           RGB%G = nint(t*255)
  58.           RGB%B = nint(p*255)
  59.         case(1)
  60.           RGB%R = nint(q*255)
  61.           RGB%G = nint(V*255)
  62.           RGB%B = nint(p*255)    
  63.         case(2)
  64.           RGB%R = nint(p*255)
  65.           RGB%G = nint(V*255)
  66.           RGB%B = nint(t*255)
  67.         case(3)
  68.           RGB%R = nint(p*255)
  69.           RGB%G = nint(q*255)
  70.           RGB%B = nint(V*255)
  71.         case(4)
  72.           RGB%R = nint(t*255)
  73.           RGB%G = nint(p*255)
  74.           RGB%B = nint(V*255)
  75.         case default
  76.           RGB%R = nint(V*255)
  77.           RGB%G = nint(p*255)
  78.           RGB%B = nint(q*255)
  79.       end select  
  80.      
  81.     end  subroutine HSV2RGB
  82.  
  83.  
  84.     integer function constrain(value, a, b)
  85.       integer, intent(in) :: value, a, b
  86.       integer c
  87.  
  88.       c = max(value, a)
  89.       constrain = min(c, b)
  90.     end function constrain
  91.    
  92.  
  93.     integer function timer_func()
  94.       integer :: q,x,y,padding, row, nrow, n1,n2,n3,decay,new
  95.  
  96.       padding=ls(rs(3*width+3,2),2)
  97.      
  98.       ! put fire emitter at bottom of the buffer
  99.       do x=0, width+1, 1
  100.         buffer(((width+2)*height) + x) = int(random@()*255)
  101.       end do
  102.       ! play with fire buffer
  103.       do y=0,199,1
  104.         ! calculate buffer row offsets
  105.         row=y*(width+2)
  106.         nrow=(y+1)*(width+2)
  107.         do x=0,width-1,1
  108.           n1=buffer(nrow+x)
  109.           n2=buffer(nrow+x+1)
  110.           n3=buffer(nrow+x+2)
  111.           decay=3
  112.           new=((n1+n2+n3)/3)-decay
  113.           new=mod(new,256)
  114.           if(new.lt.0) new=0
  115.           buffer(row+x+1)=new
  116.         end do
  117.         ! blit buffer line      
  118.         do x=0,width-1,1
  119.           q=3*x+padding*y
  120.           core1(addr+q+2)=palette(buffer(row+x))%R
  121.           core1(addr+q+1)=palette(buffer(row+x))%G
  122.           core1(addr+q)=palette(buffer(row+x))%B
  123.         end do
  124.       end do
  125.  
  126.       call size_in_pixels@(40,40)
  127.       call draw_characters@('Silverfrost',180,80,RGB@(0,0,255))
  128.       call size_in_pixels@(120,120)
  129.       call draw_characters@('FTN95',100,200,RGB@(255,0,0))
  130.       call perform_graphics_update@()
  131.      
  132.       timer_func=2
  133.     end function timer_func
  134.      
  135. end module flames
  136.  
  137.  
  138. winapp
  139.   use flames
  140.   implicit none
  141.  
  142.   integer :: i
  143.  
  144.   type(hsv_type) hsv_color
  145.  
  146.   ! calculate fire palette, from black to yellow
  147.   do i=0, 255, 1
  148.     hsv_color%H = i/3
  149.     hsv_color%S = 255
  150.     hsv_color%V = constrain(i*3, 0, 255)
  151.     call HSV2RGB(hsv_color, palette(i))
  152.   end do
  153.  
  154.   i=winio@('%ww%ca[Flames]%gr[black,'//'user_surface]&',800,200,addr)
  155.   i=winio@('%dl',0.016D0, timer_func)
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement