Another simple demo example

Discuss any general programming issues here
Post Reply
jalih
Posts: 281
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Another simple demo example

Post 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")
User avatar
Juan Carlos
Posts: 950
Joined: Mon Sep 06, 2010 1:02 pm

Re: Another simple demo example

Post by Juan Carlos »

Thank you Jalih for your examples, they are very good, with example as yours, make this forum fantastic.
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Another simple demo example

Post 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.
Post Reply