make screen resolution configurable

This commit is contained in:
nek0 2017-11-19 18:27:09 +01:00
parent 5be59df1c3
commit 437de1b61d
4 changed files with 56 additions and 12 deletions

View file

@ -27,8 +27,8 @@ import Logging as LL
import Debug.Trace import Debug.Trace
load :: IO StateData load :: Word -> Word -> IO StateData
load = do load w h = do
_ <- SDL.setMouseLocationMode SDL.RelativeLocation _ <- SDL.setMouseLocationMode SDL.RelativeLocation
GL.depthFunc $= Just GL.Less GL.depthFunc $= Just GL.Less
@ -84,12 +84,19 @@ load = do
fragmentShaderHandle = foldl BS.append BS.empty fragmentShaderHandle = foldl BS.append BS.empty
[ "varying vec2 f_texcoord;" [ "varying vec2 f_texcoord;"
, "void main(void) {" , "void main(void) {"
, " gl_FragColor = vec4(1.0,0.0,1.0,0.5);" , " gl_FragColor = vec4(1,0,1,0.5);"
, "}"
]
fragmentShaderSelHandle = foldl BS.append BS.empty
[ "varying vec2 f_texcoord;"
, "void main(void) {"
, " gl_FragColor = vec4(1,1,0,0.5);"
, "}" , "}"
] ]
hProgram <- GLU.simpleShaderProgramBS vertexShader fragmentShaderHandle hProgram <- GLU.simpleShaderProgramBS vertexShader fragmentShaderHandle
sProgram <- GLU.simpleShaderProgramBS vertexShader fragmentShaderShip sProgram <- GLU.simpleShaderProgramBS vertexShader fragmentShaderShip
cProgram <- GLU.simpleShaderProgramBS vertexShader fragmentShaderSelHandle
phys <- initPhysics phys <- initPhysics
@ -109,7 +116,7 @@ load = do
(Just t) (Just t)
(Just texture)) (Just texture))
, vertHandles = createHandles hvao vhtl (loLocations sobj) , vertHandles = createHandles hvao vhtl (loLocations sobj)
, proj = perspective (pi/2) (1600 / 900) 1 (-1) , proj = perspective (pi/2) (fromIntegral w / fromIntegral h) 1 (-1)
, camera = Camera , camera = Camera
{ cameraFocus = V3 0 0 0 { cameraFocus = V3 0 0 0
, cameraRot = Euler 0 0 0 , cameraRot = Euler 0 0 0
@ -119,6 +126,7 @@ load = do
, physicsObjects = po , physicsObjects = po
, shipProgram = sProgram , shipProgram = sProgram
, handleProgram = hProgram , handleProgram = hProgram
, selHandleProgram = cProgram
} }
initPhysics :: IO Physics initPhysics :: IO Physics

View file

@ -23,18 +23,47 @@ import System.Random (randomRIO)
import SpatialMath import SpatialMath
import Options.Applicative
import Data.Semigroup ((<>))
import Init import Init
import Types import Types
import Foreign.C.Types (CInt(..))
import Debug.Trace as T import Debug.Trace as T
opts :: Parser Opts
opts = Opts
<$> option auto
( long "width"
<> short 'w'
<> value 1600
<> showDefault
<> metavar "NUM"
<> help "Screen width"
)
<*> option auto
( long "height"
<> short 'h'
<> value 900
<> showDefault
<> metavar "NUM"
<> help "Screen height"
)
main :: IO () main :: IO ()
main = main = do
o <- execParser $ info opts
( fullDesc
<> progDesc "sample for hw game"
)
withAffection AffectionConfig withAffection AffectionConfig
{ initComponents = All { initComponents = All
, windowTitle = "hw" , windowTitle = "hw"
, windowConfig = SDL.defaultWindow , windowConfig = SDL.defaultWindow
{ SDL.windowInitialSize = SDL.V2 1600 900 { SDL.windowInitialSize = SDL.V2
(CInt $ fromIntegral $ width o)
(CInt $ fromIntegral $ height o)
, SDL.windowOpenGL = Just SDL.defaultOpenGL , SDL.windowOpenGL = Just SDL.defaultOpenGL
{ SDL.glProfile = SDL.Core SDL.Normal 3 2 { SDL.glProfile = SDL.Core SDL.Normal 3 2
} }
@ -43,8 +72,8 @@ main =
, preLoop = return () , preLoop = return ()
, eventLoop = handle , eventLoop = handle
, updateLoop = update , updateLoop = update
, drawLoop = draw , drawLoop = draw (width o) (height o)
, loadState = load , loadState = load (width o) (height o)
, cleanUp = const (return ()) , cleanUp = const (return ())
, canvasSize = Nothing , canvasSize = Nothing
} }
@ -77,10 +106,10 @@ update dt = do
vertHandles = nvhs vertHandles = nvhs
} }
draw :: Affection StateData () draw :: Word -> Word -> Affection StateData ()
draw = draw w h =
do do
GL.viewport $= (GL.Position 0 0, GL.Size 1600 900) GL.viewport $= (GL.Position 0 0, GL.Size (fromIntegral w) (fromIntegral h))
StateData{..} <- getAffection StateData{..} <- getAffection
let view = lookAt let view = lookAt
(cameraFocus camera + (cameraFocus camera +

View file

@ -9,6 +9,11 @@ import SpatialMath
import Physics.Bullet.Raw as Bullet import Physics.Bullet.Raw as Bullet
data Opts = Opts
{ width :: Word
, height :: Word
}
data StateData = StateData data StateData = StateData
{ ship :: Ship { ship :: Ship
, vertHandles :: [Ship] , vertHandles :: [Ship]
@ -18,6 +23,7 @@ data StateData = StateData
, physicsObjects :: PhysicsObjects , physicsObjects :: PhysicsObjects
, shipProgram :: GLU.ShaderProgram , shipProgram :: GLU.ShaderProgram
, handleProgram :: GLU.ShaderProgram , handleProgram :: GLU.ShaderProgram
, selHandleProgram :: GLU.ShaderProgram
} }
data Ship = Ship data Ship = Ship
@ -27,7 +33,7 @@ data Ship = Ship
, shipRot :: Quaternion Float , shipRot :: Quaternion Float
, shipTexture :: Maybe GL.TextureObject , shipTexture :: Maybe GL.TextureObject
, shipUVs :: Maybe GL.BufferObject , shipUVs :: Maybe GL.BufferObject
} } deriving (Eq)
data Camera = Camera data Camera = Camera
{ cameraFocus :: V3 Float { cameraFocus :: V3 Float

View file

@ -172,6 +172,7 @@ executable example03
, wavefront , wavefront
, shoot , shoot
, split , split
, optparse-applicative
hs-source-dirs: examples/example03 hs-source-dirs: examples/example03
default-language: Haskell2010 default-language: Haskell2010
ghc-options: -Wall ghc-options: -Wall