The watch
Posted: Tue Sep 20, 2016 9:35 pm
Edit3: Works now on PB 5.43 and 5.50
Added the CreateTorus() from the link below
http://www.purebasic.fr/english/viewtop ... reatetorus
Bye.
Added the CreateTorus() from the link below
http://www.purebasic.fr/english/viewtop ... reatetorus
Code: Select all
;Simple Watch
;Needs active network to download the media files
EnableExplicit
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UseZipPacker()
If InitEngine3D(#PB_Engine3D_DebugLog) = 0
MessageRequester("Engine error", "Check the output logfile")
End
EndIf
If InitSprite() = 0
MessageRequester("Sprite init error", "Exiting")
EndIf
If InitMouse() = 0
End
EndIf
If InitKeyboard() = 0
End
EndIf
If InitNetwork() = 0
End
EndIf
Structure _Obj
id.i
ma.i
ms.i
tx.i[3]
EndStructure
Structure _Objects
Inner._Obj
outer._Obj
Knob._Obj
Hours._Obj
Minut._Obj
Secs._Obj
Glass._Obj
Back._Obj
SunB._Obj
Moon._Obj
dots._Obj[4]
nod.i[4]
EndStructure
Structure _Par
id.i
ma.i
tx.i
no.i
EndStructure
Structure _Navigate
x.f
y.f
z.f
mx.i
my.i
EndStructure
Structure _media
gold.i
silver.i
astro.i
moon.i
fn.i
EndStructure
Declare.i GetData()
Declare.i MakeWatch()
Declare.i CreateToruss(MinorRadius.f, MajorRadius.f, Meridian = 32, Parallel = 32) ;Starbootic's torus Code
Global m._media, ret.i, ev.i, pck.i, path.s = GetPathPart(ProgramFilename())
Global ver = #PB_Compiler_Version
Global ob._Objects, pa._Par, nav._Navigate, el.i
AntialiasingMode(#PB_AntialiasingMode_x6)
OpenWindow(0, 0, 0, 800, 600, "Time - Use WASD and mouse to navigate. By DK_PETER.", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
Add3DArchive(path, #PB_3DArchive_FileSystem)
ret = GetData()
ret = MakeWatch()
CreateCamera(0, 0, 0, 100, 100)
MoveCamera(0, 0, 0, 4)
el = ElapsedMilliseconds()
Repeat
Repeat
ev = WindowEvent()
Until ev = 0
ExamineMouse()
nav\mx = -MouseDeltaX()/10
nav\my = -MouseDeltaY()/10
ExamineKeyboard()
If KeyboardPushed(#PB_Key_A)
nav\x = -0.05
ElseIf KeyboardPushed(#PB_Key_D)
nav\x = 0.05
Else
nav\x = 0
EndIf
If KeyboardPushed(#PB_Key_W)
nav\y = -0.05
ElseIf KeyboardPushed(#PB_Key_S)
nav\y = 0.05
Else
nav\y = 0
EndIf
RotateCamera(0, nav\my, nav\mx, 0, #PB_Relative)
MoveCamera(0, nav\x, 0, nav\y)
If ElapsedMilliseconds()-el > 300
SetMaterialColor(pa\ma, #PB_Material_AmbientColor, RGB(Random(255, 100), Random(255,100),Random(255,100)))
el = ElapsedMilliseconds()
EndIf
RotateNode(ob\nod[1], 0, 0, -Second(Date())*360/60, #PB_Absolute)
RotateNode(ob\nod[2], 0, 0, -Minute(Date())*360/60, #PB_Absolute)
RotateNode(ob\nod[3], 0, 0, -Hour(Date())*360/12, #PB_Absolute)
RotateNode(pa\no, 1, 1, 1, #PB_Relative)
RenderWorld()
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
Procedure.i MakeWatch()
Protected x.i, y.i, im.i
If IsImage(m\astro) > 0
im = CreateImage(#PB_Any, 2048, 2048, 32)
StartDrawing(ImageOutput(im))
DrawImage(ImageID(m\astro), 512, 256, 1024, 1536)
DrawingMode(#PB_2DDrawing_Gradient|#PB_2DDrawing_AlphaBlend)
FrontColor($AA888888)
BackColor($00888888)
EllipticalGradient(OutputWidth() / 2, OutputHeight() / 2, OutputWidth() / 4, OutputHeight() / 3)
Ellipse(OutputWidth() / 2, OutputHeight() / 2, OutputWidth(), OutputHeight())
StopDrawing()
CompilerIf #PB_Compiler_Version = 550
ob\Inner\tx[0] = CreateTexture(#PB_Any, 2048, 2048, "Astro")
CompilerElse
ob\Inner\tx[0] = CreateTexture(#PB_Any, 2048, 2048)
CompilerEndIf
StartDrawing(TextureOutput(ob\Inner\tx[0]))
DrawImage(ImageID(im), 0, 0)
StopDrawing()
FreeImage(im)
EndIf
With ob\Inner
\ms = CreateSphere(#PB_Any, 1, 50, 50)
TransformMesh(\ms, 0, 0, 0, 1, 1, 0.01, 0, 0, 0)
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
RotateMaterial(\ma, 180, #PB_Material_Fixed)
ScrollMaterial(\ma, 1024, 0, #PB_Material_Fixed)
MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $FFFFFF)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
RotateEntity(\id, 180, 0, 0)
EndWith
With ob\outer
CompilerIf #PB_Compiler_Version >= 550
\ms = CreateTorus(#PB_Any, 1, 0.03, 50, 70)
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Outer")
CompilerElse
\ms = CreateToruss(0.03, 1, 50, 70)
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(\tx[0]))
DrawImage(ImageID(m\gold), 0, 0, OutputWidth(), OutputHeight())
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
ScaleMaterial(\ma, 30, 90)
RotateMaterial(\ma, 90, #PB_Material_Fixed)
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $73D4FB)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0)
CompilerIf #PB_Compiler_Version >= 550
RotateEntity(\id, 90, 0, 0)
CompilerEndIf
EndWith
With ob\Knob
\ms = CreateCylinder(#PB_Any, 0.05, 0.05, 30, 30, #True)
CompilerIf #PB_Compiler_Version >= 550
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Knob")
CompilerElse
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(\tx[0]))
DrawImage(ImageID(m\gold), 0, 0, OutputWidth(), OutputHeight())
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $7394FB)
ScaleMaterial(\ma, 50, 3)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 1.03, 0, 0)
RotateEntity(\id, 90, 90, 0)
EndWith
With ob\Glass
\ms = CreateSphere(#PB_Any, 1, 50, 50)
TransformMesh(\ms, 0, 0, 0, 1, 1, 0.07, 0, 0, 0)
CompilerIf #PB_Compiler_Version >= 550
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Glass")
CompilerElse
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(\tx[0]))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0, 0, 1024, 1024, $49F9ED9B)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
MaterialBlendingMode(\ma, #PB_Material_AlphaBlend)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, 0.025)
RotateEntity(\id, 180, 0, 0)
EndWith
With ob\Back
\ms = CreateSphere(#PB_Any, 1, 50, 50)
TransformMesh(\ms, 0, 0, 0, 1, 1, 0.03, 0, 0, 0)
CompilerIf #PB_Compiler_Version >= 550
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Back")
CompilerElse
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(\tx[0]))
DrawingMode(#PB_2DDrawing_AllChannels)
DrawImage(ImageID(m\gold), 0, 0, OutputWidth(), OutputHeight())
DrawingMode(#PB_2DDrawing_Transparent)
DrawingFont(FontID(m\fn))
DrawText(350, 250, "Too often we give", $111111)
DrawText(352, 252, "Too often we give", $FFFFFF)
DrawText(300, 300, "children answers to remember", $111111)
DrawText(302, 302, "children answers to remember", $FFFFFF)
DrawText(330, 350, "rather than problems to", $111111)
DrawText(332, 352, "rather than problems to", $FFFFFF)
DrawText(450, 400, "solve", $111111)
DrawText(452, 402, "solve", $FFFFFF)
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $7394FB)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0, -0.025)
EndWith
With ob\Secs
\ms = CreateCube(#PB_Any, 0.1)
CompilerIf #PB_Compiler_Version >= 550
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Silver1")
CompilerElse
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(ob\Secs\tx[0]))
DrawImage(ImageID(m\silver), 0, 0, OutputWidth(), OutputHeight())
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $EEEEEE)
ScaleMaterial(\ma, 0.3, 30)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0.4, 0.01)
ScaleEntity(\id, 0.1, 9.2, 0.1)
EndWith
With ob\Minut
\ms = CreateCube(#PB_Any, 0.1)
CompilerIf #PB_Compiler_Version >= 550
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Silver2")
CompilerElse
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(ob\Minut\tx[0]))
DrawImage(ImageID(m\silver), 0, 0, OutputWidth(), OutputHeight())
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $EEEEEE)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0.35, 0.01)
ScaleEntity(\id, 0.16, 8.1, 0.1)
EndWith
With ob\Hours
\ms = CreateCube(#PB_Any, 0.1)
CompilerIf #PB_Compiler_Version >= 550
\tx[0] = CreateTexture(#PB_Any, 1024, 1024, "Silver3")
CompilerElse
\tx[0] = CreateTexture(#PB_Any, 1024, 1024)
CompilerEndIf
StartDrawing(TextureOutput(ob\Hours\tx[0]))
DrawImage(ImageID(m\silver), 0, 0, OutputWidth(), OutputHeight())
StopDrawing()
\ma = CreateMaterial(#PB_Any, TextureID(\tx[0]))
SetMaterialColor(\ma, #PB_Material_SelfIlluminationColor, $EEEEEE)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0.2, 0.01)
ScaleEntity(\id, 0.25, 6.1, 0.1)
EndWith
With ob\dots[0]
\ms = CreateCube(#PB_Any, 0.03)
\ma = CopyMaterial(ob\outer\ma, #PB_Any)
ScaleMaterial(\ma, 0.02, 0.05)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, 0.9, 0)
ScaleEntity(\id, 1, 3, 1)
EndWith
With ob\dots[1]
\ms = CreateCube(#PB_Any, 0.03)
\ma = CopyMaterial(ob\outer\ma, #PB_Any)
ScaleMaterial(\ma, 0.02, 0.05)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0.9, 0, 0)
ScaleEntity(\id, 3, 1, 1)
EndWith
With ob\dots[2]
\ms = CreateCube(#PB_Any, 0.03)
\ma = CopyMaterial(ob\outer\ma, #PB_Any)
ScaleMaterial(\ma, 0.02, 0.05)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), 0, -0.9, 0)
ScaleEntity(\id, 1, 3, 1)
EndWith
With ob\dots[3]
\ms = CreateCube(#PB_Any, 0.03)
\ma = CopyMaterial(ob\outer\ma, #PB_Any)
ScaleMaterial(\ma, 0.02, 0.05)
\id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), -0.9, 0, 0)
ScaleEntity(\id, 3, 1, 1)
EndWith
ob\nod[0] = CreateNode(#PB_Any, 0, 0, 0)
ob\nod[1] = CreateNode(#PB_Any, 0, 0, 0)
ob\nod[2] = CreateNode(#PB_Any, 0, 0, 0)
ob\nod[3] = CreateNode(#PB_Any, 0, 0, 0)
AttachNodeObject(ob\nod[0], EntityID(ob\Inner\id))
AttachNodeObject(ob\nod[0], EntityID(ob\outer\id))
AttachNodeObject(ob\nod[0], EntityID(ob\Back\id))
AttachNodeObject(ob\nod[0], EntityID(ob\Glass\id))
AttachNodeObject(ob\nod[0], EntityID(ob\Knob\id))
AttachNodeObject(ob\nod[1], EntityID(ob\Secs\id))
AttachNodeObject(ob\nod[2], EntityID(ob\Minut\id))
AttachNodeObject(ob\nod[3], EntityID(ob\Hours\id))
ProcedureReturn #True
EndProcedure
Procedure.i GetData()
Protected Res.i = 0
res = ReceiveHTTPFile("https://pixabay.com/static/uploads/photo/2016/09/12/15/13/astronaut-1664814_960_720.jpg", path + "astro.jpg")
If res > 0
m\astro = LoadImage(#PB_Any, Path + "astro.jpg")
Else
m\astro = CreateImage(#PB_Any, 256, 256, 32)
StartDrawing(ImageOutput(m\astro))
FrontColor($FFFFFF) : BackColor($535454)
CircularGradient(128, 128, 120)
Circle(128, 128, 110)
StopDrawing()
EndIf
res = ReceiveHTTPFile("http://forum.celestialmatters.org/userpix/4_moon_global_1k_1.jpg", path + "moon.jpg")
If res > 0
m\moon = LoadImage(#PB_Any, Path + "moon.jpg")
Else
m\moon = CreateImage(#PB_Any, 256, 256, 32)
StartDrawing(ImageOutput(m\moon))
Box(0, 0, 256, 256, $C0C0C0)
StopDrawing()
EndIf
res = ReceiveHTTPFile("http://pre00.deviantart.net/45b3/th/pre/f/2007/224/7/6/texture_71___gold_by_wanderingsoul_stox.jpg", path + "gold.jpg")
If res > 0
m\gold = LoadImage(#PB_Any, Path + "gold.jpg")
Else
m\gold = CreateImage(#PB_Any, 256, 256, 32)
StartDrawing(ImageOutput(m\gold))
DrawingMode(#PB_2DDrawing_Gradient)
FrontColor($1FDAFF) : BackColor($2099B1)
LinearGradient(128, 0, 128, 256)
Box(0, 0, 256, 256)
StopDrawing()
EndIf
res = ReceiveHTTPFile("https://images.freecreatives.com/wp-content/uploads/2016/01/Free-Silver-Metal-Texture1.jpg", path + "silver.jpg")
If Res > 0
m\silver = LoadImage(#PB_Any, Path + "silver.jpg")
Else
m\silver = CreateImage(#PB_Any, 256, 256, 32)
StartDrawing(ImageOutput(m\silver))
DrawingMode(#PB_2DDrawing_Gradient)
FrontColor($FFFFFF) : BackColor($ADADAD)
LinearGradient(128, 0, 128, 256)
Box(0, 0, 256, 256)
StopDrawing()
EndIf
CompilerIf #PB_Compiler_Version >= 550
res = ReceiveHTTPFile("http://dl.dafont.com/dl/?f=blackknightflf", path + "bknight.zip")
If res > 0
pck = OpenPack(#PB_Any, path + "bknight.zip")
If pck > 0
UncompressPackFile(pck, path + "BlackKnightFLF.ttf", "BlackKnightFLF.ttf")
RegisterFontFile(path + "BlackKnightFLF.ttf")
m\fn = LoadFont(#PB_Any, "BlackKnightFLF", 36)
ClosePack(pck)
EndIf
EndIf
CompilerElse
m\fn = LoadFont(#PB_Any, "Arial", 24)
CompilerEndIf
res = ReceiveHTTPFile("https://www.digitalanarchy.com/knollSPK/pix/knoll-starburst-200.jpg", path + "star.jpg")
If res > 0
pa\tx = LoadTexture(#PB_Any, "star.jpg")
pa\ma = CreateMaterial(#PB_Any, TextureID(pa\tx))
MaterialBlendingMode(pa\ma, #PB_Material_Add)
pa\id = CreateParticleEmitter(#PB_Any, 500, 0.2, 0.3, #PB_Particle_Box, 0, 0, 0)
ParticleMaterial(pa\id, MaterialID(pa\ma))
ParticleEmissionRate(pa\id, 10000)
ParticleEmitterDirection(pa\id, 0, 0, 1)
ParticleSize(pa\id, 5, 5)
ParticleTimeToLive(pa\id, 1, 2)
CompilerIf #PB_Compiler_Version >= 550
ParticleVelocity(pa\id, #PB_Particle_MinimumVelocity, 4)
ParticleVelocity(pa\id, #PB_Particle_Velocity, 10)
ParticleVelocity(pa\id, #PB_Particle_MaximumVelocity, 20)
CompilerElse
ParticleVelocity(pa\id, 4, 20)
CompilerEndIf
pa\no = CreateNode(#PB_Any, 0, 0, -100)
AttachNodeObject(pa\no, ParticleEmitterID(pa\id))
EndIf
ProcedureReturn #True
EndProcedure
Macro LinearlySpacedValue(IncrementID, IncrementMax, MinValue, MaxValue)
((MinValue) + ((MaxValue) - (MinValue)) * ((IncrementID) / (IncrementMax)))
EndMacro
Macro CalculateTorusVertex(MinorRadius, MajorRadius, Uxx, Vxx)
MeshVertexPosition((MajorRadius + MinorRadius * Cos(Vxx)) * Cos(Uxx), (MajorRadius + MinorRadius * Cos(Vxx)) * Sin(Uxx), MinorRadius * Sin(Vxx))
MeshVertexNormal(0, 1, 0)
MeshVertexTextureCoordinate(Uxx / (2*#PI), Vxx / (2*#PI))
EndMacro
Procedure.i CreateToruss(MinorRadius.f, MajorRadius.f, Meridian = 32, Parallel = 32)
Protected Handle, P00, P01, P02, P03, MeridianID, U00.d, U01.d, ParallelID, V00.d, V01.d
Handle = CreateMesh(#PB_Any)
If Meridian < 3
Meridian = 3
EndIf
If Parallel < 3
Parallel = 3
EndIf
P00 = 0
P01 = P00 + 1
P02 = P01 + 1
P03 = P02 + 1
For MeridianID = 0 To Meridian - 1
U00 = LinearlySpacedValue(MeridianID, Meridian, 0.0, 2.0 * #PI)
U01 = LinearlySpacedValue(MeridianID + 1, Meridian, 0.0, 2.0 * #PI)
For ParallelID = 0 To Parallel - 1
V00 = LinearlySpacedValue(ParallelID, Parallel, 0.0, 2.0 * #PI)
V01 = LinearlySpacedValue(ParallelID + 1, Parallel, 0.0, 2.0 * #PI)
CalculateTorusVertex(MinorRadius, MajorRadius, U00, V00) ; P00
CalculateTorusVertex(MinorRadius, MajorRadius, U01, V00) ; P01
CalculateTorusVertex(MinorRadius, MajorRadius, U01, V01) ; P02
CalculateTorusVertex(MinorRadius, MajorRadius, U00, V01) ; P03
MeshFace(P00, P01, P02)
MeshFace(P02, P03, P00)
P00 = P03 + 1
P01 = P00 + 1
P02 = P01 + 1
P03 = P02 + 1
Next
Next
FinishMesh(#True)
NormalizeMesh(Handle)
BuildMeshTangents(Handle)
ProcedureReturn Handle
EndProcedure