Compare commits

..

2 Commits

Author SHA1 Message Date
DaniTheSkunk 07ced1f430 basic font rendering implemented, and alpha blending 2022-09-20 06:58:36 +02:00
DaniTheSkunk 3edbc36844 implemented FontTileset 2022-09-20 05:15:31 +02:00
10 changed files with 90 additions and 5 deletions

BIN
EGA8x14.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

@ -7,9 +7,10 @@ public class Test {
var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png");
var tex2 = window.loadTexture("C:\\art\\pixel stuff.png");
var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16));
var font = window.loadFontTileset("EGA8x14.png");
//img.drawImage(img2, Vec2i.ZERO);
var tex = window.loadTexture(img);
window.setDebug(true);
window.setDebug(false);
while(!window.shouldClose()) {
window.tick();
@ -27,6 +28,8 @@ public class Test {
new Recti(new Vec2i(400, 400), new Vec2i(16, 16)),
tileset.get(0x30)
);
renderContext.drawString(new Vec2i(100, 100), "hello world mew", font);
window.renderFinish(renderContext);
}
}

View File

@ -0,0 +1,24 @@
package com.danitheskunk.skunkworks;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
abstract class BaseRenderContext implements IRenderContext {
public void drawString(Vec2i pos, String string, IFont font) {
int x = pos.getX();
int y = pos.getY();
IntBuffer buf;
try {
buf = ByteBuffer.wrap(string.getBytes("UTF-32")).asIntBuffer();
} catch(UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
for(int i = 0; i < string.length(); ++i) {
int ch = buf.get(i);
var tex = font.getTexture(ch);
drawTextureRectangle(new Recti(new Vec2i(x, y), tex.getSize()), tex);
x += font.getCharWidth(ch);
}
}
}

View File

@ -0,0 +1,17 @@
package com.danitheskunk.skunkworks;
abstract class BaseWindow implements IWindow {
protected final Engine engine;
BaseWindow(Engine engine) {
this.engine = engine;
}
public IFont loadFontTileset(String path) {
var img = engine.loadImage(path);
var charSize = Vec2i.div(img.getSize(), 16);
var tex = loadTextureArray(img, charSize);
assert tex.size() == 256;
return new FontTileset(tex);
}
}

View File

@ -0,0 +1,31 @@
package com.danitheskunk.skunkworks;
import java.util.List;
class FontTileset implements IFont {
private List<ITexture> textures;
private Vec2i charSize;
FontTileset(List<ITexture> textures) {
this.textures = textures;
this.charSize = textures.get(0).getSize();
}
@Override
public int getCharWidth(int ch) {
return charSize.getX();
}
@Override
public int getLineHeight(int ch) {
return charSize.getY();
}
@Override
public ITexture getTexture(int ch) {
if(ch >= 256 || ch < 0) {
ch = 0;
}
return textures.get(ch);
}
}

View File

@ -2,7 +2,7 @@ package com.danitheskunk.skunkworks;
import static org.lwjgl.opengl.GL11.*;
class GLRenderContext implements IRenderContext{
class GLRenderContext extends BaseRenderContext implements IRenderContext{
private final GLTextureAtlas atlas;
public GLRenderContext(GLTextureAtlas atlas) {

View File

@ -11,9 +11,8 @@ import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
class GLWindow implements IWindow {
class GLWindow extends BaseWindow {
private boolean debug;
private final Engine engine;
private final GLRenderContext renderContext;
private boolean shouldClose;
private final Vec2i size;
@ -21,12 +20,12 @@ class GLWindow implements IWindow {
private final long window;
public GLWindow(Vec2i size, String title, Engine engine) {
super(engine);
GLFWErrorCallback.createPrint(System.err).set();
if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
this.debug = false;
this.engine = engine;
this.size = size;
window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
@ -42,6 +41,8 @@ class GLWindow implements IWindow {
glOrtho(0.0f, size.getX(), size.getY(), 0.0f, 0.0f, 1.0f);
glEnable(GL_COLOR);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
textureAtlas = new GLTextureAtlas();
renderContext = new GLRenderContext(textureAtlas);

View File

@ -0,0 +1,7 @@
package com.danitheskunk.skunkworks;
public interface IFont {
int getCharWidth(int ch);
int getLineHeight(int ch);
ITexture getTexture(int ch);
}

View File

@ -2,6 +2,7 @@ package com.danitheskunk.skunkworks;
public interface IRenderContext {
void drawRectangle(Recti rect, Color color);
void drawString(Vec2i pos, String string, IFont font);
void drawTextureRectangle(Recti rect, ITexture texture);
void drawTextureRectangle(Recti rect, ITexture texture, Color color);
}

View File

@ -3,6 +3,7 @@ package com.danitheskunk.skunkworks;
import java.util.List;
public interface IWindow {
IFont loadFontTileset(String path);
ITexture loadTexture(Image image);
ITexture loadTexture(String path);
List<ITexture> loadTextureArray(Image image, Vec2i tileSize);