Created
April 20, 2013 19:17
-
-
Save mattdesl/5427051 to your computer and use it in GitHub Desktop.
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
package mingdx; | |
import com.badlogic.gdx.ApplicationListener; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.GL10; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; | |
import com.badlogic.gdx.graphics.glutils.ShaderProgram; | |
import com.badlogic.gdx.utils.GdxRuntimeException; | |
public class FontDropShadow implements ApplicationListener { | |
SpriteBatch batch; | |
OrthographicCamera cam; | |
BitmapFont font; | |
TextureRegion sprite; | |
//the offset for your font | |
final float SHADOW_OFFSET = 3f; | |
//the number of times to render your shadow silhouettes | |
final int SHADOW_STEPS = 2; | |
@Override | |
public void create() { | |
shader = createDefaultShader(); | |
batch = new SpriteBatch(); | |
cam = new OrthographicCamera(); | |
sprite = new TextureRegion(new Texture("data/cat4.png")); | |
FreeTypeFontGenerator g = new FreeTypeFontGenerator(Gdx.files.internal("data/testfont.ttf")); | |
font = g.generateFont(26); | |
} | |
@Override | |
public void dispose() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void pause() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(0.5f,0.5f,0.5f,1f); | |
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); | |
batch.begin(); | |
shadowFont("testing one two three lazy dog", 25, 25, Color.WHITE); | |
shadowSprite(sprite, 100, 100, Color.WHITE); | |
batch.end(); | |
} | |
void shadowFont(String s, float x, float y, Color color) { | |
int steps = SHADOW_STEPS + 1; | |
for (int i=1; i<steps; i++) { | |
float a = 1f - i/(float)steps; | |
float off = SHADOW_OFFSET * a; | |
font.setColor(0f, 0f, 0f, 1f - a); | |
font.draw(batch, s, x+off, y-off); | |
} | |
font.setColor(color); | |
font.draw(batch, s, x, y); | |
} | |
void shadowSprite(TextureRegion sprite, float x, float y, Color color) { | |
int steps = SHADOW_STEPS + 1; | |
for (int i=1; i<steps; i++) { | |
float a = 1f - i/(float)steps; | |
float off = SHADOW_OFFSET * a; | |
batch.setColor(0f, 0f, 0f, 1f - a); | |
batch.draw(sprite, x+off, y-off); | |
} | |
batch.setColor(color); | |
batch.draw(sprite, x, y); | |
} | |
@Override | |
public void resize(int width, int height) { | |
cam.setToOrtho(false); | |
batch.setProjectionMatrix(cam.combined); | |
} | |
@Override | |
public void resume() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment