Here you go, the prefix is now SDM XD
Rem ***** Included Source File *****
`Sprite drawing manager types
type SDM_Type
maxCanvas as integer
currentCanvasModify as integer
currentCanvasMemblock as integer
maxFonts as integer
maxCharacters as integer
maxLabels as integer
endtype
type SDM_CanvasType
width as integer
height as integer
depth as integer
image as integer
used as boolean
sprite as integer
endtype
type SDM_LabelType
width as integer
height as integer
image as integer
used as boolean
endtype
type SDM_FontType
charCountX as integer
charCountY as integer
used as boolean
endtype
type SDM_FontCharacterType
image as integer
charWidth as integer
charHeight as integer
endtype
`Sprite draw manager globals
global SDM as SDM_Type
`Initilizes the sprite drawing manager
function SDM_Initilize()
`Set sprite draw manager globals
SDM.maxCanvas = 1000
SDM.maxFonts = 100
SDM.maxCharacters = 256
SDM.maxLabels = 1000
`Arrays
dim SDM_Canvas(SDM.maxCanvas) as SDM_CanvasType
dim SDM_Label(SDM.maxLabels) as SDM_LabelType
dim SDM_Font(SDM.maxFonts) as SDM_FontType
dim SDM_FontCharacter(SDM.maxFonts, SDM.maxCharacters) as SDM_FontCharacterType
endfunction
`Creates a new canvas
function SDM_CreateNewCanvas(positionX as integer, positionY as integer, width as integer, height as integer, depth as integer, red as integer, green as integer, blue as integer, alpha as integer)
`Get a free image id
imageId = getFree("image")
`Get a free memblock id
memblockId = getFree("memblock")
`Calculate the image size in bytes
imageSizeBytes = (((width * height) * 4)) + 12
`Create the image memblock
make memblock memblockId, imageSizeBytes
`Set the image size
write memblock dword memblockId, 0, width
write memblock dword memblockId, 4, height
write memblock dword memblockId, 8, depth
`Fill up the whole image with black pixels
for imgX = 1 to width
for imgY = 1 to height
`Calculate the offset location
imageByteOffsetLocation = ((imgY - 1) * width + imgX - 1) * 4 + 12
`Write R, G, B, Alpha infomation in reverse
write memblock byte memblockId, imageByteOffsetLocation, blue
write memblock byte memblockId, imageByteOffsetLocation + 1, green
write memblock byte memblockId, imageByteOffsetLocation + 2, red
write memblock byte memblockId, imageByteOffsetLocation + 3, alpha
next imgY
next imgX
`Create the final image
make image from memblock imageId, memblockId
delete memblock memblockId
`Create a sprite from the canvas
spriteId = getFree("sprite")
sprite spriteId, positionX, positionY, imageId
`Get a free canvas id
canvasId = SDM_GetFreeCanvas()
`Set the canvas image
SDM_Canvas(canvasId).image = imageId
`Set the canvas sprite
SDM_Canvas(canvasId).sprite = spriteId
`Set the canvas width, height and depth
SDM_Canvas(canvasId).width = width
SDM_Canvas(canvasId).height = height
SDM_Canvas(canvasId).depth = depth
SDM_Canvas(canvasId).used = 1
endfunction canvasId
`Positions a canvas
function SDM_PositionCanvas(positionX as integer, positionY as integer)
canvasId = SDM.currentCanvasModify
sprite SDM_Canvas(canvasId).sprite, positionX, positionY, SDM_Canvas(canvasId).image
endfunction
`Starts a canvas modification
function SDM_StartCanvasModify(canvasId as integer)
`Make sure there is no canvas currently being modified
if SDM.currentCanvasModify = 0
`Set the current canvas modify
SDM.currentCanvasModify = canvasId
`Get a free memblock id
memblockId = getFree("memblock")
`Get the image id
imageId = SDM_Canvas(canvasId).image
`Create a memblock from the image
make memblock from image memblockId, imageId
`Set the current memblock
SDM.currentCanvasMemblock = memblockId
endif
endfunction
`Ends current canvas modification
function SDM_EndCanvasModify()
`Get the image id
imageId = SDM_Canvas(SDM.currentCanvasModify).image
`Get the memblock id
memblockId = SDM.currentCanvasMemblock
`Update the image
make image from memblock imageId, memblockId
`Delete the old memblock
delete memblock memblockId
`Set modification to 0
SDM.currentCanvasModify = 0
SDM.currentCanvasMemblock = 0
endfunction
`Draws a dot on the specified canvas, A modifiy must have been started
function SDM_Dot(offsetX as integer, offsetY as integer, red as integer, green as integer, blue as integer, alpha as integer)
`Get the canvas id
canvasId = SDM.currentCanvasModify
`Get the canvas image id
imageId = SDM_Canvas(canvasId).image
`Get the memblock
memblockId = SDM.currentCanvasMemblock
`Get the image width, height and depth
width = SDM_Canvas(canvasId).width
height = SDM_Canvas(canvasId).height
depth = SDM_Canvas(canvasId).depth
`Make sure the dot is within the canvas limits
if offsetX =< width and offsetY =< height and offsetX =>0 and offsetY => 0
`Find the byte location of the specified pixel, to dot
imageByteOffsetLocation = ((offsetX - 1) * width + offsetY - 1) * 4 + 12
`Now change the colour and alpha values of the pixel
write memblock byte memblockId, imageByteOffsetLocation, blue
write memblock byte memblockId, imageByteOffsetLocation + 1, green
write memblock byte memblockId, imageByteOffsetLocation + 2, red
write memblock byte memblockId, imageByteOffsetLocation + 3, alpha
endif
endfunction
`Draws a box on the current canvas
function SDM_Box(offsetX1 as integer, offsetY1 as integer, offsetX2 as integer, offsetY2 as integer, red as integer, green as integer, blue as integer, alpha as integer)
`Swap coords
tx = offsetX1
offsetX1 = offsetY1
offsetY1 = tx
tx = offsetX2
offsetX2 = offsetY2
offsetY2 = tx
`Get canvas id
canvasId = SDM.currentCanvasModify
`Get the canvas image id
imageId = SDM_Canvas(canvasId).image
`Get the current canvas memblock
memblockId = SDM.currentCanvasMemblock
`Get the image width, height and depth
width = SDM_Canvas(canvasId).width
height = SDM_Canvas(canvasId).height
depth = SDM_Canvas(canvasId).depth
`Loop through all the pixels needed to be changed, to create a box
for offsetX = offsetX1 to offsetX2
for offsetY = offsetY1 to offsetY2
`Make sure the current pixel is within the canvas limits
if offsetX =< width and offsetY =< height and offsetX >0 and offsetY > 0
`Get the current byte offset location for the memblock
imageByteOffsetLocation = ((offsetX - 1) * width + offsetY - 1) * 4 + 12
`Calculate red, green and blue
`red =
`green =
`blue =
`Write the new pixel
write memblock byte memblockId, imageByteOffsetLocation, blue
write memblock byte memblockId, imageByteOffsetLocation + 1, green
write memblock byte memblockId, imageByteOffsetLocation + 2, red
write memblock byte memblockId, imageByteOffsetLocation + 3, alpha
endif
next offsetY
next offsetX
endfunction
`Draws a line on the current canvas
function SDM_Line(offsetX1 as integer, offsetY1 as integer, offsetX2 as integer, offsetY2 as integer, red, green, blue, alpha)
`Get canvas id
canvasId = SDM.currentCanvasModify
`Get the canvas image id
imageId = SDM_Canvas(canvasId).image
`Get the current canvas memblock
memblockId = SDM.currentCanvasMemblock
`Get the image width, height and depth
width = SDM_Canvas(canvasId).width
height = SDM_Canvas(canvasId).height
depth = SDM_Canvas(canvasId).depth
`Draw the line
l = offsetY1
t = offsetX1
r = offsetY2
b = offsetX2
w = r-l : h = b-t
if w >= 0 then xstep = 1 else xstep = -1
if h >= 0 then ystep = 1 else ystep = -1
w# = ABS(w) : h# = ABS(h)
if w#=0 then w#=0.1
if h#=0 then h#=0.1
xfact# = w#/h#
yfact# = h#/w#
x = 0 : y = 0
repeat
if abs(x+xstep) > abs(w) then xstep = 0
if abs(y+ystep) > abs(h) then ystep = 0
`Get the current byte offset location for the memblock
imageByteOffsetLocation = (((x+l) - 1) * width + (y+t) - 1) * 4 + 12
`Write the new pixel
if x =< width and y =< height
write memblock byte memblockId, imageByteOffsetLocation, blue
write memblock byte memblockId, imageByteOffsetLocation + 1, green
write memblock byte memblockId, imageByteOffsetLocation + 2, red
write memblock byte memblockId, imageByteOffsetLocation + 3, alpha
endif
if yfact# > xfact#
inc y,ystep
if ABS(x) < ABS(y*xfact#) then inc x,xstep
else
inc x,xstep
if ABS(y) < ABS(x*yfact#) then inc y,ystep
endif
until xstep = 0 and ystep = 0
endfunction
`Loads in a font
function SDM_LoadFont(fontFile as string, xCharNum as integer, yCharNum as integer, spaceDistance as integer)
`Get a free image id
imageId = getFree("image")
`Load in the font image
load image fontFile, imageId
`Get a free memblock id
memblockId = getFree("memblock")
`Create a memblock from the font image
make memblock from image memblockId, imageId
`Get a free font id
fontId = SDM_GetFreeFont()
`Set font as used
SDM_Font(fontId).used = 1
`Set font x,y char nums
SDM_Font(fontId).charCountX = xCharNum
SDM_Font(fontId).charCountY = yCharNum
`Get the width, height and depth of the image
width = memblock dword(memblockId, 0)
height = memblock dword(memblockId, 4)
depth = memblock dword(memblockId, 8)
`Get the size per character
charWidth = width / xCharNum
charHeight = height / yCharNum
`Set characternumber to 32 as a startpoint
characterNumber = 31
`Now loop through each character, and create a seprate image of it
for charY = 0 to yCharNum - 1
for charX = 0 to xCharNum - 1
`Set the start and end position of the image get
charX1 = charX * charWidth
charY1 = charY * charHeight
charX2 = charWidth
charY2 = charHeight
`Set low and highs
lowestVisibleCharPixelX = charWidth
lowestVisibleCharPixelY = charHeight
highestVisibleCharPixelX = 0
highestVisibleCharPixelY = 0
`Calculate the starting position
startPosition = (width * (charY1) * 4) + (charX1 * 4) + 12
newStartPosition = 12
`Get a free character memblock id
characterMemblock = getFree("memblock")
`Find the first non-transparent column in the character - pass 1
for charPixelY = 1 to charHeight
for charPixelX = 1 to charWidth
`Read the pixel infomation
alpha = memblock byte(memblockId, startPosition + 3)
`See if the pixel is transparent
if alpha > 0
`Set low and high
if charPixelX < lowestVisibleCharPixelX then lowestVisibleCharPixelX = charPixelX
if charPixelX > highestVisibleCharPixelX then highestVisibleCharPixelX = charPixelX
endif
`Increment the memblock positions
inc startPosition, 4
inc newStartPosition, 4
next charPixelX
`Increment the memblock start position
inc startPosition, (width * 4) - (charWidth * 4)
next charPixelY
`Calculate the starting position
startPosition = (width * (charY1) * 4) + (charX1 * 4) + 12
newStartPosition = 12
`New char width
newCharWidth = (highestVisibleCharPixelX + 1) - (lowestVisibleCharPixelX )
if newCharWidth < 0 then newCharWidth = spaceDistance
`Create a new memblock for the character image
make memblock characterMemblock, ((newCharWidth) * height) + 12
write memblock dword characterMemblock, 0, newCharWidth
write memblock dword characterMemblock, 4, charHeight
write memblock dword characterMemblock, 8, depth
`Loop through each pixel of the current character - Pass 2
for charPixelY = 1 to charHeight
for charPixelX = 1 to charWidth
if charPixelX =< highestVisibleCharPixelX and charPixelX => lowestVisibleCharPixelX
`Read the pixel infomation
blue = memblock byte(memblockId, startPosition + 0)
green = memblock byte(memblockId, startPosition + 1)
red = memblock byte(memblockId, startPosition + 2)
alpha = memblock byte(memblockId, startPosition + 3)
`Write the pixel infomation to the new character image
write memblock byte characterMemblock, newStartPosition, blue
write memblock byte characterMemblock, newStartPosition + 1, green
write memblock byte characterMemblock, newStartPosition + 2, red
write memblock byte characterMemblock, newStartPosition + 3, alpha
inc newStartPosition, 4
endif
`Increment the memblock positions
inc startPosition, 4
next charPixelX
`Increment the memblock start position
inc startPosition, (width * 4) - (charWidth * 4)
next charPixelY
`Get the trim width and height
trimWidth = (highestVisibleCharPixelX + 1) - (lowestVisibleCharPixelX - 1)
trimHeight = (highestVisibleCharPixelY + 1) - (lowestVisibleCharPixelY - 1)
`Increment the character number
inc characterNumber
`Get a free image id
characterImage = getFree("image")
`Make the new character image from the character memblock
make image from memblock characterImage, characterMemblock
`Create a sprite to show and tell
spriteNum = getFree("sprite")
`Delete old character memblock
delete memblock characterMemblock
`Write the character infomation to the font array
SDM_FontCharacter(fontId, characterNumber).image = characterImage
SDM_FontCharacter(fontId, characterNumber).charWidth = newCharWidth
SDM_FontCharacter(fontId, characterNumber).charHeight = charHeight
next charX
next charY
endfunction fontId
`Draws text to a canvas
function SDM_DrawText(caption as string, fontId as integer, alignment as integer, offsetX as integer, offsetY as integer)
`Declare variables
currentCharacter as string
`Get canvas id
canvasId = SDM.currentCanvasModify
`Get the canvas image id
imageId = SDM_Canvas(canvasId).image
`Get the current canvas memblock
memblockId = SDM.currentCanvasMemblock
`Get the image width, height and depth
width = SDM_Canvas(canvasId).width
height = SDM_Canvas(canvasId).height
depth = SDM_Canvas(canvasId).depth
`Get the caption length
captionLength = len(caption)
`Loop through each character in the caption
for characterNumber = 1 to captionLength
`Get the current character
currentCharacter = mid$(caption, characterNumber)
`Get the font character image id, for the current character
charNumber = asc(currentCharacter)
`Get the image of the current character
characterImage = SDM_FontCharacter(fontId, charNumber).image
`Make sure the character exists
if characterImage > 0
if image exist(characterImage) = 1
`Get the size of the character image
charWidth = SDM_FontCharacter(fontId, charNumber).charWidth
charHeight = SDM_FontCharacter(fontId, charNumber).charHeight
`Get a free character memblock
characterMemblock = getFree("memblock")
`Make a memblock from the character image
make memblock from image characterMemblock, characterImage
`Now transfer the character from the original character image, to the new text image
for charPixelY = 1 to charHeight
for charPixelX = 1 to charWidth
`Get the memory location for the pixel
charImageByteOffsetLocation = ((charPixelY - 1) * charWidth + charPixelX - 1) * 4 + 12
`Get the pixel infomation
blue = memblock byte(characterMemblock, charImageByteOffsetLocation)
green = memblock byte(characterMemblock, charImageByteOffsetLocation + 1)
red = memblock byte(characterMemblock, charImageByteOffsetLocation + 2)
alpha = memblock byte(characterMemblock, charImageByteOffsetLocation + 3)
`Calculate the current position on the current image
canvasPixelX = offsetX + charPixelX
canvasPixelY = offsetY + charPixelY
`Check to see if its over the canvas limit
if canvasPixelX <= width and canvasPixelY <=Height and canvasPixelX => 0 and canvasPixelY => 0 and alpha > 0
`Calculate the canvas offset
canvasImageByteOffsetLocation = ((canvasPixelY - 1) * width + canvasPixelX - 1) * 4 + 12
`Add to the current drawing
write memblock byte memblockId, canvasImageByteOffsetLocation, blue
write memblock byte memblockId, canvasImageByteOffsetLocation + 1, green
write memblock byte memblockId, canvasImageByteOffsetLocation + 2, red
write memblock byte memblockId, canvasImageByteOffsetLocation + 3, alpha
endif
next charPixelX
next charPixelY
`Increment the offset by the character width
offsetX = offsetX + charWidth
`Delete character memblock
delete memblock characterMemblock
endif
endif
next characterNumber
endfunction
`Draws an image to the canvas
function SDM_DrawImage(imageImageId as integer, positionX as integer, positionY as integer, customwidth as integer, customheight as integer, transparency as integer)
`Get canvas id
canvasId = SDM.currentCanvasModify
`Get the canvas image id
imageId = SDM_Canvas(canvasId).image
`Get the current canvas memblock
memblockId = SDM.currentCanvasMemblock
`Get the image width, height and depth
width = SDM_Canvas(canvasId).width
height = SDM_Canvas(canvasId).height
depth = SDM_Canvas(canvasId).depth
`Make a memblock from the image
imageMemblockId = getFree("memblock")
make memblock from image imageMemblockId, imageImageId
`Get the image properties
imageWidth = memblock dword(imageMemblockId, 0)
imageHeight = memblock dword(imageMemblockId, 4)
imageDepth = memblock dword(imageMemblockId, 8)
`Now loop through all the pixels of the image, and write them to the canvas
for imagePixelY = 1 to imageHeight
for imagePixelX = 1 to imageWidth
`Get the byte position
imageByteOffsetLocation = ((imagePixelY - 1) * imageWidth + imagePixelX - 1) * 4 + 12
`Get the pixel infomation
blue = memblock byte(imageMemblockId, imageByteOffsetLocation)
green = memblock byte(imageMemblockId, imageByteOffsetLocation + 1)
red = memblock byte(imageMemblockId, imageByteOffsetLocation + 2)
alpha = memblock byte(imageMemblockId, imageByteOffsetLocation + 3)
`Set alpha acording to transparency settings
if transparency = 0
alpha = 255
endif
`Now copy the image to the canvas
if (positionX + imagePixelX) =< (positionX + customWidth) and (positionY + imagePixelY) =< (positionY + customHeight) and alpha > 0
`Get the byte position
canvasByteOffsetLocation = (((positionY + (imagePixelY - 1))) * width + (positionX +(imagePixelX - 1))) * 4 + 12
`Write the image to the canvas
write memblock byte memblockId, canvasByteOffsetLocation, blue
write memblock byte memblockId, canvasByteOffsetLocation+1, green
write memblock byte memblockId, canvasByteOffsetLocation+2, red
write memblock byte memblockId, canvasByteOffsetLocation+3, alpha
endif
next imagePixelX
next imagePixelY
endfunction
`Returns a free canvas id
function SDM_GetFreeCanvas()
repeat
inc canvasId
if SDM_Canvas(canvasId).used = 0 then found = 1
until found = 1
endfunction canvasId
`Returns a free font id
function SDM_GetFreeFont()
repeat
inc fontId
if SDM_Font(fontId).used = 0 then found = 1
until found = 1
endfunction fontId
`Returns a free edit id
function SDM_GetFreeLabel()
repeat
inc editId
if SDM_Label(editId).used = 0 then found = 1
until found = 1
endfunction editId
Function getFree(Free$)
select lower$(Free$)
case "file"
repeat
fnum = fnum + 1
if file open(fnum) = 0 then found = 1
until found = 1
endcase
case "object"
repeat
fnum = fnum + 1
if object exist(fnum) = 0 then found = 1
until found = 1
endcase
case "effect"
repeat
fnum = fnum + 1
if effect exist(fnum) = 0 then found = 1
until found = 1
endcase
case "image"
repeat
fnum = fnum + 1
if image exist(fnum) = 0 then found = 1
until found = 1
endcase
case "sprite"
repeat
fnum = fnum + 1
if sprite exist(fnum) = 0 then found = 1
until found = 1
endcase
case "memblock"
repeat
fnum = fnum + 1
if memblock exist(fnum) = 0 then found = 1
until found = 1
endcase
case "camera"
repeat
fnum = fnum + 1
if camera exist(fnum) = 0 then found = 1
until found = 1
endcase
case "sound"
repeat
fnum = fnum + 1
if sound exist(fnum) = 0 then found = 1
until found = 1
endcase
case "phytrigger"
repeat
fnum = fnum + 1
if phy trigger exist(fnum) = 0 then found = 1
until found = 1
endcase
case "phyforcefield"
repeat
fnum = fnum + 1
if phy get force field exist(fnum) = 0 then found = 1
until found = 1
endcase
case "phyjoint"
repeat
fnum = fnum + 1
if phy get joint exist(fnum) = 0 then found = 1
until found = 1
endcase
case "phycloth"
repeat
inc fnum
if phy get cloth exist(fnum) = 0 then found = 1
until found = 1
endcase
case "file"
repeat
fnum = fnum + 1
if file open(fnum) = 0 then found = 1
until found = 1
endcase
Endselect
EndFunction fnum
I havent had much time lately, and im still working on other features such as multiple box colours, filled circles, sperate label function - which will enable fast editing of text.
CPU: AMD X2 6000+ 3.0ghz GFX: NVIDIA BFG Geforce 8800GTS 640MB OC-550mhz core RAM: 2048mb