-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00b562a
commit a81b1f2
Showing
6 changed files
with
345 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
src/main/java/com/shc/silenceengine/graphics/SpriteBatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2014-2015 Sri Harsha Chilakapati | ||
* | ||
* 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 com.shc.silenceengine.graphics; | ||
|
||
import com.shc.silenceengine.core.SilenceEngine; | ||
import com.shc.silenceengine.core.SilenceException; | ||
import com.shc.silenceengine.graphics.opengl.Primitive; | ||
import com.shc.silenceengine.graphics.opengl.Texture; | ||
import com.shc.silenceengine.math.Vector2; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Sri Harsha Chilakapati | ||
*/ | ||
public class SpriteBatch | ||
{ | ||
private List<Sprite> sprites; | ||
private Map<Integer, Vector2> spritePositionMap; | ||
|
||
private boolean active; | ||
|
||
public SpriteBatch() | ||
{ | ||
sprites = new ArrayList<>(); | ||
spritePositionMap = new HashMap<>(); | ||
active = false; | ||
} | ||
|
||
public void begin() | ||
{ | ||
if (active) | ||
throw new SilenceException("SpriteBatch already active"); | ||
|
||
sprites.clear(); | ||
|
||
active = true; | ||
} | ||
|
||
public void flush() | ||
{ | ||
if (sprites.size() == 0) | ||
return; | ||
|
||
sortSprites(); | ||
|
||
Batcher batcher = SilenceEngine.graphics.getBatcher(); | ||
|
||
Texture originalTexture = Texture.CURRENT; | ||
|
||
Texture texture = sprites.get(0).getTexture(); | ||
texture.bind(); | ||
|
||
Vector2 temp = Vector2.REUSABLE_STACK.pop(); | ||
|
||
batcher.begin(Primitive.TRIANGLES); | ||
{ | ||
for (Sprite sprite : sprites) | ||
{ | ||
Texture t = sprite.getTexture(); | ||
|
||
if (t.getId() != texture.getId()) | ||
{ | ||
batcher.end(); | ||
|
||
texture = t; | ||
t.bind(); | ||
|
||
batcher.begin(); | ||
} | ||
|
||
Vector2 position = spritePositionMap.get(sprite.getID()); | ||
|
||
// Triangle 1 | ||
batcher.vertex(temp.set(-texture.getWidth() / 2, -texture.getHeight() / 2) // Top-left | ||
.rotateSelf(sprite.getRotation()) | ||
.scaleSelf(sprite.getScaleX(), sprite.getScaleY()) | ||
.addSelf(position).addSelf(texture.getWidth()/2, texture.getHeight()/2)); | ||
batcher.texCoord(texture.getMinU(), texture.getMinV()); | ||
batcher.vertex(temp.set(texture.getWidth() / 2, -texture.getHeight() / 2) // Top-right | ||
.rotateSelf(sprite.getRotation()) | ||
.scaleSelf(sprite.getScaleX(), sprite.getScaleY()) | ||
.addSelf(position).addSelf(texture.getWidth() / 2, texture.getHeight() / 2)); | ||
batcher.texCoord(texture.getMaxU(), texture.getMinV()); | ||
batcher.vertex(temp.set(-texture.getWidth() / 2, texture.getHeight() / 2) // Bottom-left | ||
.rotateSelf(sprite.getRotation()) | ||
.scaleSelf(sprite.getScaleX(), sprite.getScaleY()) | ||
.addSelf(position).addSelf(texture.getWidth() / 2, texture.getHeight() / 2)); | ||
batcher.texCoord(texture.getMinU(), texture.getMaxV()); | ||
|
||
// Triangle 2 | ||
batcher.vertex(temp.set(texture.getWidth() / 2, -texture.getHeight() / 2) // Top-right | ||
.rotateSelf(sprite.getRotation()) | ||
.scaleSelf(sprite.getScaleX(), sprite.getScaleY()) | ||
.addSelf(position).addSelf(texture.getWidth() / 2, texture.getHeight() / 2)); | ||
batcher.texCoord(texture.getMaxU(), texture.getMinV()); | ||
batcher.vertex(temp.set(texture.getWidth() / 2, texture.getHeight() / 2) // Bottom-right | ||
.rotateSelf(sprite.getRotation()) | ||
.scaleSelf(sprite.getScaleX(), sprite.getScaleY()) | ||
.addSelf(position).addSelf(texture.getWidth() / 2, texture.getHeight() / 2)); | ||
batcher.texCoord(texture.getMaxU(), texture.getMaxV()); | ||
batcher.vertex(temp.set(-texture.getWidth() / 2, texture.getHeight() / 2) // Bottom-left | ||
.rotateSelf(sprite.getRotation()) | ||
.scaleSelf(sprite.getScaleX(), sprite.getScaleY()) | ||
.addSelf(position).addSelf(texture.getWidth()/2, texture.getHeight()/2)); | ||
batcher.texCoord(texture.getMinU(), texture.getMaxV()); | ||
} | ||
} | ||
batcher.end(); | ||
|
||
Vector2.REUSABLE_STACK.push(temp); | ||
|
||
sprites.clear(); | ||
spritePositionMap.clear(); | ||
|
||
originalTexture.bind(); | ||
} | ||
|
||
public void end() | ||
{ | ||
if (!active) | ||
throw new SilenceException("SpriteBatch is not active"); | ||
|
||
flush(); | ||
active = false; | ||
} | ||
|
||
private void sortSprites() | ||
{ | ||
sprites.sort((s1, s2) -> s1.getTexture().getId() < s2.getTexture().getId() ? 1 : -1); | ||
} | ||
|
||
public void addSprite(Sprite sprite, Vector2 position) | ||
{ | ||
sprites.add(sprite); | ||
spritePositionMap.put(sprite.getID(), position); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.