Pixel upscaling based on monitor size

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent daf471db93
commit 8bee6dd30c
  1. BIN
      1x.png
  2. 10
      src/sig/DrawLoop.java
  3. 32
      src/sig/RabiClone.java
  4. 48
      src/sig/engine/Panel.java
  5. 1
      src/sig/engine/Sprite.java
  6. 9
      src/sig/objects/Player.java

BIN
1x.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

@ -9,9 +9,9 @@ public class DrawLoop {
DrawLoop.panel=panel; DrawLoop.panel=panel;
int[] p = panel.pixel; int[] p = panel.pixel;
for (int y=0;y<panel.getHeight();y++) { for (int y=0;y<RabiClone.BASE_HEIGHT;y++) {
for (int x=0;x<panel.getWidth();x++) { for (int x=0;x<RabiClone.BASE_WIDTH;x++) {
p[y*panel.getWidth()+x]=(0<<16)+(0<<8)+0;//RGB p[y*RabiClone.BASE_WIDTH+x]=(0<<16)+(0<<8)+0;//RGB
} }
} }
@ -24,8 +24,8 @@ public class DrawLoop {
int[] p = panel.pixel; int[] p = panel.pixel;
for(int X=0;X<sprite.getHeight();X++){ for(int X=0;X<sprite.getHeight();X++){
for(int Y=0;Y<sprite.getWidth();Y++){ for(int Y=0;Y<sprite.getWidth();Y++){
int index = (Y+(int)y)*panel.getWidth()+X+(int)x; int index = (Y+(int)y)*RabiClone.BASE_WIDTH+X+(int)x;
if (index<0||index>=p.length) { if (index<0||index>=p.length||p[index]==sprite.getBi_array()[Y*sprite.getWidth()+X]) {
continue; continue;
} else { } else {
Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X]); Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X]);

@ -1,35 +1,52 @@
package sig; package sig;
import javax.swing.JFrame; import javax.swing.JFrame;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import sig.engine.Panel; import sig.engine.Panel;
import sig.objects.Player;
import sig.engine.Object; import sig.engine.Object;
import java.awt.Toolkit;
public class RabiClone { public class RabiClone{
public static final String PROGRAM_NAME="Sig's Java Project Template"; public static final String PROGRAM_NAME="Sig's Java Project Template";
public static int UPCOUNT=0; public static int UPCOUNT=0;
public static Panel p; public static Panel p;
public static JFrame f;
public static List<Object> OBJ = new ArrayList<Object>(); public static List<Object> OBJ = new ArrayList<Object>();
boolean newSizeSet=false;
public static int BASE_WIDTH=512;
public static int BASE_HEIGHT=288;
public static void main(String[] args) { public static void main(String[] args) {
JFrame f = new JFrame(PROGRAM_NAME); RabiClone r = new RabiClone();
f = new JFrame(PROGRAM_NAME);
f.setResizable(false);
f.setUndecorated(true);
f.setSize(BASE_WIDTH,BASE_HEIGHT); //1024x576 (64x64)
ChooseBestRatio();
p = new Panel(f); p = new Panel(f);
p.init(); p.init();
f.add(p); f.add(p);
f.addComponentListener(p);
f.addKeyListener(p); f.addKeyListener(p);
f.setSize(1280,720); 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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); f.setVisible(true);
p.render(); p.render();
for (int i=0;i<10;i++) {
OBJ.add(new Player(p));
}
long lastGameTime = System.nanoTime(); long lastGameTime = System.nanoTime();
while (true) { while (true) {
long timePassed = System.nanoTime()-lastGameTime; long timePassed = System.nanoTime()-lastGameTime;
@ -44,4 +61,11 @@ public class RabiClone {
} }
} }
} }
private static void ChooseBestRatio() {
int multiplier=1;
while (f.getWidth()*(multiplier+1)<Toolkit.getDefaultToolkit().getScreenSize().getWidth()) {
multiplier++;
}
f.setSize(f.getWidth()*multiplier,(int)((f.getWidth()*multiplier)/1.77777777778d));
}
} }

@ -9,8 +9,6 @@ import java.awt.image.MemoryImageSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
@ -22,7 +20,7 @@ import java.awt.event.KeyListener;
import sig.DrawLoop; import sig.DrawLoop;
import sig.RabiClone; import sig.RabiClone;
public class Panel extends JPanel implements Runnable,ComponentListener,KeyListener { public class Panel extends JPanel implements Runnable,KeyListener {
JFrame window; JFrame window;
public int pixel[]; public int pixel[];
final int CIRCLE_PRECISION=32; final int CIRCLE_PRECISION=32;
@ -117,14 +115,14 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
*/ */
public void init(){ public void init(){
cm = getCompatibleColorModel(); cm = getCompatibleColorModel();
int screenSize = getWidth()*getHeight(); int screenSize = RabiClone.BASE_WIDTH*RabiClone.BASE_HEIGHT;
if(pixel == null || pixel.length < screenSize){ if(pixel == null || pixel.length < screenSize){
pixel = new int[screenSize]; pixel = new int[screenSize];
} }
if(thread.isInterrupted() || !thread.isAlive()){ if(thread.isInterrupted() || !thread.isAlive()){
thread.start(); thread.start();
} }
mImageProducer = new MemoryImageSource(getWidth(), getHeight(), cm, pixel,0, getWidth()); mImageProducer = new MemoryImageSource(RabiClone.BASE_WIDTH, RabiClone.BASE_HEIGHT, cm, pixel,0, RabiClone.BASE_WIDTH);
mImageProducer.setAnimated(true); mImageProducer.setAnimated(true);
mImageProducer.setFullBufferUpdates(true); mImageProducer.setFullBufferUpdates(true);
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer); imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
@ -138,7 +136,7 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
// ask ImageProducer to update image // ask ImageProducer to update image
mImageProducer.newPixels(); mImageProducer.newPixels();
// draw it on panel // draw it on panel
g.drawImage(this.imageBuffer, 0, 0, this); g.drawImage(this.imageBuffer,0,0,getWidth(),getHeight(),0,0,RabiClone.BASE_WIDTH,RabiClone.BASE_HEIGHT,this);
updateFPSCounter(); updateFPSCounter();
} }
@ -157,24 +155,10 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
public /* abstract */ void render(){ public /* abstract */ void render(){
resizeUpdate();
int[] p = pixel; // this avoid crash when resizing
//a=h/w //a=h/w
DrawLoop.drawGame(this); DrawLoop.drawGame(this);
} }
private void resizeUpdate() {
if (resizing) {
pixel = new int[getWidth()*getHeight()];
resizing=false;
mImageProducer = new MemoryImageSource(getWidth(), getHeight(), cm, pixel,0, getWidth());
mImageProducer.setAnimated(true);
mImageProducer.setFullBufferUpdates(true);
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
System.out.println("Window resized.");
}
}
public void FillRect(int[] p,Color col,double x,double y,double w,double h) { public void FillRect(int[] p,Color col,double x,double y,double w,double h) {
for (int xx=0;xx<w;xx++) { for (int xx=0;xx<w;xx++) {
for (int yy=0;yy<h;yy++) { for (int yy=0;yy<h;yy++) {
@ -375,30 +359,6 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
frameCount++; frameCount++;
} }
@Override
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void componentResized(ComponentEvent e) {
//COMPONENT_RESIZED (8,564 948x508)
resizing=true;
}
@Override
public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub // TODO Auto-generated method stub

@ -9,6 +9,7 @@ import javax.imageio.ImageIO;
public enum Sprite{ public enum Sprite{
NANA(new File("..","3x.png")), NANA(new File("..","3x.png")),
NANA_SMALL(new File("..","1x.png")),
; ;

@ -1,6 +1,7 @@
package sig.objects; package sig.objects;
import sig.DrawLoop; import sig.DrawLoop;
import sig.RabiClone;
import sig.engine.Object; import sig.engine.Object;
import sig.engine.Panel; import sig.engine.Panel;
import sig.engine.Sprite; import sig.engine.Sprite;
@ -11,8 +12,8 @@ public class Player extends Object{
public Player(Panel panel) { public Player(Panel panel) {
super(panel); super(panel);
this.setSprite(Sprite.NANA); this.setSprite(Sprite.NANA);
setX(Math.random()*panel.getWidth()); setX(Math.random()*RabiClone.BASE_WIDTH);
setY(Math.random()*panel.getHeight()); setY(Math.random()*RabiClone.BASE_HEIGHT);
} }
@ -28,10 +29,6 @@ public class Player extends Object{
if (up-down!=0) { if (up-down!=0) {
setY(getY()+(up-down)*32*updateMult); setY(getY()+(up-down)*32*updateMult);
} }
if (getX()<-this.getSprite().getWidth()||getX()>getPanel().getWidth()||
getY()<-this.getSprite().getHeight()||getY()>getPanel().getHeight()) {
setMarkedForDeletion(true);
}
} }
@Override @Override

Loading…
Cancel
Save