-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: explicit memory management (#55), cleaner file loading in examples
- Loading branch information
Showing
27 changed files
with
509 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,46 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
module Main where | ||
|
||
import Paths_h_raylib (getDataFileName) | ||
import Control.Monad (unless, void) | ||
import Raylib.Core (changeDirectory, clearBackground, getApplicationDirectory) | ||
import Raylib.Core (changeDirectory, clearBackground, getApplicationDirectory, initWindow, setTargetFPS, windowShouldClose, closeWindow) | ||
import Raylib.Core.Audio (closeAudioDevice, initAudioDevice, loadMusicStream, playMusicStream, updateMusicStream) | ||
import Raylib.Core.Text (drawText) | ||
import Raylib.Util (drawing, inGHCi, whileWindowOpen0, withWindow) | ||
import Raylib.Util (drawing, inGHCi, whileWindowOpen0, withWindow, managed, WindowResources, raylibApplication) | ||
import Raylib.Util.Colors (lightGray, rayWhite) | ||
import Raylib.Types (Music) | ||
|
||
type AppState = (WindowResources, Music) | ||
|
||
musicPath :: String | ||
musicPath = (if not inGHCi then "../../../../../../../../../../" else "./") ++ "examples/basic-audio/assets/mini1111.xm" | ||
|
||
main :: IO () | ||
main = do | ||
withWindow | ||
650 | ||
400 | ||
"raylib [audio] example - basic audio" | ||
60 | ||
( \window -> do | ||
initAudioDevice | ||
unless inGHCi (void $ changeDirectory =<< getApplicationDirectory) | ||
|
||
music <- loadMusicStream musicPath window | ||
playMusicStream music | ||
|
||
whileWindowOpen0 | ||
( drawing | ||
( do | ||
clearBackground rayWhite | ||
drawText "You should hear music playing!" 20 20 20 lightGray | ||
) | ||
>> updateMusicStream music | ||
) | ||
|
||
closeAudioDevice window | ||
) | ||
musicPath = "examples/basic-audio/assets/mini1111.xm" | ||
|
||
startup :: IO AppState | ||
startup = do | ||
window <- initWindow 650 400 "raylib [audio] example - basic audio" | ||
setTargetFPS 60 | ||
initAudioDevice | ||
|
||
music <- managed window $ loadMusicStream =<< getDataFileName musicPath | ||
|
||
playMusicStream music | ||
|
||
return (window, music) | ||
|
||
mainLoop :: AppState -> IO AppState | ||
mainLoop state@(_, music) = do | ||
drawing $ do | ||
clearBackground rayWhite | ||
drawText "You should hear music playing!" 20 20 20 lightGray | ||
updateMusicStream music | ||
return state | ||
|
||
shouldClose :: AppState -> IO Bool | ||
shouldClose _ = windowShouldClose | ||
|
||
teardown :: AppState -> IO () | ||
teardown (window, _) = do | ||
closeAudioDevice (Just window) | ||
closeWindow (Just window) | ||
|
||
raylibApplication 'startup 'mainLoop 'shouldClose 'teardown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.