basic font rendering implemented, and alpha blending

This commit is contained in:
DaniTheSkunk 2022-09-20 06:58:36 +02:00
parent 3edbc36844
commit 07ced1f430
6 changed files with 32 additions and 2 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

@ -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

@ -41,6 +41,8 @@ class GLWindow extends BaseWindow {
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

@ -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);
}