Java tutorial
/* ----------------------------------------------------------------------------- Cogaen - Component-based Game Engine V3 ----------------------------------------------------------------------------- This software is developed by the Cogaen Development Team. Please have a look at our project home page for further details: http://www.cogaen.org - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Copyright (c) 2010-2011 Roman Divotkey Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ package org.cogaen.lwjgl.scene; import org.cogaen.core.Core; import org.cogaen.resource.ResourceService; import org.lwjgl.opengl.GL11; import org.newdawn.slick.opengl.Texture; public class SpriteFxVisual extends Visual { private Texture texture; private double halfWidth; private double halfHeight; private boolean flipVertical; private int blendMode = GL11.GL_ONE_MINUS_SRC_ALPHA; private boolean mirror = false; private boolean shadow = false; public SpriteFxVisual(Core core, String textureResource, double width, double height) { super(Color.WHITE); this.texture = (Texture) ResourceService.getInstance(core).getResource(textureResource); this.halfWidth = width / 2; this.halfHeight = height / 2; } private SpriteFxVisual() { // intentionally left empty } public void setSize(double width, double height) { this.halfWidth = width / 2; this.halfHeight = height / 2; } public boolean isFlipVertical() { return flipVertical; } public void setFlipVertical(boolean upsideDown) { this.flipVertical = upsideDown; } @Override public void render() { if (this.flipVertical) { GL11.glScaled(1, -1, 1); } if (this.shadow) { double dx = this.halfWidth * getScale() * 0.02; double dy = this.halfWidth * getScale() * 0.02; GL11.glColor4d(getColor().getRed() * 0.1, getColor().getGreen() * 0.1, getColor().getBlue() * 0.1, 0.7); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0.0f, this.texture.getHeight()); GL11.glVertex2d(-this.halfWidth * getScale() + dx, -this.halfHeight * getScale() - dy); GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight()); GL11.glVertex2d(this.halfWidth * getScale() + dx, -this.halfHeight * getScale() - dy); GL11.glTexCoord2f(this.texture.getWidth(), 0); GL11.glVertex2d(this.halfWidth * getScale() + dx, this.halfHeight * getScale() - dy); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2d(-this.halfWidth * getScale() + dx, this.halfHeight * getScale() - dy); } getColor().apply(); GL11.glBegin(GL11.GL_QUADS); GL11.glTexCoord2f(0.0f, this.texture.getHeight()); GL11.glVertex2d(-this.halfWidth * getScale(), -this.halfHeight * getScale()); GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight()); GL11.glVertex2d(this.halfWidth * getScale(), -this.halfHeight * getScale()); GL11.glTexCoord2f(this.texture.getWidth(), 0); GL11.glVertex2d(this.halfWidth * getScale(), this.halfHeight * getScale()); GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2d(-this.halfWidth * getScale(), this.halfHeight * getScale()); if (this.mirror) { GL11.glColor4d(getColor().getRed() * 0.7, getColor().getGreen() * 0.7, getColor().getBlue() * 0.7, 0.7); GL11.glTexCoord2f(0.0f, this.texture.getHeight()); GL11.glVertex2d(-this.halfWidth * getScale(), -this.halfHeight * getScale()); GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight()); GL11.glVertex2d(this.halfWidth * getScale(), -this.halfHeight * getScale()); GL11.glColor4d(getColor().getRed(), getColor().getGreen(), getColor().getBlue(), 0); GL11.glTexCoord2f(this.texture.getWidth(), this.texture.getHeight() / 2); GL11.glVertex2d(this.halfWidth * getScale(), -this.halfHeight * 2 * getScale()); GL11.glTexCoord2f(0.0f, this.texture.getHeight() / 2); GL11.glVertex2d(-this.halfWidth * getScale(), -this.halfHeight * 2 * getScale()); } GL11.glEnd(); } @Override public void prolog() { GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, this.blendMode); texture.bind(); // or GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID()); } @Override public void epilog() { // intentionally left empty } @Override public SpriteFxVisual newInstance() { SpriteFxVisual instance = new SpriteFxVisual(); super.copyFields(instance); instance.halfWidth = this.halfWidth; instance.halfHeight = this.halfHeight; instance.texture = this.texture; instance.blendMode = this.blendMode; return instance; } public void setAdditive(boolean value) { this.blendMode = value ? GL11.GL_ONE : GL11.GL_ONE_MINUS_SRC_ALPHA; } public boolean isAdditive() { return this.blendMode == GL11.GL_ONE; } public double getWidth() { return this.halfWidth * 2; } public double getHeight() { return this.halfHeight * 2; } public boolean isMirror() { return mirror; } public void setMirror(boolean mirror) { this.mirror = mirror; } public boolean isShadow() { return shadow; } public void setShadow(boolean shadow) { this.shadow = shadow; } }