Example usage for com.badlogic.gdx.graphics Texture Texture

List of usage examples for com.badlogic.gdx.graphics Texture Texture

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics Texture Texture.

Prototype

public Texture(int width, int height, Format format) 

Source Link

Usage

From source file:CB_Locator.Map.TileGL_Bmp.java

License:Open Source License

private void createTexture() {
    if (inCreation)
        return;/* ww w  . j a v a2  s  .  com*/
    inCreation = true;

    if (GL.isGlThread()) {
        if (texture != null)
            return;
        if (bytes == null)
            return;
        try {
            Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
            texture = new Texture(pixmap, format, CB_UI_Base_Settings.useMipMap.getValue());
            pixmap.dispose();
            pixmap = null;
        } catch (Exception ex) {
            Log.debug(log, "[TileGL] can't create Pixmap or Texture: " + ex.getMessage());
        }
        bytes = null;
        inCreation = false;
    } else {
        // create Texture on next GlThread
        GL.that.RunOnGL(new IRunOnGL() {
            @Override
            public void run() {
                if (isDisposed)
                    return;
                if (texture != null)
                    return;
                if (bytes == null)
                    return;
                try {
                    Pixmap pixmap = new Pixmap(bytes, 0, bytes.length);
                    texture = new Texture(pixmap, format, CB_UI_Base_Settings.useMipMap.getValue());
                    pixmap.dispose();
                    pixmap = null;
                } catch (Exception ex) {
                    Log.debug(log, "[TileGL] can't create Pixmap or Texture: " + ex.getMessage());
                }
                bytes = null;
                inCreation = false;
                GL.that.renderOnce();
            }
        });
    }

}

From source file:CB_UI_Base.GL_UI.Controls.Box.java

License:Open Source License

protected void drawBorder(Batch batch) {
    if (borderSprite == null) {
        try {//from   w  w  w.ja va 2 s .  co m
            GL.that.RunOnGLWithThreadCheck(new IRunOnGL() {

                @Override
                public void run() {

                    int w = (int) getWidth();
                    int h = (int) getHeight();

                    Pixmap borderRegPixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
                    borderRegPixmap.setColor(borderColor);

                    int borderAsInt = Math.round(Box.this.borderSize);

                    for (int i = 0; i < borderAsInt + 1; i++) {
                        borderRegPixmap.drawRectangle(i, i, ((int) getWidth()) - (i),
                                ((int) getHeight()) - (i));
                    }

                    borderSprite = new Sprite(new Texture(borderRegPixmap, Pixmap.Format.RGBA8888, false),
                            (int) getWidth(), (int) getHeight());
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    if (borderSprite != null)
        batch.draw(borderSprite, 0, 0);

}

From source file:CB_UI_Base.GL_UI.Controls.PopUps.PopUpMenu.java

License:Open Source License

@Override
protected void writeDebug() {
    if (DebugSprite == null) {
        try {/*w ww .  ja  va 2 s.c om*/
            GL.that.RunOnGLWithThreadCheck(new IRunOnGL() {

                @Override
                public void run() {
                    // int w = getNextHighestPO2((int) getWidth());
                    // int h = getNextHighestPO2((int) getHeight());

                    int w = (int) getWidth();
                    int h = (int) getHeight();

                    debugRegPixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
                    debugRegPixmap.setColor(1f, 0f, 0f, 1f);
                    debugRegPixmap.drawRectangle(1, 1, (int) getWidth() - 1, (int) getHeight() - 1);
                    debugRegPixmap.drawLine(1, 1, (int) getWidth() - 1, (int) getHeight() - 1);
                    debugRegPixmap.drawLine(1, (int) getHeight() - 1, (int) getWidth() - 1, 1);
                    debugRegTexture = new Texture(debugRegPixmap, Pixmap.Format.RGBA8888, false);

                    DebugSprite = new Sprite(debugRegTexture, (int) getWidth(), (int) getHeight());
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

From source file:CB_UI_Base.GL_UI.GL_View_Base.java

License:Open Source License

protected void writeDebug() {
    if (DebugSprite == null) {
        try {/*w w w . j ava2  s.c o  m*/
            GL.that.RunOnGLWithThreadCheck(new IRunOnGL() {

                @Override
                public void run() {
                    // int w = getNextHighestPO2((int) getWidth());
                    // int h = getNextHighestPO2((int) getHeight());

                    int w = (int) getWidth();
                    int h = (int) getHeight();

                    debugRegPixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
                    debugRegPixmap.setColor(1f, 0f, 0f, 1f);
                    debugRegPixmap.drawRectangle(1, 1, (int) getWidth() - 1, (int) getHeight() - 1);

                    debugRegTexture = new Texture(debugRegPixmap, Pixmap.Format.RGBA8888, false);

                    DebugSprite = new Sprite(debugRegTexture, (int) getWidth(), (int) getHeight());
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

From source file:com.badlogic.gdx.graphics.g3d.test.KeyframedModelViewer.java

License:Apache License

@Override
public void create() {
    model = ModelLoaderRegistry.loadKeyframedModel(Gdx.files.internal(fileName));
    if (textureFileName != null)
        texture = new Texture(Gdx.files.internal(textureFileName), Format.RGB565, true);
    model.setMaterial(new Material("default", new TextureAttribute(texture, 0, "skin")));
    hasNormals = hasNormals();/*from  w  ww  .  j av a  2 s  . c  o  m*/
    anim = (KeyframedAnimation) model.getAnimations()[0];

    model.getBoundingBox(bounds);
    float len = bounds.getDimensions().len();
    System.out.println("bounds: " + bounds);

    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(bounds.getCenter().cpy().add(len, len, len));
    cam.lookAt(bounds.getCenter().x, bounds.getCenter().y, bounds.getCenter().z);
    cam.near = 1f;
    cam.far = 1000;

    renderer = new ImmediateModeRenderer10();
    batch = new SpriteBatch();
    font = new BitmapFont();
}

From source file:com.badlogic.invaders.Renderer.java

License:Apache License

public Renderer() {
    try {//from ww w. j a  v  a  2s . c  o  m
        lights = new Environment();
        lights.add(new DirectionalLight().set(Color.WHITE, new Vector3(-1, -0.5f, 0).nor()));

        spriteBatch = new SpriteBatch();
        modelBatch = new ModelBatch();

        backgroundTexture = new Texture(Gdx.files.internal("data/planet.jpg"), Format.RGB565, true);
        backgroundTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

        font = new BitmapFont(Gdx.files.internal("data/font10.fnt"), Gdx.files.internal("data/font10.png"),
                false);

        camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.badlogic.invaders.simulation.Simulation.java

License:Apache License

private void populate() {
    ObjLoader objLoader = new ObjLoader();
    shipModel = objLoader.loadModel(Gdx.files.internal("data/ship.obj"));
    invaderModel = objLoader.loadModel(Gdx.files.internal("data/invader.obj"));
    blockModel = objLoader.loadModel(Gdx.files.internal("data/block.obj"));
    shotModel = objLoader.loadModel(Gdx.files.internal("data/shot.obj"));

    final Texture shipTexture = new Texture(Gdx.files.internal("data/ship.png"), Format.RGB565, true);
    shipTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    final Texture invaderTexture = new Texture(Gdx.files.internal("data/invader.png"), Format.RGB565, true);
    invaderTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);
    shipModel.materials.get(0).set(TextureAttribute.createDiffuse(shipTexture));
    invaderModel.materials.get(0).set(TextureAttribute.createDiffuse(invaderTexture));

    ((ColorAttribute) blockModel.materials.get(0).get(ColorAttribute.Diffuse)).color.set(0, 0, 1, 0.5f);
    blockModel.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));

    shotModel.materials.get(0).set(ColorAttribute.createDiffuse(1, 1, 0, 1f));

    final Texture explosionTexture = new Texture(Gdx.files.internal("data/explode.png"), Format.RGBA4444, true);
    explosionTexture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);

    final Mesh explosionMesh = new Mesh(true, 4 * 16, 6 * 16,
            new VertexAttribute(Usage.Position, 3, "a_position"),
            new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord0"));

    float[] vertices = new float[4 * 16 * (3 + 2)];
    short[] indices = new short[6 * 16];
    int idx = 0;//  www.ja va  2  s  .  com
    int index = 0;
    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 4; column++) {
            vertices[idx++] = 1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = 1;
            vertices[idx++] = 0;
            vertices[idx++] = 0 + column * 0.25f;
            vertices[idx++] = 0 + row * 0.25f;

            vertices[idx++] = -1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            vertices[idx++] = 1;
            vertices[idx++] = -1;
            vertices[idx++] = 0;
            vertices[idx++] = 0.25f + column * 0.25f;
            vertices[idx++] = 0.25f + row * 0.25f;

            final int t = (4 * row + column) * 4;
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 1);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t);
            indices[index++] = (short) (t + 2);
            indices[index++] = (short) (t + 3);
        }
    }

    explosionMesh.setVertices(vertices);
    explosionMesh.setIndices(indices);

    explosionModel = ModelBuilder.createFromMesh(explosionMesh, GL20.GL_TRIANGLES,
            new Material(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA),
                    TextureAttribute.createDiffuse(explosionTexture)));

    ship = new Ship(shipModel);
    ship.transform.rotate(0, 1, 0, 180);

    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 8; column++) {
            Invader invader = new Invader(invaderModel, -PLAYFIELD_MAX_X / 2 + column * 2f, 0,
                    PLAYFIELD_MIN_Z + row * 2f);
            invaders.add(invader);
        }
    }

    for (int shield = 0; shield < 3; shield++) {
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -2));
        blocks.add(new Block(blockModel, -10 + shield * 10 - 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 0, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -3));
        blocks.add(new Block(blockModel, -10 + shield * 10 + 1, 0, -2));
    }
}

From source file:com.bitfire.postprocessing.demo.ResourceFactory.java

License:Apache License

public static Texture newTexture(String name, boolean mipMap) {
    long hash = Hash.APHash(name);
    Texture t = textureCache.get(hash);//  w  ww .j a  va  2 s. c o  m

    if (t != null) {
        Gdx.app.log("ResourceFactory", "Cache hit for \"" + name + "\"");
        // cache hit
        return t;
    }

    t = new Texture(Gdx.files.internal("data/" + name), Format.RGBA8888, mipMap);

    if (mipMap) {
        t.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Nearest);
    } else {
        t.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    }

    textures.add(t);
    textureCache.put(hash, t);

    return t;
}

From source file:com.blastedstudios.crittercaptors.CritterCaptors.java

License:Apache License

@Override
public void create() {
    //com.badlogic.gdx.graphics.g3d.model.still.StillModel model = 
    //   ModelLoaderRegistry.loadStillModel(Gdx.files.internal("data/models/base.g3dt"));
    //com.badlogic.gdx.graphics.g3d.loaders.g3d.chunks.G3dExporter.export(model, Gdx.files.absolute("data/models/base.g3d"));
    options = new OptionsUtil();
    creatureManager = new CreatureManager();
    textureMap = new HashMap<String, Texture>();
    textureMap.put("skydome", new Texture(Gdx.files.internal("data/sky/skydome.png"), Format.RGB565, true));
    textureMap.put("base", new Texture(Gdx.files.internal("data/textures/base.png"), Format.RGB565, true));
    textureMap.put("grass", new Texture(Gdx.files.internal("data/textures/grass1.jpg"), Format.RGB565, true));
    textureMap.get("grass").setFilter(TextureFilter.MipMap, TextureFilter.Nearest);
    textureMap.put("gravel", new Texture(Gdx.files.internal("data/textures/gravel1.jpg"), Format.RGB565, true));
    textureMap.get("gravel").setFilter(TextureFilter.MipMap, TextureFilter.Nearest);
    modelMap = new HashMap<String, Model>();
    modelMap.put("skydome", ModelLoaderRegistry.load(Gdx.files.internal("data/sky/skydome.obj")));
    modelMap.put("base", ModelLoaderRegistry.load(Gdx.files.internal("data/models/base.g3d")));
    for (String name : creatureManager.getCreatureTemplateNames()) {
        modelMap.put(name,//  w ww. j  av  a 2 s. co m
                ModelLoaderRegistry.load(Gdx.files.internal("data/models/" + name.toLowerCase() + ".g3d")));
        try {
            textureMap.put(name, new Texture(Gdx.files.internal("data/textures/" + name.toLowerCase() + ".png"),
                    Format.RGB565, true));
        } catch (Exception e) {
        }
    }
    Gdx.input.loadComplexGestures(Gdx.files.internal("data/gestures"));
    Gdx.input.removeComplexGestureListener(null);
    setScreen(new MainScreen(this));
}

From source file:com.blindtigergames.werescrewed.graphics.TextureAtlas.java

License:Apache License

private void load(TextureAtlasData data) {
    ObjectMap<TextureAtlasData.Page, Texture> pageToTexture = new ObjectMap<TextureAtlasData.Page, Texture>();
    for (TextureAtlasData.Page page : data.pages) {
        this.collideOffsetX = page.collideOffsetX;
        this.collideOffsetY = page.collideOffsetY;
        Texture texture = null;/* ww w .  j a  va 2 s.  c o m*/
        if (page.texture == null) {
            texture = new Texture(page.textureFile, page.format, page.useMipMaps);
            texture.setFilter(page.minFilter, page.magFilter);
            texture.setWrap(page.uWrap, page.vWrap);
        } else {
            texture = page.texture;
            texture.setFilter(page.minFilter, page.magFilter);
            texture.setWrap(page.uWrap, page.vWrap);
        }
        textures.add(texture);
        pageToTexture.put(page, texture);
    }

    for (TextureAtlasData.Region region : data.regions) {
        int width = region.width;
        int height = region.height;
        AtlasRegion atlasRegion = new AtlasRegion(pageToTexture.get(region.page), region.left, region.top,
                region.rotate ? height : width, region.rotate ? width : height);
        atlasRegion.index = region.index;
        atlasRegion.name = region.name;
        atlasRegion.offsetX = region.offsetX;
        atlasRegion.offsetY = region.offsetY;
        atlasRegion.originalHeight = region.originalHeight;
        atlasRegion.originalWidth = region.originalWidth;
        atlasRegion.rotate = region.rotate;
        atlasRegion.splits = region.splits;
        atlasRegion.pads = region.pads;
        if (region.flip)
            atlasRegion.flip(false, true);
        regions.add(atlasRegion);
    }
}