Rooky question on Sprite speed

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by pbdep.


Hiya all,

Well its my first time on the x86 that i try do to do some
grafix programming. I did it 15 years ago a lot on the
6502 C64 (Demo's and games) but i dont have the slightest
idea on how do do it on the x86..

I want 2 sprites moving on the screen.
Sprite #1 moves slower then sprite #2.
How to do this in Fullscreen mode?

I tried a delay(xxx) inside a repeat with flipbuffers()
but the machine hangs afterwards, so Delay is not an option.

The above is with 2 sprites but emgine it with 1000 sprites?
1000 sprites with all a random move-speed on the screen.

A hint is very welcome.

Regards,
Norman.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

you could make a structure that holds the coordinates, the diagonal speed and the horizontal speed. Where the speed is in pixels/frame. So every frame you iterate trough a list of sprites and adds the speed to the coordinates and then draw the sprites to the screen at their new location.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by pbdep.
Originally posted by Pupil

you could make a structure that holds the coordinates, the diagonal speed and the horizontal speed. Where the speed is in pixels/frame. So every frame you iterate trough a list of sprites and adds the speed to the coordinates and then draw the sprites to the screen at their new location.
Hello Pupil,

Im thinking in framerates, Instead I must be thinking in pixels per frame in coordinates... Ill give it a try...thanks..

Regards,
Norman.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kendrel.

Well, since you would store the x and y positions of both sprites in variables, it would be quite simple...

just increase sprites one x or y by 1 (x+1)
and sprites two x and y by 2 or maybe more (x+2)

so sprite2 would be faster than sprite1,right?

having a look at the sprite examples that come with purebasic may help you out aswell...

so long,
kendrel

p.s. Maniac/Paramount on the c64... and many other groups :)
best machine i have coded on :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

HI pbdep and Kendrel,
yes, if you set sprite1(x+1) and sprite2(x+2) for example, sprite2 will move double fast as the first sprite... BUt please note.. i have often say and tried to explain this on this forum, due fact i saw a lot of codesnips on this forum which are not really correct...

What does i mean? Following... Most people (as i saw) are using variables like x+1 and y+2 for example without setting any FrameRate at programstart or they init the FrameRate() command to 50 or 70 or whatever...

I dont like this methode with the FrameLimitation, due fact... If you have a very modern and fast PC.. why should the game run only in 50 fps if your machine can do more??? Otherside, if you set the FrameRate to 70 for example and someone having an old slow machine, the game (innerloop) will run to slow!! I think thats not a good way in my eyes...

My personal methode is to use x+1 and y+2 for example too... But without any frame limiations... Sure, if you only use x+1 and y+2, your programm wil run to fast on modern/fast PC and maybe to slow or in the correct speed on old slow PCs...

But we can handle this to run on all machines in same speed (without!! any frame limiations)... We only need to have a deltatime to move the objects...

I always know that on this forum are some persons saying.."ahhh mr.vain... your methode suxx, just use SetFrameRate() at programstart..." but its on you which mehtode you prefer...

Anyway take a look to the following link...:
viewtopic.php?t=3708

happy coding...
C64 rulez... SYS64738


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by pbdep.
Originally posted by MrVainSCL

HI pbdep and Kendrel,
yes, if you set sprite1(x+1) and sprite2(x+2) for example, sprite2 will move double fast as the first sprite... BUt please note.. i have often say and tried to explain this on this forum, due fact i saw a lot of codesnips on this forum which are not really correct...
...
...
greetz
MrVainSCL! aka Thorsten

Hi MrVainSCl,

That was indeed my second question,
where to get the global delay from.

Ill have a look at your link...thnx..

Regards,
Norman.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

Hiya! :) this is a small xmas card i sent a few people using sprites with random motion (snow). It may help you out:

Code: Select all

;===========================================================================
; CONSTANTS
;===========================================================================

;window
#APP_NAME = "Xmas Card"
#ROOT_WINDOW = 1
#ROOT_WINDOW_WIDTH = 320
#ROOT_WINDOW_HEIGHT = 400
#ROOT_WINDOW_FLAGS = #PB_Window_SystemMenu | #PB_Window_WindowCentered

;flakes
#NUMBER_OF_FLAKES = 20

;image
#MAIN_IMAGE = #NUMBER_OF_FLAKES+1

;===========================================================================
; GLOBAL VARIABLES / STRUCTURES / ARRAYS
;===========================================================================

Structure flake
    xPos.f
    yPos.f
    amplitude.f
    xStep.f
    yStep.f
    xCoord.f
EndStructure
Dim flakes.flake(#NUMBER_OF_FLAKES)

;===========================================================================
; FUNCTIONS
;===========================================================================

;initalise values for all flakes
Procedure initFlakes()
    For x = 0 To #NUMBER_OF_FLAKES-1
        flakes(x)\xPos = Random(#ROOT_WINDOW_WIDTH)
        flakes(x)\yPos = Random(#ROOT_WINDOW_HEIGHT)
        flakes(x)\amplitude = Random(3)
        flakes(x)\xStep = 0.02 + Random(1)/10
        flakes(x)\yStep = 1+Random(2)
        flakes(x)\xCoord = 0
    Next x
EndProcedure

;move flakes on the y axis
Procedure yMoveFlakes()
    For x = 0 To #NUMBER_OF_FLAKES-1
        flakes(x)\yPos+flakes(x)\yStep
        If flakes(x)\yPos > #ROOT_WINDOW_HEIGHT+12
            flakes(x)\yPos = -12
            flakes(x)\xPos = Random(#ROOT_WINDOW_WIDTH)
        EndIf
    Next x
EndProcedure

;move flakes on the x axis
Procedure xMoveFlakes()
    For x = 0 To #NUMBER_OF_FLAKES-1
        flakes(x)\xCoord+flakes(x)\xStep
        flakes(x)\xPos+flakes(x)\amplitude*Sin(flakes(x)\xCoord)
        If flakes(x)\xPos > #ROOT_WINDOW_WIDTH+12
            flakes(x)\xPos = -12
        EndIf
        If flakes(x)\xPos  0
        DeleteFile("song.mid")
    EndIf
EndProcedure

;===========================================================================
; GEOMETRY
;===========================================================================

If InitSprite()
    If OpenWindow(#ROOT_WINDOW, 200, 200, #ROOT_WINDOW_WIDTH, #ROOT_WINDOW_HEIGHT, #ROOT_WINDOW_FLAGS, #APP_NAME)
        If OpenWindowedScreen(WindowID(), 0, 0, #ROOT_WINDOW_WIDTH, #ROOT_WINDOW_HEIGHT, 0, 0, 0) : SetFrameRate(60)
            
            GetAsyncKeyState_(#VK_ESCAPE)

            CatchSprite(#MAIN_IMAGE, ?MainImage, 0)
            
            For x = 0 To #NUMBER_OF_FLAKES-1
                CatchSprite(x, ?FlakeImage, 0)
                TransparentSpriteColor(x, 255, 0, 255)
            Next x

            initFlakes()
            playSong()
            SetFrameRate(60)

;===========================================================================
; MAIN LOOP
;===========================================================================
            
            Repeat

                FlipBuffers()
                ClearScreen(255, 255, 255)
                
                ;main image
                DisplaySprite(#MAIN_IMAGE, 0, 0)

                For x = 0 To #NUMBER_OF_FLAKES-1
                    DisplayTransparentSprite(x, flakes(x)\xPos, flakes(x)\yPos)
                Next x

                xMoveFlakes()
                yMoveFlakes()
              
                Select WindowEvent()
                    Case #PB_EventCloseWindow
                        quit = 1
                    Case #WM_CLOSE
                        quit = 1
                EndSelect
                EscapeKey = GetAsyncKeyState_(#VK_ESCAPE)
            Until EscapeKey = -32767 Or quit = 1

            stopSong()

        EndIf
    EndIf
EndIf
End

;===========================================================================
; BINARY INCLUDES
;===========================================================================

MainImage:
IncludeBinary "front.bmp"

FlakeImage:
IncludeBinary "flake.bmp"

Song:
IncludeBinary "build\12days.mid"
SongEnd:
or you could try this using the 3d sprite library (directX) with fading sprites :):

Code: Select all

;===========================================================================
; CONSTANTS
;===========================================================================

;window
#APP_NAME = "Xmas Card"
#ROOT_WINDOW = 1
#ROOT_WINDOW_WIDTH = 320
#ROOT_WINDOW_HEIGHT = 400
#ROOT_WINDOW_FLAGS = #PB_Window_SystemMenu | #PB_Window_WindowCentered

;flakes
#NUMBER_OF_FLAKES = 20
#MAIN_IMAGE = #NUMBER_OF_FLAKES+1
#SPRITE = #NUMBER_OF_FLAKES+2

;===========================================================================
; GLOBAL VARIABLES / STRUCTURES / ARRAYS
;===========================================================================

Structure flake
    xPos.f
    yPos.f
    amplitude.f
    xStep.f
    yStep.f
    xCoord.f
    alpha.f
EndStructure
Dim flakes.flake(#NUMBER_OF_FLAKES)

;===========================================================================
; FUNCTIONS
;===========================================================================

;initalise values for all flakes
Procedure initFlakes()
    For x = 0 To #NUMBER_OF_FLAKES-1
        flakes(x)\xPos = Random(#ROOT_WINDOW_WIDTH)
        flakes(x)\yPos = Random(#ROOT_WINDOW_HEIGHT)
        flakes(x)\amplitude = Random(3)
        flakes(x)\xStep = 0.02 + Random(1)/10
        flakes(x)\yStep = 1+Random(2)
        flakes(x)\xCoord = 0
        flakes(x)\alpha= 255
    Next x
EndProcedure

;move flakes on the y axis
Procedure yMoveFlakes()
    For x = 0 To #NUMBER_OF_FLAKES-1
        flakes(x)\yPos+flakes(x)\yStep
        If flakes(x)\yPos > #ROOT_WINDOW_HEIGHT+12
            flakes(x)\yPos = -12
            flakes(x)\xPos = Random(#ROOT_WINDOW_WIDTH)
            flakes(x)\alpha = 255
        EndIf
    Next x
EndProcedure

;move flakes on the x axis
Procedure xMoveFlakes()
    For x = 0 To #NUMBER_OF_FLAKES-1
        flakes(x)\xCoord+flakes(x)\xStep
        flakes(x)\xPos+flakes(x)\amplitude*Sin(flakes(x)\xCoord)
        If flakes(x)\xPos > #ROOT_WINDOW_WIDTH+12
            flakes(x)\xPos = -12
        EndIf
        If flakes(x)\xPos  255
            flakes(x)\alpha = 255
        EndIf
    Next x
EndProcedure

;extract and play song
Procedure playSong()
    If CreateFile(1, "song.mid")
        UseFile(1)
        length = ?SongEnd-?Song
        WriteData(?Song, length)
        CloseFile(1)
        InitMovie()
        If LoadMovie(0, "song.mid")
            PlayMovie(0, ScreenID())
            Else
            Beep_(200, 300)
        EndIf
    EndIf
EndProcedure

Procedure stopSong()
    If FileSize("song.mid") > 0
        DeleteFile("song.mid")
    EndIf
EndProcedure

;===========================================================================
; GEOMETRY
;===========================================================================

If InitSprite()
    If OpenWindow(#ROOT_WINDOW, 200, 200, #ROOT_WINDOW_WIDTH, #ROOT_WINDOW_HEIGHT, #ROOT_WINDOW_FLAGS, #APP_NAME)
        If OpenWindowedScreen(WindowID(), 0, 0, #ROOT_WINDOW_WIDTH, #ROOT_WINDOW_HEIGHT, 0, 0, 0) : SetFrameRate(60)
            
            GetAsyncKeyState_(#VK_ESCAPE)

            CatchSprite(#MAIN_IMAGE, ?MainImage, 0)
            
            CatchSprite(#SPRITE, ?FlakeImage, #PB_Sprite_Texture)
            TransparentSpriteColor(#SPRITE, 255, 0, 255)
            
            If InitSprite3D()
            
                Sprite3DQuality(1)
                For x = 0 To #NUMBER_OF_FLAKES-1
                    CreateSprite3D(x, #SPRITE)
                    ;ZoomSprite3D(x, 64, 64)
                Next x

                initFlakes()
                playSong()
                SetFrameRate(60)
                If Start3D()

;===========================================================================
; MAIN LOOP
;===========================================================================
                    Repeat

                        FlipBuffers()
                        ClearScreen(255, 255, 255)
                        
                        ;main image
                        DisplaySprite(#MAIN_IMAGE, 0, 0)

                        ;If angle > 360
                            ;angle = 1
                        ;Else
                            ;angle+1
                        ;EndIf

                        calculateAlpha()

                        For x = 0 To #NUMBER_OF_FLAKES-1
                            DisplaySprite3D(x, flakes(x)\xPos, flakes(x)\yPos, flakes(x)\alpha)
                            ;RotateSprite3D(x, angle , 0)
                        Next x

                        xMoveFlakes()
                        yMoveFlakes()
                      
                        Select WindowEvent()
                            Case #PB_EventCloseWindow
                                quit = 1
                            Case #WM_CLOSE
                                quit = 1
                        EndSelect
                        EscapeKey = GetAsyncKeyState_(#VK_ESCAPE)
                    Until EscapeKey = -32767 Or quit = 1

                    Stop3D()
                    stopSong()
                Else
                    MessageRequester("Error...", "Start3D() Failed!", #PB_MessageRequester_Ok)
                EndIf
            Else
                MessageRequester("Error...", "InitSprite3D() Failed!", #PB_MessageRequester_Ok)
            EndIf
        Else
            MessageRequester("Error...", "OpenWindowedScreen() Failed!", #PB_MessageRequester_Ok)
        EndIf
    Else
        MessageRequester("Error...", "OpenWindow() Failed!", #PB_MessageRequester_Ok)
    EndIf
Else
    MessageRequester("Error...", "InitSprite() Failed!", #PB_MessageRequester_Ok)
EndIf
End

;===========================================================================
; BINARY INCLUDES
;===========================================================================

MainImage:
IncludeBinary "front.bmp"

FlakeImage:
IncludeBinary "flake3D.bmp"

Song:
IncludeBinary "build\12days.mid"
SongEnd:
Both snippets work fine with 10,000 snow flakes :) but i think it crashes above this, if i remember rightly :)

--Kale

New to PureBasic and falling in Love! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi Kale!
Can you add a link to download the included stuff (like gfx and midi i.e.) or just release the full package at resource site...

Btw as i can see in your source, you add a fixed var inside your "initFlakes()" to move the snowflakes... please note, possible it runs in correct speed on yours pc... but on faster PCs its mutchhhhhh to fast!!!!! Just use +DeltaTime for same speed moving on all PCs (FPS independent) or as some people on this forum prefer however, to set FrameRate...
;-)


PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

i've sorted the above code :) and i tried to upload to the resource site but the upload page seems to not work at the mo' so i've included a link here :)

http://www.garyw.uklinux.net/PB/Xmas%20Card.zip

--Kale

New to PureBasic and falling in Love! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hey Kale!
Links works fine! Many thanks for the really cool xmas card! Really nice work! :)

PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Tomio.

Hello Kale,

only a hint on your xmas card code:

> flakes(x)\xStep = 0.02 + Random(1)/10

..will give = 0.02 always, because:

Random(1) ;integer
Random(1)/10 ;integer

Greetings../tomio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Tomio.

Hello Kale,

sorry, I was wrong with "Random(1)/10 == 0".

It is my kind of thinking in C.

And a test I made, apparently produced several Random(1) = 0 at the beginning.

Greetings../tomio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

:)

--Kale

New to PureBasic and falling in Love! :)
Post Reply