I am making a multiplayer racing game for a school project, that draws a 4 player splitscreen on the Camera Actors Class image. The world size is set by the world class and everything works fine with world sizes smaller than 1200x800px. If i want to use a bigger world though it starts running really slow.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import org.jbox2d.common.*;
import java.awt.image.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
public class Camera extends Actor
{
//Verzögerung mit der Kamera Auto folgt: je kleiner, desto größer die Verzögerung
private float lag = 0.07f;
private Vec2[] camPos;
private Vec2[] carPos;
private Vec2[] carPosRaw;
private float[] carRot;
private MyWorld world;
private boolean setup = false;
private GreenfootImage track = new GreenfootImage("images/Download.png");
private GreenfootImage carImg = new GreenfootImage("images/car02.png");
private BufferedImage srcAwt;
private Rectangle rect;
private GreenfootImage gCrop;
private BufferedImage bCrop;
private int width = world.width;
private int height = world.height;
public Camera()
{
gCrop = new GreenfootImage(width, height);
setImage(gCrop);
camPos = new Vec2[4];
carPos = new Vec2[4];
carPosRaw = new Vec2[4];
carRot = new float[4];
for(int i = 0; i<4; i++)
{
camPos[i] = new Vec2(0,0);
carPos[i] = new Vec2(0,0);
carPosRaw[i] = new Vec2(0,0);
carRot[i] = 0;
}
}
public void setup()
{
world = (MyWorld)getWorld();
}
public void act()
{
if(!setup)
{
setup();
setup = true;
}
}
public void setCarTransform(Vec2 pos, float rot, int n)
{
carPos[n] = pos;
carRot[n] = rot;
}
public void followLocation(int n)
{ Vec2 offset = new Vec2(0,0);
if(setup)
{
camPos[n].x += (-carPos[n].x - camPos[n].x) * lag;
camPos[n].y += (-carPos[n].y - camPos[n].y) * lag;
carPos[n].x += camPos[n].x;
carPos[n].y += camPos[n].y;
splitScreen();
}
carPosRaw[n] = new Vec2(carPos[n].x-camPos[n].x, carPos[n].y-camPos[n].y);
}
public void splitScreen()
{
splitCar(track, 0,0);
splitCar(track, 1,1);
splitCar(track, 2,2);
splitCar(track, 3,3);
Vec2 offsetCam = new Vec2(0,0);
// i: Screen, u: Auto
for(int i= 0; i<4; i++)
{
switch(i)
{
case 0:
offsetCam = new Vec2(width/4, height/4);
break;
case 1:
offsetCam = new Vec2(3*width/4, height/4);
break;
case 2:
offsetCam = new Vec2(width/4, 3*height/4);
break;
case 3:
offsetCam = new Vec2(3*width/4, 3*height/4);
break;
}
for(int u = 0; u<4; u++)
{
splitCopy(carImg, i, new Vec2(carPosRaw[u].x+camPos[i].x+offsetCam.x,carPosRaw[u].y+camPos[i].y+offsetCam.y), carRot[u]);
}
}
}
public void splitCopy(GreenfootImage src, int n, Vec2 pos, float rot)
{
Vec2 posInCam = new Vec2(pos.x-38.5f, pos.y-18f);
float x = posInCam.x;
float y = posInCam.y;
splitscreen(src,n,x,y,-rot);
}
public void splitCar(GreenfootImage src, int n, int s)
{
float x = camPos[s].x;
float y = camPos[s].y;
AffineTransform at = new AffineTransform();
switch(n)
{
case 0:
at.translate(x,y);
break;
case 1:
x += width/2;
break;
case 2:
y+= height/2;
break;
case 3:
x+= width/2;
y+= height/4;
break;
}
splitscreen(src, n, x,y, 0);
}
public void splitscreen(GreenfootImage src, int n, float x, float y, float rot)
{
srcAwt = src.getAwtImage();
Graphics2D g2d = gCrop.getAwtImage().createGraphics();
switch(n)
{
case 0:
rect = new Rectangle(0,0,width/2,height/2);
break;
case 1:
rect = new Rectangle(width/2,0,width,height/2);
break;
case 2:
rect = new Rectangle(0,height/2,width/2,height);
break;
case 3:
rect = new Rectangle(width/2,height/2,width,height);
break;
}
g2d.clip(rect);
AffineTransform tx = AffineTransform.getRotateInstance(rot, x+srcAwt.getWidth()/2, y+srcAwt.getHeight()/2);
tx.translate(x,y);
// g2d.drawImage(srcAwt,x, y,null);
g2d.drawImage(srcAwt,tx,null);
g2d.setClip(null);
g2d.dispose();
}
}
