Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: hvince95 (posted 2012-10-09 02:23:10, viewed 184 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223  
import static org.lwjgl.opengl.EXTFramebufferObject.*;
import static org.lwjgl.opengl.GL11.*;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

public class Main {
   
   public Main() {
      
      try {
         Display.setDisplayMode(new DisplayMode(960, 600));
         Display.setTitle("Test Program");
         Display.setResizable(true);
         Display.create();
      }  catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
      }
      
      initGL();
      
      boolean gameRunning = true;
      FrameBuffer buffer = new FrameBuffer(960, 600);
      buffer.renderBox();
      
      while (gameRunning) {
         
         if (Display.isCloseRequested()) {
            gameRunning = false;
            break;
         }
         
         glClear(GL_COLOR_BUFFER_BIT);
         buffer.renderToDisplay();
         Display.update();
         
         try {
            Thread.sleep(10);
            System.out.println("sdfk");
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
            
      Display.destroy();
      System.exit(0);
   }
   
   private void mainGameProcess() {
      switch (Config.gameState) {
      case INTRO:
         break;
      case MAIN_MENU:
         break;
      case GAME:
         //Tutorials myClass = new Tutorials();
         TerrainF myClass = new TerrainF();
         myClass.startGameLoop();
         break;
      }
      
   }
   
   private void initGL() {      
      
      // Requirements
      if(!GLContext.getCapabilities().GL_EXT_framebuffer_object)
         throw new Error("FBOs not supported! Get a better computer!");
      
      // Defaults
      GL11.glDisable(GL11.GL_DEPTH_BUFFER_BIT);
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glEnable(GL11.GL_BLEND);//for transperancy
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);//for transperancy
      
      // Initialise OpenGL Projection for 2D
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, Config.SCREEN_WIDTH, 0, Config.SCREEN_HEIGHT, -1, 1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      
   }
   
   public static void main (String[] args) {
      new Main();
   }
   
   public class FrameBuffer {
      
      private int bufferWidth;
      private int bufferHeight;
      
      private int textureID;
      private int frameBufferID;
      
      public FrameBuffer(int width, int height) {
         
         bufferWidth = width;
         bufferHeight = height;
         
         if (!GLContext.getCapabilities().GL_EXT_framebuffer_object) {
               System.out.println("FBO not supported!!!");
               System.exit(0);
         } else {
            System.out.println("FBO is supported!!!");
         }
         
         initFBO();
         
      }
      
      public void renderToDisplay() {
         
         glEnable(GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, frameBufferID);
            
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            
         glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2i(0, 0);
            
            glTexCoord2f(1, 0);
            glVertex2i(Config.SCREEN_WIDTH, 0);

            glTexCoord2f(1, 1);;
            glVertex2i(Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT);
               
            glTexCoord2f(0, 1);
            glVertex2i(0, Config.SCREEN_HEIGHT);
         glEnd();
         
      }
      
      public void renderBox() {

         bind();
         
         glDisable(GL_TEXTURE_2D);
         glDisable(GL_BLEND);
         
         glDisable(GL_POINT_SMOOTH);
         glPointSize(2.0f);
         glBegin(GL_POINTS);
            glColor4ub((byte)0, (byte)255, (byte)255, (byte)255);
            for (int x = 0; x < 320; x++) {
               for (int y = 0; y < 200; y ++) {
                  glVertex2i(x * 2 + 1, y * 2 + 1);
               }
            }
         glEnd();
         
         glBegin(GL_QUADS);
            glColor4ub((byte)255, (byte)255, (byte)0, (byte)255);
            glVertex2i(100, 100);
            glVertex2i(100, 150);
            glVertex2i(150, 150);
            glVertex2i(150, 100);
         glEnd();
         unBind();
         
      }
      
      public void bind() {
         glBindTexture(GL_TEXTURE_2D, 0);
         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferID);
         glPushAttrib(GL_VIEWPORT_BIT);
         glViewport( 0, 0, bufferWidth, bufferHeight);
         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
         glClear(GL_COLOR_BUFFER_BIT);
         
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         
         glOrtho(0, bufferWidth, 0, bufferHeight, -1, 1);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
         
         glClear(GL_COLOR_BUFFER_BIT);
      }
      
      public void unBind() {
         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
         glPopAttrib();
         
         glViewport(0, 0, bufferWidth, bufferHeight);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glOrtho(0, Config.SCREEN_WIDTH, 0, Config.SCREEN_HEIGHT, -1, 1);
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
      }
      
      private void initFBO() {
         
         textureID = glGenTextures();
         glBindTexture(GL_TEXTURE_2D, textureID);
         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0,GL_RGBA, GL_INT, (ByteBuffer) null);
         
         IntBuffer buffer = ByteBuffer.allocateDirect(1*4).order(ByteOrder.nativeOrder()).asIntBuffer(); // allocate a 1 int byte buffer
         glGenFramebuffersEXT(buffer); // generate
         frameBufferID = buffer.get();
         
         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBufferID);
         glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureID, 0);
         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
         
      }

   }


}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (36 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (27 views)
2014-10-15 16:18:58

TehJavaDev (52 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (42 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (77 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!