I managed to get this working with the SetSpriteScissor-tip... thanks again! I'm posting the code here for 'posterity's' sake.
//
// Generate a map from file-data using a sprite and SetSpriteScissor to display a portion of it.
//
`======================================================================================
` Set variables and assign values
`--------------------------------------------------------------------------------------
// variables for the map
global MapLength as integer : MapLength = 64
global MapWidth as integer : MapWidth = 64
type MapUDT
Char$ as string
endtype
dim Map[64, 64] as MapUDT
// variables for the sprites
global SprId as integer
global SprSize as integer : SprSize = 8
global SprPlayer as integer
// variables for debug information
global DebugInfo as integer : DebugInfo = 1
// variables for setting the screen
global ScreenAspect# as float
global ScreenHeight# as float
global ScreenMax# as float
global ScreenMin# as float
global ScreenWidth# as float
// variables for minimap
global MMImage as integer
global MMPosX as integer
global MMPosZ as integer
global MMSizeX as integer
global MMSizeZ as integer
global MMRange# as float : MMRange# = 21
// variables to test the minimap
global CropStartX as integer
global CropStartZ as integer
global CropEndX as integer
global CropEndZ as integer
global PlayerPosX as integer
global PlayerPosZ as integer
`======================================================================================
` Load data and create sprite
`--------------------------------------------------------------------------------------
LoadMap("maps/Dungeon02_64.map")
SprId = CreateSprite(0)
SetSpriteSize(SprId, SprSize, SprSize)
SetSpriteTransparency(SprId, 0)
SprPlayer = CreateSprite(0)
SetSpriteSize(SprPlayer, SprSize, SprSize)
SetSpriteTransparency(SprId, 0)
SetSpriteColor(SprPlayer, 86, 117, 226, 255)
SetSpriteVisible(SprPlayer, 0)
`======================================================================================
` Set the screen
`--------------------------------------------------------------------------------------
SetOrientationAllowed(0, 0, 1, 1)
SetVirtualResolution(1024, 768)
SetResolutionMode(0)
SetPrintSize(18)
SetClearColor(0, 0, 0)
SetScissor(0, 0, 0, 0)
SetSortTextures(1)
SetSyncRate(0, 0)
ScreenWidth# = GetVirtualWidth()
ScreenHeight# = GetVirtualHeight()
if ScreenWidth# > ScreenHeight#
ScreenMin# = ScreenHeight#
ScreenMax# = ScreenWidth#
else
ScreenMin# = ScreenWidth#
ScreenMax# = ScreenHeight#
endif
ScreenAspect# = ScreenMax# / ScreenMin#
`======================================================================================
` Prepare the map in the backbuffer
`--------------------------------------------------------------------------------------
// Generate the minimap - parameters allow you to generate only a part of it, should that be desirable
GenerateMiniMap(1, 1, MapWidth, MapLength)
// update and render the map in the backbuffer
update(0)
render()
// calculate the size of the map in pixels and grab an image it
MMSizeX = MapWidth * SprSize
MMSizeZ = MapLength * SprSize
MMImage = GetImage(MMPosX, MMPosZ, (MMPosX + MMSizeX), (MMPosZ + MMSizeZ))
// delete the sprite originally used to generate the map and create a new one, containing the whole map
DeleteSprite(SprId)
SprId = CreateSprite(MMImage)
// clear the screen and swap the backbuffer to the frontbuffer
ClearScreen()
Swap()
`======================================================================================
` Prepare the execution of the main program loop
`--------------------------------------------------------------------------------------
// set coordinates to display the map (can be any position one likes)
MMPosX = 0
MMPosZ = GetVirtualHeight() - GetSpriteHeight(SprId)
// place the sprite at the desired coordinates
SetSpritePosition(SprId, MMPosX, MMPosZ)
// find a valid position for the player
repeat
PlayerPosX = Random(1, MapWidth)
PlayerPosZ = Random(1, MapLength)
until Map[PlayerPosX, PlayerPosZ].Char$ = ":"
// make player sprite visible
SetSpriteVisible(SprPlayer, 1)
SetSpriteDepth(SprPlayer, 1)
// calculate initial crop for sprite, based on set range
CalcCropRange(PlayerPosX, PlayerPosZ, MMRange#)
SetSpriteScissor(SprId, MMPosX, MMPosZ, MMPosX + (MMRange# * SprSize), MMPosZ + (MMRange# * SprSize))
`======================================================================================
` Execute main program loop
`--------------------------------------------------------------------------------------
do
// display some debug-information, should one desire so
if DebugInfo = 1
print("FPS : " + str(ScreenFPS()))
print("")
print("Map Size : " + str(MapWidth) + ", " + str(MapLength) + " (" + str(round(GetSpriteWidth(SprId))) + ", " + str(round(GetSpriteHeight(SprId))) + ")")
print("Player X/Y : " + str(PlayerPosX) + ", " + str(PlayerPosZ) + " (" + str(PlayerPosX * SprSize) + ", " + str(PlayerPosZ * SprSize) +")")
print("Crop StrX/Y: " + str(CropStartX) + ", " + str(CropStartZ) + " (" + str(CropStartX * SprSize) + ", " + str(CropStartZ * SprSize) +")")
print("Crop EndX/Y: " + str(CropEndX) + ", " + str(CropEndZ) + " (" + str(CropEndX * SprSize) + ", " + str(CropEndZ * SprSize) +")")
print("")
print("Map X/Y : " + str(round(GetSpriteX(SprId))) + ", " + str(round(GetSpriteY(SprId))))
endif
// place sprites (in this example it shouldn't really need to be called every loop, since it doesn't update it's position)
SetSpritePosition(SprPlayer, MMPosX - (CropStartX * SprSize) + (PlayerPosX * SprSize), MMPosZ - (CropStartZ * SprSize) + (PlayerPosZ * SprSize))
SetSpritePosition(SprId, MMPosX - (CropStartX * SprSize), MMPosZ - (CropStartZ * SprSize))
Sync()
loop
`======================================================================================
` Function to load map-data from file
`--------------------------------------------------------------------------------------
function LoadMap(FileName$)
if GetFileExists(FileName$) = 1
FileId = OpenToRead(FileName$)
for z = 1 to MapLength
for x = 1 to MapWidth
Char$ = chr(ReadByte(FileId))
Map[x, z].Char$ = Char$
next x
next z
CloseFile(FileId)
endif
endfunction
`======================================================================================
` Function to generate the minimap [StartX, StartZ, EndX, EndZ]
`--------------------------------------------------------------------------------------
function GenerateMiniMap(StartX, StartZ, EndX, EndZ)
// display the minimap-sprites
for z = StartZ to EndZ
for x = StartX to EndX
if z => 1 and z <= MapLength
if x => 1 and x <= MapWidth
Char$ = Map[x, z].Char$
colR = 20
colG = 20
colB = 20
// set sprite color
if Char$ = "D" or Char$ = "O" or Char$ = "U"
colR = 132
colG = 128
colB = 112
endif
if Char$ = " " or Char$ = ":"
colR = 244
colG = 226
colB = 152
endif
if Char$ = "#" or Char$ = "S"
colR = 166
colG = 113
colB = 22
endif
// draw sprite
setSpritePosition(SprId, x * SprSize, z * SprSize)
setSpriteColor(SprId, colR, colG, colB, 255)
drawSprite(SprId)
endif
endif
next x
next z
endfunction
`======================================================================================
` Function to calculate the coordinates the map-sprite should be displayed if cropped
`--------------------------------------------------------------------------------------
function CalcCropRange(PosX, PosZ, Range)
// find radius of range
RadRange = round(MMRange# / 2)
// find positions for crop e.g. SetSpriteScissor-function
CropStartX = PosX - RadRange
CropStartZ = PosZ - RadRange
CropEndX = PosX + RadRange
CropEndZ = PosZ + RadRange
endfunction
What it does:
- generates a large sprite with an image of a map
- finds a valid position for a player on the map
- crops the map according to the position of the player (e.g. the clipped sprite is moved, so only the part around the player is visible).