It's Snowing Inside ! (Source Included)

BlitzMax Forums/BlitzMax Programming/It's Snowing Inside ! (Source Included)

dw817(Posted 2016) [#1]
I know there are a lot of you people out there that say you are Summer Person and - well, that's fine. You might not be so interested in this program.

But if you are a WINTER person like myself, you're really gonna love watching the snow come down !

This program originates from my early S2 program and was derived literally by me watching snow for hours and hours when it came down here in Texas and blanketed everything.

And I think the math in it is very accurate and you actually get the 'feel' it really is snowing inside your computer ! If you watch it long enough you'll see the wind changes direction too, kinna makes your head tilt if you've been watching it for a-while. :)

As Ron thinks everyone should get credit for what they do (and they really should) I did so in my source and with links.

Anyways, here you go the code and formula to, "It's Snowing Inside !"



148 lines total.




BlitzSupport(Posted 2016) [#2]
Nice snow movement.


xlsior(Posted 2016) [#3]
Nice snow!

(Although one FYI about your scrolling text: Note that Blitzmax (and B3D, and Blitzplus) can't load ALL true type fonts: It's not compatible with Symbol fonts like Wingdings, webdings, etc.)


Brucey(Posted 2016) [#4]
It's not compatible with Symbol fonts like Wingdings, webdings, etc.

What you talking about, Willis??


col(Posted 2016) [#5]
Aww, I get an 'array out of bounds' at line 110.


Casaber(Posted 2016) [#6]
Wow nice intro lines, but I donīt get the snow on Mac? I have to check that. I want to see SNOW (got plenty here too).


Derron(Posted 2016) [#7]
@Mac

Check lines like this:
myfont=LoadImageFont("c:/windows/fonts/trebuc.ttf",48)


I assume the next line has problems to load a non-existing font.


On Linux I get this:
./bmk makeapp -t console -quick -r -x "/testcodes/snow.bmx"
[ 97%] Processing:snow.bmx
[ 98%] Compiling:snow.bmx.console.release.linux.x86.s
flat assembler  version 1.68  (32768 kilobytes memory)
3 passes, 11759 bytes.
[100%] Linking:snow
Executing:snow
	0x00000000,
	0xff000000,
	0x00ff0000,
[...]
	0xffdaff00,
	0xffffff00


But I assume this has to do with MaxGUI and Linux ... (would have to use Brucey's gtk-maxgui).

'Import maxgui.drivers ' needed for full screen without hardware snap
Import bah.gtkmaxgui


After starting it up - it does not show snow flakes , nor das it accept keystrokes (had to "kill" the app via commandline).
Also it kept windowed (small sized) - but this might be an issue with gtkmaxgui.

what it this supposed to do?
k$=Lower$(Chr$(WaitChar())) ' wait for a key
If k$="w" ' see my homepage
  OpenURL "http://www.writerscafe.org/dw817"
  End ' nifty way of opening a web page
ElseIf k$="f"
  OpenURL "https://en.wikipedia.org/wiki/Trebuchet_MS"
  End ' otherwise give credit for famous font
ElseIf k$<>"r"
  End
EndIf


I prepended something in front of this codeblock
print "waiting for key"
local ch:string = WaitChar()
print "got: "+ch

and no matter what key I press, I never reach "got: keyX".
Problem is, that WaitChar()/WaitKey()/GetChar() wont work until a graphics mode is set.

So moving above's code below the "setGraphics" command - work then as expected.





bye
Ron


Casaber(Posted 2016) [#8]
@Derron It has to do with the font. The problem is how to solve that I tried to delete the path and I tried make the name more general.
And I thought I change the font to a common one, but I failed at finding what name to use inside the string.


Derron(Posted 2016) [#9]
Just copy the font of choice next to the code and use "fontname.ttf" instead.

Alternatively just skip loading and setting the imagefont, so it uses the inbuilt one.


@no flakes
print "min clientWidth = " +ClientWidth(zvideo)
MaximizeWindow(zvideo) ' maximize above window (full-screen, no need to read coords)
print "max clientWidth = " +ClientWidth(zvideo)


outputs:
min clientWidth = 0
max clientWidth = 0

Correcting this (manual override), leads to the display of a black screen.

I then had to remove the maxGUI portions and use the classic SetGraphics-code and it works.

bye
Ron


Casaber(Posted 2016) [#10]
I got the Windows font pack (if anywone needs it https://github.com/caarlos0/msfonts ) and I put the source beside the files, doubled checked the names, but still no go.

While looking more closely in the source I noticed the snow is oval not text as I thought.
So it's about the way that things are rendered using canvas and frontbuffer which is the problem.

It will need some conversion to run on Macs. Iīm all new to Bmax's GUI commands. I tried it once and I found on both platforms (WIN and MAC, that the restore buttons was buggy,
I thought it was code first but all examples had this). It works and then it does'nt. So that was offputting for me. But I will try BMax UI more someday.

I put a quickly made graphicscreen version that's good enough for me, nice SNOW !!
And for other Mac users who are okay with a teared-up version and just want to see the snow.

EDIT Got font working on Mac! , I guess anyone who can see both versions could say how true it is, and alter it. I just did by best shoot in the dark.

As a bonus I think I may confirm that a bug in OSX/Bmax is fixed.
(I read this http://www.blitzmax.com/Community/posts.php?topic=47744)

I had no problem on an updated Mac to move the executable away from its fonts and still have the embedded font working.
Embedding fonts with INCBIN seem to work now on Mac.




Casaber(Posted 2016) [#11]
I love how it seem to be blowing a wind sometimes, mesmerizing.


Derron(Posted 2016) [#12]
Had a bit of time and wanted to contribute something: a bit improved snowflakes.

I converted an recursive "KOCH"-Snowflakes-algorithm.
Do not get frightened by the length of the code - I had two options: use DrawPoly() and implement a lengthy clipping-ears-algorithm (non convex shape portions) - or draw the polygon and implement a simple flood-fill. I decided for flood filling the alpha-zero-portions.

To use it, just replace

SetBlend lightblend ' all colors get lighter
SetAlpha .018 ' super fuzzy
For i=63 To 1 Step -1
  DrawOval 32-i/2,32-i/2,i,i ' draw a snowflake
Next
Local snow:TImage=CreateImage(64,64)
GrabImage snow,0,0 ' grab it !


with



There is a portion "Rand(2,4)" which describes the recursion level of the draw function - higher ones get more "detailled" - so if you want variation...

Another thing to make it look more exciting is to use a random "SetRotation" for the flakes (so each one looks a bit different) - of course this is only useful if you use "my" snowflakes (rotating a gradient-circle wouldn't look so astonishing :-)


bye
Ron


Casaber(Posted 2016) [#13]
@ Derron Wow, that looked GOOD. Great finishing touch.

I wanted to try and see how hard it would be to do some good looking rotation but I think my version didn't quiet make it.
It got abit clumpsy and draws too much attention from the wind effect. It should be really nice to do a proper realistic rotation that reacted on the wind somehow.




Derron(Posted 2016) [#14]
If you want to have the wind influence the rotation, you would need to store individual rotations for each snowflake

So next to ya[], xa[], x[] ... you need r[].
Then you check the X-axis-acceleration and the higher this is, the more you add to the current rotation of that snowflake. If the acceleration is negative, you rotate counter-clockwise, and if it is positive, you rotate it clockwise.

You could also generate multiple snow-flake-images and assign them randomly on snow-flake-object creation.

Howto:

Replace
Global sc#[snowmax],mode,xp,milli

with
Global sc#[snowmax],rotation#[snowmax],mode,xp,milli




Replace
    x#[i]:+Sin(xa#[i]*4.0)*2.0+Sin(snowflurry/16.0)*4.0*snowfx ' beautiful lovely wind effect
    If x#[i]<0 Then x#[i]:+maxx
    x#[i]=x#[i]Mod maxx ' keep snow on the screen
  Next
  snowflurry:+1   

with
    local dx:float = Sin(xa#[i]*4.0)*2.0+Sin(snowflurry/16.0)*4.0*snowfx
    rotation[i] :+ dx * 0.2
    SetRotation(rotation[i]) 'rotate snowflakes according to wind
    x#[i]:+dx ' beautiful lovely wind effect
    If x#[i]<0 Then x#[i]:+maxx
    x#[i]=x#[i]Mod maxx ' keep snow on the screen
  Next
  SetRotation(0)
  snowflurry:+1   


bye
Ron


dw817(Posted 2016) [#15]
Hi gang, boy you guys really tackled this thing. :) I'm pretty sure anything I write is not (never ?) going to work on all the platforms and resolutions you guys are trying.

Although I DID set it so it didn't force 1024x768 resolution this time and tried to calculate just how many snowflakes you would have for whatever resolution you are running it in.

Ron, your snowflakes look pretty good ! But no two snowflakes are alike, at least that's what Dad says.

Oh, for those of you it crashes on, I was not running debug mode, try this change:
  For i=0 To snowmax-2
Hopefully that'll fix it.

And no, Ron, rotating a circle does not gather very much moss. :) You have enough code up there that space could be used instead for 6-bit data (my CarryAll program) of a perfect snowflake image. :)

Going to be a busy morning ! I'll be gone for several hours today so if you're writing and I'm not replying it's just cause I'm not here. Likely be back at 5pm or so.


Bobysait(Posted 2016) [#16]
For i=0 To snowmax-1
    i:+(3-snowfx) ' how big a storm ? check top for configuration
    SetScale sc#[i],sc#[i]

This can't run in debugmode.

"i" can be higher than snowmax-1 due to "i:+(3-snowfx)" so sc[i] is out of array bounds.


dw817(Posted 2016) [#17]
Hi Bobysait, look above. Change the snowmax-1 to snowmax-2 and that oughta fix it.


Casaber(Posted 2016) [#18]
@dw817 It's great though to attack problems on different platforms, it helps the community as whole alot. Font works perfectly now everywhere.

If only your screen technique worked on OSX then everything would work absolute perfectly.
The gaps between the two platforms are otherwise pretty much non-existent finally.


xlsior(Posted 2016) [#19]
What you talking about, Willis??


It's explicitly stated in the Blitz3D docs:
Note: Blitz doesn't work with SYMBOL fonts, like Webdings and WingDings.


The same appears to hold true for Blitzmax too - If you try to substitute wingdings for a normal font like tahoma in Blitzmax, all it prints are blanks, no actual characters.

(There's been a couple of other fonts I've run into over the years that won't print anything either)


Brucey(Posted 2016) [#20]
That's interesting... because I just tried it here on OS X, with wingdings.ttf, drawing "Hello World", and it has drawn some different symbols on the screen... which is how I would expect it to work.

Unless your fonts aren't ttf, in which case sure, the true-type font loader probably won't load non-true-type fonts.


Casaber(Posted 2016) [#21]
I tried snowdemo now with the wingdings ttf and it crashes before I got to see anything. Other fonts seem to be allright but not wingding nor webdings.
Iīm not sure what why? So far all fonts have worked nicely on Mac but not those two yet What makes a font "symbol"? Is a special class of Unicode fonts?


Brucey(Posted 2016) [#22]
Works fine for me.

Note that the code loads the font in three different sizes, for which you'll need to change the name - as it is the filename for the incbinn'd data.


Casaber(Posted 2016) [#23]
I used this for testing, does it work for you?




Brucey(Posted 2016) [#24]
No, because your paths are wrong for loading from memory. You need to prefix the path with "incbin::"


dw817(Posted 2016) [#25]
I'm back but just for a bit, it's the weekend, time to par-tay ! :D

Casaber, I tried your program, while it does hardware snap my screen, I get a black screen until I hit ESCAPE. No graphics at all.

Changing this line:
Graphics maxx,maxy ',32,60 (added remark)
then it works just fine, but displays the system BlitzMAX font. But if I run it in DEBUG mode, it crashes.

Change this line:
For i=0 To snowmax-3
and it runs just fine but no text.

You guys are sure messing with my code. *Grin* That's fine. Now as far as snowflakes go, I KNOW there was this one cool math formula to build snowflakes in GWBASIC, and it was super small coding, like 5-lines, had to do with recursion of drawing at 90 degree angles based on a 3x3 pixeled image.

If I can unearth that, might be the same snowflake or even one more detailed at a considerably lesser price of coding space.

. . .

Didn't know about other fonts. I'm actually changing my vector generator so it can have oval slopes, circle cuts, fill in region, you name it, very powerful. Was thinking of giving it an option to display each character from a Truetype Font in the background, use [ ] to change characters, and you can doodle a copy of each character in my system.

Then you wouldn't need Truetype at all and could use my vector generator to recreate a near exact copy of all the characters for any font with a ton less space required - and you could use it for systems that don't support Truetype.

Oh yes, of course you can also build your own font or use it to doodle flying saucers and spaceships and stuff too if you want.

It'll have a grid maximum size of 26x26, (25x25 squares), you can change that of course, and will use letters A-Z to represent points, and special characters to represent lines, slopes, and paints.

All in all, it's gonna rock ! :D But ... save it for Monday, it's the weekend now, with my G/F, so I'm outie !


Casaber(Posted 2016) [#26]
But I donīt even INCBINned the font? I have the file beside the executable.
Is it a req to incbin wingding to have it to work?

I have to check that later, Iīm confused eight now.


PARTYY sounds good *

Iīm dead, I coded all today It took the pain away. The worst weekend ever.

Smile everyday.
Happiness is not trying or finding it's deciding.


Brucey(Posted 2016) [#27]
But I donīt even INCBINned the font?

Oh, neither you did ;-) My Bad...

No, it doesn't work here until I give it the correct filename, which in my case is "wingdings.ttf"
Then it draws symbols as expected.


dw817(Posted 2016) [#28]
Interesting ideas abound ! Derron wanted something more than circles. I've got that.

Casaber wanted rotation, I've got that.

So awright guys, see what you think of this new code !




xlsior(Posted 2016) [#29]
No, it doesn't work here until I give it the correct filename, which in my case is "wingdings.ttf"
Then it draws symbols as expected.


On my PC, it doesn't -- neither when using wingdings.ttf in the current folder, nor when pointing it to the windows fonts directory.

almost any other font does work, but not wingdings.ttf


dw817(Posted 2016) [#30]
Umm ... Okay ? I didn't choose Wingdings as the font, but I can certainly try.

... Done. Easy fix, now try it with WingDings ! They won't show up, but they won't crash either.




Casaber(Posted 2016) [#31]
Just.. wow. Those snowflakes looks amazing, how did you do that?

What makes a font into a "symbol font"? are't all charcacter symbols? Is it the size? the number of letters? Curious if anyone knows I could't find it online.

Does BMax use Freetype to smash the font onto the screen?


Derron(Posted 2016) [#32]
If you want the "KOCH-snowflakes" to be random, you could "jitter" the calculated points.

dw's random flakes look a bit like flowers (to me). Snowflakes should look "crystalline".

Maybe someone can come up with some "crystalline" snowflakes (algorithms, no fixed "DrawLine"-chains).


@dw
You might play with varying alpha of the things you draw on your snowflake "texture" - to simulate "thinner ice". You also might consider light blueish colorizing to make it look a bit more interesting.

The rotation looks a bit "rolling" to me, maybe you should manipulate the influence (10% of the wind).
For now each dx adds to the rotation - while the wind might "push" the flake into a direction.
So you might think of adding a variable rotation speed (eg. array-index based randomness ... "(((5 * i) mod 100)/100.0) * windMod * wind" or something similar ).


Have Fun.

bye
Ron


dw817(Posted 2016) [#33]
Hi Casaber. Glad you like it. *Grin* I think I found out something about myself. I can basically write any code I set my mind to doing, I just don't go about doing it the way other people think I should. :)

Casaber, giving less than a minute looking at, Wing Dings font just doesn't have a space character. That's all. That's why my program up and bails. I'm using the SPACE character to determine the length of how many spaces to add text so the scroller looks nice when you put a message across.

Wing Dings, as mentioned, quite simply, has a ZERO length of pixels for the space character.

If there's a real need for Wing Dings to work in BlitzMAX, I can certainly put my head to it and see what's up.


Casaber(Posted 2016) [#34]
I tried to write some other letters but It did not work (no rrash but all empty).

I noticed online that Unicode now has equivalents for all of the Wingdings symbols, so there might be another font out there. But still just curious why this one was so hard to access in Bmax.


Casaber(Posted 2016) [#35]
now it really went dead on me whole Bmax :s

Building testing
Compiling:testing.bmx
fasm2as failed: Unable to open file wingdings.ttf
Fasm2as failed - please contact BRL!
Process complete


Casaber(Posted 2016) [#36]
I guess you will be without my services from now on. Bmax died on my. I wonder how to reinstall something on a Mac. Thatīs a new for me.


dw817(Posted 2016) [#37]
Casaber, I had the same problem. I backed up my /MOD/ directory first and then recovered. Do the following and we'll see you soon:

[1] Backup your sources
[2] Uninstall BlitzMAX if you can
[3] Reinstall (let me know if you need a new download link)
[4] Put back your sources.

That oughta do it. If you have trouble, let one of us know. We wantcha back in the game ! :)


Derron(Posted 2016) [#38]
BTW: The German word "Dings" means "doodad" or "thingy" ... might explain the symbols :-)

As it does not contain letters or numbers I would call it a symbol font (regardless of whether letters are symbols too).


@zero pixels wideness
hmm.. one would have to look at the freetypefont-module.

A wonderful piece of code is for example this:

	Method Pixels:TPixmap()
		If _pixmap Return _pixmap
		
		Return _pixmap
	End Method

Wonder what is returned instead of "_pixmap" if _pixmap is not existing ;-)

Nonetheless I did not see a problem in the code as max2d.mod/imagefont.bmx checks for a valid image before accessing it (zero-width character-glyphs do not contain a _pixmap and therefore then also no loaded image). So if the first glance did not miss something, the bug might be hidden somewhere else.


Edit:
how to uninstall on a Mac:

BlitzMax is self-contained, so if you remove the blitzmax-folder, blitzmax is vanished from your computer. Of course "renaming" will do too.


PS: does it happen to "webdings.ttf" too (think this was a font of office 2000 or so) - or other "symbols"-fonts?

bye
Ron


Casaber(Posted 2016) [#39]
I donīt know what's with me today, I had correctly at first as "Wingding.ttf" and then I thought I saw "wingdingS" inside
the font folder, so I got all happy thinking it was just a spelling error. But no I was right the first time.

I tried INCBIN it, I tried to load it as a file. and Iīve tried to missproununce the name. I did it all !

No luck with that bloody font. It wont budge and. Thatīs just rude.
Happy to announce my services are back though.


TomToad(Posted 2016) [#40]
Tried my own hand at generating snowflakes. Didn't quite work out as I hoped, but I did get this neat little Kaleidescope thingy going. :)



dw817(Posted 2016) [#41]
Hi Tom, this is neat. Don't mind me, I always try to figure out code - and sometimes, I'm not figuring out what something does ??

You have writepixel in the code and I'm not too sure why, here is the code where it still runs and does your great Kaleidoscope:

' *************************************************
'
' Flaky Kaleidescope
'    by TomToad
'
' ************************************************
SuperStrict
Graphics 800,600 'set the graphics amd hide the mouse
SetBlend alphablend
HideMouse
SeedRnd(MilliSecs())

Local background:TImage = CreateImage(800,600) 'The background

Local flakesegment:TImage=CreateImage(256,256)

'Now we need to create the background.  Alternateively, you can use LoadImage to load an 800x600 picture for a 
'   neat kaleidescope effect
Cls
For Local i:Int = 0 To 100 Step 2'draw some random lines
	SetLineWidth 4
	SetColor Rand(128,255),Rand(128,255),Rand(128,255)
	DrawLine Rand(0,799),Rand(0,599),Rand(0,799),Rand(0,599)
Next
SetColor 255,255,255
GrabImage background,0,0 'grab the background into a TImage
MidHandleImage background 'Midhandle the background
Local x:Int = 0, y:Int = 0, xd:Int = 2, yd:Int = 2 'Keep track of where we are scrolling the background to
While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
	Cls
	SetRotation 0
	DrawImage background,x,y 'We will draw the background at the current x,y coordinates
	GrabImage FlakeSegment,272,172 'Now capture what's left into our flake segment
	Cls
	For Local a:Float = 0 To 359 Step 60 'Now To draw half the flake segments at 60 degree intervals
		SetRotation a
		DrawImage FlakeSegment, 400,300
	Next
	
	SetScale 1,-1	'mirror the flake segment
	For Local a:Float = 0 To 359 Step 60 'draw the mirrored segments to fill in the gaps
		SetRotation a
		DrawImage FlakeSegment,400,300
	Next
	SetScale 1,1
	
	Flip 'Flip to show the results
	
	'Update x and y to keep the image varying
	x :+ xd
	If x >= 800 Then xd = -2
	If x < 0 Then xd = 2
	y :+ yd
	If y >= 600 Then yd = -2
	If y < 0 Then yd = 2

Wend



TomToad(Posted 2016) [#42]
The WritePixel part is creating a mask. Your version creates some nice images, but they overlap, not like what a kaleidoscope does. If you replace
Cls
For Local i:Int = 0 To 100 Step 2'draw some random lines
	SetLineWidth 4
	SetColor Rand(128,255),Rand(128,255),Rand(128,255)
	DrawLine Rand(0,799),Rand(0,599),Rand(0,799),Rand(0,599)
Next
SetColor 255,255,255
GrabImage background,0,0 'grab the background into a TImage
MidHandleImage background 'Midhandle the background

With this
Local Filename:String = RequestFile("Choose an image")
background = LoadImage(ResizePixmap(LoadPixmap(Filename),800,600))
MidHandleImage background 'Midhandle the background

Then when you run the program, it will ask for a file. Just pick an image from your drive. You will see how your version overlaps the image. But if you try replacing the same code in my version, you will see how a portion is masked out before the GrabImage and renders without overlap like a proper kaleidoscope.


dw817(Posted 2016) [#43]
I didn't notice, ok, sorry 'bout that, Tom. I was just noticing lines in both codes. I see now you are doing some tricky stuff there. Good deal !