2013-03-20 21 views
5

Ich versuche, eine Pixmap zu ändern und zu rendern, aber modifizierte Pixel werden nicht auf dem Bildschirm angezeigt. Ich bin mir nicht sicher, ob eine Pixmap der beste Weg ist. Kann mir jemand erklären, wo meine Fehler in dem Code unten sind? dankelibgdx - Wie zeichne ich ein Pixel

package com.me.mygdxgame; 
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.Pixmap; 
import com.badlogic.gdx.graphics.Pixmap.Format; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.utils.Array; 

public class MyGdxGame implements ApplicationListener { 

    private OrthographicCamera camera; 
    private SpriteBatch batch; 
    private Pixmap _pixmap; 
    private int _width; 
    private int _height; 
    private Texture _pixmapTexture; 
    private Sprite _pixmapSprite; 
    private int _x = 0; 
    private int _y = 0; 

    @Override 
    public void create() {  
     float w = Gdx.graphics.getWidth(); 
     float h = Gdx.graphics.getHeight(); 

     camera = new OrthographicCamera(1, h/w); 
     batch = new SpriteBatch(); 

     _width = (int)Math.round(w); 
     _height = (int)Math.round(h); 
     _pixmap = new Pixmap(_width, _height, Format.RGBA8888); 
     _pixmap.setColor(Color.RED); 
     _pixmap.fillRectangle(0, 0, _width, _height); 
     _pixmapTexture = new Texture(_pixmap, Format.RGB888, false); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     _pixmap.dispose(); 
     _pixmapTexture.dispose(); 
    } 

    @Override 
    public void render() { 
     updatePixMap(); 

     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     batch.draw(_pixmapTexture, -_width/2, -_height/2); 
     batch.end(); 
    } 

    private void updatePixMap() { 
     _x += 1; 
     if (_x >= _width) { 
      _x = 0; 
     } 

     _y += 1; 
     if (_y >= _height/2) { 
      return; 
     } 

     _pixmap = new Pixmap(_width, _height, Format.RGBA8888); 
     _pixmap.setColor(Color.CYAN); 
     _pixmap.drawPixel(_x, _y); 
     _pixmapTexture = new Texture(_pixmap, Format.RGB888, false); 
    } 

    @Override 
    public void resize(int width, int height) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 
} 
+0

Anstatt eine Textur zu verwenden, könnten Sie vielleicht eine bessere Leistung mit [ShapeRenderer] erzielen (http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html) ? – 7yl4r

Antwort

3

Sie erstellen ein neues Pixmap für jede Schleife und Sie zeichnen nicht die komplette Textur in Ihrer Ansicht.

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.Pixmap; 
import com.badlogic.gdx.graphics.Pixmap.Format; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.MathUtils; 

public class MyGdxGame implements ApplicationListener { 

    private OrthographicCamera camera; 
    private SpriteBatch   batch; 
    private Pixmap    _pixmap; 
    private Texture    _pixmapTexture; 
    private int     _x = 0; 
    private int     _y = 0; 
    private float    _w; 
    private float    _h; 
    private int     _width; 
    private int     _height; 

    @Override 
    public void create() { 
     _w = Gdx.graphics.getWidth(); 
     _h = Gdx.graphics.getHeight(); 
     _width = MathUtils.round(_w); 
     _height = MathUtils.round(_h); 

     camera = new OrthographicCamera(1f, _h/_w); 
     camera.setToOrtho(false); 
     batch = new SpriteBatch(); 

     _pixmap = new Pixmap(_width, _height, Format.RGBA8888); 
     _pixmap.setColor(Color.RED); 
     _pixmap.fillRectangle(0, 0, _width, _height); 
     _pixmapTexture = new Texture(_pixmap, Format.RGB888, false); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     _pixmap.dispose(); 
     _pixmapTexture.dispose(); 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void render() { 
     updatePixMap(); 

     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

     batch.setProjectionMatrix(camera.combined); 

     batch.begin(); 
     batch.draw(_pixmapTexture, 1f/2f, _h/_w/2f); 
     batch.end(); 
    } 

    @Override 
    public void resize(final int width, final int height) { 
    } 

    @Override 
    public void resume() { 
    } 

    private void updatePixMap() { 
     _x += 1; 
     if (_x >= _width) _x = 0; 

     _y += 1; 
     if (_y >= _height/2) return; 

     _pixmap.setColor(Color.CYAN); 
     _pixmap.drawPixel(_x, _y); 
     _pixmapTexture = new Texture(_pixmap, Format.RGB888, false); 
    } 
} 

Aber das ist sehr langsam, also warum willst du es tun?

+0

Danke, ich dachte, es wäre effizienter als viele Texturobjekte zu zeichnen. Ziel ist es, eine Art "Conways Spiel des Lebens" zu schaffen, in dem Pixel Zellen sind. Ich versuche nur ein paar Sachen aus dieser Bibliothek, um es mehr zu verstehen. Ich werde Ihren Code in wenigen Stunden versuchen, nach dem Schlafen :) – user2190718

+0

Ihr Code funktioniert gut. Ich denke, ich verstehe, warum das Rendern der Linie so langsam ist. Die Pixmap-Behandlung erfolgt in der render() -Methode. Die Rendermethode wird 60 Mal pro Sekunde aufgerufen (60 fps), also kann ich in 1 Sekunde nur 60 Pixel zeichnen. Am Ende ist es nicht so langsam, es ist nur mit der Render-Frequenz "synchronisiert". Sie können den folgenden Code hinzufügen, um eine bessere Sicht auf diesen Punkt zu haben: BitmapFont fontmessage = new BitmapFont(); fontmessage.draw (Stapelverarbeitung, "fps:" + Gdx.graphics.getFramesPerSecond() + "/ DeltaTime:" + Gdx.graphics.getDeltaTime(), 20, 30); – user2190718

+0

Ich meinte, dass Pixmaps sehr langsam sind, weil sie von der CPU gehandhabt werden und wenn man eine Textur daraus macht, dann wird sie an die GPU gesendet. –

Verwandte Themen