From 0fba7a2d5082bffeda214b40b4ab6939c797ecdc Mon Sep 17 00:00:00 2001 From: smparsons Date: Mon, 3 Sep 2018 11:35:01 -0400 Subject: [PATCH] Creating function to retrieve graphics as bytestring for rendering --- lazy-chip8.cabal | 6 ++++-- src/Chip8.hs | 16 ++++++++++++++-- test/Chip8Spec.hs | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lazy-chip8.cabal b/lazy-chip8.cabal index 47def9e..11fdae5 100644 --- a/lazy-chip8.cabal +++ b/lazy-chip8.cabal @@ -34,7 +34,8 @@ library vector, random, lens, - mtl + mtl, + bytestring default-language: Haskell2010 executable lazy-chip8 @@ -57,7 +58,8 @@ test-suite hspec vector, random, lens, - mtl + mtl, + bytestring default-language: Haskell2010 hs-source-dirs: test main-is: Spec.hs diff --git a/src/Chip8.hs b/src/Chip8.hs index f41d5b8..f5b1de5 100644 --- a/src/Chip8.hs +++ b/src/Chip8.hs @@ -2,7 +2,8 @@ module Chip8 ( emulateCpuCycle, initializeChip8, loadFontsetIntoMemory, - loadGameIntoMemory + loadGameIntoMemory, + getGraphicsAsByteString ) where import System.Random @@ -10,6 +11,7 @@ import Data.Word import qualified Data.Vector as V import Control.Monad.State import Control.Lens +import qualified Data.ByteString as BS import Constants import Cpu @@ -38,4 +40,14 @@ emulateCpuCycle :: Chip8 () emulateCpuCycle = do executeOpcode decrementDelayTimer - decrementSoundTimer \ No newline at end of file + decrementSoundTimer + +getGraphicsAsByteString :: Chip8 BS.ByteString +getGraphicsAsByteString = do + chip8Graphics <- gets (\givenState -> V.toList $ givenState^.graphics) + let black = [0, 0, 0, 0] :: [Word8] + white = [255, 255, 255, 255] :: [Word8] + flatten xs = (\z n -> foldr (\x y -> foldr z y x) n xs) (:) [] + rgbaFormatGraphics = flatten $ map (\pixelState -> if pixelState == 1 then white else black) chip8Graphics + graphicsByteString = BS.pack rgbaFormatGraphics + return graphicsByteString diff --git a/test/Chip8Spec.hs b/test/Chip8Spec.hs index 069693c..9a44701 100644 --- a/test/Chip8Spec.hs +++ b/test/Chip8Spec.hs @@ -10,6 +10,7 @@ import Constants import qualified Data.Vector as V import Control.Monad.State import Control.Lens +import qualified Data.ByteString as BS spec :: Spec spec = do @@ -37,3 +38,23 @@ spec = do let numberOfBytesToSlice = 0x22 let loadedGame = V.toList $ V.slice 0x200 numberOfBytesToSlice resultingMemory loadedGame `shouldMatchList` mazeGame + + describe "getGraphicsAsByteString" $ do + it "packs the pixel array into a bytestring" $ do + let chip8State = chip8InitialState { + _graphics = V.update (chip8InitialState^.graphics) $ V.fromList + [ (0x1, 1) + , (0x3, 1) + , (0x7FC, 1) + , (0x7FE, 1) ]} + bsResult = evalState getGraphicsAsByteString chip8State + byteList = BS.unpack bsResult + slice start end = take (end - start + 1) . drop start + beginningByteSlice = slice 0 0xF byteList + endingByteSlice = slice 0x1FF0 0x2000 byteList + + beginningByteSlice `shouldMatchList` + [0x0, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF] + + endingByteSlice `shouldMatchList` + [0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 0x0] \ No newline at end of file