Advertisement
juliand665

Renderer.java

Jan 19th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package src;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4.  
  5. import java.awt.Font;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10.  
  11. import org.lwjgl.input.Mouse;
  12. import org.lwjgl.opengl.GL11;
  13. import org.newdawn.slick.Color;
  14. import org.newdawn.slick.TrueTypeFont;
  15. import org.newdawn.slick.opengl.Texture;
  16. import org.newdawn.slick.opengl.TextureLoader;
  17.  
  18. public class Renderer
  19. {
  20.  
  21.     public Art art;
  22.     private Metamorph mm;
  23.     public static final String MAINFONT = "Smudger LET";
  24.    
  25.     public Renderer(Art a)
  26.     {
  27.         art = a;
  28.     }
  29.    
  30.     public void render()
  31.     {
  32.         mm = Metamorph.instance;
  33.        
  34.         if(Metamorph.instance.currentScreen == EnumScreens.MAIN)
  35.         {
  36.             renderMainMenuWithGL();
  37.         }
  38.        
  39.         if(Metamorph.instance.currentScreen == EnumScreens.GAME)
  40.         {
  41.             renderRunningGameWithGL();
  42.         }
  43.        
  44.         //System.out.printf("Rendering ");
  45.     }
  46.  
  47.     private void renderMainMenuWithGL()
  48.     {
  49.         //System.out.println("Main Menu!");
  50.        
  51.         glColor4f(1, 1, 1, 1);
  52.        
  53.         try
  54.         {
  55.             Texture bg = loadTexture("mockery");
  56.             bg.bind();
  57.         } catch (Exception e)
  58.         {
  59.             e.printStackTrace();
  60.         }
  61.        
  62.         //drawQuad(0, 0, 1280, 720, 0, 0, 1280, 720);
  63.         glPushMatrix();
  64.         {
  65.             glBegin(GL_QUADS);
  66.             glTexCoord2f(0, 0);glVertex2f(0, 0);
  67.             glTexCoord2f(0, 1);glVertex2f(0, 720);
  68.             glTexCoord2f(1, 1);glVertex2f(1280, 720);
  69.             glTexCoord2f(1, 0);glVertex2f(1280, 0);
  70.             glEnd();
  71.         }
  72.         glPopMatrix();
  73.        
  74.         TrueTypeFont f = loadFont(MAINFONT, Font.PLAIN, 50);
  75.         TrueTypeFont fb = loadFont(MAINFONT, Font.PLAIN, 48);
  76.  
  77.         int sel = -1;
  78.         if(Mouse.getX() > 1000 && Mouse.getX() < 1240 && Mouse.getY() > 282.5F && Mouse.getY()< 737.5F)
  79.             sel = Math.round((Mouse.getY() - 337.5F)/75F);
  80.        
  81.         if(sel == 0)
  82.             drawStringRight(fb, 1240, 350, "Story", new Color(0xff516b6b));
  83.         else
  84.             drawStringRight(f, 1240, 350, "Story", new Color(0xff516b6b));
  85.     }
  86.    
  87.     private void renderRunningGameWithGL()
  88.     {
  89.        
  90.     }
  91.    
  92.     private void drawQuad(float drawX, float drawY, float drawX2, float drawY2,
  93.             float srcX, float srcY, float srcX2, float srcY2) {
  94.         float DrawWidth = drawX2 - drawX;
  95.         float DrawHeight = drawY2 - drawY;
  96.         float TextureSrcX = srcX / 1280;
  97.         float TextureSrcY = srcY / 720;
  98.         float SrcWidth = srcX2 - srcX;
  99.         float SrcHeight = srcY2 - srcY;
  100.         float RenderWidth = (SrcWidth / 1280);
  101.         float RenderHeight = (SrcHeight / 720);
  102.  
  103.         GL11.glTexCoord2f(TextureSrcX, TextureSrcY);
  104.         GL11.glVertex2f(drawX, drawY);
  105.         GL11.glTexCoord2f(TextureSrcX, TextureSrcY + RenderHeight);
  106.         GL11.glVertex2f(drawX, drawY + DrawHeight);
  107.         GL11.glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY + RenderHeight);
  108.         GL11.glVertex2f(drawX + DrawWidth, drawY + DrawHeight);
  109.         GL11.glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY);
  110.         GL11.glVertex2f(drawX + DrawWidth, drawY);
  111.     }
  112.  
  113.     public static Texture loadTexture(String key)
  114.     {
  115.         try
  116.         {
  117.             return TextureLoader.getTexture("png", new FileInputStream(new File("res/images/" + key + ".png")));
  118.         } catch (Exception e)
  119.         {
  120.             e.printStackTrace();
  121.         }
  122.         return null;
  123.     }
  124.    
  125.     private void drawStringCentered(TrueTypeFont f, int x, int y, String s, Color c)
  126.     {
  127.         glPushMatrix();
  128.         f.drawString(x-f.getWidth(s)/2, y, s, c);
  129.         glPopMatrix();
  130.     }
  131.    
  132.     private void drawStringRight(TrueTypeFont f, int x, int y, String s, Color c)
  133.     {
  134.         glPushMatrix();
  135.         f.drawString(x-f.getWidth(s), y, s, c);
  136.         glPopMatrix();
  137.     }
  138.    
  139.     private TrueTypeFont loadFont(String name, int style, int size)
  140.     {
  141.         Font f = new Font(name, style, size);
  142.         return new TrueTypeFont(f, false);
  143.     }
  144.    
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement