English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 19 December 2008, 12:26   #1
Anding
Registered User
 
Join Date: Nov 2008
Location: Hong Kong
Posts: 56
Lightbulb Starfield in Blitz Basic - helpful critique sought

This probably seems totally basic and in the age of the dinosaurs to most of you, but I'm refreshing my Amiga coding by trying to rework some classic algorithms from scratch in Blitz Basic 2. The idea of the exercise, of course, is to learn rather than produce something objectively useful

This routine is for a starfield, except that it is rather slow! I'm sure I'm missing some tricks. I'd be very grateful for any helpful critique or suggestions.

Many thanks indeed

Anding

; Starfield research

DEFTYPE .w ; the default variable type will be 16 bit integers
; to speed up calculations

; Establish the initial starfield on a
; 3D coordinate system with the centre of the screen at (0,0,0)
; and project these stars onto the screen
Dim x(50), y(50), z(50), h(50) ; 3D coordinate and colour of each star
Dim sx(50), sy(50) ; screen position of each star
For i = 1 To 50
h(i) = Rnd*7+1 ; colour
x(i) = Rnd*320-160: y(i) = Rnd*256-128: z(i) = Rnd*512 ; 3D position
; simple projection that scales the star's 3D x and y positions onto
; screen x and y positions according to its depth behind the screen
sx(i) = (x(i) * 512/z(i)) + 160: sy(i) = (y(i) * 512/z(i)) + 128
Next

; Establish the display in lo-res with 3 colours
BLITZ ; BLITZ mode direct access to the hardware
BitMap 0,320,256,3 ; Allocate memory for the bitmap
Slice 0,44,3 ; Set-up the copper list
Use Slice 0 ; Attach the bitmap
Show 0,0,0 ; to the slice

main
; Plot the stars
For i = 1 To 50
;Clear the last position occupied by the star
Plot sx(i),sy(i),0
;Move the stars towards the screen (to simulate forward velocity)
z(i) = z(i) -10
If z(i) <1 ;If the star passes behind the screen, renew it
x(i) = Rnd*320-160: y(i) = Rnd*256-128: z(i) = 512
EndIf
; calculate screen positions using the simple projection as before
sx(i) = (x(i) * 512/z(i)) +160: sy(i) = (y(i) * 512/z(i)) +128
Plot sx(i),sy(i),h(i)
Next

; wait for a mouse button to exit and continue in a loop until then
If Joyb(0)>0 Then End
Goto main
Anding is offline  
AdSense AdSense  
Old 19 December 2008, 13:02   #2
Codetapper
2 contact me: email only!

Codetapper's Avatar
 
Join Date: May 2001
Location: Auckland / New Zealand
Posts: 3,138
I don't know how well Blitz optimises things, but I see a few things straight away:

1. You are calculating 512/z(i) twice in the main loop. Best to calculate that once and buffer the result for the second time, as div is a very expensive operation.

2. If the x co-ordinate is not on the screen, there is no need to calculate the y co-ordinate (or call the Plot() function). Ditto if the y co-ordinate is not on the screen, the call to Plot() might be wasting some CPU cycles. (Then again, Plot() might check the co-ordinates are on-screen and do nothing itself, I have never really used blitz basic so have no idea sorry!)

3. You might see some star flickering as you are not clearing all (old) stars before plotting all the new ones. If a new star position ends up being where an old one was (and a lower position in the array), the new position may well get erased.

Good luck!
Codetapper is offline  
Old 26 December 2008, 13:12   #3
ChrisH
Registered User
ChrisH's Avatar
 
Join Date: Jun 2008
Location: England
Posts: 18
To follow-on from Codetapper's post...

"Rnd" is likely quite expensive, and is being called twice per loop. With a few tricks you could reduce that to one. And with a LUT (look up table) you could get rid of it entirely.

Not sure about 68k, but multiplications may be slightly expensive (nothing like division though), so if you can make them part of the LUT then so much the better.

It's hard to read the code without indentation, but why do you do "x(i) * 512" & "y(i) * 512" every loop? Why not just do the multiplication once, before entering the loop?
ChrisH is offline  
Old 26 December 2008, 16:02   #4
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 776
Why do you don´t change BlitzBasic2 by AmiBlitz3??.. I read somewhere that it is a great compiler
Someone knows where to find some code to learn BlitzBasic like foruns?

Last edited by AlfaRomeo; 26 December 2008 at 16:27.
AlfaRomeo is offline  
Old 27 December 2008, 21:26   #5
prowler
Global Moderator

prowler's Avatar
 
Join Date: Aug 2008
Location: Sidcup, England
Posts: 10,284
Quote:
Originally Posted by AlfaRomeo View Post
Someone knows where to find some code to learn BlitzBasic like foruns?
Hi AlfaRomeo,

I've had a look in my Amiga archives and found the following files which may help you in your quest to learn Blitz Basic:

Blitz Support Suite ADFs (3) - RAR archive [1.13MB]
Blitz Basic 2.1 ADFs (3) - RAR archive [1.41MB]
Ultimate Blitz Basic 2.1 CD ISO image - RAR archive [8.61MB]
Blitz Basic 2 Examples ADFs (4) - ZIP archive [1.13MB]

Let me know if you would find any or all of these files useful and I'll upload them for you.

prowler
prowler is offline  
Old 31 December 2008, 16:02   #6
Anding
Registered User
 
Join Date: Nov 2008
Location: Hong Kong
Posts: 56
These are great suggestions and have moved me on quite a bit. Thanks a lot everyone for the helpful ideas.

Merry Christmas and Happy New Year to all Amiga Coders!
Anding is offline  
Old 12 January 2009, 23:47   #7
syphus
Registered User
syphus's Avatar
 
Join Date: Jan 2009
Location: Newcastle, UK
Posts: 18
Thanks for posting your source, Anding! I've not been having much luck finding good snippets to learn from, but this sort of thing is great for people like me, who are just starting out. It helped me to get stuff displayed on screen for the first time
syphus is offline  
Old 13 January 2009, 01:09   #8
KevG
Banned
 
Join Date: Jan 2009
Location: U.K.
Posts: 93
You can get rid of the 3d calculation completely inside the loop and things would speed up really good!

To do this, simply create an array of x & y values for each star. Calculate all the valid x & y values from 3D space and put them in their own list before entering the main loop. Now all you have to do now is read the lists for each star and plot the x & y values.

This is an ultra fast way of doing it because you are just reading from a list and not performing any calculations at all!

Hope that helps :-)

Kev G.
KevG is offline  
Old 13 January 2009, 03:40   #9
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 776
Quote:
Originally Posted by prowler View Post

Let me know if you would find any or all of these files useful and I'll upload them for you.
Hi prowler,
Thanks for your help and sorry for the delay in my answer but I was out during christmas/new year and so I only saw your message today
I think the BB2 examples ADFs may be useful to me if you could upload to the zone

Thanks in advance
AlfaRomeo is offline  
Old 13 January 2009, 12:53   #10
prowler
Global Moderator

prowler's Avatar
 
Join Date: Aug 2008
Location: Sidcup, England
Posts: 10,284
Quote:
Originally Posted by AlfaRomeo View Post
I think the BB2 examples ADFs may be useful to me if you could upload to the zone
Hi Alfa,

They're in the Zone for you.

Have fun!

prowler
prowler is offline  
Old 13 January 2009, 22:46   #11
AlfaRomeo
A1200 040 SAM440EP 667
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 776
Thanks prowler, you´re the best
AlfaRomeo is offline  
AdSense AdSense  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Blitz basic 2 Help Havie Coders. Blitz Basic 30 08 September 2013 10:15
Blitz Basic 1 & 2 twiggy Coders. General 14 21 February 2009 16:29
blitz basic petza request.Apps 11 08 April 2007 02:49
Blitz Basic 2 anyone? jobro request.Apps 12 28 November 2005 19:15
Blitz Basic 2 LaundroMat Retrogaming General Discussion 5 24 July 2001 09:10

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 07:38.


Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Page generated in 0.19409 seconds with 14 queries