Author Topic: Any trick to make sprites move faster?  (Read 6074 times)

0 Members and 2 Guests are viewing this topic.

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Any trick to make sprites move faster?
« on: August 10, 2010, 02:44:05 am »
I'm using the below routine to scroll sprites down my screen, and it's not that they scroll very slow, but just not quite as fast as I want to.
Are there any ways/tricks to let 'em scroll faster? Besides the obvious optimizing (since that will probably only lower the ammount of time by a few nanoseconds  :-\).
Code: [Select]
moveallspritesdown1pixel:
PUSH BC
    LD     HL, emptyscreen                 ; A picture with the game-screen, but then empty, without any sprites , so it basically overwrites the screen
    LD     DE, PlotSScreen+(0*12)       ; so that I can draw the sprites again.
    LD     BC, 64*12
    LDIR
LD A, (Yspr1)
CP 64 ; checks if the sprite is out of the screen, and doesn't move the sprite down if so.
JR Z, DownSpr2
DownSpr1:
LD A, (xspr1)
LD (xpos), A
LD A, (yspr1)
INC A
LD (ypos), A
LD (yspr1), A
CALL loadthesprite
DownSpr2:
LD A, (Yspr2)
CP 64
JR Z, DownSpr3
LD A, (xspr2)
LD (xpos), A
LD A, (yspr2)
INC A
LD (ypos), A
LD (yspr2), A
CALL loadthesprite
DownSpr3:
LD A, (Yspr3)
CP 64
JR Z, DownSpr4
LD A, (xspr3)
LD (xpos), A
LD A, (yspr3)
INC A
LD (ypos), A
LD (yspr3), A
CALL loadthesprite
DownSpr4:
ect ect ect untill 16 sprites are moved down.


------ Check Key routine here

------ big routine here that checks the location of all the sprites, and acts accordingly
------ routine here that shows 2 more sprites
------ big routine here that shows 4 more sprites

        b_call _GrBufCpy
POP BC
DEC B
JP NZ, moveallspritesdown1pixel

The sprites that scroll down are 8x8 in size.

Of course I could just move the sprites down 2 pixels each loop, but I'm afraid that will look ugly and not smooth anymore, and it might feel unresponsive when playing the game.
Any tricks? Or are my options limited to optimizing?
And what's faster: loading an empty picture on the screen, and then draw ~20 sprites, or using XOR to remove the sprites, then draw them again on their new location?

Thanks!
« Last Edit: August 10, 2010, 02:48:32 am by Jerros »


79% of all statistics are made up randomly.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Any trick to make sprites move faster?
« Reply #1 on: August 10, 2010, 02:58:54 am »
The first thing that stands out at me is that you are using _GrBufCpy which is a very slow routine. You can use a custom one to increase the speed a lot and use the wasted clock cycles of that routine to clear the buffer as it draws to the screen so then you only need the 20 sprites total and no clearing at all since its done automatically.  You can find one of those routines here.

EDIT: Actually, it looks like you're double buffering instead of clearing the screen.  You can do that too with a few modifications.
« Last Edit: August 10, 2010, 03:00:57 am by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Any trick to make sprites move faster?
« Reply #2 on: August 10, 2010, 03:18:42 am »
Actually, it looks like you're double buffering instead of clearing the screen.  You can do that too with a few modifications.

I'm just overwriting the graphbuffer to "clear" the screen, since I don't want the screen to be cleared, but just the sprites.
And thanks, I'll look into that link you gave me.

EDIT:
Woot!
Using the routine in the link you gave me, the sprites now schwoosh at light-speed!
You're incredible man, I ask for a trick to speed things up, and 5 minutes later you give me the PERFECT answer.

1 Thing now though, how do I DECREASE the speed now?  ;D
It's a bit TOO fast.
Will a simple Loop do?
Code: [Select]
  LD   B, *number to slow things down with*
Loop:
   LD   A, 1    ; Some random dummy instruction
   djnz   Loop

Will this work in slowing things down to desired speed?

EVEN MORE EDIT:
I should try things before asking questions here.
Looping works and allows me to adjust the rate at which it's slowed.
I use a slightly different loop than I posted above though.
Thanks Quigibo, you rule!
« Last Edit: August 10, 2010, 05:27:40 am by Jerros »


79% of all statistics are made up randomly.

_player1537

  • Guest
Re: Any trick to make sprites move faster?
« Reply #3 on: August 10, 2010, 09:41:02 am »
Nops can slow it down a bit.  Nice to hear that you got the answer to your problem :D

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Any trick to make sprites move faster?
« Reply #4 on: August 10, 2010, 10:57:50 am »
Nops can slow it down a bit.

I'vebeen through the forum, and Quigibo stated somewhere that you shouldn't use NOP's for that.
I dont remember why, but I'll just be a good boy and listen to him! ^_^


79% of all statistics are made up randomly.

_player1537

  • Guest
Re: Any trick to make sprites move faster?
« Reply #5 on: August 10, 2010, 11:02:26 am »
Probably because of the size, and that you can't fine tune the delay would be my guess.  If you needed even more pause, you could do something like this:
Code: [Select]
ld bc, number
Loop:
 dec bc
 ld a, b
 or c
 jr nz, Loop

That will let you make a 16 bit loop :D
« Last Edit: August 10, 2010, 11:03:43 am by _player1537 »

Offline Jerros

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 137
  • Rating: +9/-0
    • View Profile
Re: Any trick to make sprites move faster?
« Reply #6 on: August 10, 2010, 11:11:04 am »
Doing this now:
Code: [Select]
LD A, (slowfactor)        ; lets me change the delay during the routine. Loading 255 will be the slowest
LD B, A                       ; speed, while loading 0 will be the fastest.
lowerspeed1:
CALL lowerspeed
djnz lowerspeed1
--------------------------------
              rest of the loop in my programm
             -------------------------------------
lowerspeed:
LD A, 8              ; Found out that with 255 in (slowfactor), doing 8 loops here gives me the slowest speed
decreasespeed:                        ; I want, so that I have the largest "area" to fine tune in.
LD DE, 65535      ; <-- The dummie instruction
DEC A
cp 0
JR NZ, decreasespeed
ret


79% of all statistics are made up randomly.