Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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: theagentd (posted 2014-03-29 16:56:17, viewed 106 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   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310  
package test.net.indiespot.struct;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.Random;

import javax.swing.JOptionPane;

import net.indiespot.struct.cp.ForceUninitializedMemory;
import net.indiespot.struct.cp.StructField;
import net.indiespot.struct.cp.StructType;
import net.indiespot.struct.runtime.StructMemory;
import net.mokyu.threading.DrawTask;
import net.mokyu.threading.GameExecutor;
import net.mokyu.threading.MultithreadedExecutor;
import net.mokyu.threading.SingleThreadExecutor;
import net.mokyu.threading.SplitTask;
import net.mokyu.threading.TaskTree;
import net.mokyu.threading.TaskTreeBuilder;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL31.*;
import static org.lwjgl.opengl.GL44.*;

import org.lwjgl.util.Timer;

import test.net.indiespot.struct.StructUtil;

/**
 * Old LWJGL MappedObject version. 
 *
*/

public class ParticleTestStruct {

    private static final int WIDTH = 1600, HEIGHT = 900;
    private static final int PARTICLE_BYTE_SIZE = 28;
    private static final int NUM_COPY_VBOS = 3;
    
    private static final float spawnX = WIDTH / 2f, spawnY = HEIGHT * 3 / 4f;
    private static final float airResistance = 0.99f;
    private static final float launchX = 35, launchY = 20;
    private static final float gravity = 0.05f;
    
    private static int NUM_PARTICLES = 3000000;
    private static final int lifeTime = 200;
    
    private static final float particlePointSize = 1;
    private static final int defaultThreads = 8;
    
    private boolean keyPressed = false;
    private GameExecutor executor;
    private TaskTree taskTree;
    private int threads;
    private int particlesPerSubtask;
    
    private int persistentVBO;
    private ByteBuffer persistentBuffer;
    private Particle[] particleStructArray;
    
    private int[] copyVBOs;
    private int currentCopyVBO;
    
    private Random random;
    

    public static void main(String[] args) {
        new ParticleTestStruct().gameloop();
    }

    public ParticleTestStruct() {
        try {
            //Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
            Display.setVSyncEnabled(false);
            Keyboard.create();
        } catch (LWJGLException ex) {
            ex.printStackTrace();
        }

        glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);

        init(defaultThreads);
    }

    private void init(int numThreads) {
        threads = numThreads;
        particlesPerSubtask = NUM_PARTICLES / numThreads;
        
        /*if(bufferID != 0){
           glDeleteBuffers(bufferID);
           buffer = null;
        }

        if (executor != null && executor instanceof MultithreadedExecutor) {
            ((MultithreadedExecutor) executor).close();
        }*/

        if (threads == 1) {
            executor = new SingleThreadExecutor();
        } else {
            executor = new MultithreadedExecutor(numThreads);
        }
        TaskTreeBuilder builder = new TaskTreeBuilder();
        builder.addTask(new UpdateTask());
        builder.addTask(new RenderTask());
        taskTree = builder.build();

        persistentVBO = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, persistentVBO);
      glBufferStorage(GL_ARRAY_BUFFER, PARTICLE_BYTE_SIZE * NUM_PARTICLES, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
      persistentBuffer = glMapBufferRange(GL_ARRAY_BUFFER, 0, PARTICLE_BYTE_SIZE * NUM_PARTICLES, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT, null);
      StructMemory.alignBufferToWord(persistentBuffer);
      
      
      copyVBOs = new int[NUM_COPY_VBOS];
      for(int i = 0; i < NUM_COPY_VBOS; i++){
         int vbo = glGenBuffers();
           glBindBuffer(GL_ARRAY_BUFFER, vbo);
         glBufferStorage(GL_ARRAY_BUFFER, PARTICLE_BYTE_SIZE * NUM_PARTICLES, 0);
         copyVBOs[i] = vbo;
      }
      currentCopyVBO = 0;
      
      
      
      random = new Random();

      particleStructArray = StructUtil.map(Particle.class, persistentBuffer);
      for(int i = 0; i < NUM_PARTICLES; i++){
         particleStructArray[i].init(
               spawnX, spawnY, 
               (random.nextFloat() - 0.5f) * launchX, -random.nextFloat() * launchY,
               (byte)random.nextInt(256), (byte)random.nextInt(256), (byte)random.nextInt(256),
               random.nextFloat() * lifeTime / 60f
         );
      }
    }

    public void gameloop() {

        Timer timer = new Timer();

        while (!Display.isCloseRequested()) {

            executor.run(taskTree);

            Timer.tick();
            if (timer.getTime() >= 1) {
                timer.reset();
                System.out.println("\n//////////////////////");
                //System.out.println("FPS: " + fps.getCurrentFPS());
                System.out.println("Particles: " + NUM_PARTICLES);
                System.out.println("Threads: " + threads + " / " + Runtime.getRuntime().availableProcessors());
            }
        }
    }

    private class UpdateTask extends SplitTask {

        public UpdateTask() {
            super(0, 0, threads);
        }

        @Override
        protected void runSubtask(int subtask) {
           
           int start = particlesPerSubtask*subtask;
           int end = Math.min(particlesPerSubtask*(subtask+1), NUM_PARTICLES);
           
            for(int i = start; i < end; i++){
               particleStructArray[i].update(1);
            }
        }

        @Override
        public void finish() {
        }
    }
    
    public class RenderTask extends DrawTask {

        public RenderTask() {
            super(1, 0);
            addRequiredTask(0);
        }

      @Override
      protected void run() {
         glBindBuffer(GL_COPY_READ_BUFFER, persistentVBO);
         glBindBuffer(GL_COPY_WRITE_BUFFER, copyVBOs[currentCopyVBO]);
         glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, NUM_PARTICLES * PARTICLE_BYTE_SIZE);

         
           glClearColor(0, 0, 0, 0);
           glClear(GL_COLOR_BUFFER_BIT);

           glEnable(GL_POINT_SMOOTH);
           glEnable(GL_BLEND);
           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

           glPointSize(particlePointSize);

         
           
           glBindBuffer(GL_ARRAY_BUFFER, copyVBOs[currentCopyVBO]);
         
           glEnableClientState(GL_VERTEX_ARRAY);
           glEnableClientState(GL_COLOR_ARRAY);
           
           glVertexPointer(2, GL_FLOAT, PARTICLE_BYTE_SIZE, 0);
           glColorPointer(4, GL_UNSIGNED_BYTE, PARTICLE_BYTE_SIZE, 16);

           glDrawArrays(GL_POINTS, 0, NUM_PARTICLES);

           Display.update();
           
           currentCopyVBO = (currentCopyVBO + 1) % NUM_COPY_VBOS;
           
           
            boolean newKey = false;
            for (int i = 0; i < 10; i++) {
                int keyToCheck = Keyboard.KEY_1 + i;
                if (i == 9) {
                    keyToCheck = Keyboard.KEY_0;
                    i = 15;
                }
                if (Keyboard.isKeyDown(keyToCheck)) {
                    newKey = true;
                    if (!keyPressed) {
                        init(i + 1);
                        System.out.println("NUMBER OF THREADS CHANGED TO " + (i + 1));
                        keyPressed = true;
                    }
                    break;
                }
            }
            if (!newKey) {
                keyPressed = false;
            }
      }
    }

    

   @StructType(sizeof = PARTICLE_BYTE_SIZE)
   @ForceUninitializedMemory
    private class Particle {


        @StructField(offset =  0) private float x;
        @StructField(offset =  4) private float y;
        @StructField(offset =  8) private float vx;
        @StructField(offset = 12) private float vy;
        
        @StructField(offset = 16) private byte r;
        @StructField(offset = 17) private byte g;
        @StructField(offset = 18) private byte b;
        @StructField(offset = 19) private byte a;
        
        @StructField(offset = 20) private float lifeLeft; 
        @StructField(offset = 24) private float totalLife;

        public void init(float x, float y, float vx, float vy, byte r, byte g, byte b, float life) {
           this.x = x;
           this.y = y;
           this.vx = vx;
           this.vy = vy;
           
           this.r = r;
           this.g = g;
           this.b = b;
           
           totalLife = lifeLeft = life;
        }

        public void update(int updates) {
           for(int i = 0; i < updates; i++){
              
                vy += gravity;
                
                vx *= airResistance;
                vy *= airResistance;
                
                x += vx;
                y += vy;
                
                a = (byte)(255 * lifeLeft / totalLife);
                
               if ((vx < 0 && x < 0) || (vx > 0 && x > WIDTH)) {
                   vx = -vx;
               }
               if ((vy < 0 && y < 0) || (vy > 0 && y > HEIGHT)) {
                   vy = -vy;
               }
               
               lifeLeft -= 1/60f;
           }
        }
    }
}





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.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (65 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!