Mirage Source
http://web.miragesource.net/forums/

[Feature] Dynamic sprite sizes!
http://web.miragesource.net/forums/viewtopic.php?f=183&t=5047
Page 1 of 63

Author:  Robin [ Sun Feb 01, 2009 8:53 pm ]
Post subject:  [Feature] Dynamic sprite sizes!

Dynamic sprite sizes! Now 100% free!

Basically, this system is a more generic BltPlayer and DrawPlayerName subroutine fix, which calculates the size of the sprite by the width and height of the image file which is loaded. This will only work with a sprite system that houses each sprite in a seperate file, but in the default Mirage layout.

It's a very easy fix, and I'm really not sure how anyone could have trouble adding it.

General Change
Replace BltSprite with:

Code:
Private Sub BltSprite(ByVal Sprite As Long, ByVal x As Long, ByVal y As Long, rec As DxVBLib.RECT)
    If Sprite < 1 Or Sprite > NumSprites Then Exit Sub
   
    Call DD_BltFast(x, y, DDS_Sprite(Sprite).Surface, rec, DDBLTFAST_WAIT Or DDBLTFAST_SRCCOLORKEY)
End Sub


Players
BltPlayer Subroutine:

Find;
Code:
Dim Sprite As Long


and replace with;
Code:
Dim Sprite As Long, spriteleft As Long


Find;
Code:
.Top = 0


and replace that entire block with;
Code:
    'Pre-load graphic to get the width and height used in calculation
    Call DD_ReadySurface("sprites\" & Sprite, DDS_Sprite(Sprite))
   
    Select Case GetPlayerDir(Index)
        Case DIR_UP
            spriteleft = DIR_UP
        Case DIR_RIGHT
            spriteleft = DIR_RIGHT
        Case DIR_DOWN
            spriteleft = DIR_DOWN
        Case DIR_LEFT
            spriteleft = DIR_LEFT
    End Select
   
    With rec
        .Top = 0
        .Bottom = DDS_Sprite(Sprite).SurfDescription.lHeight
        .Left = (spriteleft * 3 + Anim) * (DDS_Sprite(Sprite).SurfDescription.lWidth / 12)
        .Right = .Left + (DDS_Sprite(Sprite).SurfDescription.lWidth / 12)
    End With


Find;
Code:
X = GetPlayerX(Index) * PIC_X + Player(Index).XOffset


and replace that entire block with;
Code:
    ' Calculate the X
    X = GetPlayerX(Index) * PIC_X + Player(Index).XOffset - ((DDS_Sprite(Sprite).SurfDescription.lWidth / 12 - 32) / 2)
    ' Is the player's height more than 32..?
    If (DDS_Sprite(Sprite).SurfDescription.lHeight) > 32 Then
        ' Create a 32 pixel offset for larger sprites
        Y = GetPlayerY(Index) * PIC_Y + Player(Index).YOffset - ((DDS_Sprite(Sprite).SurfDescription.lHeight) - 32)
    Else
        ' Proceed as normal
        Y = GetPlayerY(Index) * PIC_Y + Player(Index).YOffset
    End If


Find;
Code:
If Y < 0 Then


and replace that entire block with;
Code:
    ' Is player's Y less than 0..?
    If Y < 0 Then
        With rec
            .Top = .Top - Y
        End With
        Y = 0
    End If

    ' Is player's X less than 0..?
    If X < 0 Then
        With rec
            .Left = .Left + (X * -1)
        End With
        X = 0
    End If

    ' Is player's X more than max map values..?
    If X + (DDS_Sprite(Sprite).SurfDescription.lWidth / 12) > MAX_MAPX * 32 + 32 Then
        With rec
            .Right = .Right + (X - (MAX_MAPX * 32))
        End With
    End If


That finishes the BltPlayer stuff! Basically, the Spriteleft is just a system I made so the sprite layout doesn't have to be Up, Down, Left, Right. The X, Y and REC edits are just changing it so instead of assuming the sprite is 32x32, it reads the values from the sprite surface. The height is set to the height of the surface, and the width is set to the width of the surface divided by 12, which is the amount of different sprite frames are set in one surface. Very simple.

Now, we edit the name code.

DrawPlayerName Subroutine:

Find;
Code:
TextY = GetPlayerY(Index) * PIC_Y


and replace with;
Code:
TextY = GetPlayerY(Index) * PIC_Y + Player(Index).YOffset - (DDS_Sprite(GetPlayerSprite(Index)).SurfDescription.lHeight) + 16


Again, we're just using the height of the surface to calculate how high it should be, then removing 16 pixels, because we need to ;D

None Player Characters
BltNPC Subroutine:

Find;
Code:
Dim Sprite As Long


and replace with;
Code:
Dim Sprite As Long, spriteleft As Long


Find;
Code:
.Top = 0


and replace entire block with;
Code:
    'Pre-load graphic to get the width and height used in calculation
    Call DD_ReadySurface("sprites\" & Sprite, DDS_Sprite(Sprite))
   
    Select Case MapNpc(MapNpcNum).Dir
        Case DIR_UP
            spriteleft = DIR_UP
        Case DIR_RIGHT
            spriteleft = DIR_RIGHT
        Case DIR_DOWN
            spriteleft = DIR_DOWN
        Case DIR_LEFT
            spriteleft = DIR_LEFT
    End Select
   
    With rec
        .Top = 0
        .Bottom = DDS_Sprite(Sprite).SurfDescription.lHeight
        .Left = (spriteleft * 3 + Anim) * (DDS_Sprite(Sprite).SurfDescription.lWidth / 12)
        .Right = .Left + (DDS_Sprite(Sprite).SurfDescription.lWidth / 12)
    End With


Find;
Code:
With MapNpc(MapNpcNum)


and replace entire block with;
Code:
     With MapNpc(MapNpcNum)
        ' Calculate X
        x = .x * PIC_X + .XOffset - ((DDS_Sprite(Sprite).SurfDescription.lWidth / 12 - 32) / 2)
        ' Is sprite more than 32..?
        If ((DDS_Sprite(Sprite).SurfDescription.lHeight) - 32) > 0 Then
            ' Create a 32 pixel offset for larger sprites
            y = MapNpc(MapNpcNum).y * PIC_Y + MapNpc(MapNpcNum).YOffset - ((DDS_Sprite(Sprite).SurfDescription.lHeight) - 32)
        Else
            ' Proceed as normal
            y = MapNpc(MapNpcNum).y * PIC_Y + MapNpc(MapNpcNum).YOffset
        End If
    End With


Find;
Code:
If Y < 0 Then


and replace entire block with;
Code:
    ' Is player's Y less than 0..?
    If y < 0 Then
        With rec
            .Top = .Top - y
        End With
        y = 0
    End If

    ' Is player's X less than 0..?
    If x < 0 Then
        With rec
            .Left = .Left + (x * -1)
            '.Right = .Left + 48 - (x * -1)
        End With
        x = 0
    End If

    ' Is player's X more than max map values..?
    If x + (DDS_Sprite(Sprite).SurfDescription.lWidth / 12) > MAX_MAPX * 32 + 32 Then
        With rec
            .Right = .Right + (x - (MAX_MAPX * 32))
        End With
    End If


Same things happening as last time, just updated syntax for the MapNPC variables.

That's it! Enjoy your new dynamically driven system.

This feature only uses one subroutine for the rendering of the player, meaning you'll have clipping issues unless you use a Y-based sprite rendering system. You can find my quick tutorial on how to make this by following this link;
http://web.miragesource.com/forums/viewtopic.php?f=124&t=5048&start=0

Can use this tutorial freely in your game/engine, as long as you don't claim it as your own. That means you, Frozengod!

Author:  Robin [ Sun Feb 01, 2009 8:57 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Here it is in action!

Image

Image

Author:  Nean [ Sun Feb 01, 2009 9:30 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Awesome dude. About time someone posted a tut, besides me.

Author:  Robin [ Sun Feb 01, 2009 9:33 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Nean wrote:
Awesome dude. About time someone posted a tut, besides me.


I did tons of work a few years back. Most of the games around have had me do features on them, hence why everyone knows me so well xD I just stopped after a while. DFA motivated me to finish some of my older work though, so you might see some more tutorials being posted by me.

Author:  Nean [ Sun Feb 01, 2009 9:39 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

I'm glad. I can post tutorials on the simple stuff, but stuff like this, I probably wont be able to do for another year or more.

Author:  Robin [ Sun Feb 01, 2009 11:10 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Updated to cover NPCs.

Author:  Jacob [ Sun Feb 01, 2009 11:23 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Great job. It's good to see tutorials being written.

Author:  Robin [ Sun Feb 01, 2009 11:36 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Screenshot of a few different sprite sizes. I've got an 84x84 unicorn, there's my classic 48x64 RM2K sprites and a small 32x32 RMVX :3

Also shows the NPCs working.

Image

Author:  timster0 [ Mon Feb 02, 2009 3:13 am ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Very nice, this is good for dungeon bosses.

Author:  genusis [ Mon Feb 02, 2009 1:55 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

1problem when adding this though for players it works just fine but npcs there characters walk in reverse 0.0 in stead of the spirit there expose to use the rendering reverses there spirits. any idea?


NVM i fixed it heres the fix if it happens to you too.
NPC side fix ^^
Code:
With rec
        .Top = 0
        .Bottom = DDSD_Sprite(Sprite).lHeight
        .Left = (MapNpc(MapNpcNum).Dir * 3 + Anim) * (DDSD_Sprite(Sprite).lWidth / 12)
        .Right = .Left + (DDSD_Sprite(Sprite).lWidth / 12)
    End With


And remove the

Code:
Select Case GetPlayerDir(Index)
        Case DIR_UP
            spriteleft = 0
        Case DIR_RIGHT
            spriteleft = 3
        Case DIR_DOWN
            spriteleft = 1
        Case DIR_LEFT
            spriteleft = 2
    End Select


Its is not needed and you can remove the dim Spiritleft as long as well.^^.

Author:  Robin [ Mon Feb 02, 2009 2:09 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Oh yeah, it's this part:

Code:
    Case DIR_UP
            spriteleft = 0
        Case DIR_RIGHT
            spriteleft = 1
        Case DIR_DOWN
            spriteleft = 2
        Case DIR_LEFT
            spriteleft = 3


I set it up like that because my sprite sheet is set up in a different order.

Just change it to:

Code:
    Case DIR_UP
            spriteleft = 0
        Case DIR_RIGHT
            spriteleft = 3
        Case DIR_DOWN
            spriteleft = 1
        Case DIR_LEFT
            spriteleft = 2


Updated the tutorial with the fix. Sorry about that, just copied and pasted it without editing it for Ms4 xD

Author:  genusis [ Mon Feb 02, 2009 2:13 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

its ok haha well really you don't have to have the new case stuff ^^ you could remove it. like what i did in my other post i edited it ^^.

You did a good job robin^^. i still get over 200fps even with this added. i just optimize stuff more and more to improve speed so far i have my own roofing tiles and another animation and it goes beyond 200FPS Woot =]

Author:  Robin [ Mon Feb 02, 2009 2:29 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

genusis wrote:
its ok haha well really you don't have to have the new case stuff ^^ you could remove it. like what i did in my other post i edited it ^^.

You did a good job robin^^. i still get over 200fps even with this added. i just optimize stuff more and more to improve speed so far i have my own roofing tiles and another animation and it goes beyond 200FPS Woot =]


I could remove it, but there's really no point removing something that just adds extra functions to the game. Although, moving it to a constant would probably be more optimised.

Author:  genusis [ Mon Feb 02, 2009 2:32 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

in bltnpc and player for spells you can do this
Player
With rec
.Top = 0
.Bottom = DDSD_Spell(SpellNum).lHeight
.Left = Player(Index).SpellAnimations(i).FramePointer * (DDSD_Spell(SpellNum).lWidth / 12)
.Right = .Left + (DDSD_Spell(SpellNum).lWidth / 12)
End With

NPC
With rec
.Top = 0
.Bottom = DDSD_Spell(SpellNum).lHeight
.Left = MapNpc(MapNpcNum).SpellAnimations(i).FramePointer * (DDSD_Spell(SpellNum).lWidth / 12)
.Right = .Left + (DDSD_Spell(SpellNum).lWidth / 12)
End With

ya i removed the new cases you added and it seemed to speed the fps up a bit ^^.

Author:  GIAKEN [ Mon Feb 02, 2009 6:22 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

I had this done for Asphodel a while ago :(

Author:  Robin [ Mon Feb 02, 2009 6:25 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

GIAKEN wrote:
I had this done for Asphodel a while ago :(


I've had it for years. Used to sell it.

Author:  GIAKEN [ Mon Feb 02, 2009 6:27 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Oh...

Well when I release Asphodel people will be like "You got this from Robin lol!!!!!!!!!!!!"

Author:  Robin [ Mon Feb 02, 2009 6:34 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

GIAKEN wrote:
Oh...

Well when I release Asphodel people will be like "You got this from Robin lol!!!!!!!!!!!!"


Well of course. I'm much more popular than you.

Author:  GIAKEN [ Mon Feb 02, 2009 6:38 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

Well my way is different than yours so I'll be ok.

Author:  Matt [ Mon Feb 02, 2009 8:03 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

They won't know it's different.. Lol.

Author:  Robin [ Mon Feb 02, 2009 8:16 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

GIAKEN wrote:
Well my way is different than yours so I'll be ok.


How do you do it different?

The only way to do it is by calculating the size from the sprite images.

Author:  GIAKEN [ Mon Feb 02, 2009 9:55 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

I'll upload the source soon.

Author:  jsventor [ Mon Feb 02, 2009 11:01 pm ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

I added this and everything is working except for the walking animations, the frames dont change, and I have no Idea what it could be, I spent about 20 minutes looking >.<

Author:  james1992_2006 [ Tue Feb 03, 2009 12:44 am ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

eh had this working in 3.0.3 then my laptop battery died before i had saved it... o well guess i'll just readd in abit


anywayz thanks, nice tut

Author:  Matt [ Tue Feb 03, 2009 12:55 am ]
Post subject:  Re: [Feature] Dynamic sprite sizes!

You shouldn't use 303.

Page 1 of 63 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/