/**
* 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.
*/packagemdesl.test;
importstaticorg.lwjgl.opengl.GL11.glClearColor;
importjava.io.IOException;
importmdesl.graphics.Color;
importmdesl.graphics.SpriteBatch;
importmdesl.graphics.Texture;
importmdesl.graphics.TextureRegion;
importmdesl.graphics.text.BitmapFont;
importorg.lwjgl.LWJGLException;
importorg.lwjgl.Sys;
importorg.lwjgl.input.Keyboard;
importorg.lwjgl.opengl.Display;
importorg.lwjgl.util.vector.Matrix4f;
importorg.lwjgl.util.vector.Vector2f;
importorg.lwjgl.util.vector.Vector3f;
publicclassSpriteBatchTestextendsSimpleGame {
publicstaticvoidmain(String[] args) throwsLWJGLException {
Gamegame = newSpriteBatchTest();
game.setDisplayMode(640, 480, false);
game.start();
}
Texturetex, tex2;
TextureRegiontile;
SpriteBatchbatch;
floatpanX, panY, rot, zoom=1f;
BitmapFontfont;
finalfloatMOVE_SPEED = 10f;
finalfloatZOOM_SPEED = 0.025f;
finalfloatROT_SPEED = 0.05f;
protectedvoidcreate() throwsLWJGLException {
super.create();
//Load some texturestry {
tex = newTexture(Util.getResource("res/tiles.png"), Texture.NEAREST);
tex2 = newTexture(Util.getResource("res/ptsans_00.png"));
tile = newTextureRegion(tex, 128, 64, 64, 64);
font = newBitmapFont(Util.getResource("res/ptsans.fnt"), Util.getResource("res/ptsans_00.png"));
} catch (IOExceptione) {
// ... do something here ...Sys.alert("Error", "Could not decode images!");
e.printStackTrace();
System.exit(0);
}
glClearColor(0.5f, .5f, .5f, 1f);
//create our sprite batchbatch = newSpriteBatch();
}
voiddrawGame() {
//get the instance of the view matrix for our batchMatrix4fview = batch.getViewMatrix();
//reset the matrix to identity, i.e. "no camera transform"view.setIdentity();
//scale the viewif (zoom != 1f) {
view.scale(newVector3f(zoom, zoom, 1f));
}
//pan the camera by translating the view matrixview.translate(newVector2f(panX, panY));
//after translation, we can rotate...if (rot!=0f) {
//we want to rotate by a center origin point, so first we translateview.translate(newVector2f(Display.getWidth()/2, Display.getHeight()/2));
//then we rotateview.rotate(rot, newVector3f(0, 0, 1));
//then we translate backview.translate(newVector2f(-Display.getWidth()/2, -Display.getHeight()/2));
}
//apply other transformations here...//update the new view matrixbatch.updateUniforms();
//start the sprite batchbatch.begin();
//draw a tile from our sprite sheetbatch.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, srcHeight10, 250, 32, 32); //dstX, dstY, dstWidth, dstHeight//tint batch redbatch.setColor(Color.RED);
batch.draw(tex2, 0, 0, Display.getWidth(), Display.getHeight());
//reset colorbatch.setColor(Color.WHITE);
//finish the sprite batch and push the tiles to the GPUbatch.end();
}
voiddrawHUD() {
//draw the text with identity matrix, i.e. no camera transformationbatch.getViewMatrix().setIdentity();
batch.updateUniforms();
batch.begin();
// ... render any hud elementsfont.drawText(batch, "Control camera with WASD, UP/DOWN and LEFT/RIGHT", 10, 10);
batch.end();
}
protectedvoidrender() throwsLWJGLException {
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();
}
protectedvoidresize() throwsLWJGLException {
super.resize();
batch.resize(Display.getWidth(), Display.getHeight());
}
}
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 '~~~~'
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