Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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: davedes (posted 2012-11-27 00:27:15, viewed 402 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  
import java.applet.Applet;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;

public class T extends Applet implements Runnable {

   int thread = 0;
   
   //TODO: remove these if not needed
   public static final Random RANDOM = new Random();
   public static final boolean[] keys = new boolean[32767];
   float mouseX, mouseY;
   
   
   public void start() {
      enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
      new Thread(this).start();
   }
   
   public void run() {
      // ---------------- SET UP AUDIO ---------------- //      
      SourceDataLine out = null;
      // -- AUDIO CONSTANTS -- //
      final int SAMPLE_RATE = 8000;
      final AudioFormat AUDIO_FORMAT = new AudioFormat(Encoding.PCM_SIGNED, SAMPLE_RATE, 8, 1, 1, SAMPLE_RATE, false);
      int BUFFER_SIZE = 1024;
      byte[] AUDIO_BUFFER = new byte[BUFFER_SIZE];
      
      try {
         out = AudioSystem.getSourceDataLine(AUDIO_FORMAT);
         out.open(AUDIO_FORMAT);
         
         //is this really necessary??
         FloatControl volume = (FloatControl) out.getControl(FloatControl.Type.MASTER_GAIN);
         volume.setValue(volume.getMaximum());
         out.start();
      } catch (Exception e) {
         out = null;
          e.printStackTrace(); //TODO: remove later
      }
      
      // ---------------- SET UP THREADS ---------------- //
      if (thread == 0) {
         thread = 1;
         //start music thread
         new Thread(this).start();
         //fall-through to video stuff
      } else if (thread == 1) {
         int t = 0; //TODO: remove if not needed
         //We are in music thread... 
         while (out != null) {
            //fill audio buffer with data... here's an example
            for (int i=0; i<AUDIO_BUFFER.length; i++, t++) {
                AUDIO_BUFFER[i] = (byte)( (t*(5+((t>>11)&5)))&(t>>7) );
            }
            
            //now play back the data
            out.write(AUDIO_BUFFER, 0, AUDIO_BUFFER.length);
         }
         // don't fall through to video thread
         return;
      }
      
      // ---------------- SET UP GRAPHICS ---------------- //      
      
      // we are now in the video thread
      
      final int WIDTH = 240;
      final int HEIGHT = 160;
      setSize(WIDTH, HEIGHT); // For AppletViewer, remove later.
      
      // Set up the graphics stuff, double-buffering.
      final BufferedImage screen = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
      final int[] pixels = ((DataBufferInt)screen.getRaster().getDataBuffer()).getData();
      final Graphics g = screen.getGraphics();
      final Graphics appletGraphics = getGraphics();

      // Some variables to use for the fps.
      int tick = 0, fps = 0, acc = 0;
      long lastTime = System.nanoTime();
      
      // ---------------- GAME LOOP ---------------- //      
      while (true) {
         long now = System.nanoTime();
         long delta = now - lastTime;
         acc += delta;
         tick++;
         if (acc >= 1000000000L) {
            acc -= 1000000000L;
            fps = tick;
            tick = 0;
         }

         // Update
         lastTime = now;
         
         //TODO: remove later if not needed
         g.clearRect(0, 0, WIDTH, HEIGHT);
         
         //If you want to do per-pixel modifications, it might look like this:
         for (int i=0; i<WIDTH*HEIGHT; i++) {
            int y = i / WIDTH;
            int x = i - WIDTH*y;
            
            // ---------------- per-pixel example... ---------------- //            
            float px = (2*(x/(float)WIDTH-.5f)) * (WIDTH/(float)HEIGHT);
            float py = (2*(y/(float)HEIGHT-.5f));
            
            float radius = 1f;
            //evaluate z-depth of sphere
            double z = Math.sqrt(radius*radius - px*px - py*py);
                        
            //evaluate normal of sphere
            double dist = Math.sqrt(px * px + py * py + z * z);
            px/=dist; py/=dist; z/=dist;
            
            //centered mouse position
            float mx = mouseX/(float)WIDTH-.5f, my = mouseY/(float)HEIGHT-.5f;
            
            //normalize light direction
            dist = Math.sqrt(mx*mx+my*my+1);
            mx/=dist; my/=dist;
            
            //N dot L and clamp
            double c = Math.max(0f, px*mx + py*my + z*(1/dist));
            
            //place pixel
            int C = (int)(c*255);
            pixels[i] = (C << 16) | (C << 8) | C;
         }
         
         //TODO: remove later
         g.setColor(Color.white);
         g.drawString("FPS " + String.valueOf(fps), 20, 30);

         // Draw the entire results on the screen.
         appletGraphics.drawImage(screen, 0, 0, WIDTH, HEIGHT, null);
         
         do {
            Thread.yield();
         } while (System.nanoTime() - lastTime < 0);
         
         if (!isActive()) {
            return;
         }
      }
   }
   
   public void processEvent(AWTEvent e)
    {
        boolean down = false;
        switch (e.getID())
        {
            case KeyEvent.KEY_PRESSED:
                down = true;
            case KeyEvent.KEY_RELEASED:
               keys[((KeyEvent) e).getKeyCode()] = down;
                break;
            case MouseEvent.MOUSE_PRESSED:
                down = true;
            case MouseEvent.MOUSE_RELEASED:
                keys[((MouseEvent) e).getButton()] = down;
            case MouseEvent.MOUSE_MOVED:
            case MouseEvent.MOUSE_DRAGGED:
                mouseX = ((MouseEvent) e).getX();
                mouseY = ((MouseEvent) e).getY();
        }
    }
}





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.

toopeicgaming1999 (46 views)
2014-11-26 15:22:04

toopeicgaming1999 (39 views)
2014-11-26 15:20:36

toopeicgaming1999 (8 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (25 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
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!