Background Maker
A while back, I did a BGMaker program. That's a program that takes in graphic screen (GRP0) and turn it into background (SCU0) and background tiles (BGU0, BGU1, BGU2,BGU3). I had suspected it that it fits into one screen, and it did. Here is my entry to the single screen programming challenge:
- 'BGMAKER BY HARRY HARDJONO
- CLS:CLEAR:DIM TL$[1024]:MT=-1
- OX=0:OY=0:RX=31:RY=23:P$="G2"
- ?"GRAPHIC?":INPUT P$
- LOAD "GRP0:"+P$,FALSE
- FOR Y=0 TO RY:FOR X=0 TO RX
- X1=X*8:X2=X1+7:Y1=Y*8:Y2=Y1+7
- G$="":FOR YY=Y1 TO Y2
- FOR XX=X1 TO X2
- G$=G$+HEX$(GSPOIT(XX,YY)%16)
- NEXT:NEXT
- CT=-1:FOR I=MT TO 0 STEP -1
- IF G$==TL$[I] THEN CT=I
- NEXT
- IF CT==-1 THEN GOSUB @SETCHR
- BGPUT 0,OX+X,OY+Y,CT,0,0,0
- NEXT:NEXT:SAVE "SCU0:T0"
- FOR I=0 TO FLOOR(MT/256)
- B$="BGU"+STR$(I)
- SAVE B$+":B"+STR$(I):NEXT:END
- @SETCHR
- MT=MT+1:TL$[MT]=G$:CT=MT
- BU$="BGU"+STR$(FLOOR(MT/256))
- CHRSET BU$,MT%256,G$:RETURN
I also did a version with no memory. I'm not going to show it here because it's not only complicated, but also very, very slow. In fact, at one point, I actually put in an array to do it. That's when I decided to do a version with arrays instead.
Anyway, if you look at the code above, you'll see that it only works on single graphic screen. That's not what I want to have. I want to have the full 64 by 64 background tile generator. As you can see, from the variables OX, OY, P$, even though they do nothing in this version, the original design does take the full background field into account.
Here is the updated BGMAKER program that includes all available background screen. You'll need six graphic screens. They're all loaded into GRP0, but the build is done on different parts of BG screen.
'BGMAKER BY HARRY HARDJONO
CLS:CLEAR:DIM TL$[1024]:MT=-1
@PICFILE
DATA "G1","G2","G3","G6","G5","G4"
RESTORE @PICFILE
All this does is some initialization. I put the file names into DATA statements because I'm too lazy to modify the code. So I simply replaced all INPUT P$ into READ P$.
- 'LOAD GRPS
- OX=0:OY=0:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
Okay, you can see the pattern here. OX, OY are relative shifting or original X,Y coordinate. RX,RY are the size or width and height of the screen. I mentioned changing INPUT P$ into READ P$. Once I loaded the graphic into screen 0, I shifted the background to the appropriate place and call the @TILER subroutine. Since OX,OY are zeros, I guess I don't need to shift the background (using BGOFS), but I'm doing it here for consistency.
- OX=32:OY=0:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=0:OY=24:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=32:OY=24:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=0:OY=48:RX=31:RY=15:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=32:OY=48:RX=31:RY=15:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
As you can see, only the first line is being changed. I could have just put the whole thing into a subroutine. However, I'm not doing it because of the possibility that I don't have the whole 6 graphics to play with. If I only have four graphic screens, for example, it's easy for me to just comment out the necessary lines. This is judgement call, so you can certainly do it different. Whatever works.
- @SAVEBG
- SAVE "SCU0:T0"
- FOR I=0 TO FLOOR(MT/256)
- B$="BGU"+STR$(I)
- SAVE B$+":B"+STR$(I)
- NEXT
- WAIT 300
Even though this is labelled as a subroutine. This is just a piece of code. It's not being called by GOSUB. I simply put the label there because I don't need to use comment. I could have, but simply decided not to.
- FOR I=0 TO 99
- X=RND(512):Y=RND(512)
- BGOFS 0,X,Y,60:WAIT 60
- NEXT
- WAIT 300
This is simply the scrolling of the background. You see 60 at the end of BGOFS call. This is equivalent of one second. So, I scroll for one second, while waiting for one second. What happens if I don't wait? Then the scroll will be reset immediately.
- BGCLR
- CHRINIT "BGU0"
- CHRINIT "BGU1"
- CHRINIT "BGU2"
- CHRINIT "BGU3"
- GCLS
- END
This code is necessary to reset the states to the original state. Otherwise, you will see that your screen is overlaid with your background. Of course, during debugging, that's quite problematic for me, since oftentimes the program does not run to completion, so I end up doing these commands by hand.
- @TILER
- FOR Y=0 TO RY:FOR X=0 TO RX
- X1=X*8:X2=X1+7:Y1=Y*8:Y2=Y1+7
- GOSUB @TILESTR
- CT=-1:FOR I=MT TO 0 STEP -1
- IF G$==TL$[I] THEN CT=I
- NEXT
- IF CT==-1 THEN GOSUB @SETCHR
- BGPUT 0,OX+X,OY+Y,CT,0,0,0
- NEXT:NEXT
- RETURN
There's some confusion here. I should have put the X1,X2,Y1,Y2 line into @TILESTR as it serves no purpose here. TL$[I] is the 64 hexadecimal string that contains the tile info. What the program does is simply set CT (Current Tile) to -1. Whenever the tile is found, CT is set to I. If the tile is not found, then CT will contain the value of -1. Then we know to store the current string (G$) into the tile array. After that, it's trivial to just set CT into the background.
Notice that I'm not doing mirror comparison. The documentation mentioned X,Y rotation. I'm ignoring that here. Actually, those aren't X,Y rotation. Those are mirror flip. If you want to you may want to modify the code to include that check. It's easy enough to do.
- @TILESTR
- G$=""
- FOR YY=Y1 TO Y2
- FOR XX=X1 TO X2
- G$=G$+HEX$(GSPOIT(XX,YY)%16)
- NEXT:NEXT
- RETURN
I have to make a decision here. Graphic screens can handle 256 colors. BG tiles can only handle 16 colors. What do I do? Well, if you notice, BG colors can actually have 240 colors. 256 colors-16 transparent colors. You can differentiate the different palettes, but I simply decided not to.
If you want to differentiate the palettes, then you want to store FLOOR(GSPOIT(XX,YY)/16) value and enter it into G$ so that it doesn't match equivalent pattern but different palette.
Another factor to consider is the X,Y mirror. Now you see why there's X1,X2,Y1,Y2. This is done for ease of mirroring. Normal: Y1-Y2/X1-X2. X Mirror: Y1-Y2/X2-X1. Y Mirror: Y2-Y1/X1-X2. XY Mirror: Y2-Y1/X2-X1. Note that I haven't done so, so the above is not guaranteed to work. Making it work is left as homework for the readers. ;)
- @SETCHR
- MT=MT+1:IF MT>1023 THEN MT=1023
- TL$[MT]=G$:CT=MT
- BU$="BGU"+STR$(FLOOR(MT/256))
- CHRSET BU$,MT%256,G$
- RETURN
Nothing to it. This simply increment the MT (MaxTile) counter and set the character to the appropriate BGUX bank.
And there you go. A very easy to do Background Maker. Here is the whole completed source code for your convenience. I suppose I should do the QR. Well, maybe sometimes in the future, or maybe not. Easy enough for you to do it by hand.
- 'BGMAKER BY HARRY HARDJONO
- CLS:CLEAR:DIM TL$[1024]:MT=-1
- @PICFILE
- DATA "G1","G2","G3","G6","G5","G4"
- RESTORE @PICFILE
- 'LOAD GRPS
- OX=0:OY=0:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=32:OY=0:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=0:OY=24:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=32:OY=24:RX=31:RY=23:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=0:OY=48:RX=31:RY=15:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- OX=32:OY=48:RX=31:RY=15:READ P$
- LOAD "GRP0:"+P$,FALSE
- BGOFS 0,OX*8,OY*8,60:GOSUB @TILER
- @SAVEBG
- SAVE "SCU0:T0"
- FOR I=0 TO FLOOR(MT/256)
- B$="BGU"+STR$(I)
- SAVE B$+":B"+STR$(I)
- NEXT
- WAIT 300
- FOR I=0 TO 99
- X=RND(512):Y=RND(512)
- BGOFS 0,X,Y,60:WAIT 60
- NEXT
- WAIT 300
- BGCLR
- CHRINIT "BGU0"
- CHRINIT "BGU1"
- CHRINIT "BGU2"
- CHRINIT "BGU3"
- GCLS
- END
- @TILER
- FOR Y=0 TO RY:FOR X=0 TO RX
- X1=X*8:X2=X1+7:Y1=Y*8:Y2=Y1+7
- GOSUB @TILESTR
- CT=-1:FOR I=MT TO 0 STEP -1
- IF G$==TL$[I] THEN CT=I
- NEXT
- IF CT==-1 THEN GOSUB @SETCHR
- BGPUT 0,OX+X,OY+Y,CT,0,0,0
- NEXT:NEXT
- RETURN
- @TILESTR
- G$=""
- FOR YY=Y1 TO Y2
- FOR XX=X1 TO X2
- G$=G$+HEX$(GSPOIT(XX,YY)%16)
- NEXT:NEXT
- RETURN
- @SETCHR
- MT=MT+1:IF MT>1023 THEN MT=1023
- TL$[MT]=G$:CT=MT
- BU$="BGU"+STR$(FLOOR(MT/256))
- CHRSET BU$,MT%256,G$
- RETURN
No comments:
Post a Comment