graphics: Solar System

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6651
Joined: 19 Dec 2016, 01:58
Location: UK

graphics: Solar System

20 Jul 2017, 23:11

I wanted to produce some dynamic images via Gdip, and I thought this was a good starting point.

If anyone has produced anything similar in AHK, or has any interesting links regarding producing dynamic images in AHK (i.e. images that are changing and not static), then please post some links. Thanks.

Code: Select all

;Solar System by jeeswg
;based on Gdip.Tutorial.2-Draw.Outlined.Shapes.ahk
;requires Gdip.ahk or Gdip_All.ahk

#SingleInstance, Force
#NoEnv

vMakePlanetsEquidistant := 0

vYearOffset := 0.25
vYearOffset := 0.01

vSleepMSec := 10

vIncludePluto := 1
vIncludeSun := 1
vIncludeMoon := 1

vPlanetColours := 1

vDrawOrbits := 1

;==============================

;Retrograde and prograde motion - Wikipedia
;https://en.wikipedia.org/wiki/Retrograde_and_prograde_motion
;All eight planets in the Solar System orbit the Sun
;in the direction that the Sun is rotating, which is
;counterclockwise when viewed from above the Sun's north pole.

;Is the Sun yellow or white? · Ask an Astronomer
;http://askanastronomer.org/stars/faq/2015/11/06/is-the-sun-yellow-or-white/

;Moon - Wikipedia
;https://en.wikipedia.org/wiki/Moon

;Dwarf planet - Wikipedia
;https://en.wikipedia.org/wiki/Dwarf_planet
;Pluto - Wikipedia
;https://en.wikipedia.org/wiki/Pluto

;Planet - Wikipedia
;https://en.wikipedia.org/wiki/Planet
;1 Name
;2 Equatorial diameter [h]
;3 Mass [h]
;4 Semi-major axis (AU)
;5 Orbital period (years) [h]
vText := "
(
Mercury	0.382	0.06	0.39	0.24
Venus	0.949	0.82	0.72	0.62
Earth	1.00	1.00	1.00	1.00
Mars	0.532	0.11	1.52	1.88
Jupiter	11.209	317.8	5.20	11.86
Saturn	9.449	95.2	9.54	29.46
Uranus	4.007	14.6	19.22	84.01
Neptune	3.883	17.2	30.06	164.8
)"
if vIncludePluto
	vText .= "`nPluto	0.22	0.00218	39.48	248.09"
;Moon (orbital period) = 27.32 / 365.25 = 0.074798
;Moon	0.273	0.012300	0.00257	0.07
oArray := {}
Loop, Parse, vText, `n
	oArray[A_Index] := StrSplit(A_LoopField, "`t")
;MsgBox, % oArray[4, 5] ;Mars: 1.88
if vMakePlanetsEquidistant
	Loop, % oArray.Length()
		oArray[A_Index, 4] := A_Index*4

;==============================

SetBatchLines, -1
if !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}
OnExit, Exit
Width := A_ScreenWidth, Height := A_ScreenHeight
Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, 1: Show, NA
hwnd1 := WinExist()
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
pPenBlk1 := Gdip_CreatePen(0xff000000, 1) ;thickness 1
pPenBlk2 := Gdip_CreatePen(0xff000000, 2) ;thickness 2
;pBrushRed := Gdip_BrushCreateSolid(0xffff0000)
pBrushBlk := Gdip_BrushCreateSolid(0xff000000)
pBrushSun := Gdip_BrushCreateSolid(vPlanetColours?0xffffffff:0xff000000)
pBrushMoon := Gdip_BrushCreateSolid(vPlanetColours?0xff797373:0xff000000)

;planet colours: 8 planets + Pluto
vListRGB := "5A5A5A,D79537,3C6581,ED7D58,E4DFCC,D8B87B,C8EEF1,436AFF,B3AA9B"
if vPlanetColours
	Loop, Parse, vListRGB, % ","
		pBrush%A_Index% := Gdip_BrushCreateSolid(0xff000000+Format("{:i}", "0x" A_LoopField))
else
	Loop, Parse, vListRGB, % ","
		pBrush%A_Index% := Gdip_BrushCreateSolid(0xff000000)

vImgCtrX := A_ScreenWidth/2
vImgCtrY := A_ScreenHeight/2
vYears := 0
Loop
{
	Gdip_GraphicsClear(G)
	Loop, % oArray.Length()
	{
		vRadius := oArray[A_Index].4 * 10
		if vDrawOrbits
			Gdip_DrawEllipse(G, pPenBlk1, vImgCtrX-vRadius, vImgCtrY-vRadius, vRadius*2, vRadius*2)
	}
	vYears += vYearOffset
	if vIncludeSun
		Gdip_FillEllipse(G, pBrushSun, vImgCtrX-vRadius2, vImgCtrY-vRadius2, vRadius2*2, vRadius2*2)
	Loop, % oArray.Length()
	{
		vRadius := oArray[A_Index].4 * 10
		vPeriod := oArray[A_Index].5
		;angle = (Mod(years (elapsed), years (orbit duration)) / years (orbit duration)) * 360
		vAngle := (Mod(vYears, vPeriod)/vPeriod) * 2 * 3.141592653589793
		vPosX := Sin(vAngle+3.141592653589793) * vRadius
		vPosY := Cos(vAngle+3.141592653589793) * vRadius
		;vRadius2 := oArray[A_Index].2 * 4
		vRadius2 := 6
		;Gdip_FillEllipse(G, pBrushRed, vImgCtrX-vRadius2, vImgCtrY-vRadius-vRadius2, vRadius2*2, vRadius2*2)
		;Gdip_DrawEllipse(G, pPenBlk2, vImgCtrX-vRadius2, vImgCtrY-vRadius-vRadius2, vRadius2*2, vRadius2*2)
		;Gdip_FillEllipse(G, pBrushBlk, vImgCtrX-vRadius2, vImgCtrY-vRadius-vRadius2, vRadius2*2, vRadius2*2)
		;Gdip_FillEllipse(G, pBrushBlk, vImgCtrX+vPosX-vRadius2, vImgCtrY+vPosY-vRadius2, vRadius2*2, vRadius2*2)
		Gdip_FillEllipse(G, pBrush%A_Index%, vImgCtrX+vPosX-vRadius2, vImgCtrY+vPosY-vRadius2, vRadius2*2, vRadius2*2)
		if (A_Index = 3) && vIncludeMoon
		{
			vEarthCtrX := vImgCtrX+vPosX
			vEarthCtrY := vImgCtrY+vPosY
			vRadius := 10
			vPeriod := 0.07
			vAngle := (Mod(vYears, vPeriod)/vPeriod) * 2 * 3.141592653589793
			vPosX := Sin(vAngle+3.141592653589793) * vRadius
			vPosY := Cos(vAngle+3.141592653589793) * vRadius
			Gdip_FillEllipse(G, pBrushMoon, vEarthCtrX+vPosX-vRadius2, vEarthCtrY+vPosY-vRadius2, vRadius2*2, vRadius2*2)
		}

	}
	UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
	Sleep, % vSleepMSec
}
return

Exit:
Gdip_DeletePen(pPenBlk1)
Gdip_DeletePen(pPenBlk2)
;Gdip_DeleteBrush(pBrushRed)
Gdip_DeleteBrush(pBrushBlk)
Gdip_DeleteBrush(pBrushSun)
Gdip_DeleteBrush(pBrushMoon)
Loop, 9
	Gdip_DeleteBrush(pBrush%A_Index%)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
return
Some other recommended Gdip scripts:
Laughing Man - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=30718
[GDI+][Class] Particle System 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26485
Fun with GDIPlus (GDI+) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=6071

Gdip_All and Gdip tutorial scripts:
GDI+ standard library 1.45 by tic - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=6517
GitHub - tariqporter/Gdip: GDI+ library for Autohotkey
https://github.com/tariqporter/Gdip
Last edited by jeeswg on 26 Jul 2017, 20:39, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
iPhilip
Posts: 409
Joined: 02 Oct 2013, 12:21

Re: graphics: Solar System

21 Jul 2017, 00:55

The script and associated file below demonstrate background scrolling. It was part of this post.

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

File = stars.jpg
Font = Arial                             ; Text font
Options = Center vCenter s26 cffffffff   ; Text options

if !pToken := Gdip_Startup()
{  MsgBox, 48, Gdiplus Error, Gdiplus failed to start. Please ensure you have Gdiplus on your system.
   ExitApp
}
if !Gdip_FontFamilyCreate(Font)
{  MsgBox, 48, Font Error, The font you have specified does not exist on the system.
   ExitApp
}

OnExit, Exit
Gui, -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs +Hwndhwnd
Gui, Show, NA

pBitmap := Gdip_CreateBitmapFromFile(File)   ; Create a bitmap from the file
Width   := Gdip_GetImageWidth(pBitmap)       ; Get the width of the bitmap
Height  := Gdip_GetImageHeight(pBitmap)      ; Get the height of the bitmap

hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)

Loop % Width > Height ? Height+1 : Width+1                                                               ; Loop so that the full image scrolls once
{  Gdip_DrawImage(G, pBitmap, 1-A_Index, 1-A_Index, Width, Height, 0, 0, Width, Height)                  ; Draw the upper-left image
   Gdip_DrawImage(G, pBitmap, Width+1-A_Index, 1-A_Index, Width, Height, 0, 0, Width, Height)            ; Draw the upper-right image
   Gdip_DrawImage(G, pBitmap, Width+1-A_Index, Height+1-A_Index, Width, Height, 0, 0, Width, Height)     ; Draw the lower-right image
   Gdip_DrawImage(G, pBitmap, 1-A_Index, Height+1-A_Index, Width, Height, 0, 0, Width, Height)           ; Draw the lower-left image
   Gdip_TextToGraphics(G, "Background Scrolling", Options, Font, Width, Height)                          ; Draw the text on top of the image
   UpdateLayeredWindow(hwnd, hdc, (A_ScreenWidth-Width)//2, (A_ScreenHeight-Height)//2, Width, Height)   ; Update the window
   Sleep, 40
   Gdip_GraphicsClear(G)
}
Return

Esc::
Exit:
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
ExitApp
Return
stars.jpg
stars.jpg (44.81 KiB) Viewed 1884 times
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
TheDewd
Posts: 1323
Joined: 19 Dec 2013, 11:16
Location: USA

Re: graphics: Solar System

21 Jul 2017, 07:34

Pluto will always be a planet to me! :(
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
jeeswg
Posts: 6651
Joined: 19 Dec 2016, 01:58
Location: UK

Re: graphics: Solar System

21 Jul 2017, 13:49

Look again ... #Update
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
TheDewd
Posts: 1323
Joined: 19 Dec 2013, 11:16
Location: USA

Re: graphics: Solar System

21 Jul 2017, 14:33

vIncludeSun doesn't seem to work. I see no sun either way.
Indeed, use a dark background! Working!
Last edited by TheDewd on 21 Jul 2017, 15:58, edited 1 time in total.
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
User avatar
jeeswg
Posts: 6651
Joined: 19 Dec 2016, 01:58
Location: UK

Re: graphics: Solar System

21 Jul 2017, 14:38

Look again ... #UseADarkBackground
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6651
Joined: 19 Dec 2016, 01:58
Location: UK

Re: graphics: Solar System

22 Jul 2017, 06:39

Cheers for your script iPhilip, it's really nice. I had thought to possibly include some stars. It works nicely if I double the image size (and turn off always-on-top) (as in my edit below) and if I set my script to vDrawOrbits := 0 (which I added to my script).

convert/resize image with Gdip (solved) - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/5203 ... ip-solved/

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

File = stars.jpg
;File = %A_Desktop%\stars.jpg
Font = Arial                             ; Text font
Options = Center vCenter s26 cffffffff   ; Text options

if !pToken := Gdip_Startup()
{  MsgBox, 48, Gdiplus Error, Gdiplus failed to start. Please ensure you have Gdiplus on your system.
   ExitApp
}
if !Gdip_FontFamilyCreate(Font)
{  MsgBox, 48, Font Error, The font you have specified does not exist on the system.
   ExitApp
}

OnExit, Exit
;Gui, -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs +Hwndhwnd
Gui, -Caption +E0x80000 +LastFound +ToolWindow +OwnDialogs +Hwndhwnd
Gui, Show, NA

pBitmapFile := Gdip_CreateBitmapFromFile(File)   ; Create a bitmap from the file
Width   := Gdip_GetImageWidth(pBitmapFile)       ; Get the width of the bitmap
Height  := Gdip_GetImageHeight(pBitmapFile)      ; Get the height of the bitmap

w := Width*2
h := Height*2
pBitmap := Gdip_CreateBitmap(w, h)
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_DrawImage(G, pBitmapFile, 0, 0, w, h, 0, 0, Width, Height)

Width := w
Height := h

hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)

Loop % Width > Height ? Height+1 : Width+1                                                               ; Loop so that the full image scrolls once
{  Gdip_DrawImage(G, pBitmap, 1-A_Index, 1-A_Index, Width, Height, 0, 0, Width, Height)                  ; Draw the upper-left image
   Gdip_DrawImage(G, pBitmap, Width+1-A_Index, 1-A_Index, Width, Height, 0, 0, Width, Height)            ; Draw the upper-right image
   Gdip_DrawImage(G, pBitmap, Width+1-A_Index, Height+1-A_Index, Width, Height, 0, 0, Width, Height)     ; Draw the lower-right image
   Gdip_DrawImage(G, pBitmap, 1-A_Index, Height+1-A_Index, Width, Height, 0, 0, Width, Height)           ; Draw the lower-left image
;   Gdip_TextToGraphics(G, "Background Scrolling", Options, Font, Width, Height)                          ; Draw the text on top of the image
   UpdateLayeredWindow(hwnd, hdc, (A_ScreenWidth-Width)//2, (A_ScreenHeight-Height)//2, Width, Height)   ; Update the window
   Sleep, 40
   Gdip_GraphicsClear(G)
}
Return

Esc::
Exit:
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
ExitApp
Return
Btw thanks so much for GetMsgBoxFontInfo and SetMsgBoxFontInfo:
[Function] MsgBox Font Information - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=9122
Gui that looks like a MsgBox - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=9043

Which I used as a basis for JEE_SystemGetFont/JEE_SystemSetFont:
GUI COMMANDS: COMPLETE RETHINK (latest: get/set system fonts, ComboBox choose string notify) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 78#p131078

It was a revelation to find out that you could set the MsgBox font, and was/is very useful to me. I wonder how you came across it. I eventually managed to find a way to set the icon label font for files, which I added to the functions I mentioned.

Btw the scrolling script is slightly similar to one or two scripts I did here, in case you were interested (I believe I have another one which I will add soon):
Use Gdip to split a single image into multiple images - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=29101
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Masonjar13
Posts: 1473
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: graphics: Solar System

24 Jul 2017, 23:51

Two things:

1. Your last posted code, jeeswg, has a 1-pixel transparent border between the images, so you can clearly see there's more than one image. Also, it's unfortunately blurry, being up-scaled.

2. You might try using separate threads, so that you could do multiple things at once, say, both the solar system and image scrolling. I've got a thread manager class (that probably needs to be updated) on my Github that should work for that.

Edit: Just tried multi-threading it. It works, but every time I go to close them, it throws an error on both threads: CONTINUABLE EXCEPTION_ACCESS_VIOLATION - Mouse and Keyboard hooks have been disabled.. The background scrolling one is causing the crash; the solar system script runs fine on its own (on a separate thread), but the stars background will always crash.
OS: Windows 7 Ultimate | Editor: Notepad++
My Personal Function Library | Check Out My Computer Rig
User avatar
jeeswg
Posts: 6651
Joined: 19 Dec 2016, 01:58
Location: UK

Re: graphics: Solar System

25 Jul 2017, 00:19

@Masonjar13, I run them as separate scripts which worked fine, although your scripts re. threads sound interesting. I did notice the 1 pixel border, if that doesn't appear when it's at 100%, then why does it appear at 200%, or does it appear as well at 100% but less visible. Anyhow, I hadn't had a chance to investigate it yet, in case you happen to know the reason. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Masonjar13
Posts: 1473
Joined: 20 Jul 2014, 10:16
GitHub: Masonjar13
Location: Не Россия

Re: graphics: Solar System

25 Jul 2017, 00:36

Figured out why it was throwing an error.. I didn't realize the scrolling background script had Esc::, which I was using in the main script to close them. Removing that, works like a charm :) though I recommend adding #noTrayIcon to both of them. The mini DLL's won't work, of course, just in case anyone decides to try that.

Code: Select all

#singleInstance force
#persistent
#include *i <Lib_1>
#include <threadMan>

solarSystem:=new threadMan(fInstall . "\..\dlls\AutoHotkey_64.dll") ; change DLL path accordingly
stars:=new threadMan(fInstall . "\..\dlls\AutoHotkey_64.dll") ; change DLL path accordingly

solarSystem.newFromFile("Solar System.ahk")
stars.newFromFile("Background Scrolling 2.ahk")

esc::exitApp
And no, I don't know why that happens. Though, you could perhaps fix that by having them overlap by 1-pixel on the top and left sides.
OS: Windows 7 Ultimate | Editor: Notepad++
My Personal Function Library | Check Out My Computer Rig

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Kellyzkorner_NJ and 36 guests