Bug fixes and add Label
This commit is contained in:
parent
60f16efe7f
commit
8272b153ff
12 changed files with 481 additions and 463 deletions
|
@ -2,6 +2,7 @@ package globalgamejam.render;
|
|||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL12.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
|
@ -9,20 +10,82 @@ import java.nio.*;
|
|||
import javax.imageio.*;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
/**
|
||||
* Class created by MrDev023 (Florian RICHER) on 14/01/2017
|
||||
*/
|
||||
public class Texture {
|
||||
|
||||
int width, height;
|
||||
private static final int BYTES_PER_PIXEL = 4;//3 for RGB, 4 for RGBA
|
||||
public int width, height;
|
||||
int id;
|
||||
|
||||
public Texture(int width,int height,int id){
|
||||
System.out.println("Texture loaded ! " + width + "x" + height + " id:" + id);
|
||||
this.id = id;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public static Texture loadFont(String text, Color color, String font, int size){
|
||||
Font f_font = new Font(font, Font.PLAIN, size);
|
||||
|
||||
// to get the width of the text
|
||||
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
|
||||
FontMetrics fm = img.getGraphics().getFontMetrics(f_font);
|
||||
|
||||
int width = fm.stringWidth(text);
|
||||
int height = fm.getHeight();
|
||||
|
||||
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
// makeTransparent(image);
|
||||
|
||||
Graphics g = image.getGraphics();
|
||||
g.setFont(f_font);
|
||||
g.setColor(color);
|
||||
g.drawString(text, 0, size);
|
||||
g.dispose();
|
||||
|
||||
int[] pixels = new int[image.getWidth() * image.getHeight()];
|
||||
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
|
||||
|
||||
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
|
||||
|
||||
for(int y = 0; y < image.getHeight(); y++){
|
||||
for(int x = 0; x < image.getWidth(); x++){
|
||||
int pixel = pixels[y * image.getWidth() + x];
|
||||
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
|
||||
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
|
||||
buffer.put((byte) (pixel & 0xFF)); // Blue component
|
||||
buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
|
||||
}
|
||||
}
|
||||
|
||||
buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
|
||||
|
||||
// You now have a ByteBuffer filled with the color data of each pixel.
|
||||
// Now just create a texture ID and bind it. Then you can load it using
|
||||
// whatever OpenGL method you want, for example:
|
||||
|
||||
int textureID = glGenTextures(); //Generate texture ID
|
||||
glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
|
||||
|
||||
//Setup wrap mode
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
|
||||
|
||||
//Setup texture scaling filtering
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
//Send texel data to OpenGL
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
return new Texture(image.getWidth(),image.getHeight(),textureID);
|
||||
}
|
||||
|
||||
public static Texture loadTexture(String path){
|
||||
try {
|
||||
|
@ -60,8 +123,6 @@ public class Texture {
|
|||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
System.out.println("Texture loaded ! " + width + "x" + height + " id:" + id);
|
||||
|
||||
return new Texture(width, height, id);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -82,6 +143,7 @@ public class Texture {
|
|||
}
|
||||
|
||||
public void bind(){
|
||||
if(!glIsEnabled(GL_TEXTURE_2D))glEnable(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue