Skip to content

Commit

Permalink
Just draw some static pixels on the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
smparsons committed Sep 3, 2018
1 parent fc405ef commit 1feb4b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
44 changes: 29 additions & 15 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module Main where

import System.Environment
import SDL
import Linear (V4(..))
import Control.Monad (unless)
import qualified Data.ByteString as BS
import Control.Monad.State
import System.Random
import Data.Word
import Foreign.C.Types

import Constants
import Chip8
import Types

main :: IO ()
main = do
Expand All @@ -28,24 +28,38 @@ startEmulator filepath = do
contents <- BS.readFile filepath
let game = BS.unpack contents
let chip8State' = execState (loadGameIntoMemory game) chip8State
renderer <- setupEmulatorGraphics
emulatorLoop renderer

setupEmulatorGraphics :: IO Renderer
setupEmulatorGraphics = do
initializeAll
window <- createWindow "Chip-8 Emulator" defaultWindow { windowInitialSize = V2 512 256 }
window <- createWindow "Chip-8 Emulator" defaultWindow { windowInitialSize = V2 640 320 }
renderer <- createRenderer window (-1) defaultRenderer
return renderer
texture <- createTexture renderer RGBA8888 TextureAccessStatic (V2 64 32)

emulatorLoop renderer texture

emulatorLoop :: Renderer -> IO ()
emulatorLoop renderer = do
destroyTexture texture
destroyRenderer renderer
destroyWindow window

emulatorLoop :: Renderer -> Texture -> IO ()
emulatorLoop renderer texture = do
events <- pollEvents
let userHasQuit = any isQuitEvent events
rendererDrawColor renderer $= V4 0 0 0 0
clear renderer
present renderer
unless userHasQuit (emulatorLoop renderer)
drawGraphics renderer texture
unless userHasQuit (emulatorLoop renderer texture)

isQuitEvent :: Event -> Bool
isQuitEvent event = eventPayload event == QuitEvent
isQuitEvent event = eventPayload event == QuitEvent

drawGraphics :: Renderer -> Texture -> IO ()
drawGraphics renderer texture = do
let black = [0, 0, 0, 0] :: [Word8]
white = [255, 255, 255, 255] :: [Word8]
pixelArray = (flatten [black,white,black,white]) ++ (take 8160 (cycle black)) ++ (flatten [black,white,black,white])
pixels = BS.pack pixelArray

updatedTexture <- updateTexture texture Nothing pixels (256 :: CInt)
copy renderer updatedTexture Nothing Nothing
present renderer

flatten :: [[a]] -> [a]
flatten xs = (\z n -> foldr (\x y -> foldr z y x) n xs) (:) []
3 changes: 2 additions & 1 deletion lazy-chip8.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ executable lazy-chip8
linear,
bytestring,
mtl,
random
random,
lens

test-suite hspec
build-depends: base,
Expand Down

0 comments on commit 1feb4b6

Please sign in to comment.