Page 1 of 1

Another simple demo example

Posted: Thu Sep 22, 2011 12:18 pm
by jalih
Here is a simple but slow fire effect for Hollywood.

It probably would be good idea to use small buffer and blit fire "pixels" as bigger blocks (4x4?) to minimize blitting. Buffer calculation offsets could also be put into tables. Oh, and did I mention getting rid of the global variables...

Have fun! ;-)

Code: Select all

@SCREEN {Mode = "FullScreen"}
@DISPLAY {Width = 360, Height = 240, Title = "Hollywood in Flames"}



; Convert HSV (HSB) to RGB
Function HSV2RGB(h, s, v)
	h = Round(h * 255 / 360)
	s = Round(s / 255)
	v = Round(v / 255)
	
	Local i
	Local f, p, q, t
	
	Local r, g, b
		
	If s = 0
		r = v
		g = v
		b = v
		Return( RGB(Round(r * 255), Round(g * 255), Round(b * 255)) )
	EndIf
		
	h = h / 60
	i = Floor(h)
	f = h - i
	p = v * (1 - s)
	q = v * (1 - s * f)
	t = v * (1 - s * (1 - f))
	
	Switch(i)
	Case 0:
		r = v
		g = t
		b = p
	Case 1:
		r = q
		g = v
		b = p
	Case 2:
		r = p
		g = v
		b = t
	Case 3:
		r = p
		g = q
		b = v
	Case 4:
		r = t
		g = p
		b = v
	Default:
		r = v
		g = p
		b = q
	EndSwitch
	
	Return( RGB(Round(r * 255), Round(g * 255), Round(b * 255)) )	
EndFunction


Function constrain(value, a, b)
	Local c = Max(value, a)
	Return(Min(c, b))
EndFunction


; Fire palette
palette = {}

For x = 0 To 255
	palette[x] = HSV2RGB(x/3, 255, constrain(x*3, 0, 255))
Next

; Screen dimensions
width = 360
height = 240

; Our fire buffer 362*243
Dim buffer[87966]


BeginDoubleBuffer()

Repeat
	Cls()
	SetFont(#SANS,48)
	SetFontColor(#RED)
	SetFontStyle(#BOLD)
	TextOut(#CENTER, #BOTTOM, "Holllywood 4.8")

	; Put random stuff from palette into bottom fire buffer row as fire emitter.	
	For x = 0 To (width+2) - 1
		buffer[(362*242) + x] = Rnd(240)
	Next
	
	; Play with fire buffer.	
	For y = 210 To height + 1
		; Calculate fire buffer line.
		row = Int(y * (width + 2))
		nrow = Int((width + 2) * (y+1))
		For x = 0 To width - 1
			n1 = buffer[nrow + x]
			n2 = buffer[nrow + x + 1]
			n3 = buffer[nrow + x + 2]
			decay = 4
			new = Int(((n1 + n2 + n3) / 3 ) - decay)
			new = new % 256
			If new < 0 Then new = 0
			buffer[row + x + 1] = new
		Next

		; Blit calculated fire buffer line
		For x = 0 To width - 1
			If palette[buffer[row + x]] = 0 Then alpha = 255 Else alpha = 32
			Plot(x, y, ARGB(alpha, palette[buffer[row + x]]))
		Next

	Next
			
	VWait()
	Flip()
	
Until IsKeyDown("ESC")

Re: Another simple demo example

Posted: Thu Sep 22, 2011 2:12 pm
by Juan Carlos
Thank you Jalih for your examples, they are very good, with example as yours, make this forum fantastic.

Re: Another simple demo example

Posted: Fri Sep 23, 2011 5:16 pm
by airsoftsoftwair
Hint: Try to use Local For loops when possible. I.e.

For Local x = 0 To width-1

is faster than

For x = 0 To width-1

because Hollywood can access "x" much quicker because it is local to the current scope.