Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 2013-07-18 13:37:19, viewed 316 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  
/**
 * Copyright (c) 2012, Matt DesLauriers All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, this
 *     list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary
 *     form must reproduce the above copyright notice, this list of conditions and
 *     the following disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *
 *   * Neither the name of the Matt DesLauriers nor the names
 *     of his contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 *   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *   POSSIBILITY OF SUCH DAMAGE.
 */
package mdesl.test;

import static org.lwjgl.opengl.GL11.glClearColor;

import java.io.IOException;

import mdesl.graphics.Color;
import mdesl.graphics.SpriteBatch;
import mdesl.graphics.Texture;
import mdesl.graphics.TextureRegion;
import mdesl.graphics.text.BitmapFont;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

public class SpriteBatchTest extends SimpleGame {

   public static void main(String[] args) throws LWJGLException {
      Game game = new SpriteBatchTest();
      game.setDisplayMode(640, 480, false);
      game.start();
   }

   Texture tex, tex2;
   TextureRegion tile;
   SpriteBatch batch;
   
   float panX, panY, rot, zoom=1f;
   BitmapFont font;
   final float MOVE_SPEED = 10f;
   final float ZOOM_SPEED = 0.025f;
   final float ROT_SPEED = 0.05f;
   
   protected void create() throws LWJGLException {
      super.create();

      //Load some textures
      try {
         tex = new Texture(Util.getResource("res/tiles.png"), Texture.NEAREST);
         tex2 = new Texture(Util.getResource("res/ptsans_00.png"));
         tile = new TextureRegion(tex, 128, 64, 64, 64);
         
         font = new BitmapFont(Util.getResource("res/ptsans.fnt"), Util.getResource("res/ptsans_00.png"));
         
      } catch (IOException e) {
         // ... do something here ...
         Sys.alert("Error", "Could not decode images!");
         e.printStackTrace();
         System.exit(0);
      }
      glClearColor(0.5f, .5f, .5f, 1f);
      //create our sprite batch
      batch = new SpriteBatch();
   }
   
   void drawGame() {
      //get the instance of the view matrix for our batch
      Matrix4f view = batch.getViewMatrix();
      
      //reset the matrix to identity, i.e. "no camera transform"
      view.setIdentity();
      
      //scale the view
      if (zoom != 1f) {
         view.scale(new Vector3f(zoom, zoom, 1f));
      }
      
      //pan the camera by translating the view matrix
      view.translate(new Vector2f(panX, panY));
      
      //after translation, we can rotate...
      if (rot!=0f) {
         //we want to rotate by a center origin point, so first we translate
         view.translate(new Vector2f(Display.getWidth()/2, Display.getHeight()/2));
         
         //then we rotate
         view.rotate(rot, new Vector3f(0, 0, 1));
         
         //then we translate back
         view.translate(new Vector2f(-Display.getWidth()/2, -Display.getHeight()/2));
      }
      
      //apply other transformations here...
      
      
      //update the new view matrix
      batch.updateUniforms();
      
      //start the sprite batch
      batch.begin();

      //draw a tile from our sprite sheet
      batch.draw(tile, 10, 10);
      
      batch.draw(tile, 10, 100, 128, 128); //we can stretch it with a new width/height
      
      //we can also draw a region of a Texture on the fly like so:
      batch.drawRegion(tex, 0, 0, 32, 32,      //srcX, srcY, srcWidth, srcHeight
                        10, 250, 32, 32);  //dstX, dstY, dstWidth, dstHeight
      
      //tint batch red
      batch.setColor(Color.RED); 
      batch.draw(tex2, 0, 0, Display.getWidth(), Display.getHeight());
      
      //reset color
      batch.setColor(Color.WHITE);
      

      //finish the sprite batch and push the tiles to the GPU
      batch.end();
   }
   
   void drawHUD() {
      //draw the text with identity matrix, i.e. no camera transformation
      batch.getViewMatrix().setIdentity();
      batch.updateUniforms();
      
      
      batch.begin();
      // ... render any hud elements
      font.drawText(batch, "Control camera with WASD, UP/DOWN and LEFT/RIGHT", 10, 10);
      batch.end();
   }

   protected void render() throws LWJGLException {
      super.render();      
      
      
      if (Keyboard.isKeyDown(Keyboard.KEY_A))
         panX -= MOVE_SPEED;
      if (Keyboard.isKeyDown(Keyboard.KEY_D))
         panX += MOVE_SPEED;
      if (Keyboard.isKeyDown(Keyboard.KEY_W))
         panY -= MOVE_SPEED;
      if (Keyboard.isKeyDown(Keyboard.KEY_S))
         panY += MOVE_SPEED;
      
      if (Keyboard.isKeyDown(Keyboard.KEY_UP))
         zoom += ZOOM_SPEED;
      if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
         zoom -= ZOOM_SPEED;

      zoom = Math.max(0.15f, zoom);
      
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
         rot -= ROT_SPEED;
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
         rot += ROT_SPEED;
      
      drawGame();
      
      drawHUD();
   }
   

   protected void resize() throws LWJGLException {
      super.resize();
      batch.resize(Display.getWidth(), Display.getHeight());
   }
   
}





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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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!