Skip to content

Commit

Permalink
Completed basic sprite system; removed redundant documents
Browse files Browse the repository at this point in the history
  • Loading branch information
cspang1 committed Feb 16, 2018
1 parent 421f1f9 commit 66b14ae
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 31 deletions.
52 changes: 40 additions & 12 deletions Dev/Game/Software/graphics.spin
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ PUB main
DAT
org 0

tester mov temp, par
tester mov time, cnt
add time, delay
loop mov temp, par
wrlong satt0, temp
add temp, #4
wrlong satt1, temp
Expand All @@ -66,20 +68,46 @@ tester mov temp, par
wrlong satt6, temp
add temp, #4
wrlong satt7, temp
loop jmp #loop

waitcnt time, delay
mov temp, satt2
shr temp, #7
and temp, #255
add temp, #1
and temp, #255
shl temp, #7
and satt2, ymask

This comment has been minimized.

Copy link
@konimaru

konimaru Feb 16, 2018

Collaborator
        shr             temp,   #7
        and             temp,   #255
        add             temp,   #1
        and             temp,   #255
        shl             temp,   #7

You might want to shorten that:

        add     temp, #1 << 7
        and     temp, h07F8             ' $FF << 7

This comment has been minimized.

Copy link
@cspang1

cspang1 Feb 16, 2018

Author Owner

@konimaru thanks but no worries! This code was spat out in 5 seconds just to test sprite animation; completely temporary. I make sure to take these shortcuts for critical code sections: https://github.com/cspang1/JCAP/blob/wip/video/sprites/Dev/Game/Software/vga_render.spin#L192

or satt2, temp

mov temp, satt5
shr temp, #15 ' Shift horizontal position to LSB
and temp, #511 ' Mask out horizontal position
add temp, #3
and temp, #511
shl temp, #15
and satt5, xmask

This comment has been minimized.

Copy link
@konimaru

konimaru Feb 16, 2018

Collaborator

Same here:

        add     temp, h018000
        and     temp, hFF8000
or satt5, temp

jmp #loop

' sprite x position y position color v h size
' |<------------->|<--------------->|<------------->|<--->|-|-|<->|
' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
satt0 long %0_0_0_0_0_0_0_0_1_0_1_0_0_0_0_0_0_1_0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_0_0_0_1_0_1_1_1_1_1_1_0_0_1_1_1_1_1_1_0_0_0_0_0_0_0

ymask long %1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_0_0_0_0_0_0_0_0_1_1_1_1_1_1_1
xmask long %1_1_1_1_1_1_1_1_0_0_0_0_0_0_0_0_0_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1

delay long 1500000

time res 1
temp res 1

fit
Expand Down Expand Up @@ -200,11 +228,11 @@ sprite_ship long $0_0_0_0_1_0_0_0 ' Sprite 0

sprite_color_palettes
' Sprite color palettes
s_palette0 byte %00000011,%11000011,%00001111,%11111111 ' Tile color palette 0
s_palette0 byte %00000000,%00110011,%11000011,%11111111 ' Tile color palette 0
byte %11110011,%00111111,%11001111,%11000011
byte %11010011,%00110111,%01001111,%01111011
byte %11010111,%01110111,%01011111,%00000011
s_palette1 byte %00000011,%00110011,%11111111,%11000011 ' Tile color palette 1
s_palette1 byte %00000000,%00110011,%11111111,%11000011 ' Tile color palette 1
byte %00000011,%00110011,%11111111,%11000011
byte %00000011,%00110011,%11111111,%11000011
byte %00000011,%00110011,%11111111,%11000011
70 changes: 51 additions & 19 deletions Dev/Game/Software/vga_render.spin
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ tile rdword curmt, tmindx ' Load current map tile from Mai
rdlong curpt, tpindx ' Load current palette tile from Main RAM

' Parse palette tile pixels
mov ftindx, #2 ' Initialize full tile index
ftile mov htindx, #4 ' Initialize half tile index
mov findx, #2 ' Initialize full tile index
ftile mov hindx, #4 ' Initialize half tile index
mov pxbuff, #0 ' Initialize half-tile pixel buffer
htile mov temp, curpt ' Load current palette tile into temp variable
shr temp, #28 ' LSB align palette index
Expand All @@ -122,12 +122,12 @@ htile mov temp, curpt ' Load current palette tile into
or pxbuff, curcp ' Store color
ror pxbuff, #8 ' Allocate space for next color
shl curpt, #4 ' Shift palette tile left 4 bits
djnz htindx, #htile ' Repeat for half of tile
djnz hindx, #htile ' Repeat for half of tile

' Store tile pixels
shbuf mov slbuff+0, pxbuff ' Allocate space for color
add shbuf, d0 ' Increment scanline buffer OR position
djnz ftindx, #ftile ' Repeat for second half of tile
djnz findx, #ftile ' Repeat for second half of tile
add tmindx, #2 ' Increment pointer to tile in tile map
djnz index , #tile ' Repeat for all tiles in scanline
movd shbuf, #slbuff+0 ' Reset shbuf destination address
Expand Down Expand Up @@ -164,42 +164,74 @@ sprites rdlong curmt, tmindx ' Load sprite attributes from Ma
mov spyoff, spysz ' Copy sprite vertical size to sprite vertical offset
sub temp, cursl ' Subtract current scanline from sprite lower bound
sub spyoff, temp ' Subtract vertical sprite position from vertical sprite size
sub spyoff, #1 ' Decrement offset for inclusivity
:contx if_nc mov spyoff, cursl ' Store current scanline into sprite offset
if_nc sub spyoff, spypos ' Subtract vertical sprite position from sprite offset
sub spyoff, #1 ' Decrement vertical sprite pixel palette offset
shl spyoff, #2 ' Calculate vertical sprite pixel palette offset

' Check if sprite is within scanline horizontally
mov temp, curmt ' Copy sprite attributes to temp variable
shr temp, #15 ' Shift horizontal position to LSB
and temp, #511 ' Mask out horizontal position
mov spxpos, temp ' Store sprite horizontal position
add temp, spxsz ' Calculate sprite horizontal position upper bound
sub temp, #1 ' Modify for inclusivity
cmp maxVis, spxpos wc ' Check sprite upper bound
if_nc jmp #:cont ' Render sprite
add temp, spxsz ' Calculate sprite horizontal position upper bound
sub temp, #1 ' Modify for inclusivity
cmpsub temp, maxHor wc ' Force wrap (carry if wrapped)
if_nc jmp #:skip ' Skip sprite

' Calculate horizontal scanline buffer offset
mov spxoff, spxsz ' Copy sprite horizontal size to sprite horizontal offset
sub spxoff, temp ' Subtract horizontal sprite position from horizontal sprite size
sub spxoff, #1 ' Decrement offset for inclusivity
mov spxpos, #0 ' Move sprite horizontal position to origin
:cont if_nc mov spxoff, #0 ' Start rendering the sprite at its origin
sub spxoff, #1 ' Decrement horizontal sprite pixel palette offset
shl spxoff, #2 ' Calculate horizontal sprite pixel palette offset

' Render current sprite
' Retrieve sprite color palette
mov cpindx, curmt ' Copy sprite attributes to color palette index
and cpindx, #112 ' Mask out color palette index * 16
add cpindx, scpptr ' cpindx * 16 += scpptr

' Retrieve sprite pixel palette line
mov temp, curmt ' Copy sprite attributes to temp variable
shr temp, #24 ' Align sprite pixel palette attribute to LSB
and temp, #255 ' Mask out sprite pixel palette attribute
shl temp, #5 ' Calculate sprite pixel palette Main RAM location offset
add temp, spptr ' Calculate sprite pixel palette Main RAM base location
add temp, spyoff ' Calculate final sprite pixel palette Main RAM location
rdlong pxbuff, temp ' Load sprite pixel palette line from Main RAM
rdlong curpt, temp ' Load sprite pixel palette line from Main RAM
shl curpt, spxoff ' Shift sprite pixel palette line to compensate for wrapping

{{ TEST }}
mov slbuff, tColor
{{ TEST }}
' Parse sprite pixel palette line
mov findx, spxsz ' Store sprite horizontal size into index
:sprite mov temp, curpt ' Load current sprite pixel palette line into temp variable
and temp, #15 ' Mask out current pixel
add temp, cpindx ' Calculate color palette offset
rdbyte curcp, temp ' Load color
cmp curcp, #0 wz ' Check if pixel is transparent
if_z jmp #:trans ' Skip pixel if so
mov temp, spxpos ' Store sprite horizontal position into temp variable
add temp, findx ' Add current pixel index
sub temp, #1 ' Decrement for inclusivity
mov slboff, temp ' Store scanline buffer offset
shr slboff, #2 ' slboff /= 2
add slboff, #slbuff ' slboff += @slbuff
movs :slbget, slboff ' Move target scanline buffer segment source
movd :slbput, slboff ' Move target scanline buffer segment destination
and temp, #3 ' temp %= 4
shl temp, #3 ' temp *= 8
shl curcp, temp ' Shift pixel color to calculated pixel location
:slbget mov tmpslb, 0-0 ' Store target scanline buffer segment into temp variable
mov slboff, pxmask ' Temporarily store scanline buffer segment mask
rol slboff, temp ' Rotate mask to calculated pixel location
and tmpslb, slboff ' Mask away calculated pixel location
or tmpslb, curcp ' Insert pixel
:slbput mov 0-0, tmpslb ' Re-store target scanline buffer segment

:trans shr curpt, #4 ' Shift palette line right 4 bits to next pixel
djnz findx, #:sprite ' Repeat for all pixels on sprite palette line

:skip mov spxsz, #8 ' Re-initialize sprite horizontal size
mov spysz, #8 ' Re-initialize sprite vertical size
Expand All @@ -223,9 +255,6 @@ write wrlong slbuff+0, curvb ' If so, write scanline buffer t
cmp cursl, numLines wc ' Check if at bottom of screen
if_nc mov cursl, initsl ' Reinitialize current scanline if so
jmp #slgen ' Generate next scanline

' Test values
tColor long %00000011_11000011_00001111_11111111

' Video attributes
maxHor long 512 ' Maximum horizontal position
Expand All @@ -248,7 +277,8 @@ spptr long 24 ' Pointer to location of sprite palettes in Main
scpptr long 28 ' Pointer to location of sprite color palettes in Main RAM w/ offset

' Other values
d0 long 1 << 9 ' Value to increment destination register
d0 long 1 << 9 ' Value to increment destination register
pxmask long $FFFFFF00 ' Mask for sprites in scanline buffer

' Scanline buffer
slbuff long 0[80] ' Buffer containing scanline
Expand Down Expand Up @@ -278,8 +308,10 @@ tgtsl res 1 ' Container for target scanline
curvb res 1 ' Container for current video buffer Main RAM location being written
index res 1 ' Container for temporary index
pxbuff res 1 ' Container for temporary pixel buffer
htindx res 1 ' Container for half-tile index
ftindx res 1 ' Container for full-tile index
hindx res 1 ' Container for half-tile index
findx res 1 ' Container for full-tile index
slboff res 1 ' Container for scanline buffer offset
tmpslb res 1 ' Container for temporary scanline buffer segment
temp res 1 ' Container for temporary variables

fit
Binary file removed Docs/Other/Compatible_Tile_Calc.xlsx
Binary file not shown.
Binary file modified Docs/Other/Counter_PLL_Calc.xlsx
Binary file not shown.
Binary file removed Docs/Other/Multicog_Sandbox.xlsx
Binary file not shown.
Binary file modified Docs/Other/sprite_bounds_check.xlsx
Binary file not shown.

0 comments on commit 66b14ae

Please sign in to comment.