Better CPU renderer

This commit is contained in:
Andrea Cavalli 2018-04-02 18:29:55 +02:00
parent 095c6a0094
commit a591a39d87
13 changed files with 166 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package org.warp.picalculator.gui;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Semaphore;
@ -152,6 +153,10 @@ public final class DisplayManager implements RenderingLoop {
public void setScreen(Screen screen) {
if (screen.initialized == false) {
if (screen.canBeInHistory) {
if (DisplayManager.INSTANCE.currentSession > 0) {
int sl = DisplayManager.INSTANCE.sessions.length + 5; //TODO: I don't know why if i don't add +5 or more some items disappear
DisplayManager.INSTANCE.sessions = Arrays.copyOfRange(DisplayManager.INSTANCE.sessions, DisplayManager.INSTANCE.currentSession, sl);
}
DisplayManager.INSTANCE.currentSession = 0;
for (int i = DisplayManager.INSTANCE.sessions.length - 1; i >= 1; i--) {
DisplayManager.INSTANCE.sessions[i] = DisplayManager.INSTANCE.sessions[i - 1];

View File

@ -107,24 +107,32 @@ public class CPURenderer implements Renderer {
for (double pixelX = 0; pixelX < x1-x0; pixelX++) {
for (double pixelY = 0; pixelY < y1-y0; pixelY++) {
final int index = (int) (x0+pixelX + (y0+pixelY) * size[0]);
if (canvas2d.length > index) {
if (index >= 0 && index < canvas2d.length && pixelX < size[0]) {
int texx = (int) (pixelX / incrementX);
int texy = (int) (pixelY / incrementY);
int skinIndex = (int) (s0 + texx + (t0 + texy) * currentSkin.skinSize[0]);
if (skinIndex >= 0 && skinIndex < currentSkin.skinData.length) {
newColor = currentSkin.skinData[skinIndex];
final int a = (int) ((double)(newColor >> 24 & 0xFF) * ((double)(color >> 24 & 0xFF)/(double)0xFF));
int r = (int) ((double)(newColor >> 16 & 0xFF) * ((double)(color >> 16 & 0xFF)/(double)0xFF));
int g = (int) ((double)(newColor >> 8 & 0xFF) * ((double)(color >> 8 & 0xFF)/(double)0xFF));
int b = (int) ((double)(newColor & 0xFF) * ((double)(color & 0xFF)/(double)0xFF));
newColor = a << 24 | r << 16 | g << 8 | b;
int expX = 0;
int expY = 0;
if (incrementX < 1) {
expX = (int) Math.round(1d/incrementX/2d);
}
if (incrementY < 1) {
expY = (int) Math.round(1d/incrementY/2d);
}
int[] newColors = new int[(1+expX*2)*(1+expY*2)];
for (int expXi = -expX; expXi <= expX; expXi++) {
for (int expYi = -expY; expYi <= expY; expYi++) {
int skinIndex = (int) (s0 + (texx+expXi) + (t0 + (texy+expYi)) * currentSkin.skinSize[0]);
newColors[(expXi+expX)+(expYi+expY)*(1+expY*2)] = getSkinColorAt(currentSkin.skinData, skinIndex);
}
}
newColor = joinColors(newColors);
if (transparent) {
oldColor = canvas2d[index];
final float a2 = (newColor >> 24 & 0xFF) / 255f;
final float a1 = 1f - a2;
r = (int) ((oldColor >> 16 & 0xFF) * a1 + (newColor >> 16 & 0xFF) * a2);
g = (int) ((oldColor >> 8 & 0xFF) * a1 + (newColor >> 8 & 0xFF) * a2);
b = (int) ((oldColor & 0xFF) * a1 + (newColor & 0xFF) * a2);
int r = (int) ((oldColor >> 16 & 0xFF) * a1 + (newColor >> 16 & 0xFF) * a2);
int g = (int) ((oldColor >> 8 & 0xFF) * a1 + (newColor >> 8 & 0xFF) * a2);
int b = (int) ((oldColor & 0xFF) * a1 + (newColor & 0xFF) * a2);
newColor = 0xFF000000 | r << 16 | g << 8 | b;
}
@ -133,6 +141,33 @@ public class CPURenderer implements Renderer {
}
}
}
private int joinColors(int[] newColors) {
int a = 0;
int r = 0;
int g = 0;
int b = 0;
for (int i = 0; i < newColors.length; i++) {
int newColor = newColors[i];
a += newColor >> 24 & 0xFF;
r += newColor >> 16 & 0xFF;
g += newColor >> 8 & 0xFF;
b += newColor & 0xFF;
}
return (a/newColors.length) << 24 | (r/newColors.length) << 16 | (g/newColors.length) << 8 | (b/newColors.length);
}
private int getSkinColorAt(int[] skinData, int skinIndex) {
int newColor = 0;
if (skinIndex >= 0 && skinIndex < skinData.length) {
newColor = skinData[skinIndex];
final int a = (int) ((double)(newColor >> 24 & 0xFF) * ((double)(color >> 24 & 0xFF)/(double)0xFF));
int r = (int) ((double)(newColor >> 16 & 0xFF) * ((double)(color >> 16 & 0xFF)/(double)0xFF));
int g = (int) ((double)(newColor >> 8 & 0xFF) * ((double)(color >> 8 & 0xFF)/(double)0xFF));
int b = (int) ((double)(newColor & 0xFF) * ((double)(color & 0xFF)/(double)0xFF));
newColor = a << 24 | r << 16 | g << 8 | b;
}
return newColor;
}
@Override

View File

@ -23,9 +23,14 @@ public class CPUSkin implements Skin {
@Override
public void load(String file) throws IOException {
final BufferedImage img = ImageIO.read(isResource ? this.getClass().getResource("/" + file) : new File(file).toURI().toURL());
if (img == null) {
skinData = new int[0];
skinSize = new int[] {0,0};
} else {
skinData = getMatrixOfImage(img);
skinSize = new int[] { img.getWidth(), img.getHeight() };
}
}
public static int[] getMatrixOfImage(BufferedImage bufferedImage) {
final int width = bufferedImage.getWidth(null);

View File

@ -39,7 +39,7 @@ public class SwingWindow extends JFrame {
// Transparent 16 x 16 pixel cursor image.
final BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
if ((StaticVars.windowZoom != 1) | (StaticVars.debugOn & StaticVars.debugWindow2x)) mult = 2;
if (StaticVars.debugOn & StaticVars.debugWindow2x) mult = 2;
if (StaticVars.debugOn) {
if (Utils.debugThirdScreen) {
this.setLocation(2880, 900);

View File

@ -134,6 +134,7 @@ public class GPUFont implements BinaryFont {
System.out.println(((int)Math.ceil(Math.sqrt(totalChars) * charW)) + " * " + ((int)Math.ceil(Math.sqrt(totalChars) * charH)) + " --> " + w + " * " + h);
File f = Files.createTempFile("texture-font-", ".png").toFile();
f.deleteOnExit();
final FileOutputStream outputStream = new FileOutputStream(f);
final ImageInfo imi = new ImageInfo(w, h, 8, true); // 8 bits per channel, alpha
// open image for writing to a output stream

View File

@ -251,6 +251,7 @@ public class GPURenderer implements Renderer {
File f;
if (isResource) {
f = Files.createTempFile("texture-", ".png").toFile();
f.deleteOnExit();
ImageIO.write(img, "png", f);
} else {
f = new File(file);

View File

@ -44,4 +44,14 @@ public class Headless24bitFont implements BinaryFont {
return true;
}
@Override
public int getSkinWidth() {
return 0;
}
@Override
public int getSkinHeight() {
return 0;
}
}

View File

@ -59,4 +59,14 @@ public class Headless24bitSkin implements Skin {
return true;
}
@Override
public int getSkinWidth() {
return 0;
}
@Override
public int getSkinHeight() {
return 0;
}
}

View File

@ -44,4 +44,14 @@ public class Headless256Font implements BinaryFont {
return true;
}
@Override
public int getSkinWidth() {
return 0;
}
@Override
public int getSkinHeight() {
return 0;
}
}

View File

@ -65,4 +65,14 @@ public class Headless256Skin implements Skin {
return true;
}
@Override
public int getSkinWidth() {
return 0;
}
@Override
public int getSkinHeight() {
return 0;
}
}

View File

@ -44,4 +44,14 @@ public class Headless8Font implements BinaryFont {
return true;
}
@Override
public int getSkinWidth() {
return 0;
}
@Override
public int getSkinHeight() {
return 0;
}
}

View File

@ -60,4 +60,14 @@ public class Headless8Skin implements Skin {
return true;
}
@Override
public int getSkinWidth() {
return 0;
}
@Override
public int getSkinHeight() {
return 0;
}
}

View File

@ -199,6 +199,18 @@ public class NoGuiEngine implements GraphicEngine {
public int getCharacterHeight() {
return 1;
}
@Override
public int getSkinWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSkinHeight() {
// TODO Auto-generated method stub
return 0;
}
};
}
@ -236,6 +248,18 @@ public class NoGuiEngine implements GraphicEngine {
public int getCharacterHeight() {
return 1;
}
@Override
public int getSkinWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSkinHeight() {
// TODO Auto-generated method stub
return 0;
}
};
}
@ -258,6 +282,18 @@ public class NoGuiEngine implements GraphicEngine {
@Override
public void initialize(GraphicEngine d) {
}
@Override
public int getSkinWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSkinHeight() {
// TODO Auto-generated method stub
return 0;
}
};
}