mirror of
https://github.com/robmcmullen/asmgen.git
synced 2024-06-26 07:29:35 +00:00
Code cleanup
- Also fixed a bug with high bit determination
This commit is contained in:
parent
56e75f426e
commit
386b338c23
127
HiSprite.py
127
HiSprite.py
|
@ -3,14 +3,14 @@
|
||||||
import sys,os,png
|
import sys,os,png
|
||||||
|
|
||||||
class Colors:
|
class Colors:
|
||||||
black,magenta,green,orange,blue,white = range(6)
|
black,magenta,green,orange,blue,white,key = range(7)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
|
|
||||||
if len(argv)<1:
|
if len(argv)<1:
|
||||||
printHorzontalLookup()
|
printHorizontalLookup()
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
pngfile = sys.argv[1]
|
pngfile = sys.argv[1]
|
||||||
|
@ -23,7 +23,7 @@ def main(argv):
|
||||||
|
|
||||||
width = pngdata[0]
|
width = pngdata[0]
|
||||||
height = pngdata[1]
|
height = pngdata[1]
|
||||||
pixeldata = pngdata[2]
|
pixelData = pngdata[2]
|
||||||
byteWidth = width/2+1+1 # TODO: Calculate a power of two for this
|
byteWidth = width/2+1+1 # TODO: Calculate a power of two for this
|
||||||
niceName = os.path.splitext(pngfile)[0].upper()
|
niceName = os.path.splitext(pngfile)[0].upper()
|
||||||
|
|
||||||
|
@ -49,43 +49,37 @@ def main(argv):
|
||||||
print "\tldx PARAM1"
|
print "\tldx PARAM1"
|
||||||
print rowStartCalculatorCode();
|
print rowStartCalculatorCode();
|
||||||
|
|
||||||
spriteChunks = layoutSpriteChunk(pixeldata,width,height,shift)
|
spriteChunks = layoutSpriteChunk(pixelData,width,height,shift)
|
||||||
|
|
||||||
for row in range(height):
|
for row in range(height):
|
||||||
for chunkIndex in range(len(spriteChunks)):
|
for chunkIndex in range(len(spriteChunks)):
|
||||||
print spriteChunks[chunkIndex][row]
|
print spriteChunks[chunkIndex][row]
|
||||||
|
|
||||||
print "\n"
|
print "\n"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def layoutSpriteChunk(pixeldata,width,height,shift):
|
|
||||||
|
|
||||||
bitmap = [[0 for x in range(width)] for y in range(height)]
|
def layoutSpriteChunk(pixelData,width,height,shift):
|
||||||
|
|
||||||
byteWidth = width/2+1+1 # TODO: Calculate a power of two for this
|
colorStreams = byteStreamsFromPixels(pixelData,width,height,shift,bitsForColor,highBitForColor)
|
||||||
spriteChunks = [["" for y in range(height)] for x in range(byteWidth)]
|
maskStreams = byteStreamsFromPixels(pixelData,width,height,shift,bitsForMask,highBitForMask)
|
||||||
|
code = generateBlitter(colorStreams,maskStreams,height)
|
||||||
|
|
||||||
|
return code
|
||||||
|
|
||||||
|
|
||||||
|
def byteStreamsFromPixels(pixelData,width,height,shift,bitDelegate,highBitDelegate):
|
||||||
|
|
||||||
|
byteStreams = ["" for x in range(height)]
|
||||||
|
byteWidth = width/2+1+1
|
||||||
|
|
||||||
# Layout rows
|
|
||||||
for row in range(height):
|
for row in range(height):
|
||||||
pixelRow = bitmap[row]
|
|
||||||
bitStream = ""
|
bitStream = ""
|
||||||
|
|
||||||
# Compute raw bitstream for row from PNG pixels
|
# Compute raw bitstream for row from PNG pixels
|
||||||
for pixelIndex in range(width):
|
for pixelIndex in range(width):
|
||||||
pixel = pixelColor(pixeldata,row,pixelIndex)
|
pixel = pixelColor(pixelData,row,pixelIndex)
|
||||||
if pixel == Colors.black:
|
bitStream += bitDelegate(pixel)
|
||||||
bitStream += "00"
|
|
||||||
else:
|
|
||||||
if pixel == Colors.white:
|
|
||||||
bitStream += "11"
|
|
||||||
else:
|
|
||||||
if pixel == Colors.green or pixel == Colors.orange:
|
|
||||||
bitStream += "01"
|
|
||||||
else:
|
|
||||||
bitStream += "10"
|
|
||||||
|
|
||||||
# Shift bit stream as needed
|
# Shift bit stream as needed
|
||||||
bitStream = shiftStringRight(bitStream,shift)
|
bitStream = shiftStringRight(bitStream,shift)
|
||||||
bitStream = bitStream[:byteWidth*8]
|
bitStream = bitStream[:byteWidth*8]
|
||||||
|
@ -107,17 +101,28 @@ def layoutSpriteChunk(pixeldata,width,height,shift):
|
||||||
bitChunk += fillOutByte(7-remainingBits)
|
bitChunk += fillOutByte(7-remainingBits)
|
||||||
else:
|
else:
|
||||||
bitChunk = bitStream[bitPos:bitPos+7]
|
bitChunk = bitStream[bitPos:bitPos+7]
|
||||||
|
|
||||||
bitChunk = bitChunk[::-1]
|
bitChunk = bitChunk[::-1]
|
||||||
|
|
||||||
# Set palette bit as needed. Note that we prefer high-bit white
|
# Determine palette bit from first pixel on each row
|
||||||
# because blue fringe is less noticeable than magenta
|
highBit = highBitDelegate(pixelData[row][0])
|
||||||
highBit = "0"
|
|
||||||
if pixel == Colors.orange or pixel == Colors.blue or pixel == Colors.white:
|
|
||||||
highBit = "1"
|
|
||||||
|
|
||||||
byteSplits[byteIndex] = highBit + bitChunk
|
byteSplits[byteIndex] = highBit + bitChunk
|
||||||
bitPos += 7
|
bitPos += 7
|
||||||
|
|
||||||
|
byteStreams[row] = byteSplits;
|
||||||
|
|
||||||
|
return byteStreams
|
||||||
|
|
||||||
|
|
||||||
|
def generateBlitter(colorStreams,maskStreams,height):
|
||||||
|
|
||||||
|
byteWidth = len(colorStreams[0])
|
||||||
|
spriteChunks = [["" for y in range(height)] for x in range(byteWidth)]
|
||||||
|
|
||||||
|
for row in range(height):
|
||||||
|
|
||||||
|
byteSplits = colorStreams[row]
|
||||||
|
|
||||||
# Generate blitting code
|
# Generate blitting code
|
||||||
for chunkIndex in range(len(byteSplits)):
|
for chunkIndex in range(len(byteSplits)):
|
||||||
|
@ -152,7 +157,8 @@ def rowStartCalculatorCode():
|
||||||
"\tldy PARAM0\n" + \
|
"\tldy PARAM0\n" + \
|
||||||
"\tlda DIV7_2,y\n" + \
|
"\tlda DIV7_2,y\n" + \
|
||||||
"\ttay\n";
|
"\ttay\n";
|
||||||
|
|
||||||
|
|
||||||
def fillOutByte(numBits):
|
def fillOutByte(numBits):
|
||||||
filler = ""
|
filler = ""
|
||||||
for bit in range(numBits):
|
for bit in range(numBits):
|
||||||
|
@ -174,11 +180,49 @@ def shiftStringRight(string,shift):
|
||||||
result += string
|
result += string
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def pixelColor(pixeldata,row,col):
|
def bitsForColor(pixel):
|
||||||
r = pixeldata[row][col*3]
|
|
||||||
g = pixeldata[row][col*3+1]
|
if pixel == Colors.black:
|
||||||
b = pixeldata[row][col*3+2]
|
return "00"
|
||||||
|
else:
|
||||||
|
if pixel == Colors.white:
|
||||||
|
return "11"
|
||||||
|
else:
|
||||||
|
if pixel == Colors.green or pixel == Colors.orange:
|
||||||
|
return "01"
|
||||||
|
|
||||||
|
# blue or magenta
|
||||||
|
return "10"
|
||||||
|
|
||||||
|
|
||||||
|
def bitsForMask(pixel):
|
||||||
|
|
||||||
|
if pixel == Colors.black:
|
||||||
|
return "00"
|
||||||
|
|
||||||
|
return "11"
|
||||||
|
|
||||||
|
|
||||||
|
def highBitForColor(pixel):
|
||||||
|
|
||||||
|
# Note that we prefer high-bit white because blue fringe is less noticeable than magenta.
|
||||||
|
highBit = "0"
|
||||||
|
if pixel == Colors.orange or pixel == Colors.blue or pixel == Colors.white:
|
||||||
|
highBit = "1"
|
||||||
|
|
||||||
|
return highBit
|
||||||
|
|
||||||
|
|
||||||
|
def highBitForMask(pixel):
|
||||||
|
|
||||||
|
return "1"
|
||||||
|
|
||||||
|
|
||||||
|
def pixelColor(pixelData,row,col):
|
||||||
|
r = pixelData[row][col*3]
|
||||||
|
g = pixelData[row][col*3+1]
|
||||||
|
b = pixelData[row][col*3+2]
|
||||||
color = Colors.black
|
color = Colors.black
|
||||||
|
|
||||||
if r==255 and g==0 and b==255:
|
if r==255 and g==0 and b==255:
|
||||||
|
@ -195,10 +239,13 @@ def pixelColor(pixeldata,row,col):
|
||||||
else:
|
else:
|
||||||
if r==255 and g==255 and b==255:
|
if r==255 and g==255 and b==255:
|
||||||
color = Colors.white
|
color = Colors.white
|
||||||
|
else:
|
||||||
|
if r==g and r==b and r!=0 and r!=255: # Any gray is chroma key
|
||||||
|
color = Colors.key
|
||||||
return color
|
return color
|
||||||
|
|
||||||
|
|
||||||
def printHorzontalLookup():
|
def printHorizontalLookup():
|
||||||
disclaimer()
|
disclaimer()
|
||||||
|
|
||||||
print "DIV7_2:"
|
print "DIV7_2:"
|
||||||
|
|
BIN
V2Make.scpt
BIN
V2Make.scpt
Binary file not shown.
BIN
boxw_mix.png
BIN
boxw_mix.png
Binary file not shown.
Before Width: | Height: | Size: 242 B After Width: | Height: | Size: 247 B |
BIN
hisprite.dsk
BIN
hisprite.dsk
Binary file not shown.
Loading…
Reference in New Issue
Block a user