<commit_before>module Network (runNetwork) where

--------------------
-- Global Imports --
import Graphics.Rendering.OpenGL
import Graphics.GLUtil.Camera2D
import Graphics.UI.GLFW as GLFW
import Data.Vinyl.Universe
import Control.Concurrent
import Control.Wire
import Data.IORef
import Data.Vinyl

-------------------
-- Local Imports --
import Assets
import Render
import World

----------
-- Code --

-- | Loading the project's assets.
loadAssets :: AssetLoads
loadAssets = do
  loadTexture "white.png"
  loadTexture "apple.png"
  loadTexture "cherry.png"
  loadTexture "crate.png"
  loadTexture "lemon.png"
  loadTexture "mouse.png"
  loadTexture "white.png"

  loadShaderProgram "game2d"

-- | The loop for updating the game.
updateGame :: Session IO s -> Wire s e IO a World -> IORef World -> IORef Bool -> IO ()
updateGame session wire worldRef closedRef = do
  closed <- readIORef closedRef
  if closed
    then return ()
    else do
      (st, session') <- stepSession session
      (wt, wire'   ) <- stepWire wire st $ Right undefined

      case wt of
        Left  _     -> writeIORef closedRef True
        Right world -> do
          writeIORef worldRef world
          threadDelay 1000000
          updateGame session' wire' worldRef closedRef

-- | The loop for rendering the game.
renderGame :: IORef (Maybe World) -> IORef Bool -> Assets -> Camera GLfloat -> IO ()
renderGame worldRef closedRef assets cam = do
  closed <- readIORef closedRef
  if closed
    then return ()
    else do
      mworld <- readIORef worldRef

      case mworld of
        Nothing    -> renderGame worldRef closedRef assets cam
        Just world -> do
          clear [ColorBuffer, DepthBuffer]
          render (SField =: camMatrix cam) assets world
          swapBuffers

          renderGame worldRef closedRef assets cam

-- | The backend to running the network.
runNetwork' :: HasTime t s => IORef Bool -> Assets -> Camera GLfloat -> Session IO s -> Wire s e IO AppInfo World -> IO ()
runNetwork' closedRef assets cam session wire = do
  closed <- readIORef closedRef
  if closed
    then return ()
    else do
      (st, session') <- stepSession session
      (wt, wire'   ) <- stepWire wire st $ Right undefined

      case wt of
        Left  _     -> return ()
        Right world -> do
          clear [ColorBuffer, DepthBuffer]
          render (SField =: camMatrix cam) assets world
          swapBuffers

          runNetwork' closedRef assets cam session' wire'

-- | The frontend of running the network. Really just passes everything along
--   to @'runNetwork''@ where all the work gets done.
runNetwork :: IORef Bool -> IO ()
runNetwork closedRef = do
  worldRef <- newIORef Nothing
  assets   <- performAssetLoads loadAssets

  forkIO $ updateGame clockSession_ worldWire worldRef closedRef
  threadDelay
  renderGame worldRef closedRef assets camera2D<commit_msg>Fixed some stuff and clean some stuff up.<commit_after>module Network (runNetwork) where

--------------------
-- Global Imports --
import Graphics.Rendering.OpenGL
import Graphics.GLUtil.Camera2D
import Graphics.UI.GLFW as GLFW
import Data.Vinyl.Universe
import Control.Concurrent
import Control.Wire
import Data.IORef
import Data.Vinyl

-------------------
-- Local Imports --
import Assets
import Render
import World

----------
-- Code --

-- | Loading the project's assets.
loadAssets :: AssetLoads
loadAssets = do
  loadTexture "white.png"
  loadTexture "apple.png"
  loadTexture "cherry.png"
  loadTexture "crate.png"
  loadTexture "lemon.png"
  loadTexture "mouse.png"
  loadTexture "white.png"

  loadShaderProgram "game2d"

-- | The loop for updating the game.
updateGame :: Session IO s -> Wire s e IO a World -> IORef (Maybe World) -> IORef Bool -> IO ()
updateGame session wire worldRef closedRef = do
  closed <- readIORef closedRef
  if closed
    then return ()
    else do
      (st, session') <- stepSession session
      (wt, wire'   ) <- stepWire wire st $ Right undefined

      case wt of
        Left  _     -> writeIORef closedRef True
        Right world -> do
          writeIORef worldRef $ Just world
          threadDelay 1000000
          updateGame session' wire' worldRef closedRef

-- | The loop for rendering the game.
renderGame :: IORef (Maybe World) -> IORef Bool -> Assets -> Camera GLfloat -> IO ()
renderGame worldRef closedRef assets cam = do
  closed <- readIORef closedRef
  if closed
    then return ()
    else do
      mworld <- readIORef worldRef

      case mworld of
        Nothing    -> renderGame worldRef closedRef assets cam
        Just world -> do
          clear [ColorBuffer, DepthBuffer]
          render (SField =: camMatrix cam) assets world
          swapBuffers

          renderGame worldRef closedRef assets cam

-- | The frontend of running the network. Really just passes everything along
--   to @'runNetwork''@ where all the work gets done.
runNetwork :: IORef Bool -> IO ()
runNetwork closedRef = do
  worldRef <- newIORef Nothing
  assets   <- performAssetLoads loadAssets

  forkIO $ updateGame clockSession_ worldWire worldRef closedRef
  renderGame worldRef closedRef assets camera2D
{'fraction_non_alphanumeric': 0.09306853582554517, 'fraction_numerical': 0.003894080996884735, 'mean_word_length': 2.5923076923076924, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6836486', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1402, 'n_words': 541}