Post by Tasp on Aug 6, 2020 14:20:07 GMT -5
It's me again, sorry!
This is a follow on from my previous question. Which is here link.
So you can create a map using the linked code, whereby you select a pre drawn background image and then place objects (sprites) as an overlay. Each of the objects are assigned a "zone" number.
This program is supposed to load the background map, load the objects, from a text file into an array, find the zone and position X and Y, then move the background image to make the found object centralised. Simple huh!
Well no, I know how to grab, window height and width, image size and location of object.
However I'm stuck on the equation I need to make. Nothing I seem to think of is correct. It's like I have 3 values that I don't need.
My theory is, if the object is past the center, remove the amount that its past it by, if its before then add the difference to move it to the center.
The code may seem messy again but I think I'm caught in the loop of trying to fix different sections again.
I've included all the images and a larger test map with data file in case you need it.
Maps.zip (338.53 KB)
Thank you in advance.
This is a follow on from my previous question. Which is here link.
So you can create a map using the linked code, whereby you select a pre drawn background image and then place objects (sprites) as an overlay. Each of the objects are assigned a "zone" number.
This program is supposed to load the background map, load the objects, from a text file into an array, find the zone and position X and Y, then move the background image to make the found object centralised. Simple huh!
Well no, I know how to grab, window height and width, image size and location of object.
However I'm stuck on the equation I need to make. Nothing I seem to think of is correct. It's like I have 3 values that I don't need.
My theory is, if the object is past the center, remove the amount that its past it by, if its before then add the difference to move it to the center.
The code may seem messy again but I think I'm caught in the loop of trying to fix different sections again.
'The Zone is the object searched for when looking through the txt file of sprite positions
Zone = 1001 : ChipNumber$ = "000003a"
'TO DO
'ERROR CHECK TO SEE IF MAP DATA FILE EXISTS OTHERWISE IT'll CRASH
[MapForAlarm]
'nomainwin
mapID$ = ChipNumber$
'Define file paths
iconimg$ = DefaultDir$ + "\Images\Icons\"
maskimg$ = DefaultDir$ + "\Images\Masked\"
mappath$ = DefaultDir$ + "\Maps\Data\"
Mapfile$="Maps\" + mapID$ + ".bmp"
'load the map image and get its size
open Mapfile$ for input as #bmp
'the bmpfileheader
bmp$ = Input$(#bmp,lof(#bmp))
bmpw=value(mid$(bmp$,19,4))'width
bmph=value(mid$(bmp$,23,4))'height
close #bmp
'now find out how much space the
'window style takes for frame and title
WindowWidth=200
WindowHeight=200
open "Measuring" for graphics_nf_nsb as #1
#1, "home ; down ; posxy x y"
ThemeWidth=WindowWidth-2*x
ThemeHeight=WindowHeight-2*y
close #1
WindowWidth = bmpw + ThemeWidth-2
if WindowWidth > DisplayWidth then WindowWidth = DisplayWidth
WindowHeight = bmph + ThemeHeight-2
if WindowHeight > DisplayHeight then WindowHeight = DisplayHeight
UpperLeftX = (DisplayWidth - WindowWidth) / 2
UpperLeftY = (DisplayHeight - WindowHeight) / 2
graphicbox #1.g 0,0, WindowWidth , WindowHeight
open "Alarm" for window_nf as #1
#1 "trapclose quit"
loadbmp "image",Mapfile$
#1.g "down ; background image"
#1.g "drawsprites"
global BGPX, BGPY
global BGX, BGY
global type,x, y, w, h, zone, objects, currentobject, iconimg$, maskimg$, mappath$, mapID$, comp, MaxIcons, Icontype, IconSize
DIM object(1000,10)
z=1 'zone object assigned to
type=2 'type of object
x=3 'x
y=4 'y
w=5 'width of object
h=6 'hight of object
MaxIcons = 40
Icontype = 0
IconSize = 40
#1.g "when leftButtonDouble coords"
call LoadMapImages
call LoadObjects
'#1.g "drawsprites"
'Search for zonenumber and display on map
FOR findzone = 0 to objects
IF Zone = object(findzone,zone) THEN
print "item found!"
TRACE 3
'print "object position "; object(findzone, x) ;" ";object(findzone, y)
'print "background x y "; object(findzone, x) ;" ";object(findzone, y)
'Collect object position from array
objectposX = object(findzone, x)
objectposY = object(findzone, y)
Print "objectposX :"; objectposX ; " objectposY :" ; objectposY
'Collect Window dimensions then find the center
centreWindowX = WindowWidth / 2
centreWindowY = WindowHeight / 2
print "centreWindowX :"; centreWindowX ; " centreWindowY :" ;centreWindowY
'Collect Image dimensions the find the center, don't think we actually need this.
centreImageW = bmpw / 2
centreImageH = bmph / 2
print "centreImageW :"; centreImageW ; " centreImageH :" ;centreImageH
'Calculate where to place the BG so the object is centred on the screen
'BGPX = Backgground Position X and Y
IF objectposX > centreWindowX THEN
valueX = objectpositionX - centreWindowX
print "value X "; valueX
BGPX = valueX - objectpositionX
ELSE
valueX = objectpositionX - centreWindowX
print "value X "; valueX
BGPX = valueX + objectpositionX
END IF
IF objectposY > centreWindowY THEN
valueY = objectpositionY - centreWindowY
print "value Y "; valueY
BGPY = valueY - objectpositionY
ELSE
valueY = objectpositionY - centreWindowY
print "value Y " ;valueY
BGPY = valueY + objectpositionY
END IF
'Reload objects with the new calculation info, so we move the objects with the BackGround
CALL LoadObjects
Print "BGPX: "; BGPX ; " BGPY: ";BGPY
'Move background
#1.g "backgroundxy ";BGPX;" "; BGPY
'Draw object in the middle of the screen
#1.g "drawsprites"
#1.g "DRAWBMP activation "; BGPX;" "; BGPY
found = 1
END IF
NEXT
IF found = 0 THEN NOTICE "An alarm occurred but no matching zone in database!"
WAIT
'Sub to get info and display when object is double clicked.
SUB coords handle$, xpos, ypos
print "Double Clicked on "; handle$ , xpos, ypos
'Get window coords
STRUCT Rect, x1 As Long, y1 As Long, x2 As Long, y2 As Long : MapCordshandle = hwnd(#1)
CallDLL #user32, "GetWindowRect", MapCordshandle as uLong, Rect As struct, result As Long
CurrentMapWinPosX = Rect.x1.struct : CurrentMapWinPosY = Rect.y1.struct 'upper y coord
for n = 1 to objects
if xpos>object(n,x) and xpos<object(n,x)+20 and ypos>object(n,y) and ypos<object(n,y)+20 then
print "Item: ";n
UpperLeftX = CurrentMapWinPosX + MouseX: UpperLeftY = CurrentMapWinPosY + MouseY + 10 : WindowWidth = 50 : WindowHeight = 30
STATICTEXT #comp.1, "", 5, 5, 50, 18
IF comp = 0 THEN
comp = 1
OPEN "Component" FOR window_popup as #comp
#comp.1 "!font Trebuchet 12 BOLD"
#comp.1, object(n,zone)
#1.g "when mouseMove [CloseCoordsCompWindow]"
END IF
exit for
end if
next
END SUB
[CloseCoordsCompWindow]
IF comp THEN
CLOSE #comp
#1.g "when mouseMove"
comp = 0
END IF
WAIT
SUB quit h$
CLOSE #1
END
END SUB
'This loop creates images for the main mapping window
SUB LoadMapImages
FOR a = 1 to MaxIcons
LOADBMP "i" ; a , maskimg$ ; a ;".bmp"
NEXT
LOADBMP "activation", iconimg$ + "activation.bmp"
END SUB
'*************************************************LOAD MAP*********************************************************
SUB LoadObjects
OPEN mappath$ + mapID$ + ".txt" FOR INPUT AS #readFile
objects=0
WHILE NOT(EOF(#readFile))
objects = objects + 1
INPUTCSV #readFile, a$, b$, c$, d$, e$, f$, g$, h$
object(objects,zone) = VAL(a$)
object(objects,type) = VAL(b$) ' Icontype
object(objects,x) = VAL(c$) ' x position
object(objects,y) = VAL(d$) ' y position
object(objects,w) = VAL(e$)
object(objects,h) = VAL(f$)
object(objects,7) = VAL(g$)
object(objects,8) = VAL(h$)
#1.g "addsprite s";objects;" i";object(objects,type)
#1.g "spritexy s";objects;" ";object(objects,x) + BGPX;" ";object(objects,y) + BGPY
WEND
#1.g "drawsprites"
CLOSE #readFile
END SUB
'*********************************************END LOAD MAP*********************************************************
'This is a Rod function to get the bmp size, I still have no clue how this actually works but it does so leave it alone!
function value(x$)
select case len(x$)
case 1
value = asc(x$)
case 2
value=asc(mid$(x$,1,1))
value=value+(asc(mid$(x$,2,1))*256)
case 3
value=asc(mid$(x$,1,1))
value=value+(asc(mid$(x$,2,1))*256)
value=value+(asc(mid$(x$,3,1))*65536)
case 4
value=asc(mid$(x$,1,1))
value=value+(asc(mid$(x$,2,1))*256)
value=value+(asc(mid$(x$,3,1))*65536)
value=value+(asc(mid$(x$,4,1))*16777216)
end select
end function
I've included all the images and a larger test map with data file in case you need it.
Maps.zip (338.53 KB)
Thank you in advance.