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:
