Some advice would be appreciated.
What I’m trying to do is draw to the screen based on a calculated/stored imagemap in an array. I can do it just fine, but it’s painfully slow: 4FPS on this machine. I’d pre-create my images, but the memory required would be too large for ny tastes.
Maybe I’m just doing something wrong in my code, but it might be that the whole thing might have to be re-thought.
Here’s my test code I whipped up, just in case it is me messing up.
import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;
class map
{
public int[] pixels;
public int width, height;
public map()
{
width=height=100;
pixels=new int[width*height];
for(int i=0;i<width*height;i++)
pixels[i]=(int)(Math.random()*1073741823);
}
public void draw(BufferStrategy bs, int x, int y)
{
Graphics g=bs.getDrawGraphics();
//draw
BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
WritableRaster wr=bi.getRaster();
for(int p=0;p<200;p++)
{
for(int row=0;row<height;row++)
{
for(int col=0;col<width;col++)
{
wr.setSample(row,col,0,pixels[(row*width)+col]);
}
}
g.drawImage(bi,x,y,null);
}
bs.show();
}
}
class drawtest extends JFrame
{
private static long ctm, cnt;
public drawtest()
{
super("DrawTest");
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
drawtest frame = new drawtest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250,250);
frame.setVisible(true);
frame.createBufferStrategy(2);
BufferStrategy bs=frame.getBufferStrategy();
ctm=System.currentTimeMillis();
cnt=0;
map bm=new map();
while(true)
{
bm.draw(bs, 100,100);
cnt++;
if((cnt%100==0) && ((System.currentTimeMillis()-ctm)>2000))
System.out.print("Framerate: " + (cnt/((System.currentTimeMillis()-ctm)/1000)) + " Time: " + ((System.currentTimeMillis()-ctm)/1000) + "\n");
}
}
}
Thanks in advance.