added aspect stretch and dumb stretch mode to window

This commit is contained in:
DaniTheSkunk 2022-11-05 23:54:24 +00:00
parent 87c6bd90e9
commit bc706369d0
4 changed files with 55 additions and 10 deletions

View File

@ -8,9 +8,11 @@ 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;
public BaseWindow(Engine engine) { public BaseWindow(Engine engine) {
this.engine = engine; this.engine = engine;
this.stretchMode = WindowStretchMode.ASPECT;
} }
@Override @Override
@ -121,4 +123,10 @@ abstract public class BaseWindow implements IWindow {
return loadNineSlice(engine.loadImage(path)); return loadNineSlice(engine.loadImage(path));
} }
@Override
public void setStretchMode(WindowStretchMode mode) {
this.stretchMode = mode;
}
} }

View File

@ -35,5 +35,7 @@ public interface IWindow {
boolean shouldClose(); boolean shouldClose();
void setStretchMode(WindowStretchMode mode);
void tick(); void tick();
} }

View File

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

View File

@ -8,7 +8,6 @@ import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image; import com.danitheskunk.skunkworks.gfx.Image;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWWindowCloseCallbackI;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@ -225,17 +224,48 @@ public class Window extends BaseWindow {
windowSize.getY() windowSize.getY()
); );
int scalex = windowSize.getX() / size.getX(); int tlx1 = 0;
int scaley = windowSize.getY() / size.getY(); int tly1 = 0;
int scale = Math.max(1, Math.min(scalex, scaley)); int tlx2 = 0;
int tly2 = 0;
int xoff = (windowSize.getX() - size.getX() * scale) / 2; switch(this.stretchMode) {
int yoff = (windowSize.getY() - size.getY() * scale) / 2; case STRETCH: {
tlx1 = 0;
tly1 = 0;
tlx2 = windowSize.getX();
tly2 = windowSize.getY();
break;
}
case ASPECT: {
float scalex = (float) windowSize.getX() / (float) size.getX();
float scaley = (float) windowSize.getY() / (float) size.getY();
float scale = Math.min(scalex, scaley);
int tlx1 = xoff; int xoff = (int)((windowSize.getX() - size.getX() * scale) / 2);
int tly1 = yoff; int yoff = (int)((windowSize.getY() - size.getY() * scale) / 2);
int tlx2 = xoff + size.getX() * scale;
int tly2 = yoff + size.getY() * scale; tlx1 = xoff;
tly1 = yoff;
tlx2 = (int)(xoff + size.getX() * scale);
tly2 = (int)(yoff + size.getY() * scale);
break;
}
case INTEGER: {
int scalex = windowSize.getX() / size.getX();
int scaley = windowSize.getY() / size.getY();
int scale = Math.max(1, Math.min(scalex, scaley));
int xoff = (windowSize.getX() - size.getX() * scale) / 2;
int yoff = (windowSize.getY() - size.getY() * scale) / 2;
tlx1 = xoff;
tly1 = yoff;
tlx2 = xoff + size.getX() * scale;
tly2 = yoff + size.getY() * scale;
break;
}
}
var texCoordIndex = programScaler.getAttribLocation("texCoord"); var texCoordIndex = programScaler.getAttribLocation("texCoord");