|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +module RenderGeometry where |
| 4 | + |
| 5 | +import Control.Monad |
| 6 | +import Data.Word (Word8) |
| 7 | +import Foreign (castPtr, plusPtr, sizeOf) |
| 8 | +import Foreign.C.Types |
| 9 | +import SDL.Vect |
| 10 | +import qualified Data.ByteString as BS |
| 11 | +import qualified Data.Vector.Storable as V |
| 12 | +import System.Exit (exitFailure) |
| 13 | +import System.IO |
| 14 | + |
| 15 | +import SDL (($=)) |
| 16 | +import qualified SDL |
| 17 | +-- import qualified Graphics.Rendering.OpenGL as GL |
| 18 | +import SDL.Raw.Types (FPoint(..), Color(..)) |
| 19 | + |
| 20 | +screenWidth, screenHeight :: CInt |
| 21 | +(screenWidth, screenHeight) = (640, 480) |
| 22 | + |
| 23 | +main :: IO () |
| 24 | +main = do |
| 25 | + SDL.initialize [SDL.InitVideo] |
| 26 | + SDL.HintRenderScaleQuality $= SDL.ScaleLinear |
| 27 | + do renderQuality <- SDL.get SDL.HintRenderScaleQuality |
| 28 | + when (renderQuality /= SDL.ScaleLinear) $ |
| 29 | + putStrLn "Warning: Linear texture filtering not enabled!" |
| 30 | + |
| 31 | + window <- |
| 32 | + SDL.createWindow |
| 33 | + "SDL / OpenGL Example" |
| 34 | + SDL.defaultWindow |
| 35 | + { SDL.windowInitialSize = V2 screenWidth screenHeight |
| 36 | + , SDL.windowGraphicsContext = SDL.OpenGLContext SDL.defaultOpenGL |
| 37 | + { SDL.glProfile = SDL.Core SDL.Normal 3 2 |
| 38 | + } |
| 39 | + } |
| 40 | + SDL.showWindow window |
| 41 | + |
| 42 | + -- SDL.windowOpacity window $= 0.5 |
| 43 | + renderer <- SDL.createRenderer window (-1) SDL.defaultRenderer |
| 44 | + |
| 45 | + _ <- SDL.glCreateContext window |
| 46 | + |
| 47 | + let |
| 48 | + l = fromIntegral screenWidth * 0.33 |
| 49 | + t = fromIntegral screenHeight * 0.33 |
| 50 | + r = fromIntegral screenWidth * 0.66 |
| 51 | + b = fromIntegral screenHeight * 0.66 |
| 52 | + |
| 53 | + triVertices = V.fromList |
| 54 | + [ SDL.Vertex |
| 55 | + (FPoint l b) |
| 56 | + (Color 0xFF 0 0 255) |
| 57 | + (FPoint 0 0) |
| 58 | + , SDL.Vertex |
| 59 | + (FPoint r b) |
| 60 | + (Color 0 0xFF 0 255) |
| 61 | + (FPoint 0 1) |
| 62 | + , SDL.Vertex |
| 63 | + (FPoint r t) |
| 64 | + (Color 0 0 0xFF 255) |
| 65 | + (FPoint 1 1) |
| 66 | + ] |
| 67 | + |
| 68 | + let |
| 69 | + l = fromIntegral screenWidth * 0.2 |
| 70 | + t = fromIntegral screenHeight * 0.2 |
| 71 | + r = fromIntegral screenWidth * 0.8 |
| 72 | + b = fromIntegral screenHeight * 0.8 |
| 73 | + |
| 74 | + quadVertices = V.fromList |
| 75 | + [ SDL.Vertex |
| 76 | + (FPoint l b) |
| 77 | + (Color 0xFF 0 0xFF 127) |
| 78 | + (FPoint 0 0) |
| 79 | + , SDL.Vertex |
| 80 | + (FPoint r b) |
| 81 | + (Color 0xFF 0 0xFF 127) |
| 82 | + (FPoint 1 0) |
| 83 | + , SDL.Vertex |
| 84 | + (FPoint r t) |
| 85 | + (Color 0xFF 0xFF 0 127) |
| 86 | + (FPoint 1 1) |
| 87 | + , SDL.Vertex |
| 88 | + (FPoint l t) |
| 89 | + (Color 0 0 0 127) |
| 90 | + (FPoint 0 1) |
| 91 | + ] |
| 92 | + quadIndices = V.fromList |
| 93 | + [ 0, 1, 3 |
| 94 | + , 2, 3, 1 |
| 95 | + ] |
| 96 | + stride = fromIntegral $ sizeOf (undefined :: SDL.Vertex) |
| 97 | + |
| 98 | + let loop = do |
| 99 | + events <- SDL.pollEvents |
| 100 | + let quit = elem SDL.QuitEvent $ map SDL.eventPayload events |
| 101 | + |
| 102 | + SDL.rendererDrawColor renderer $= V4 maxBound maxBound maxBound maxBound |
| 103 | + SDL.clear renderer |
| 104 | + |
| 105 | + SDL.renderGeometry |
| 106 | + renderer |
| 107 | + Nothing |
| 108 | + triVertices |
| 109 | + mempty |
| 110 | + |
| 111 | + SDL.rendererDrawBlendMode renderer $= SDL.BlendAlphaBlend |
| 112 | + V.unsafeWith quadVertices $ \ptr -> |
| 113 | + SDL.renderGeometryRaw |
| 114 | + renderer |
| 115 | + Nothing |
| 116 | + (castPtr ptr) |
| 117 | + stride |
| 118 | + (castPtr ptr `plusPtr` sizeOf (undefined :: FPoint)) |
| 119 | + stride |
| 120 | + (castPtr ptr `plusPtr` sizeOf (undefined :: FPoint) `plusPtr` sizeOf (undefined :: Color)) |
| 121 | + stride |
| 122 | + (fromIntegral $ V.length quadVertices) |
| 123 | + (quadIndices :: V.Vector Word8) |
| 124 | + |
| 125 | + SDL.present renderer |
| 126 | + |
| 127 | + unless quit loop |
| 128 | + |
| 129 | + loop |
| 130 | + |
| 131 | + SDL.destroyWindow window |
| 132 | + SDL.quit |
0 commit comments