Results 1 to 4 of 4

Thread: [RESOLVED] Cairo Skewing Matrix, How Many Can We Have?

  1. #1

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Resolved [RESOLVED] Cairo Skewing Matrix, How Many Can We Have?

    Hi All,

    Run into a bit of an issue. I set up a Cario Skewing Matrix to Pre-Determine shadows of everything. Works well as the day treks along, the shadows follow the sun. Called {Skewing_Identity_Matrix_1}, to which I dump a surface to and presto, instant shadow.

    But, due to the smaller 'Items' icons (bottles, bananas, oranges), they exist on much smaller PNG surfaces, and have a central-bottom located positioning. What that means is, shadows deform incorrectly for these little items.
    To counter this, I set up a New Skewing Matrix under a New Identity for these small surface Item Icons. {Skewing_Identity_Matrix_2}

    BUT, Cairo now tunes every single object and item to the new Skewing Matrix {Skewing_Identity_Matrix_2}. Does that mean I can only have 1 ACTIVE skewing matrix in the game. (WUT?!)

    Here you can see the same default skewing matrix for everything, at different times of the day. The bottles, oranges, bananas are all 'items'. And have a different surface size aspect than any other thing.
    Name:  Cario Skewing Matrix Issue.jpg
Views: 185
Size:  211.9 KB

    Are we really only allowed to have 1 identity matrix per program?

  2. #2

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Re: Cairo Skewing Matrix, How Many Can We Have?

    I decided to retry this again. Re-Copied the work I did before. Magically it worked. Not sure why.

    It Does mean you can have multiple Identity Matrices though.

    And now you can watch the sunset and eat your road banana in peace, as the shadows climb ever so higher, before the wolves start howling...

    Name:  Shadow Fixed.jpg
Views: 190
Size:  310.1 KB

  3. #3
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: [RESOLVED] Cairo Skewing Matrix, How Many Can We Have?

    The issue you've encountered probably came into play,
    due to "bottom-offsets" of the Sprite-Resources...

    Meaning, when the Sprite-ImageContent does not sit exactly at the Bottom of the "surrounding PNG-area",
    it becomes difficult to set the skew-values for the shadow.

    As for Matrices - these are (relative) lightweight Objects and could be held in an array
    (or a collection) for each Sprite ... creatable via Cairo.CreateMatrix or Cairo.CreateIdentityMatrix).

    Well - and instead of two separate collections (one for Sprite-Surfaces and one for Sprite-Matrices) -
    you could also store all Sprite-Surfaces as "Sprite-Patterns" instead.

    One can derive a SurfacePattern (as a lightweight "Hull-Object" around a Surface) via:
    Set MySpritePattern = MySpriteSrf.CreateSurfacePattern

    The advantage of that is, that the Surface is still available (via Property) behind such a Pattern-Object -
    but these cCairoPatterns have a separate "Pattern-Matrix-Object" already attached as well.

    FWIW, here's an example, which draws a shadow on such an already mentioned "Sprite with Bottom-Offset":

    Code:
    Option Explicit
     
    Private Sub Form_Load()
        Dim Sprite As cCairoSurface
        Set Sprite = Cairo.ImageList.AddImage("", App.Path & "\cactus.png", 128, 128)
        
        Dim CC As cCairoContext
        Set CC = Cairo.CreateSurface(800, 600).CreateContext
            CC.Paint 1, Cairo.CreateCheckerPattern
     
            RenderSpriteWithShadow CC, Sprite, 120, 80, _
                                               68, Sprite.Height - 16, _
                                               50, 20
     
        Set Picture = CC.Surface.Picture
    End Sub
    
    Sub RenderSpriteWithShadow(CC As cCairoContext, Srf As cCairoSurface, ByVal x#, ByVal y#, _
                               Optional ByVal OffsX#, Optional ByVal OffsY#, _
                               Optional ByVal SkewX#, Optional ByVal SkewY#)
      CC.Save
        CC.TranslateDrawings x, y 'ensure the Render-Offset for the Sprite itself (on the CC)
        
        'draw a border in the size of the sprite (to make the Offsets more obvious)
        CC.Rectangle 0, 0, Srf.Width, Srf.Height
        CC.Stroke , Cairo.CreateSolidPatternLng(vbMagenta)
        
        With Srf.CreateSurfacePattern 'derives a (lightweight) Pattern-Object from the Sprite-Surface
          Dim PM As cCairoMatrix
          Set PM = .Matrix
              PM.TranslateCoords OffsX, OffsY
                  PM.SkewXDeg SkewX
                  PM.SkewYDeg SkewY
              PM.TranslateCoords -OffsX, -OffsY
          Set .Matrix = PM 'apply the changed matrix to the Pattern-instance
          
          CC.SetSourceColor vbBlack, 0.5
          CC.Mask .This 'render the shadow from the now transformed Surface-Pattern
        End With
     
        CC.RenderSurfaceContent Srf, 0, 0, , , , 0.5 '<- remove the transparency-arg for production-mode
      CC.Restore
    End Sub
    The above prodces this output (from a little cactus-png I've downloaded beforehand and placed in my App.Path):
    Name:  SpriteShadow.png
Views: 212
Size:  34.2 KB

    HTH

    Olaf

  4. #4

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Re: [RESOLVED] Cairo Skewing Matrix, How Many Can We Have?

    Oh, Olaf, the shadows change per every 4 seconds (one turn), so it would need to update all the things on a refresh.
    And yes, I found the 'middle' origin thing was the main problem. So that's why I set up a new transformation surface to handle the small PNG's. I must have coded something wrong during the late night, though I went over it with a fine tooth comb. Deleted it. Came back in the morning, typed it all out again and it worked. By having the Shadow_Surface(with Matrix) and Item_Shadow_Surface(with other Matrix), I just dump icons to those surface and then paste that result surface onto the viewport(main screen). Those Matrices both update per sun motion. So, it's hassle free and fast. It's triggered on screen change and from the 'stagnant timer', to which, the User won't notice it, as shadows move very slowly.

    Name:  Beach Sunset.jpg
Views: 153
Size:  329.7 KB
    Beach Sunset

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width