made scalers chooseable by string

This commit is contained in:
DaniTheSkunk 2022-12-13 02:46:57 +00:00
parent e32fa9e095
commit 5aa96f2b7f
9 changed files with 59 additions and 57 deletions

View File

@ -8,11 +8,15 @@ import com.danitheskunk.skunkworks.gfx.font.IFont;
abstract public class BaseWindow implements IWindow { abstract public class BaseWindow implements IWindow {
protected final Engine engine; protected final Engine engine;
protected WindowStretchMode stretchMode; protected String scaler;
public void setScaler(String scaler) {
this.scaler = scaler;
}
public BaseWindow(Engine engine) { public BaseWindow(Engine engine) {
this.engine = engine; this.engine = engine;
stretchMode = WindowStretchMode.ASPECT; scaler = "aspect";
} }
@Override @Override
@ -116,11 +120,4 @@ abstract public class BaseWindow implements IWindow {
public NineSlice loadNineSlice(String path) { public NineSlice loadNineSlice(String path) {
return loadNineSlice(engine.loadImage(path)); return loadNineSlice(engine.loadImage(path));
} }
@Override
public void setStretchMode(WindowStretchMode mode) {
stretchMode = mode;
} }
}

View File

@ -44,7 +44,7 @@ public interface IWindow {
void setDebug(boolean on); void setDebug(boolean on);
void setStretchMode(WindowStretchMode mode); void setScaler(String scaler);
boolean shouldClose(); boolean shouldClose();

View File

@ -19,7 +19,6 @@ public class Test extends BaseGame {
var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png"); var fontThin2 = window.loadFontTileset("fonts\\thin-12x12.png");
term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin); term = new Terminal(new Vec2i(40, 22), fontThin2, fontThin);
window.setStretchMode(WindowStretchMode.INTEGER);
var c1 = Color.GREEN; var c1 = Color.GREEN;
var c2 = new Color(0, 128, 0); var c2 = new Color(0, 128, 0);
@ -58,6 +57,7 @@ public class Test extends BaseGame {
@Override @Override
protected void render(IRenderContext rc) { protected void render(IRenderContext rc) {
rc.clear(Color.DARK_GRAY);
rc.drawTerminal(term); rc.drawTerminal(term);
} }

View File

@ -1,5 +0,0 @@
package com.danitheskunk.skunkworks;
public enum WindowStretchMode {
STRETCH, ASPECT, INTEGER
}

View File

@ -67,4 +67,8 @@ public abstract class Filter {
private void calcSizeRatio() { private void calcSizeRatio() {
sizeRatio = Vec2f.div(output.getSize(), input.getSize()); sizeRatio = Vec2f.div(output.getSize(), input.getSize());
} }
public Vec2i translateScreenCoordinate(Vec2i coord) {
return coord;
}
} }

View File

@ -1,11 +1,5 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.WindowStretchMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.glVertexAttrib2f;
import static org.lwjgl.opengl.GL41.glProgramUniform2f;
//todo: remove //todo: remove
public class Scaler { public class Scaler {
/* /*

View File

@ -1,5 +1,8 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.Vec2i;
import static org.lwjgl.opengl.GL46.glUniform1f; import static org.lwjgl.opengl.GL46.glUniform1f;
import static org.lwjgl.opengl.GL46.glUniform2f; import static org.lwjgl.opengl.GL46.glUniform2f;
@ -59,4 +62,16 @@ public class ScalerAspect extends Filter {
(float)sizeRatio.getY() (float)sizeRatio.getY()
); );
} }
@Override
public Vec2i translateScreenCoordinate(Vec2i coord) {
var scale = Vec2f.div(input.getSize(), output.getSize());
var scalef = Math.max(scale.getX(), scale.getY());
var off = Vec2f.div(Vec2i.sub(output.getSize(),
Vec2f.mul(input.getSize().toVec2f(), 1.0 / scalef).toVec2i()
).toVec2f(), 2).toVec2i();
return Vec2i.mul(Vec2i.sub(coord, off), 1.0 / scalef);
}
} }

View File

@ -1,5 +1,7 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.Vec2i;
import static org.lwjgl.opengl.GL46.glUniform1f; import static org.lwjgl.opengl.GL46.glUniform1f;
import static org.lwjgl.opengl.GL46.glUniform2f; import static org.lwjgl.opengl.GL46.glUniform2f;
@ -61,4 +63,16 @@ public class ScalerInteger extends Filter {
(float)sizeRatio.getY() (float)sizeRatio.getY()
); );
} }
@Override
public Vec2i translateScreenCoordinate(Vec2i coord) {
var scale = Vec2i.div(output.getSize(), input.getSize());
var scalei = Math.max(1, Math.min(scale.getX(), scale.getY()));
var off = Vec2i.div(Vec2i.sub(output.getSize(),
Vec2i.mul(input.getSize(), scalei)
), 2);
return Vec2i.div(Vec2i.sub(coord, off), scalei);
}
} }

View File

@ -8,9 +8,7 @@ import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL46.*; import static org.lwjgl.opengl.GL46.*;
@ -28,7 +26,8 @@ public class Window extends BaseWindow {
private Vec2i windowSize; private Vec2i windowSize;
private final Pipeline2D pipeline2D; private final Pipeline2D pipeline2D;
private final Pipeline3D pipeline3D; private final Pipeline3D pipeline3D;
private final Filter scaler; private final Map<String, Filter> scalers;
//private final FilterFXAA fxaa; //private final FilterFXAA fxaa;
public Window(Vec2i size, String title, Engine engine) { public Window(Vec2i size, String title, Engine engine) {
@ -71,7 +70,13 @@ public class Window extends BaseWindow {
pipeline3D = new Pipeline3D(framebuffer, textureAtlas); pipeline3D = new Pipeline3D(framebuffer, textureAtlas);
//fxaa = new FilterFXAA(framebuffer); //fxaa = new FilterFXAA(framebuffer);
//scaler = new Scaler(framebuffer); //scaler = new Scaler(framebuffer);
scaler = new ScalerInteger(framebuffer); scalers = new HashMap<>();
scalers.put("stretch", new ScalerStretch(framebuffer));
scalers.put("aspect", new ScalerAspect(framebuffer));
scalers.put("integer", new ScalerInteger(framebuffer));
scaler = "aspect";
// = new ScalerInteger(framebuffer);
shouldClose = false; shouldClose = false;
@ -97,33 +102,7 @@ public class Window extends BaseWindow {
double[] y = {0.0}; double[] y = {0.0};
glfwGetCursorPos(window, x, y); glfwGetCursorPos(window, x, y);
var mousePos = new Vec2i((int) x[0], (int) y[0]); var mousePos = new Vec2i((int) x[0], (int) y[0]);
var scaledMousePos = scalers.get(scaler).translateScreenCoordinate(mousePos);
var scaledMousePos = switch(stretchMode) {
case STRETCH -> {
var stretch = Vec2f.div(size, windowSize);
yield Vec2i.mul(stretch, mousePos);
}
case ASPECT -> {
var scale = Vec2f.div(size, windowSize);
var scalef = Math.max(scale.getX(), scale.getY());
var off = Vec2f.div(Vec2i.sub(windowSize,
Vec2f.mul(size.toVec2f(), 1.0 / scalef).toVec2i()
).toVec2f(), 2).toVec2i();
yield Vec2i.mul(Vec2i.sub(mousePos, off), 1.0 / scalef);
}
case INTEGER -> {
var scale = Vec2i.div(windowSize, size);
var scalei = Math.max(1, Math.min(scale.getX(), scale.getY()));
var off = Vec2i.div(Vec2i.sub(windowSize,
Vec2i.mul(size, scalei)
), 2);
yield Vec2i.div(Vec2i.sub(mousePos, off), scalei);
}
};
return Vec2i.max(Vec2i.ZERO, return Vec2i.max(Vec2i.ZERO,
Vec2i.min(Vec2i.sub(size, new Vec2i(1, 1)), scaledMousePos) Vec2i.min(Vec2i.sub(size, new Vec2i(1, 1)), scaledMousePos)
); );
@ -203,8 +182,8 @@ public class Window extends BaseWindow {
public void runScaler() { public void runScaler() {
//fxaa.apply(); //fxaa.apply();
//scaler.setStretchMode(stretchMode); //scaler.setStretchMode(stretchMode);
scaler.apply(); scalers.get(scaler).apply();
scaler.getOutput().copyToScreen(); scalers.get(scaler).getOutput().copyToScreen();
} }
@Override @Override
@ -242,6 +221,10 @@ public class Window extends BaseWindow {
System.out.printf("new window size %d x %d\n", width, height); System.out.printf("new window size %d x %d\n", width, height);
windowSize = new Vec2i(width, height); windowSize = new Vec2i(width, height);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
scaler.setOutputSize(windowSize);
//todo: do we really wanna do it like this?
for(var entry : scalers.entrySet()) {
entry.getValue().setOutputSize(windowSize);
}
} }
} }