Skip to content

Commit

Permalink
Creating function to retrieve graphics as bytestring for rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
smparsons committed Sep 3, 2018
1 parent 1feb4b6 commit 0fba7a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lazy-chip8.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ library
vector,
random,
lens,
mtl
mtl,
bytestring
default-language: Haskell2010

executable lazy-chip8
Expand All @@ -57,7 +58,8 @@ test-suite hspec
vector,
random,
lens,
mtl
mtl,
bytestring
default-language: Haskell2010
hs-source-dirs: test
main-is: Spec.hs
Expand Down
16 changes: 14 additions & 2 deletions src/Chip8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ module Chip8
( emulateCpuCycle,
initializeChip8,
loadFontsetIntoMemory,
loadGameIntoMemory
loadGameIntoMemory,
getGraphicsAsByteString
) where

import System.Random
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
Expand Down Expand Up @@ -38,4 +40,14 @@ emulateCpuCycle :: Chip8 ()
emulateCpuCycle = do
executeOpcode
decrementDelayTimer
decrementSoundTimer
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
21 changes: 21 additions & 0 deletions test/Chip8Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

0 comments on commit 0fba7a2

Please sign in to comment.