Just for fun, starfield with planets orbiting

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Just for fun, starfield with planets orbiting

Post by dodicat »

short to long works OK.
Thanks.
But I wonder why 64 bits is quite happy with short.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Just for fun, starfield with planets orbiting

Post by fxm »

This also worked with 'gcc 32-bit' (but not with 'gas').
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Just for fun, starfield with planets orbiting

Post by SARG »

I changed the datatype of m : dim as integer m = fix(size)
And leaving x and y as short : no freeze (gas32)

So ????
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Just for fun, starfield with planets orbiting

Post by dodicat »

It gets stuck in the loop in 32 bits. sub grid_add_bol()
(actual values substituted)

Code: Select all


dim as short a,b,x,y
dim as single m
m=-35878
a=m
m=35879
b=m
print a;" to ";b

dim as string s
for y  =  a to b step -100
    for x =a to b step -49
    s+="*"
next
next
print mid(s,1,50)
print "out"
sleep 
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Just for fun, starfield with planets orbiting

Post by deltarho[1859] »

I replaced every instance of short with long and went five minutes with gas, gas64, and gcc 32/64 without a freeze.

The moral of this saga is, do not use short unless we are 100% certain that will not be violated. However, if we use long, we don't have to worry about that.

Can anyone think of a reason why using long instead of short would be problematic?

OK we will use a bit more memory but is that a problem today? I doubt that we will notice any difference performancewise.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Just for fun, starfield with planets orbiting

Post by deltarho[1859] »

Off topic:

A lot of grahics programs use the default Rnd sequence, so we get the same output for every application session. To show your work in the best light, a different Rnd sequence should be used on every application session.

To accomplish that, we need to use Randomize. That will use a seed “value based on Timer”. The seed value should be double "but the fractional part is clipped for all algorithms except algorithm #4"

For a Windows session running for ten minutes will see a seed value of 600. Running for 24 hours will see a seed value of 86400.

The following will see a seed value between 0 and 4xGigs-1 no matter how long the Windows session.

Code: Select all

Declare Function RtlGenRandom Lib "Advapi32.dll" Alias "SystemFunction036" _
  ( RandomBuffer As Any Ptr, RandomBufferLength As Ulong ) As Byte
Dim As Ulong Seed
RtlGenRandom( @Seed, 4 )
Randomize Seed
The unlikely event of Seed = 0 will give the default Rnd sequence.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Just for fun, starfield with planets orbiting

Post by deltarho[1859] »

I had forgotten about this platform independent method.

Code: Select all

Function GetSeed() As ULong
Dim As ULong Seed
Asm
  rdtsc
  bswap eax ' fast moving to upper bits
  mov Dword Ptr [Seed], eax
End Asm
Return Seed
End Function
For i As ULong = 1 To 10
  Print GetSeed
Next
Print
Dim tot As Double
For i As ULong = 1 To 10^8
  tot += GetSeed
Next
Print "Average of 10^8:";tot/10^8
Print "Expected value:";2^31 - 0.5
Print "Ratio ";tot/10^8/(2^31 - 0.5)
Sleep
On my machine, GetSeed returns in just less than one micro-second.
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: Just for fun, starfield with planets orbiting

Post by Gunslinger »

I see everyone had fun with my code :D
Sorry i did not have time to check all the replies.

I have seem to forget some checks not to go beyond memory pointers and data types.
The things is, i just start coding without a real plan.
When code comes along i go in a direction is i like, especial with this project.
My only pupres has been getting into coding again.

The function grid_add_bol() has serious problems.
Drawing a true sphere over a screen 32000+ pixels with is a bad idea.

Big thanks to all replays, really helping me.
I'm back in neural network again.
Post Reply