magazine logo
Welcome to the official Attitude website - Europe's #1 C64 scene disk magazine
Logo Swinger
by Puterman/Civitas

Right, as the header implies, this time we'll do something quite simple, which might nevertheless be quite daunting to coding newbies: swinging a logo. If you haven't seen this effect in countless intros and demos, you've been probably living under a rock for the last 20 years. And if you have, maybe you should consider catching up on some other stuff before reading articles in "Attitude".

So, what we want to do is to get a logo move horizontally, and we don't just want it to move, we want it to move according to a sine table (i.e. not just one pixel or so per frame). As is the case with many effects, there are several ways of doing it. And in this case, there are two pretty obvious ways: use $D011 to get a VSP, or copy char data. The first alternative takes less raster time (and is more cool), but if you don't know how to swing a logo, you probably want to learn about the second alternative first, as it doesn't require any serious timing.

As we're going to copy char data, you'll want your logo (or whatever it is you're going to move around) as a charset. That means you're limited to single colour hires or 3-colour lores, but that's never stopped any oldschool maniac before. We'll also assume your logo is 40 chars wide and 6 chars high, but using other formats shouldn't be a problem. If you want a logo that needs more than one charset of graphics data, you'll need to alter $D018 somewhere in the middle of the logo, to switch charsets.

If you've got a logo in a bitmap, there are plenty of programs to convert a bitmap to a screen and a charset, and if you can't find any such tool, you could write your own. See it as an exercise!

Now, if you have the logo, the only other thing you need is a sine table. Let's make it simple and use one where the values are in the range 0-255. That means the logo won't move more than 256 pixels, but that should be enough for our purposes.

If you haven't understood what we're going to do yet, here's the outline: each frame we copy the chars onto the screen, with the 5 high bits of the sine value as offset (which means a maximum of 32 chars), and set the lower 3 bits of $D016 (x scroll) to the lower 3 bits of the since value. Yes, it's pretty much like a text scroller, usually just moves in one direction.

Each frame we'll update the offset into the sine table (let's assume the sine table is 256 bytes long, so that we don't have to check the value after we increment it):

INC SINEOFFS

Wow, that's a very nice code snippet! Then we set $D016:

LDX SINEOFFS
LDA SINE,X
AND #$07
EOR #$07
ORA #$10
STA $D016

The AND is there to mask out the 3 lower bits of the sine value, and you'll get the explanation for the EOR below.

Now all we have to do is to copy the logo chars onto the screen. We don't have to clear the screen first, as we'll overwrite the previous contents each frame. To use the five high bits of the sine value as an offset, we'll shift the value 3 times to the right:

LDA SINE,X
LSR
LSR
LSR
TAX

Now we just need a clever way to copy the chars onto the screen, and then we're all set. We want the logo to move out of the screen both to the left and to the right, and to make it as simple as possible, we'll pad each char line of the logo with empty chars on both sides. As we'll move the logo 32 chars, you'll need to pad it with 16 empty chars on each side, i.e. the first char line should look like this (assuming $FF is an empty char):

FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF
00 01 02 03 04 05 06 07
08 09 0A 0B 0C 0D 0E 0F
[...]
20 21 22 23 24 25 26 27
FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF

The second line should look the same, excepting that you should have chars 28-4F instead of 00-27, and then you can probably figure out the rest yourself. Anyway, what we get is a wider logo, which starts and ends with 16 empty rows, with the actual logo in the middle. Here's a small program to convert a logo, where the chars are located at $4000, to the format above, which stores the new char layout at $4400 (it only moves the data for 6 lines, but it's trivial to modify it to handle larger areas as well):

LDX #$00
LDA #$FF
L1 STA $4400,X
STA $4500,X
STA $4600,X
STA $4700,X
DEX
BNE L1

LDA #$00
LDX #$40
LDY #$44
STA $FC
LDA #$10
STA $FE
STA $FD
STA $FF

LDX #$05
L3 LDY #$27
L2 LDA ($FC),Y
STA ($FE),Y
DEY
BPL L2

CLC
LDA $FC
ADC #$28
STA $FC
BCC NXT
INC $FD
NXT CLC
LDA $FE
ADC #$48
STA $FE
BCC NXT2
INC $FF

NXT2 DEX
BPL L3

(I shouldn't have to give you this code, but people always complain when there isn't lots of code examples in coding articles, so I include it just to make you happy).

Now we have the char lines at $4400, so let's assume that $D018 is set to $18 and $DD00 to $03, which means the screen memory will be at $0400 and the charset will be at $2000. Then the code to actually copy the chars onto the screen will look something like this (assuming that the 5 high bits of the sine value are in the x register, shifted 3 bits to the right, so that we get a value between 0 and 31):

LDY #$00
LOOP LDA $4400,X
STA $0400,Y
LDA $4448,X
STA $0428,Y
LDA $4490,X
STA $0450,Y
LDA $44D8,X
STA $0478,Y
LDA $4520,X
STA $04A0,Y
LDA $4568,X
STA $04C8,Y
INX
INY
CPY #$28
BNE LOOP

That was simple, wasn't it? Note that as the value in the x register increases, the logo will move farther to the left of the screen, and that's why we needed to EOR the 3 low bits of the x offset (see above). If you remove the EOR, you'll get a result that's not as nice to look at, but try it just to make sure you understand what's going on.

And thus ends this article.

An example of a swinger much like the one described in this article is used in my demo "Visitors". Check out the part with the big picture of George, Usama etc. which moves back and forth.

PUTERMAN/CIVITAS

   Add/view comments on this article (comments: 0)

SCENE GROUPS
 
OPINION POLL
Do you believe we are
able to cope with
releasing "Attitude"
on a regular basis?

yes no

 YES: 279 (70.45%)
NO:117 (29.55%) 

NEWS COMMENTS

ART COMMENTS

STATISTICS
all visits:

visits today:


website started:
23/09/2004
 
Official Webpage
of Attitude
Copyright 2004-2018
 
DJ Gruby/TRIAD