Qbasicnews.com

Full Version: Map Loading help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all,

Im working on a rpg, but the map loading wont work Sad I dunno if its because my code reads the map in one long line, but I am hoping you can help. here is the loading of the map:

Code:
SUB loadmap (map$)

DIM fh AS INTEGER
fh = FREEFILE

OPEN map$ FOR INPUT AS #fh

INPUT #fh, MaxX, MaxY

FOR j = 0 TO MaxY
  FOR i = 0 TO MaxX
    INPUT #fh, map(i, j)
  NEXT
NEXT

CLOSE #fh

END SUB

Then before this I have the obvious stuff ect:

Code:
DIFINT A-Z

DIM SHARED map(50, 50)

the map looks like this(im not going to write it all out:

Code:
49,49
1,1,1,1,1...
1,1,1,1,1...
1,1,1,1,1...
.....

Now when I run the program I get a subscript out of range error on this line:

Code:
tilenum = map(X + xtile, Y + ytile)

Any help??
Mofu
I suppose you know what that error means...

do something like this in your program

at the start after sub/func declarations
Code:
ON ERROR GOTO errquit:

and in the end of the program (NOT in any sub)
Code:
errquit:
SCREEN 0
CLS
PRINT "X:",X
PRINT "Y:",Y
PRINT "xtile:",xtile
PRINT "ytile:",ytile
'----This I added to make it clearer       ...or something
PRINT "the error was caused by the fact that"
IF X+xtile<0 OR X+xtile>50 THEN
  PRINT "X or xtile"
END IF
IF Y+ytile<0 OR Y+ytile>50 THEN
  PRINT "Y or ytile"
END IF
PRINT "is too big/small"
'------ end of addition
SLEEP
END

Also you need to make X,Y,xtile,ytile SHARED and also add aditional
stuff like resetting the keyboard, if you have any keyboardhandler
and such in the errquit part.

It should output some help, and if it's this I/we need some
more code to solve it (which we do anyway I think)

...Or even better, wait for the pro's to come and help you...
Well Ill just post the whole code(its quite small):

Code:
DEFINT A-Z
'$INCLUDE: 'rellib.bi'

DECLARE SUB loadgraphics ()
DECLARE SUB loadmap (map$)
DECLARE SUB drawmap ()
DECLARE SUB up ()
DECLARE SUB down ()
DECLARE SUB left ()
DECLARE SUB right ()

DIM SHARED TileMaxX, TileMaxY, PlayerX, PlayerY, MaxX, MaxY

DIM SHARED buffer(31999), tile(260), player(130)
DIM SHARED map(50, 50)

TileMaxX = MaxX * 16
TileMaxY = MaxY * 16
PlayerX = 160
PlayerY = 96

SCREEN 13

loadgraphics
loadmap "map1.map"
drawmap
relkeyboardON
vsync = 0

DO
IF vsync = 1 THEN
  relwait
  LOCATE 2, 1: PRINT "vsync: ON"
END IF

IF RelKey(KEYENTER) THEN
  vsync = 1
END IF

IF RelKey(KEYDOWN) THEN
    down
END IF
IF RelKey(KEYUP) THEN
    up
END IF
IF RelKey(KEYRIGHT) THEN
    right
END IF
IF RelKey(KEYLEFT) THEN
    left
END IF
IF RelKey(KEYESC) THEN
    relkeyboardoff
    END
END IF

drawmap
relpcopy video, VARSEG(buffer(0))

fps = fps + 1
IF starttime& + 1 < TIMER THEN
  fps2 = fps
  fps = 0
  starttime& = TIMER
END IF
LOCATE 1, 1: PRINT fps2

LOOP

REM $DYNAMIC
SUB down

  PlayerY = PlayerY + 1

END SUB

SUB drawmap

IF PlayerX < 0 THEN PlayerX = 0
IF PlayerY < 0 THEN PlayerY = 0
IF PlayerX > TileMaxX - (160 + 16) THEN PlayerX = TileMaxX - (160 + 16)
IF PlayerY > TileMaxY - (96 + 16) THEN PlayerY = TileMaxY - (96 + 16)

cameraX = PlayerX - 160
cameraY = PlayerY - 96

IF cameraX < 0 THEN cameraX = 0
IF cameraY < 0 THEN cameraY = 0
IF cameraX > TileMaxX - (320 + 160) THEN cameraX = TileMaxX - (320 + 160)
IF cameraY > TileMaxY - (200 + 96) THEN cameraY = TileMaxY - (200 + 96)


xtile = INT(cameraX \ 16)
ytile = INT(cameraY \ 16)

FOR X = 0 TO 21
  FOR Y = 0 TO 13
    tilenum = map(X + xtile, Y + ytile)
    Xpos = cameraX MOD 16
    Ypos = cameraY MOD 16
    relspritesolid VARSEG(buffer(0)), X * 16 - Xpos, Y * 16 - Ypos, VARSEG(tile(tilenum * 130)), VARPTR(tile(tilenum * 130))
  NEXT
NEXT

pXpos = PlayerX - cameraX
pYpos = PlayerY - cameraY

  relspritesolid VARSEG(buffer(0)), pXpos, pYpos, VARSEG(player(0)), VARPTR(player(0))

END SUB

SUB left

  PlayerX = PlayerX - 1

END SUB

SUB loadgraphics

LINE (0, 0)-(15, 15), 1, BF
GET (0, 0)-(15, 15), tile(0)

LINE (0, 0)-(15, 15), 2, BF
GET (0, 0)-(15, 15), tile(130)

LINE (0, 0)-(15, 15), 4, BF
GET (0, 0)-(15, 15), player(0)

END SUB

SUB loadmap (map$)

DIM fh AS INTEGER
fh = FREEFILE

OPEN map$ FOR INPUT AS #fh

INPUT #fh, MaxX, MaxY

FOR j = 0 TO MaxY
  FOR i = 0 TO MaxX
    INPUT #fh, map(i, j)
  NEXT
NEXT

CLOSE #fh

END SUB

SUB right

  PlayerX = PlayerX + 1

END SUB

SUB up

  PlayerY = PlayerY - 1

END SUB
I investigated the maximum value of "X + PlayerX" and "Y + PlayerY", under bug-free circumstances, and they are 39.

What's causing the error, in fact, is that you are setting TileMaxX and TileMaxY to zero at the beginning of the code, because MaxX and MaxY haven't been loaded yet. Thus PlayerX and PlayerY are -160, and your initial subscripts are (-16, 16).
Thanks soooooo much dude, its working now Big Grin
could you tell me how I can change it to display the whole 50 tiles instead of 39?

Im a noob to scrolling Wink

Thanks,
Mofu
not sure..... can you actually see all 50x50 tiles all at once? If you can, just modify the constraints..... it's late here.....
Quote:could you tell me how I can change it to display the whole 50 tiles instead of 39?
Has nothing to do with scrolling. Look at your screen width/height, and then at the sprite's width/height.

E.g.:
Screenwidth = 320 (mode 13h)
Spritewidth = 20
Number of tiles horizontal = 320 / 20 = 16

Screenheight = 200 (mode 13h)
Spriteheight = 20
Number of tiles vertical = 200 / 20 = 10

Number of sprites on screen = 16 * 10 = 160

If you don't want this number of tiles on the screen, make your sprites smaller or larger, or make the screen smaller or larger Wink
Oops I forgot to post that I got it working Wink

This was the problem:

Code:
IF cameraX < 0 THEN cameraX = 0
IF cameraY < 0 THEN cameraY = 0
IF cameraX > TileMaxX - (320 + 160) THEN cameraX = TileMaxX - (320 + 160)
IF cameraY > TileMaxY - (200 + 96) THEN cameraY = TileMaxY - (200 + 96)