Basic Univers
;*********************************
; soleil,terre,lune, perspective cavalière
; PB V4.02
;*********************************
; constantes
#l_ecran = 1280
#h_ecran = 1024

#e = 8 ; grandeur caracteristique pour les dessins
#l_cadre=(#l_ecran - 3*#e)*0.5
#h_cadre=(#h_ecran - 3*#e)*0.5

;**********cadre A**********
#x_cadre_a=#e
#y_cadre_a=#h_ecran-#e
#x_soleil=#x_cadre_a+#l_cadre*0.5
#y_soleil=#y_cadre_a-#h_cadre*0.5
#r_soleil = 4*#e
#r_terre_cadre_a = 2*#e
#r_lune_cadre_a = 0.5*#e
#rayon_orbite_terre_a = 7*#r_soleil
#rayon_orbite_lune_a=#r_soleil
;**********cadre B**********
#x_cadre_b = 2*#e+#l_cadre
#y_cadre_b=#y_cadre_a
#x_terre_cadre_b=#x_cadre_b+#l_cadre*0.5
#y_terre_cadre_b=#y_cadre_b-#h_cadre*0.5
#r_terre_cadre_b = 4*#e
#r_lune_cadre_b = 2*#e
#changement_echelle_lune_b = 7
;**********cadre C**********
#x_cadre_c=#x_cadre_b
#y_cadre_c=#e+#h_cadre
;**********cadre D**********
#x_cadre_d=#x_cadre_a
#y_cadre_d=#y_cadre_c

;**********Divers*********
#coefficient_quadrilatere = 7.2
#k_cos_alpha = 0.35
#k_sin_alpha = 0.35
#trimestre_1 = 365*0.25
#trimestre_2 = 365*0.5
#trimestre_3 = 365*0.75


Enumeration
#spr_arriere_plan
#spr_terreA_d
#spr_terreA_p
#spr_lune_A
#spr_terreB_d
#spr_terreB_p
#spr_luneB_d
#spr_luneB_p

EndEnumeration
Structure donnees
x_3D.f
y_3D.f
z_3D.f
x_2D.f
y_2D.f
phase.f
rayon_orbite.f
inclinaison_orbite.f
EndStructure

; variables
nombres_etoile.l
x_etoile.l
y_etoile.l
jour.l = 0
terre.donnees
lune.donnees


Procedure.f conversion_3D_2D(*astre.donnees)
*astre\x_2D =*astre\x_3D +#k_cos_alpha**astre\y_3D
*astre\y_2D =*astre\z_3D +#k_sin_alpha**astre\y_3D
EndProcedure
Procedure.f rotation_selon_axe_y(*coordonnees.donnees)
*coordonnees\x_3D =*coordonnees\x_3D*Cos(*coordonnees\inclinaison_orbite)+*coordonnees\z_3D*Sin(*coordonnees\inclinaison_orbite)
*coordonnees\z_3D =-*coordonnees\x_3D*Sin(*coordonnees\inclinaison_orbite)+*coordonnees\z_3D*Cos(*coordonnees\inclinaison_orbite)
EndProcedure
Procedure.f tracer_quadrilatere_MNOP(x_centre.f, y_centre.f, rotation.l)
M.donnees
N.donnees
O.donnees
P.donnees

M\x_3D =-#coefficient_quadrilatere*#r_soleil
M\y_3D =#coefficient_quadrilatere*#r_soleil
M\z_3D = 0
N\x_3D =#coefficient_quadrilatere*#r_soleil
N\y_3D =#coefficient_quadrilatere*#r_soleil
N\z_3D = 0
O\x_3D =#coefficient_quadrilatere*#r_soleil
O\y_3D =-#coefficient_quadrilatere*#r_soleil
O\z_3D = 0
P\x_3D =-#coefficient_quadrilatere*#r_soleil
P\y_3D =-#coefficient_quadrilatere*#r_soleil
P\z_3D = 0
If rotation<>0
    M\inclinaison_orbite = rotation*#PI/180
    rotation_selon_axe_y(@M)
    N\inclinaison_orbite = rotation*#PI/180
    rotation_selon_axe_y(@N)
    O\inclinaison_orbite = rotation*#PI/180
    rotation_selon_axe_y(@O)
    P\inclinaison_orbite = rotation*#PI/180
    rotation_selon_axe_y(@P)
EndIf
conversion_3D_2D(@M)
conversion_3D_2D(@N)
conversion_3D_2D(@O)
conversion_3D_2D(@P)
StartDrawing(SpriteOutput(#spr_arriere_plan))
; DrawText(M\xA_2D+x_centre,#h_ecran*0.5-(M\yA_2D+y_centre+2*#intercadre),"Plan de l'écliptique")
LineXY(M\x_2D + x_centre, #h_ecran -(M\y_2D + y_centre), N\x_2D + x_centre, #h_ecran -(N\y_2D + y_centre), RGB(55, 55, 55))
LineXY(P\x_2D + x_centre, #h_ecran -(P\y_2D + y_centre), O\x_2D + x_centre, #h_ecran -(O\y_2D + y_centre), RGB(55, 55, 55))
LineXY(N\x_2D + x_centre, #h_ecran -(N\y_2D + y_centre), O\x_2D + x_centre, #h_ecran -(O\y_2D + y_centre), RGB(55, 55, 55))
LineXY(M\x_2D + x_centre, #h_ecran -(M\y_2D + y_centre), P\x_2D + x_centre, #h_ecran -(P\y_2D + y_centre), RGB(55, 55, 55))
StopDrawing()

EndProcedure
Procedure.f tracer_cercle(*coordonnees.donnees, inclinaison.l)

EndProcedure

InitSprite()
InitMouse()
InitKeyboard()
OpenScreen(#l_ecran, #h_ecran, 32, "Soleil, Terre, Lune 3D")

;**********création du sprite de fond d'écran**********
CreateSprite(#spr_arriere_plan, #l_ecran, #h_ecran)
StartDrawing(SpriteOutput(#spr_arriere_plan))
DrawingMode(#PB_2DDrawing_Outlined )
; dessin des cadres
Box(#x_cadre_a, #h_ecran-#y_cadre_a, #l_cadre, #h_cadre, RGB(255, 255, 255))
Box(#x_cadre_b, #h_ecran-#y_cadre_b, #l_cadre, #h_cadre, RGB(255, 255, 255))
Box(#x_cadre_c, #h_ecran-#y_cadre_c, #l_cadre, #h_cadre, RGB(255, 255, 255))
Box(#x_cadre_d, #h_ecran-#y_cadre_d, #l_cadre, #h_cadre, RGB(255, 255, 255))
; fond étoilé
For nombre_etoile = 0 To 1000
    x_etoile =#e + Random(2*#l_cadre+#e)
    y_etoile =#e + Random(2*#h_cadre+#e)
 
        ; cadre A
        If  x_etoile>#x_cadre_a And x_etoile<#x_cadre_a+#l_cadre And y_etoile>(#h_ecran-#y_cadre_a) And y_etoile<(#h_ecran-#y_cadre_a+#h_cadre)
           Plot(x_etoile, y_etoile, RGB(200, 200, 200))
        ; cadre B
        ElseIf  x_etoile>#x_cadre_b And x_etoile<#x_cadre_b+#l_cadre And y_etoile>(#h_ecran-#y_cadre_b) And y_etoile<(#h_ecran-#y_cadre_b+#h_cadre)
           Plot(x_etoile, y_etoile, RGB(200, 200, 200))
        ; cadre C
        ElseIf  x_etoile>#x_cadre_c And x_etoile<#x_cadre_c+#l_cadre And y_etoile>(#h_ecran-#y_cadre_c) And y_etoile<(#h_ecran-#y_cadre_c+#h_cadre)
           Plot(x_etoile, y_etoile, RGB(200, 200, 200))
        EndIf
Next nombre_etoile

; dessin du  Soleil)
DrawingMode(#PB_2DDrawing_Default)
For i =#r_soleil To 1 Step - 1
Circle(#x_soleil,(#h_ecran-#y_soleil), i, RGB(255, 255 - 2*i, 255 - 4*i))
Next i
StopDrawing()
; tracé des plans
tracer_quadrilatere_MNOP(#x_soleil, #y_soleil, 0)
tracer_quadrilatere_MNOP(#x_terre_cadre_b, #y_terre_cadre_b, 0)
tracer_quadrilatere_MNOP(#x_terre_cadre_b, #y_terre_cadre_b, - 5)

; dessins de la Terre cadre A
CreateSprite(#spr_terreA_d, 2*#r_terre_cadre_a, 2*#r_terre_cadre_a)
StartDrawing(SpriteOutput(#spr_terreA_d))
DrawingMode(#PB_2DDrawing_Outlined )
Circle(#r_terre_cadre_a, #r_terre_cadre_a, #r_terre_cadre_a, RGB(0, 0, 155))
Line(#r_terre_cadre_a, 0, 0, 2*#r_terre_cadre_a, RGB(0, 0, 155))
FillArea(#r_terre_cadre_a - 2, #r_terre_cadre_a, RGB(0, 0, 155), RGB(0, 0, 155))
FillArea(#r_terre_cadre_a + 2, #r_terre_cadre_a, RGB(0, 0, 155), RGB(0, 0, 100))
StopDrawing()

CreateSprite(#spr_terreA_p, 2*#r_terre_cadre_a, 2*#r_terre_cadre_a)
StartDrawing(SpriteOutput(#spr_terreA_p))
DrawingMode(#PB_2DDrawing_Outlined )
Circle(#r_terre_cadre_a, #r_terre_cadre_a, #r_terre_cadre_a, RGB(0, 0, 155))
Line(#r_terre_cadre_a, 0, 0, 2*#r_terre_cadre_a, RGB(0, 0, 155))
FillArea(#r_terre_cadre_a + 2, #r_terre_cadre_a, RGB(0, 0, 155), RGB(0, 0, 155))
FillArea(#r_terre_cadre_a - 2, #r_terre_cadre_a, RGB(0, 0, 155), RGB(0, 0, 100))
StopDrawing()

; dessins de la Terre cadre B
CreateSprite(#spr_terreB_d, 2*#r_terre_cadre_b, 2*#r_terre_cadre_b)
StartDrawing(SpriteOutput(#spr_terreB_d))
DrawingMode(#PB_2DDrawing_Outlined )
Circle(#r_terre_cadre_b, #r_terre_cadre_b, #r_terre_cadre_b, RGB(0, 0, 155))
Line(#r_terre_cadre_b, 0, 0, 2*#r_terre_cadre_b, RGB(0, 0, 155))
FillArea(#r_terre_cadre_b - 2, #r_terre_cadre_b, RGB(0, 0, 155), RGB(0, 0, 155))
FillArea(#r_terre_cadre_b + 2, #r_terre_cadre_b, RGB(0, 0, 155), RGB(0, 0, 100))
StopDrawing()

CreateSprite(#spr_terreB_p, 2*#r_terre_cadre_b, 2*#r_terre_cadre_b)
StartDrawing(SpriteOutput(#spr_terreB_p))
DrawingMode(#PB_2DDrawing_Outlined )
Circle(#r_terre_cadre_b, #r_terre_cadre_b, #r_terre_cadre_b, RGB(0, 0, 155))
Line(#r_terre_cadre_b, 0, 0, 2*#r_terre_cadre_b, RGB(0, 0, 155))
FillArea(#r_terre_cadre_b + 2, #r_terre_cadre_b, RGB(0, 0, 155), RGB(0, 0, 155))
FillArea(#r_terre_cadre_b - 2, #r_terre_cadre_b, RGB(0, 0, 155), RGB(0, 0, 100))
StopDrawing()


; dessins de la Lune cadre B
CreateSprite(#spr_luneB_d, 2*#r_lune_cadre_b, 2*#r_lune_cadre_b)
StartDrawing(SpriteOutput(#spr_luneB_d))
DrawingMode(#PB_2DDrawing_Outlined )
Circle(#r_lune_cadre_b, #r_lune_cadre_b, #r_lune_cadre_b, RGB(100, 100, 100))
Line(#r_lune_cadre_b, 0, 0, 2*#r_lune_cadre_b, RGB(100, 100, 100))
FillArea(#r_lune_cadre_b - 2, #r_lune_cadre_b, RGB(100, 100, 100), RGB(200, 200, 200))
FillArea(#r_lune_cadre_b + 2, #r_lune_cadre_b, RGB(100, 100, 100), RGB(100, 100, 100))
StopDrawing()

CreateSprite(#spr_luneB_p, 2*#r_lune_cadre_b, 2*#r_lune_cadre_b)
StartDrawing(SpriteOutput(#spr_luneB_p))
DrawingMode(#PB_2DDrawing_Outlined )
Circle(#r_lune_cadre_b, #r_lune_cadre_b, #r_lune_cadre_b, RGB(100, 100, 100))
Line(#r_lune_cadre_b, 0, 0, 2*#r_lune_cadre_b, RGB(100, 100, 100))
FillArea(#r_lune_cadre_b + 2, #r_lune_cadre_b, RGB(100, 100, 100), RGB(200, 200, 200))
FillArea(#r_lune_cadre_b - 2, #r_lune_cadre_b, RGB(100, 100, 100), RGB(100, 100, 100))
StopDrawing()

CreateSprite(#spr_lune_A, 2*#r_lune_cadre_a, 2*#r_lune_cadre_a)
StartDrawing(SpriteOutput(#spr_lune_A))
DrawingMode(#PB_2DDrawing_Default)
Circle(#r_lune_cadre_a, #r_lune_cadre_a, #r_lune_cadre_a, RGB(150, 150, 150))
StopDrawing()



; initialisation
 terre\rayon_orbite =#rayon_orbite_terre_a
 terre\x_3D = #rayon_orbite_terre_a
 terre\y_3D = 0
 terre\z_3D = 0
 terre\phase = 2*#PI/365
 
lune\rayon_orbite =#rayon_orbite_lune_a
lune\inclinaison_orbite =- 5*#PI/180
lune\x_3D =#rayon_orbite_lune_a
lune\y_3D = 0
lune\z_3D = 0
lune\phase = 2*#PI/28
 
Repeat
    ExamineMouse()
    ExamineKeyboard()
    Delay(100)
    DisplaySprite(#spr_arriere_plan, 0, 0)

    terre\x_3D = terre\rayon_orbite*Cos(terre\phase*jour)
    terre\y_3D = terre\rayon_orbite*Sin(terre\phase*jour)
    terre\z_3D = 0
    conversion_3D_2D(@terre)
   
    lune\x_3D = lune\rayon_orbite*Cos(lune\phase*jour)
    lune\y_3D = lune\rayon_orbite*Sin(lune\phase*jour)
    lune\z_3D = 0
    rotation_selon_axe_y(@lune)
    conversion_3D_2D(@lune)
   
    ; cadre A
;     DrawingMode(#PB_2DDrawing_Outlined )
;     Circle(terre\x_2D+#x_soleil,#h_ecran-( terre\y_2D+#y_soleil),#r_terre,RGB(0,0,255))
;     LineXY(#x_soleil,#h_ecran-#y_soleil,terre\x_2D+#x_soleil,#h_ecran-( terre\y_2D+#y_soleil),RGB(255,255,255))
;     Ellipse(terre\x_2D+#x_soleil,#h_ecran-( terre\y_2D+#y_soleil),#r_terre*Sin( terre\phase*jour),#r_terre,RGB(0,0,255))
;     Circle(lune\x_2D+terre\x_2D+#x_soleil,#h_ecran-(lune\y_2D+ terre\y_2D+#y_soleil),#r_lune,RGB(200,200,200))


   ; DrawingMode(#PB_2DDrawing_Default  )
 DisplayTransparentSprite(#spr_lune_A, lune\x_2D + terre\x_2D +#x_soleil-#r_lune_cadre_a, #h_ecran -(lune\y_2D + terre\y_2D +#y_soleil)-#r_lune_cadre_a)
 
If jour<#trimestre_1
    DisplayTransparentSprite(#spr_terreA_d, terre\x_2D +#x_soleil-#r_terre_cadre_a, #h_ecran -( terre\y_2D +#y_soleil)-#r_terre_cadre_a)
    DisplayTransparentSprite(#spr_terreB_d, #x_terre_cadre_b-#r_terre_cadre_b, #h_ecran-#y_terre_cadre_b-#r_terre_cadre_b)
    DisplayTransparentSprite(#spr_luneB_d, #changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b-#r_lune_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b)-#r_lune_cadre_b)
    StartDrawing(ScreenOutput())
    Ellipse(terre\x_2D +#x_soleil, #h_ecran -( terre\y_2D +#y_soleil), #r_terre_cadre_a*Sin( terre\phase*jour), #r_terre_cadre_a, RGB(0, 0, 155))
    Ellipse(#x_terre_cadre_b, #h_ecran-#y_terre_cadre_b, #r_terre_cadre_b*Sin( terre\phase*jour), #r_terre_cadre_b, RGB(0, 0, 155))
    Ellipse(#changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b), #r_lune_cadre_b*Sin( terre\phase*jour), #r_lune_cadre_b, RGB(200, 200, 200))
    StopDrawing()
ElseIf jour<#trimestre_2
    DisplayTransparentSprite(#spr_terreA_p, terre\x_2D +#x_soleil-#r_terre_cadre_a, #h_ecran -( terre\y_2D +#y_soleil)-#r_terre_cadre_a)
    DisplayTransparentSprite(#spr_luneB_p, #changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b-#r_lune_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b)-#r_lune_cadre_b)
    DisplayTransparentSprite(#spr_terreB_p, #x_terre_cadre_b-#r_terre_cadre_b, #h_ecran-#y_terre_cadre_b-#r_terre_cadre_b)
    StartDrawing(ScreenOutput())
    Ellipse(terre\x_2D +#x_soleil, #h_ecran -( terre\y_2D +#y_soleil), #r_terre_cadre_a*Sin( terre\phase*jour), #r_terre_cadre_a, RGB(0, 0, 155))
    Ellipse(#x_terre_cadre_b, #h_ecran-#y_terre_cadre_b, #r_terre_cadre_b*Sin( terre\phase*jour), #r_terre_cadre_b, RGB(0, 0, 155))
    Ellipse(#changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b), #r_lune_cadre_b*Sin( terre\phase*jour), #r_lune_cadre_b, RGB(200, 200, 200))
    StopDrawing()

ElseIf jour<#trimestre_3
    DisplayTransparentSprite(#spr_terreA_p, terre\x_2D +#x_soleil-#r_terre_cadre_a, #h_ecran -( terre\y_2D +#y_soleil)-#r_terre_cadre_a)
    DisplayTransparentSprite(#spr_luneB_p, #changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b-#r_lune_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b)-#r_lune_cadre_b)
    DisplayTransparentSprite(#spr_terreB_p, #x_terre_cadre_b-#r_terre_cadre_b, #h_ecran-#y_terre_cadre_b-#r_terre_cadre_b)
    StartDrawing(ScreenOutput())
    Ellipse(terre\x_2D +#x_soleil, #h_ecran -( terre\y_2D +#y_soleil), #r_terre_cadre_a*Sin( terre\phase*jour), #r_terre_cadre_a, RGB(0, 0, 100))
    Ellipse(#x_terre_cadre_b, #h_ecran-#y_terre_cadre_b, #r_terre_cadre_b*Sin( terre\phase*jour), #r_terre_cadre_b, RGB(0, 0, 100))
    Ellipse(#changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b), #r_lune_cadre_b*Sin( terre\phase*jour), #r_lune_cadre_b, RGB(100, 100, 100))
    StopDrawing()

ElseIf jour>#trimestre_3
    DisplayTransparentSprite(#spr_terreA_d, terre\x_2D +#x_soleil-#r_terre_cadre_a, #h_ecran -( terre\y_2D +#y_soleil)-#r_terre_cadre_a)
    DisplayTransparentSprite(#spr_luneB_d, #changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b-#r_lune_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b)-#r_lune_cadre_b)
    DisplayTransparentSprite(#spr_terreB_d, #x_terre_cadre_b-#r_terre_cadre_b, #h_ecran-#y_terre_cadre_b-#r_terre_cadre_b)
    StartDrawing(ScreenOutput())
    Ellipse(terre\x_2D +#x_soleil, #h_ecran -( terre\y_2D +#y_soleil), #r_terre_cadre_a*Sin( terre\phase*jour), #r_terre_cadre_a, RGB(0, 0, 100))
    Ellipse(#x_terre_cadre_b, #h_ecran-#y_terre_cadre_b, #r_terre_cadre_b*Sin( terre\phase*jour), #r_terre_cadre_b, RGB(0, 0, 100))
    Ellipse(#changement_echelle_lune_b*lune\x_2D +#x_terre_cadre_b, #h_ecran -(#changement_echelle_lune_b*lune\y_2D +#y_terre_cadre_b), #r_lune_cadre_b*Sin( terre\phase*jour), #r_lune_cadre_b, RGB(100, 100, 100))
    StopDrawing()

EndIf

    FlipBuffers()
 
jour = jour + 1
   
If jour>365
jour = 0
; initialisation
 terre\rayon_orbite =#rayon_orbite_terre_a
 terre\x_3D = #rayon_orbite_terre_a
 terre\y_3D = 0
 terre\z_3D = 0
 terre\phase = 2*#PI/365
 
lune\rayon_orbite =#rayon_orbite_lune_a
lune\x_3D =#rayon_orbite_lune_a
lune\y_3D = 0
lune\z_3D = 0
lune\phase = 2*#PI/28
lune\inclinaison_orbite =- 5*#PI/180

EndIf

Until MouseButton(#PB_MouseButton_Right)<>0 Or KeyboardPushed(#PB_Key_Escape)