Slick Forums

Discuss the Slick 2D Library
It is currently Sun Oct 20, 2019 10:33 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Jan 22, 2012 8:39 am 
Offline
User avatar

Joined: Tue May 03, 2011 7:45 pm
Posts: 13
Location: Poland, Cracow
We have setCenterOfRotation() in Image right now, but it only sets the rotation point. But most of the time it would be nice for that point to be an offset for image drawing. For example, I could center an image (set center of rotation to width/2,height/2) and then draw it easily on screenW/2,screenH/2. And now I can rotate it without problems, too.

I've coded it for myself:
Code:
package pl.shockah.easyslick;

import org.newdawn.slick.Color;
import org.newdawn.slick.SlickException;

public class Image extends org.newdawn.slick.Image {
   protected float cX, cY;
   
   public Image(String path) throws SlickException {
      super(path);
   }
   public Image(int width, int height) throws SlickException {
      super(width,height);
   }
   
   public void draw(float x, float y, float x2, float y2, float srcx, float srcy, float srcx2, float srcy2, Color filter) {
      super.draw(x-cX,y-cY,x2-cX,y2-cY,srcx,srcy,srcx2,srcy2,filter);
   }
   public void draw(float x, float y, float width, float height, Color col) {
      super.draw(x-cX,y-cY,width,height,col);
   }
   public void drawFlash(float x, float y, float width, float height, Color col) {
      super.drawFlash(x-cX,y-cY,width,height,col);
   }
   public void drawSheared(float x, float y, float hshear, float vshear) {
      super.drawSheared(x-cX,y-cY,hshear,vshear);
   }
   
   public void setRotation(float angle) {
      super.setRotation(-angle);
   }
   
   public void setCenterOfRotation(float x, float y) {
      super.setCenterOfRotation(x,y);
      cX = x; cY = y;
   }
   public void center() {
      setCenterOfRotation(getWidth()/2f,getHeight()/2f);
   }
}
(as you probably already know, setCenterOfRotation is actually broken, so I'm using my own cX and cY)

Test code:
Code:
package pl.shockah.easyslick.tests;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Circle;
import pl.shockah.easyslick.EHandler;
import pl.shockah.easyslick.Image;
import pl.shockah.easyslick.MainGame;
import pl.shockah.easyslick.Room;

public class TestCenterOfRotation {
   public static void main(String[] args) {
      MainGame.start(null,new RoomTest(),"Test - centerOfRotation");
   }
   
   public static class RoomTest extends Room {
      private static Image img = null;
      private float angle = 0;
      
      protected void onCreate() {
         img = EHandler.newImage("pngFile2.png");
         img.center();
      }
      
      protected void onTick() {
         angle += 2;
      }
      protected void onRender(Graphics g) {
         img.setRotation(angle);
         img.draw(width/2,height/2);
         
         g.setColor(Color.red);
         g.draw(new Circle(width/2,height/2,3,8));
      }
   }
}
(heheh, using my EasySlick as base)

Result:
Image


Top
 Profile  
 
PostPosted: Fri Feb 03, 2012 5:48 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1482
I don't think this should be added to Slick.

  • This adds more clutter; something which devs are trying to reduce at the moment
  • Everything in Slick is "rendered from the top left corner" for consistency -- if you want to draw rectangles or images at their center, it is not difficult to offset it yourself (or write your own helper classes like you did).
  • It's poor design and it breaks things: setCenterOfRotation, based on its name and purpose, should not affect the x/y position of the image.

    For example, say we want to render a background image that takes up the entire screen. We render it at (0, 0) and stretch the size to (screenWidth, screenHeight). Then say we want to rotate it around its top-middle point (screenWidth/2f, 0).
    Code:
            img.setCenterOfRotation(container.getWidth()/2f, 0);
            img.setRotation(angle);
            img.draw(0, 0, container.getWidth(), container.getHeight());

    Whereas the above code is broken using your Image class.


The nice thing about Slick is that it provides a solid base for you to build your work on, but it doesn't try to do everything for you. If you need this feature, you can add your own helper methods for images like so:

Code:
    public static void drawCentered(Image img, GameContainer screen) {
        drawCentered(img, screen, 0, 0);
    }
   
    public static void drawCentered(Image img, GameContainer screen, float x, float y) {
        drawCentered(img, screen.getWidth(), screen.getHeight(), x, y);
    }
   
    public static void drawCentered(Image img, float parentWidth, float parentHeight, float x, float y) {
        img.draw(x + parentWidth/2f-img.getWidth()/2f, y + parentHeight/2f-img.getHeight()/2f);
    }


Top
 Profile  
 
PostPosted: Thu Feb 16, 2012 4:59 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
It's a good idea, but I'm with davedes. This particular feature is specialized enough that it should probably be left up to the developer to implement (like you did).

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group