|
|
|
@ -1,24 +1,35 @@ |
|
|
|
|
package sig.engine; |
|
|
|
|
import java.awt.Graphics; |
|
|
|
|
import java.awt.GraphicsConfiguration; |
|
|
|
|
import java.awt.GraphicsEnvironment; |
|
|
|
|
import java.awt.Image; |
|
|
|
|
import java.awt.Toolkit; |
|
|
|
|
import java.awt.image.ColorModel; |
|
|
|
|
import java.awt.image.MemoryImageSource; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
|
import java.awt.event.MouseMotionListener; |
|
|
|
|
import java.awt.event.MouseWheelEvent; |
|
|
|
|
import java.awt.event.MouseWheelListener; |
|
|
|
|
|
|
|
|
|
import java.awt.GraphicsEnvironment; |
|
|
|
|
import java.awt.GraphicsConfiguration; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.JFrame; |
|
|
|
|
import javax.swing.JPanel; |
|
|
|
|
import javax.swing.event.MouseInputListener; |
|
|
|
|
|
|
|
|
|
import java.awt.event.KeyListener; |
|
|
|
|
import java.awt.Graphics2D; |
|
|
|
|
import java.awt.RenderingHints; |
|
|
|
|
|
|
|
|
|
import sig.JavaProjectTemplate; |
|
|
|
|
|
|
|
|
|
public class Panel extends JPanel implements Runnable { |
|
|
|
|
public class Panel extends JPanel implements Runnable,KeyListener { |
|
|
|
|
JFrame window; |
|
|
|
|
public int pixel[]; |
|
|
|
|
public int width=1280; |
|
|
|
|
public int height=720; |
|
|
|
|
final int CIRCLE_PRECISION=32; |
|
|
|
|
final int OUTLINE_COL=Color.BRIGHT_WHITE.getColor(); |
|
|
|
|
private Thread thread; |
|
|
|
@ -31,15 +42,111 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
double y_offset=0; |
|
|
|
|
int frameCount=0; |
|
|
|
|
long lastSecond=0; |
|
|
|
|
int lastFrameCount=0; |
|
|
|
|
boolean resizing=false; |
|
|
|
|
long lastUpdate=System.nanoTime(); |
|
|
|
|
final long TARGET_FRAMETIME = 8333333l; |
|
|
|
|
public double nanaX = 0; |
|
|
|
|
public double nanaY = 0; |
|
|
|
|
public int button = 0; |
|
|
|
|
public HashMap<Integer,Boolean> MOUSE = new HashMap<>(); |
|
|
|
|
private Point mousePosition = new Point(0,0); |
|
|
|
|
private MouseScrollValue scrollWheel=null; |
|
|
|
|
|
|
|
|
|
public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); |
|
|
|
|
|
|
|
|
|
static long lastReportedTime = System.currentTimeMillis(); |
|
|
|
|
public static long TIME = 0; |
|
|
|
|
public static long scaleTime; |
|
|
|
|
public static Panel p; |
|
|
|
|
public static JFrame f; |
|
|
|
|
|
|
|
|
|
public static void InitializeEngine(){ |
|
|
|
|
System.setProperty("sun.java2d.transaccel", "True"); |
|
|
|
|
System.setProperty("sun.java2d.d3d", "True"); |
|
|
|
|
System.setProperty("sun.java2d.ddforcevram", "True"); |
|
|
|
|
System.setProperty("sun.java2d.xrender", "True"); |
|
|
|
|
|
|
|
|
|
RENDERHINTS.put(RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_COLOR_RENDER_SPEED); |
|
|
|
|
RENDERHINTS.put(RenderingHints.KEY_DITHERING,RenderingHints.VALUE_DITHER_DISABLE); |
|
|
|
|
RENDERHINTS.put(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_OFF); |
|
|
|
|
RENDERHINTS.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_SPEED); |
|
|
|
|
|
|
|
|
|
f = new JFrame(JavaProjectTemplate.PROGRAM_NAME); |
|
|
|
|
f.setResizable(false); |
|
|
|
|
f.setSize(JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT); |
|
|
|
|
p = new Panel(f); |
|
|
|
|
JavaProjectTemplate.game=p; |
|
|
|
|
|
|
|
|
|
p.init(); |
|
|
|
|
|
|
|
|
|
f.add(p); |
|
|
|
|
f.addKeyListener(p); |
|
|
|
|
f.setLocation((int) ((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - f.getWidth()) / 2), |
|
|
|
|
(int) ((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - f.getHeight()) / 2)); |
|
|
|
|
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
|
|
|
f.setVisible(true); |
|
|
|
|
f.createBufferStrategy(2); |
|
|
|
|
|
|
|
|
|
p.render(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Panel(JFrame f) { |
|
|
|
|
super(true); |
|
|
|
|
this.window=f; |
|
|
|
|
thread = new Thread(this, "MyPanel Thread"); |
|
|
|
|
|
|
|
|
|
this.addMouseListener(new MouseInputListener(){ |
|
|
|
|
@Override |
|
|
|
|
public void mouseClicked(MouseEvent e) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mousePressed(MouseEvent e) { |
|
|
|
|
MOUSE.put(e.getButton(),true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseReleased(MouseEvent e) { |
|
|
|
|
MOUSE.put(e.getButton(),false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseEntered(MouseEvent e) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseExited(MouseEvent e) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseDragged(MouseEvent e) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseMoved(MouseEvent e) { |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
this.addMouseMotionListener(new MouseMotionListener(){ |
|
|
|
|
@Override |
|
|
|
|
public void mouseDragged(MouseEvent e) { |
|
|
|
|
mousePosition.set(e.getX(),e.getY()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void mouseMoved(MouseEvent e) { |
|
|
|
|
mousePosition.set(e.getX(),e.getY()); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
this.addMouseWheelListener(new MouseWheelListener(){ |
|
|
|
|
//-1 is UP, 1 is DOWN
|
|
|
|
|
@Override |
|
|
|
|
public void mouseWheelMoved(MouseWheelEvent e) { |
|
|
|
|
scrollWheel=MouseScrollValue.getValue(e.getWheelRotation()); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
/** |
|
|
|
|
* Get Best Color model available for current screen. |
|
|
|
|
* @return color model |
|
|
|
|
*/ |
|
|
|
@ -50,41 +157,32 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
return gfx_config.getColorModel(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Call it after been visible and after resizes. |
|
|
|
|
*/ |
|
|
|
|
public void init(){ |
|
|
|
|
cm = getCompatibleColorModel(); |
|
|
|
|
int screenSize = width * height; |
|
|
|
|
int screenSize = JavaProjectTemplate.WINDOW_WIDTH * JavaProjectTemplate.WINDOW_HEIGHT; |
|
|
|
|
if(pixel == null || pixel.length < screenSize){ |
|
|
|
|
pixel = new int[screenSize]; |
|
|
|
|
} |
|
|
|
|
if(thread.isInterrupted() || !thread.isAlive()){ |
|
|
|
|
thread.start(); |
|
|
|
|
} |
|
|
|
|
mImageProducer = new MemoryImageSource(width, height, cm, pixel,0, width); |
|
|
|
|
} |
|
|
|
|
mImageProducer = new MemoryImageSource(JavaProjectTemplate.WINDOW_WIDTH, JavaProjectTemplate.WINDOW_HEIGHT, cm, pixel,0, JavaProjectTemplate.WINDOW_WIDTH); |
|
|
|
|
mImageProducer.setAnimated(true); |
|
|
|
|
mImageProducer.setFullBufferUpdates(true); |
|
|
|
|
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer); |
|
|
|
|
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer); |
|
|
|
|
if(thread.isInterrupted() || !thread.isAlive()){ |
|
|
|
|
thread.start(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void paintComponent(Graphics g) { |
|
|
|
|
super.paintComponent(g); |
|
|
|
|
// perform draws on pixels
|
|
|
|
|
render(); |
|
|
|
|
// ask ImageProducer to update image
|
|
|
|
|
mImageProducer.newPixels(); |
|
|
|
|
// draw it on panel
|
|
|
|
|
g.drawImage(this.imageBuffer, 0, 0, this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (window!=null&&System.currentTimeMillis()-lastSecond>=1000) { |
|
|
|
|
window.setTitle(JavaProjectTemplate.PROGRAM_NAME+" - FPS: "+(frameCount-lastFrameCount)); |
|
|
|
|
lastFrameCount=frameCount; |
|
|
|
|
lastSecond=System.currentTimeMillis(); |
|
|
|
|
} |
|
|
|
|
frameCount++; |
|
|
|
|
//super.paintComponent(g);
|
|
|
|
|
// perform draws on pixels
|
|
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
|
g.drawImage(this.imageBuffer,0,0,JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT,0,0,JavaProjectTemplate.WINDOW_WIDTH,JavaProjectTemplate.WINDOW_HEIGHT,this); |
|
|
|
|
scaleTime=System.currentTimeMillis()-startTime; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -99,54 +197,14 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
* Do your draws in here !! |
|
|
|
|
* pixel is your canvas! |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public /* abstract */ void render(){ |
|
|
|
|
int[] p = pixel; // this avoid crash when resizing
|
|
|
|
|
//a=h/w
|
|
|
|
|
|
|
|
|
|
for (int x=0;x<width;x++) { |
|
|
|
|
for (int y=0;y<height;y++) { |
|
|
|
|
p[y*width+x]=(0<<16)+(0<<8)+0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
x_offset+=1; |
|
|
|
|
y_offset=50; |
|
|
|
|
|
|
|
|
|
FillPolygon(p,Color.WHITE,50,50,new Point[] { |
|
|
|
|
new Point(135,2), |
|
|
|
|
new Point(166,96), |
|
|
|
|
new Point(265,97), |
|
|
|
|
new Point(185,156), |
|
|
|
|
new Point(215,251), |
|
|
|
|
new Point(134,192), |
|
|
|
|
new Point(54,251), |
|
|
|
|
new Point(84,156), |
|
|
|
|
new Point(4,97), |
|
|
|
|
new Point(103,96), |
|
|
|
|
}); |
|
|
|
|
FillPolygon(p,Color.BRIGHT_CYAN,x_offset,y_offset,new Point[] { |
|
|
|
|
new Point(28,29), |
|
|
|
|
new Point(78,103), |
|
|
|
|
new Point(120,31), |
|
|
|
|
new Point(123,221), |
|
|
|
|
new Point(30,218), |
|
|
|
|
}); |
|
|
|
|
//FillRect(p,Color.BRIGHT_RED,200,200,600,64);
|
|
|
|
|
final Color testAlpha = new Color(150,0,0,128); |
|
|
|
|
FillCircle(p,testAlpha,150,150,100); |
|
|
|
|
FillOval(p,Color.BRIGHT_GREEN,300,150,100,50); |
|
|
|
|
JavaProjectTemplate.drawGame(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void FillRect(int[] p,Color col,double x,double y,double w,double h) { |
|
|
|
|
for (int xx=0;xx<w;xx++) { |
|
|
|
|
for (int yy=0;yy<h;yy++) { |
|
|
|
|
int index = ((int)y+yy)*width+(int)x+xx; |
|
|
|
|
p[index]=col.getColor(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void FillCircle(int[] p,Color col,double center_x,double center_y,double r) { |
|
|
|
|
public void FillCircle(byte[] p,byte col,double center_x,double center_y,double r) { |
|
|
|
|
int counter=0; |
|
|
|
|
Point[] points = new Point[CIRCLE_PRECISION]; |
|
|
|
|
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) { |
|
|
|
@ -158,7 +216,7 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void FillOval(int[] p,Color col,double center_x,double center_y,double w,double h) { |
|
|
|
|
public void FillOval(byte[] p,byte col,double center_x,double center_y,double w,double h) { |
|
|
|
|
int counter=0; |
|
|
|
|
Point[] points = new Point[CIRCLE_PRECISION]; |
|
|
|
|
double r = Math.max(w,h); |
|
|
|
@ -179,7 +237,7 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
FillPolygon(p,col,0,0,points); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void FillPolygon(int[] p,Color col,double x_offset,double y_offset,Point...points) { |
|
|
|
|
public void FillPolygon(byte[] p,byte col,double x_offset,double y_offset,Point...points) { |
|
|
|
|
Edge[] edges = new Edge[points.length]; |
|
|
|
|
List<Edge> edges_sorted = new ArrayList<Edge>(); |
|
|
|
|
for (int i=0;i<points.length;i++) { |
|
|
|
@ -213,9 +271,9 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
Edge e2 = active_edges.get(i+1); |
|
|
|
|
//System.out.println("Drawing from "+((int)Math.round(e1.x_of_min_y))+" to "+e2.x_of_min_y+" on line "+scanLine);
|
|
|
|
|
for (int x=(int)Math.round(e1.x_of_min_y);x<=e2.x_of_min_y;x++) { |
|
|
|
|
int index = (scanLine+(int)y_offset)*width+x+(int)x_offset; |
|
|
|
|
int index = (scanLine+(int)y_offset)*getWidth()+x+(int)x_offset; |
|
|
|
|
if (index<p.length&&index>=0) { |
|
|
|
|
Draw(p,index,col.getColor()); |
|
|
|
|
Draw(p,index,col); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -277,38 +335,237 @@ public class Panel extends JPanel implements Runnable { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw(int[] canvas,int index, int col) { |
|
|
|
|
int alpha = col>>>24; |
|
|
|
|
if (alpha==0) { |
|
|
|
|
return;} |
|
|
|
|
else |
|
|
|
|
if (alpha==255) { |
|
|
|
|
canvas[index]=col; |
|
|
|
|
} else { |
|
|
|
|
float ratio=alpha/255f; |
|
|
|
|
int prev_col=canvas[index]; |
|
|
|
|
int prev_r=(prev_col&0xFF); |
|
|
|
|
int prev_g=(prev_col&0xFF00)>>>8; |
|
|
|
|
int prev_b=(prev_col&0xFF0000)>>>16; |
|
|
|
|
int r=(col&0xFF); |
|
|
|
|
int g=(col&0xFF00)>>>8; |
|
|
|
|
int b=(col&0xFF0000)>>>16; |
|
|
|
|
|
|
|
|
|
int new_r=(int)(ratio*r+(1-ratio)*prev_r); |
|
|
|
|
int new_g=(int)(ratio*g+(1-ratio)*prev_g); |
|
|
|
|
int new_b=(int)(ratio*b+(1-ratio)*prev_b); |
|
|
|
|
|
|
|
|
|
canvas[index]=new_r+(new_g<<8)+(new_b<<16)+(col&0xFF000000); |
|
|
|
|
} |
|
|
|
|
public void Draw(int index, byte col) { |
|
|
|
|
pixel[index]=col; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
if (mImageProducer==null) return; |
|
|
|
|
while (true) { |
|
|
|
|
// request a JPanel re-drawing
|
|
|
|
|
repaint(); |
|
|
|
|
//repaint();
|
|
|
|
|
render(); |
|
|
|
|
mImageProducer.newPixels(); |
|
|
|
|
if (f!=null&&f.getBufferStrategy()!=null) { |
|
|
|
|
do { |
|
|
|
|
do { |
|
|
|
|
if (f.getBufferStrategy()!=null) { |
|
|
|
|
Graphics2D g2 = (Graphics2D)f.getBufferStrategy().getDrawGraphics(); |
|
|
|
|
g2.setRenderingHints(RENDERHINTS); |
|
|
|
|
if (g2!=null) { |
|
|
|
|
try { |
|
|
|
|
paintComponent(g2); |
|
|
|
|
} finally { |
|
|
|
|
g2.dispose(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} while (f.getBufferStrategy().contentsRestored()); |
|
|
|
|
f.getBufferStrategy().show(); |
|
|
|
|
Toolkit.getDefaultToolkit().sync(); |
|
|
|
|
} while (f.getBufferStrategy().contentsLost()); |
|
|
|
|
} |
|
|
|
|
updateFPSCounter(); |
|
|
|
|
//System.out.println("Repaint "+frameCount++);
|
|
|
|
|
//try {Thread.sleep(1);} catch (InterruptedException e) {}
|
|
|
|
|
waitForNextFrame(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void waitForNextFrame() { |
|
|
|
|
long newTime = System.nanoTime(); |
|
|
|
|
if (newTime-lastUpdate<TARGET_FRAMETIME) { |
|
|
|
|
long timeRemaining=TARGET_FRAMETIME-(newTime-lastUpdate); |
|
|
|
|
long millis = timeRemaining/1000000l; |
|
|
|
|
int nanos = (int)(timeRemaining-millis*1000000l); |
|
|
|
|
//System.out.println(timeRemaining+"/"+millis+" Nanos:"+nanos);
|
|
|
|
|
try { |
|
|
|
|
Thread.sleep(millis,nanos); |
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
lastUpdate=newTime; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void updateFPSCounter() { |
|
|
|
|
if (window!=null&&System.currentTimeMillis()-lastSecond>=1000) { |
|
|
|
|
window.setTitle(JavaProjectTemplate.PROGRAM_NAME+" - FPS: "+(frameCount)); |
|
|
|
|
frameCount=0; |
|
|
|
|
lastSecond=System.currentTimeMillis(); |
|
|
|
|
} |
|
|
|
|
frameCount++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void keyTyped(KeyEvent e) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void keyPressed(KeyEvent e) { |
|
|
|
|
if (!Key.isKeyHeld(e.getKeyCode())) { |
|
|
|
|
Key.setKeyHeld(e.getKeyCode(), true); |
|
|
|
|
} |
|
|
|
|
//System.out.println("Key List: "+KEYS);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void keyReleased(KeyEvent e) { |
|
|
|
|
Key.setKeyHeld(e.getKeyCode(), false); |
|
|
|
|
//System.out.println("Key List: "+KEYS);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Text(double x, double y, String s, Font f) { |
|
|
|
|
Draw_Text_Ext(x,y,s,f,Color.BLACK); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Text_Ext(double x, double y, String s, Font f, Color col) { |
|
|
|
|
java.lang.String finalS = s.toString(); |
|
|
|
|
int charCount=0; |
|
|
|
|
int yOffset=0; |
|
|
|
|
int xOffset=0; |
|
|
|
|
Color currentCol = col; |
|
|
|
|
for (int i=0;i<finalS.length();i++) { |
|
|
|
|
if (finalS.charAt(i)=='\n') { |
|
|
|
|
xOffset+=(charCount+1)*f.getGlyphWidth(); |
|
|
|
|
yOffset+=f.getGlyphHeight(); |
|
|
|
|
charCount=0; |
|
|
|
|
} else { |
|
|
|
|
Draw_Sprite_Partial_Ext(x+i*f.getGlyphWidth()-xOffset, y+yOffset, f.getCharInfo(finalS.charAt(i)).getX(), f.getCharInfo(finalS.charAt(i)).getY(), f.getCharInfo(finalS.charAt(i)).getWidth(), f.getCharInfo(finalS.charAt(i)).getHeight(), f.getSprite(), 0,currentCol,Transform.NONE); |
|
|
|
|
charCount++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Line(byte[] canvas,int x1,int y1,int x2,int y2,byte col) { |
|
|
|
|
int x,y,dx,dy,dx1,dy1,px,py,xe,ye; |
|
|
|
|
dx=x2-x1;dy=y2-y1; |
|
|
|
|
dx1=Math.abs(dx);dy1=Math.abs(dy); |
|
|
|
|
px=2*dy1-dx1;py=2*dx1-dy1; |
|
|
|
|
if (dy1<=dx1) { |
|
|
|
|
if (dx>=0) { |
|
|
|
|
x=x1;y=y1;xe=x2-1; |
|
|
|
|
} else { |
|
|
|
|
x=x2-1;y=y2-1;xe=x1; |
|
|
|
|
} |
|
|
|
|
Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col); |
|
|
|
|
while (x<xe) { |
|
|
|
|
x=x+1; |
|
|
|
|
if (px<0) { |
|
|
|
|
px=px+2*dy1; |
|
|
|
|
} else { |
|
|
|
|
if ((dx<0&&dy<0)||(dx>0&&dy>0)) { |
|
|
|
|
y=y+1; |
|
|
|
|
} else { |
|
|
|
|
y=y-1; |
|
|
|
|
} |
|
|
|
|
px=px+2*(dy1-dx1); |
|
|
|
|
} |
|
|
|
|
Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (dy>=0) { |
|
|
|
|
x=x1;y=y1;ye=y2-1; |
|
|
|
|
} else { |
|
|
|
|
x=x2-1;y=y2-1;ye=y1; |
|
|
|
|
} |
|
|
|
|
Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col); |
|
|
|
|
while (y<ye) { |
|
|
|
|
y=y+1; |
|
|
|
|
if (py<=0) { |
|
|
|
|
py=py+2*dx1; |
|
|
|
|
} else { |
|
|
|
|
if ((dx<0&&dy<0)||(dx>0&&dy>0)) { |
|
|
|
|
x=x+1; |
|
|
|
|
} else { |
|
|
|
|
x=x-1; |
|
|
|
|
} |
|
|
|
|
py=py+2*(dx1-dy1); |
|
|
|
|
} |
|
|
|
|
Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Fill_Rect(byte[] p,byte col,double x,double y,double w,double h) { |
|
|
|
|
for (int xx=0;xx<w;xx++) { |
|
|
|
|
for (int yy=0;yy<h;yy++) { |
|
|
|
|
if (x+xx>=0&&y+yy>=0&&x+xx<JavaProjectTemplate.WINDOW_WIDTH&&y+yy<JavaProjectTemplate.WINDOW_HEIGHT) { |
|
|
|
|
int index = ((int)y+yy)*JavaProjectTemplate.WINDOW_WIDTH+(int)x+xx; |
|
|
|
|
Draw(p,index,col); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Sprite(double x, double y, Sprite sprite){ |
|
|
|
|
Draw_Sprite_Partial(x,y,0,0,sprite.getWidth(),sprite.getHeight(),sprite,0,Transform.NONE ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Animated_Sprite(double x, double y, AnimatedSprite sprite, double frameIndex){ |
|
|
|
|
Rectangle frameRectangle=sprite.getFrame((int)frameIndex); |
|
|
|
|
Draw_Sprite_Partial(x,y,frameRectangle.getX(),frameRectangle.getY(),frameRectangle.getWidth(),frameRectangle.getHeight(),sprite,frameIndex,Transform.NONE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Sprite(double x, double y, Sprite sprite, Transform transform){ |
|
|
|
|
Draw_Sprite_Partial(x,y,0,0,sprite.getWidth(),sprite.getHeight(),sprite,0,transform); |
|
|
|
|
} |
|
|
|
|
public void Draw_Animated_Sprite(double x, double y, AnimatedSprite sprite, double frameIndex, Transform transform){ |
|
|
|
|
Rectangle frameRectangle=sprite.getFrame((int)frameIndex); |
|
|
|
|
Draw_Sprite_Partial(x,y,frameRectangle.getX(),frameRectangle.getY(),frameRectangle.getWidth(),frameRectangle.getHeight(),sprite,frameIndex, transform); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, double frame_index, Transform transform){ |
|
|
|
|
Draw_Sprite_Partial_Ext(x,y,xOffset,yOffset,w,h,sprite,frame_index, Color.WHITE,transform); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, Transform transform){ |
|
|
|
|
Draw_Sprite_Partial_Ext(x, y, xOffset, yOffset, w, h, sprite, 0, Color.WHITE, transform); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Animated_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, AnimatedSprite sprite, double frameIndex, Transform transform){ |
|
|
|
|
Rectangle frameRectangle=sprite.getFrame((int)frameIndex); |
|
|
|
|
Draw_Sprite_Partial_Ext(x, y, frameRectangle.getX(), frameRectangle.getY(), frameRectangle.getWidth(), frameRectangle.getHeight(), sprite, 0, Color.WHITE, transform); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, double frame_index, Color col, Transform transform){ |
|
|
|
|
boolean horizontal = transform==Transform.HORIZONTAL||transform==Transform.HORIZ_VERTIC; |
|
|
|
|
boolean vertical = transform==Transform.VERTICAL||transform==Transform.HORIZ_VERTIC; |
|
|
|
|
for(int X=(int)xOffset;X<(int)(w+xOffset);X++){ |
|
|
|
|
for(int Y=(int)yOffset;Y<(int)(h+yOffset);Y++){ |
|
|
|
|
if (X+x-xOffset<0||Y+y-yOffset<0||X-xOffset+x>=JavaProjectTemplate.WINDOW_WIDTH||Y-yOffset+y>=JavaProjectTemplate.WINDOW_HEIGHT) { |
|
|
|
|
continue; |
|
|
|
|
} else { |
|
|
|
|
int index = |
|
|
|
|
((vertical? |
|
|
|
|
sprite.getHeight()-(Y-(int)yOffset): |
|
|
|
|
(Y-(int)yOffset)) |
|
|
|
|
+(int)y)*JavaProjectTemplate.WINDOW_WIDTH+ |
|
|
|
|
(horizontal? |
|
|
|
|
sprite.getWidth()-(X-(int)xOffset): |
|
|
|
|
(X-(int)xOffset)) |
|
|
|
|
+(int)x; |
|
|
|
|
|
|
|
|
|
if (((sprite.getImg().getRGB(X,Y)>>>24)&0xFF)==0||index<0||index>=pixel.length) { |
|
|
|
|
continue; |
|
|
|
|
} else { |
|
|
|
|
Draw(pixel,index,(col==Color.WHITE)?sprite.getImg().getRGB(X,Y):col.getColor()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Clear(Color col){ |
|
|
|
|
for (int y=0;y<JavaProjectTemplate.WINDOW_HEIGHT;y++) { |
|
|
|
|
for (int x=0;x<JavaProjectTemplate.WINDOW_WIDTH;x++) { |
|
|
|
|
Draw(pixel,y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void Draw(int[] canvas,int index, int col) { |
|
|
|
|
canvas[index]=col; |
|
|
|
|
} |
|
|
|
|
} |