Thread: Drawing Scaled Sprites

Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1 Drawing Scaled Sprites 
    Rune Synergy


    Dane's Avatar
    Join Date
    Jan 2007
    Posts
    189
    Thanks given
    110
    Thanks received
    337
    Rep Power
    2323


    Code:
    	public static void drawSprite(Sprite s, int x, int y, int w, int h) {
    		if (w <= 1 || h <= 1) {
    			return;
    		}
    
    		targetOffset = x + (y * targetWidth);
    		targetStep = targetWidth - w;
    
    		// our texture coordinates as 24.8 fixed points
    		int u = 0, v = 0;
    		int uStep = (s.width << 8) / w;
    		int vStep = (s.height << 8) / h;
    
    		// clip the top
    		if (y < top) {
    			int cut = top - y;
    			h -= cut;
    			y = top;
    
    			v += vStep * cut;
    			targetOffset += cut * targetWidth;
    		}
    
    		// clip the bottom
    		if (y + h > bottom) {
    			h -= (y + h) - bottom;
    		}
    
    		// clip the left
    		if (x < left) {
    			int cut = left - x;
    			w -= cut;
    			x = left;
    
    			u += (cut << 8);
    			targetOffset += cut;
    			targetStep += cut;
    		}
    
    		// clip the right
    		if (x + w > right) {
    			int cut = (x + w) - right;
    			w -= cut;
    			targetStep += cut;
    		}
    
    		// if our image is too small then just don't draw it.
    		if (w <= 1 || h <= 1) {
    			return;
    		}
    
    		// local reference
    		int[] pixels = s.pixels;
    
    		// we'll be coming back to this.
    		int startU = u;
    
    		for (y = 0; y < h; y++) {
    
    			// this only needs to be calculated once per row
    			int vOffset = (v >> 8) * s.width;
    
    			// loop through row
    			for (x = 0; x < w; x++) {
    				// apply values
    				target[targetOffset++] = pixels[(u >> 8) + vOffset];
    
    				// step to the right
    				u += uStep;
    			}
    
    			// step down a row of pixels on the destination
    			targetOffset += Graphics2D.targetWidth - w;
    
    			// step down vertically
    			v += vStep;
    
    			// reset back to left
    			u = startU;
    		}
    	}
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Extreme Donator

    Jilic-Matt's Avatar
    Join Date
    May 2011
    Age
    29
    Posts
    1,612
    Thanks given
    799
    Thanks received
    494
    Rep Power
    540
    Cool dont know how useful it would be though, resizing kinda sacrifices the image this way.
    Reply With Quote  
     

  4. #3  
    Registered Member
    Stephen OG's Avatar
    Join Date
    Aug 2014
    Posts
    173
    Thanks given
    114
    Thanks received
    56
    Rep Power
    182
    Could be useful for login background on resizable clients
    Reply With Quote  
     

  5. #4  
    ಠ_ಠ

    Joshua's Avatar
    Join Date
    May 2010
    Posts
    1,903
    Thanks given
    397
    Thanks received
    708
    Rep Power
    803
    Quote Originally Posted by Jilic-Matt View Post
    Cool dont know how useful it would be though, resizing kinda sacrifices the image this way.
    Could maybe be ok for masks

    Quote Originally Posted by Dane View Post
    Well if you made your own NinePatch class, you could use this to stretch the middle pieces of the border.
    http://www.rune-server.org/runescape...ite-sizes.html
    Reply With Quote  
     

  6. #5  
    強い者は生き残る
    Ashpire's Avatar
    Join Date
    Mar 2012
    Age
    27
    Posts
    2,721
    Thanks given
    914
    Thanks received
    1,897
    Rep Power
    2231
    Damnn nice.
    Attached image
    Reply With Quote  
     

  7. #6  
    Registered Member
    Shoutbox's Avatar
    Join Date
    Sep 2013
    Posts
    1,748
    Thanks given
    580
    Thanks received
    363
    Rep Power
    834
    Could be useful Thanks man.
    S A D B O Y S

    私は自分自身を殺すために待つことができない
    Reply With Quote  
     

  8. #7  
    ಠ_ಠ

    Joshua's Avatar
    Join Date
    May 2010
    Posts
    1,903
    Thanks given
    397
    Thanks received
    708
    Rep Power
    803
    Quote Originally Posted by Dane View Post
    I don't see the point in advertising your own work in my thread though just because I mentioned something relating to it. If you at least used this method, then that would be a reasonable explanation.
    I was not trying to advertise my work, I was only sharing info on the topic you mentioned. If someone wished to write a NinePatch class they could reference it.
    We both resized an image represented in a single dimension array of pixels, so they're not that different.
    Reply With Quote  
     

  9. Thankful user:


  10. #8  
    Extreme Donator

    Jilic-Matt's Avatar
    Join Date
    May 2011
    Age
    29
    Posts
    1,612
    Thanks given
    799
    Thanks received
    494
    Rep Power
    540
    Found a use for it cheers, (scaling item sprites)
    Reply With Quote  
     

  11. #9  
    Registered Member Jumper's Avatar
    Join Date
    Aug 2013
    Posts
    721
    Thanks given
    19
    Thanks received
    103
    Rep Power
    63
    you could use this to stretch the xp gained bar i suppose
    other than that, can't think of any practicle uses
    Reply With Quote  
     

  12. #10  
    Donator


    Join Date
    Sep 2007
    Age
    27
    Posts
    2,426
    Thanks given
    125
    Thanks received
    505
    Rep Power
    386
    Quote Originally Posted by Dane View Post
    How about drawing particles and scaling them depending on their distance from the camera?
    That's not really a valid use for this. For particles you'd have to project their 3D location anyway, and RS texture drawing methods already support reduced sizes (via X/Y axis gradients)
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Drawing Wrong Sprites?
    By exploiting in forum Help
    Replies: 1
    Last Post: 01-25-2012, 11:27 AM
  2. Drawing a sprite [REP + THANKS]
    By TheRedArmy in forum Help
    Replies: 3
    Last Post: 12-17-2011, 11:47 PM
  3. [GALKONS] Drawing a sprite over another?
    By Alexander in forum Help
    Replies: 3
    Last Post: 11-28-2010, 08:24 PM
  4. Drawing a sprite with specific dimensions
    By Eternal Darknes in forum Help
    Replies: 4
    Last Post: 06-06-2009, 04:07 PM
  5. Replies: 10
    Last Post: 05-30-2009, 06:57 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •