-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
70 lines (56 loc) · 1.8 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad
import Data.Complex
import Data.Word
import Foreign.C.Types
import Linear.V2
import SDL
import SDL.Vect
import System.Environment
mapRange n stop1 start2 stop2 = (n / stop1 * (stop2 - start2)) + start2
mapWidth n = mapRange n width (-2) 2
mapHeight n = mapRange n height (-2) 2
width = 600
height = 600
data Pointo = Pointo
{ x :: CInt,
y :: CInt,
it :: Word8
}
getX Pointo {x=x} = x
getY Pointo {y=y} = y
getIt Pointo {it=it} = it
renderor :: Renderer -> Pointo -> IO ()
renderor renderer its = do
let it = getIt its
rendererDrawColor renderer $= V4 0 it 200 255
drawPoint renderer (P (V2 (getX its) (getY its)))
iterations :: Complex Double -> Complex Double -> Word8 -> Pointo
iterations c z it = do
let c' = mapWidth (realPart c) :+ mapHeight (imagPart c)
if realPart (abs z) < 2 && it < 255 then iterations c ((z * z) + c') (it + 1) else Pointo {it = it, x = round $ realPart c, y = round $ imagPart c}
renderMandel renderer = do
let c = concatMap (\x -> map (:+ x) [0 .. height]) [0 .. width]
its = map (\x -> iterations x (0.0 :+ 0.0) 0) c
mapM_ (renderor renderer) its
appLoop :: Renderer -> IO ()
appLoop renderer = do
events <- pollEvents
let eventIsKeyPress event = case eventPayload event of
KeyboardEvent keyboardEvent -> keyboardEventKeyMotion keyboardEvent == Pressed
_ -> False
keyPressed = any eventIsKeyPress events
rendererDrawColor renderer $= V4 0 0 255 255
clear renderer
renderMandel renderer
present renderer
unless keyPressed (appLoop renderer)
main :: IO ()
main = do
initializeAll
window <- createWindow "Hi" defaultWindow
renderer <- createRenderer window (-1) defaultRenderer
appLoop renderer
destroyWindow window