Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.nio.ByteBuffer;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.GL12;
- import fr.openrp_cl.main.Main;
- import static org.lwjgl.opengl.GL11.*;
- public class Render{
- public static int loadTexture(BufferedImage Image){
- 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() * 4);
- 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));
- Buffer.put((byte) ((Pixel >> 8) & 0xFF));
- Buffer.put((byte) (Pixel & 0xFF));
- Buffer.put((byte) ((Pixel >> 24) & 0xFF));
- }
- }
- Buffer.flip();
- int textureID = glGenTextures();
- glBindTexture(GL_TEXTURE_2D, textureID);
- 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);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Image.getWidth(), Image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Buffer);
- glBindTexture(GL_TEXTURE_2D, 0);
- return textureID;
- }
- public static void drawLine(float x, float y, float x2, float y2){
- glBegin(GL_LINES);
- glVertex2f(x, y);
- glVertex2f(x2, y2);
- glEnd();
- }
- public static void drawQuad(float x, float y, float width, float height){
- glBegin(GL_TRIANGLES);
- glVertex2f(x + width, y);
- glVertex2f(x, y);
- glVertex2f(x, y + height);
- glVertex2f(x, y + height);
- glVertex2f(x + width, y + height);
- glVertex2f(x + width, y);
- glEnd();
- }
- public static void drawTexturedQuad(int textureID, float x, float y, float width, float height, float alpha){
- glLoadIdentity();
- glPushMatrix();
- glBindTexture(GL_TEXTURE_2D, textureID);
- glColor4f(1, 1, 1, alpha);
- glBegin(GL_TRIANGLES);
- glTexCoord2f(1, 0);
- glVertex2f(x + width, y);
- glTexCoord2f(0, 0);
- glVertex2f(x, y);
- glTexCoord2f(0, 1);
- glVertex2f(x, y + height);
- glTexCoord2f(0, 1);
- glVertex2f(x, y + height);
- glTexCoord2f(1, 1);
- glVertex2f(x + width, y + height);
- glTexCoord2f(1, 0);
- glVertex2f(x + width, y);
- glEnd();
- glLoadIdentity();
- glPopAttrib();
- glPushMatrix();
- glBindTexture(GL_TEXTURE_2D, 0);
- glColor4f(1, 1, 1, 1);
- }
- public static void drawTexturedQuad(int textureID, float x, float y, float width, float height){
- drawTexturedQuad(textureID, x, y, width, height, 1);
- }
- public static void drawColoredQuad(float x, float y, float width, float height, float r, float g, float b, float alpha){
- glColor4f(r, g, b, alpha);
- drawQuad(x, y, width, height);
- glColor4f(1, 1, 1, 1);
- }
- public static void drawColoredQuad(float x, float y, float width, float height, float r, float g, float b){
- drawColoredQuad(x, y, width, height, r, g, b, 1);
- }
- public static void drawString(float x, float y, String String, Font Font, int r, int g, int b, int alpha){
- BufferedImage Image = new BufferedImage(Main.Width, Main.Height, BufferedImage.TYPE_INT_ARGB);
- Graphics2D Graphics2D = Image.createGraphics();
- Graphics2D.setFont(Font);
- Graphics2D.setColor(new Color(r, g, b, alpha));
- Graphics2D.drawString(String, x, y);
- Graphics2D.dispose();
- int textureID = loadTexture(Image);
- drawTexturedQuad(textureID, 0, 0, Main.Width, Main.Height);
- glDeleteTextures(textureID);
- }
- public static void drawString(float x, float y, String String, Font Font, int r, int g, int b){
- drawString(x, y, String, Font, r, g, b, 1);
- }
- public static void drawString(float x, float y, String String, Font Font){
- drawString(x, y, String, Font, 1, 1, 1, 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment