package sig.engine; import java.awt.Graphics; 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.image.IndexColorModel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.event.MouseInputListener; import java.awt.event.KeyListener; import sig.DrawLoop; import sig.RabiClone; public class Panel extends JPanel implements Runnable,KeyListener { JFrame window; public byte pixel[]; final int CIRCLE_PRECISION=32; final int OUTLINE_COL=Color.BRIGHT_WHITE.getColor(); private Thread thread; private Image imageBuffer; private MemoryImageSource mImageProducer; private ColorModel cm; int scanLine=0; int nextScanLine=0; double x_offset=0; double y_offset=0; int frameCount=0; long lastSecond=0; boolean resizing=false; long lastUpdate=System.nanoTime(); final long TARGET_FRAMETIME = 8333333l; public double nanaX = 0; public double nanaY = 0; public Point mousePos=new Point(0,0); public int button = 0; public HashMap KEYS = new HashMap<>(); public HashMap MOUSE = new HashMap<>(); public static byte[] generalPalette = new byte[]{ (byte)0x5b,(byte)0xa6,(byte)0x75, (byte)0x6b,(byte)0xc9,(byte)0x6c, (byte)0xab,(byte)0xdd,(byte)0x64, (byte)0xfc,(byte)0xef,(byte)0x8d, (byte)0xff,(byte)0xb8,(byte)0x79, (byte)0xea,(byte)0x62,(byte)0x62, (byte)0xcc,(byte)0x42,(byte)0x5e, (byte)0xa3,(byte)0x28,(byte)0x58, (byte)0x75,(byte)0x17,(byte)0x56, (byte)0x39,(byte)0x09,(byte)0x47, (byte)0x61,(byte)0x18,(byte)0x51, (byte)0x87,(byte)0x35,(byte)0x55, (byte)0xa6,(byte)0x55,(byte)0x5f, (byte)0xc9,(byte)0x73,(byte)0x73, (byte)0xf2,(byte)0xae,(byte)0x99, (byte)0xff,(byte)0xc3,(byte)0xf2, (byte)0xee,(byte)0x8f,(byte)0xcb, (byte)0xd4,(byte)0x6e,(byte)0xb3, (byte)0x87,(byte)0x3e,(byte)0x84, (byte)0x1f,(byte)0x10,(byte)0x2a, (byte)0x4a,(byte)0x30,(byte)0x52, (byte)0x7b,(byte)0x54,(byte)0x80, (byte)0xa6,(byte)0x85,(byte)0x9f, (byte)0xd9,(byte)0xbd,(byte)0xc8, (byte)0xff,(byte)0xff,(byte)0xff, (byte)0xae,(byte)0xe2,(byte)0xff, (byte)0x8d,(byte)0xb7,(byte)0xff, (byte)0x6d,(byte)0x80,(byte)0xfa, (byte)0x84,(byte)0x65,(byte)0xec, (byte)0x83,(byte)0x4d,(byte)0xc4, (byte)0x7d,(byte)0x2d,(byte)0xa0, (byte)0x4e,(byte)0x18,(byte)0x7c, }; 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); mousePos.set(e.getX()/RabiClone.SIZE_MULTIPLIER,e.getY()/RabiClone.SIZE_MULTIPLIER); //System.out.println("Mouse List: "+MOUSE); } @Override public void mouseReleased(MouseEvent e) { MOUSE.put(e.getButton(),false); mousePos.set(e.getX()/RabiClone.SIZE_MULTIPLIER,e.getY()/RabiClone.SIZE_MULTIPLIER); } @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) { mousePos.set(e.getX()/RabiClone.SIZE_MULTIPLIER,e.getY()/RabiClone.SIZE_MULTIPLIER); } @Override public void mouseMoved(MouseEvent e) { mousePos.set(e.getX()/RabiClone.SIZE_MULTIPLIER,e.getY()/RabiClone.SIZE_MULTIPLIER); } }); this.addMouseWheelListener(new MouseWheelListener(){ //-1 is UP, 1 is DOWN @Override public void mouseWheelMoved(MouseWheelEvent e) { for(int i=0; i edges_sorted = new ArrayList(); for (int i=0;i=edges[i].min_y) { edges_sorted.add(j,edges[i]); inserted=true; break; } } if (!inserted) { edges_sorted.add(edges[i]); } } } } //System.out.println(edges_sorted); List active_edges = new ArrayList(); scanLine = edges_sorted.get(0).min_y-1; nextScanLine = scanLine+1; do { for (int i=0;i=0) { //TODO Old color system. //Draw(p,index,col.getColor()); } } } List new_active_edges = new ArrayList(); for (int i=0;ie.x_of_min_y) { new_active_edges.add(j,e); inserted=true; break; } } if (!inserted) { new_active_edges.add(e); } } active_edges=new_active_edges; GetNextScanLineEdges(edges_sorted, active_edges); } while (active_edges.size()>0); } private void GetNextScanLineEdges(List edges_sorted, List active_edges) { if (scanLine==nextScanLine) { for (int i=0;iscanLine) { nextScanLine=e.min_y; break; } } } } public void Draw(byte[] canvas,int index, byte col) { canvas[index]=col; } @Override public void run() { while (true) { // request a JPanel re-drawing repaint(); //System.out.println("Repaint "+frameCount++); waitForNextFrame(); } } private void waitForNextFrame() { long newTime = System.nanoTime(); if (newTime-lastUpdate=1000) { window.setTitle(RabiClone.PROGRAM_NAME+" - FPS: "+(frameCount)); frameCount=0; lastSecond=System.currentTimeMillis(); } frameCount++; } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if (!KEYS.getOrDefault(e.getKeyCode(),false)) { KEYS.put(e.getKeyCode(),true); for (int i=0;i