com.badlogic.gdx.utils.BufferUtils Java Examples

The following examples show how to use com.badlogic.gdx.utils.BufferUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScreenshotService.java    From talos with Apache License 2.0 6 votes vote down vote up
private void takeScreenshot() {
    byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);

    float scaleX = (float)Gdx.graphics.getBackBufferWidth()/Gdx.graphics.getWidth();
    float scaleY = (float)Gdx.graphics.getBackBufferHeight()/Gdx.graphics.getHeight();

    // This loop makes sure the whole screenshot is opaque and looks exactly like what the user is seeing
    for (int i = 4; i < pixels.length; i += 4) {
        pixels[i - 1] = (byte) 255;
    }

    Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888);
    BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);

    color.set(pixmap.getPixel((int)(posX * scaleX), (int)(posY * scaleY)));

    pixmap.dispose();
}
 
Example #2
Source File: Screenshot.java    From Cubes with MIT License 6 votes vote down vote up
public static Setting screenshotResolutionSetting() {
  return new DropDownSetting(resolutions) {
    @Override
    public void onChange() {
      if ("normal".equals(selected) || "max".equals(selected)) return;

      IntBuffer intBuffer = BufferUtils.newIntBuffer(2);
      Gdx.gl20.glGetIntegerv(GL20.GL_MAX_VIEWPORT_DIMS, intBuffer);
      int maxWidth = intBuffer.get(0);
      int maxHeight = intBuffer.get(1);

      int[] resolution = resolutionMap.get(selected);
      if (resolution[0] > maxWidth || resolution[1] > maxHeight) selected = "normal";
    }

    @Override
    public boolean shouldDisplay() {
      return Compatibility.get().getApplicationType() == Application.ApplicationType.Desktop;
    }
  };
}
 
Example #3
Source File: Renderer.java    From VuforiaLibGDX with MIT License 6 votes vote down vote up
public Renderer() {

        lights = new Environment();
        lights.set(new ColorAttribute(ColorAttribute.AmbientLight, Color.WHITE));

        camera = new PerspectiveCamera(60, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.near = 1.0F;
        camera.far = 1000.0F;
        //set camera into "Vuforia - style" direction
        camera.position.set(new Vector3(0,0,0));
        camera.lookAt(new Vector3(0,0,1));

        IntBuffer buffer = BufferUtils.newIntBuffer(16);
        Gdx.gl.glGetIntegerv(GL20.GL_MAX_TEXTURE_IMAGE_UNITS, buffer);
        int units = buffer.get(0);
        Log.d("TAG", "Max texture units: "+units);
        modelBatch = new ModelBatch(new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.WEIGHTED, 0)));
    }
 
Example #4
Source File: Screenshot.java    From Cubes with MIT License 6 votes vote down vote up
@Override
public void frameStart() {
  if (!state.compareAndSet(0,  1)) return;

  String setting = ((DropDownSetting) Settings.getSetting(Settings.GRAPHICS_SCREENSHOT_SIZE)).getSelected();
  if ("max".equals(setting)) {
    IntBuffer intBuffer = BufferUtils.newIntBuffer(2);
    Gdx.gl20.glGetIntegerv(GL20.GL_MAX_VIEWPORT_DIMS, intBuffer);
    screenshotWidth = Math.min(intBuffer.get(0), 16384);
    screenshotHeight = Math.min(intBuffer.get(1), 16384);
  } else {
    screenshotWidth = Screenshot.resolutionMap.get(setting)[0];
    screenshotHeight = Screenshot.resolutionMap.get(setting)[1];
  }

  Log.debug("Attempting to take " + setting + " (" + screenshotWidth + "x" + screenshotHeight + ") resolution screenshot");
  frameBuffer = new FrameBuffer(Pixmap.Format.RGB888, screenshotWidth, screenshotHeight, true, true);
  frameBuffer.begin();

  oldWidth = Graphics.RENDER_WIDTH;
  oldHeight = Graphics.RENDER_HEIGHT;
  Adapter.getInterface().resize(screenshotWidth, screenshotHeight);
}
 
Example #5
Source File: BasePicker.java    From Mundus with Apache License 2.0 6 votes vote down vote up
public Pixmap getFrameBufferPixmap(Viewport viewport) {
    int w = viewport.getScreenWidth();
    int h = viewport.getScreenHeight();
    int x = viewport.getScreenX();
    int y = viewport.getScreenY();
    final ByteBuffer pixelBuffer = BufferUtils.newByteBuffer(w * h * 4);

    Gdx.gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, fbo.getFramebufferHandle());
    Gdx.gl.glReadPixels(x, y, w, h, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE, pixelBuffer);
    Gdx.gl.glBindFramebuffer(GL20.GL_FRAMEBUFFER, 0);

    final int numBytes = w * h * 4;
    byte[] imgLines = new byte[numBytes];
    final int numBytesPerLine = w * 4;
    for (int i = 0; i < h; i++) {
        pixelBuffer.position((h - i - 1) * numBytesPerLine);
        pixelBuffer.get(imgLines, i * numBytesPerLine, numBytesPerLine);
    }

    Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
    BufferUtils.copy(imgLines, 0, pixmap.getPixels(), imgLines.length);

    return pixmap;
}
 
Example #6
Source File: MetalBasic3DRenderer.java    From robovm-samples with Apache License 2.0 6 votes vote down vote up
private void updateConstantBuffer() {
    baseModelViewMatrix.set(viewMatrix).translate(0.0f,  0.0f,  5f).rotate(1, 1, 1, rotation);
    ByteBuffer constant_buffer = dynamicConstantBuffer[constantDataBufferIndex].getContents();
    constant_buffer.order(ByteOrder.nativeOrder());
    for (int i = 0; i < kNumberOfBoxes; i++) {
        int multiplier = ((i % 2) == 0?1: -1);
        modelViewMatrix.set(baseModelViewMatrix).translate(0, 0, multiplier * 1.5f).rotate(1, 1, 1, rotation);

        normalMatrix.set(modelViewMatrix).tra().inv();
        constant_buffer.position((sizeOfConstantT *i) + normalMatrixOffset);
        BufferUtils.copy(normalMatrix.getValues(), 0, 16, constant_buffer);

        combinedMatrix.set(projectionMatrix).mul(modelViewMatrix);
        constant_buffer.position((sizeOfConstantT * i) + modelViewProjectionMatrixOffset);
        BufferUtils.copy(combinedMatrix.getValues(), 0, 16, constant_buffer);
    }
    rotation += 0.01;
}
 
Example #7
Source File: MSI.java    From riiablo with Apache License 2.0 6 votes vote down vote up
@Override
public void create() {
  Gdx.app.setLogLevel(Application.LOG_DEBUG);

  final Calendar calendar = Calendar.getInstance();
  DateFormat format = DateFormat.getDateTimeInstance();
  Gdx.app.log(TAG, format.format(calendar.getTime()));

  try {
    InetAddress address = InetAddress.getLocalHost();
    Gdx.app.log(TAG, "IP Address: " + address.getHostAddress() + ":" + PORT);
    Gdx.app.log(TAG, "Host Name: " + address.getHostName());
  } catch (UnknownHostException e) {
    Gdx.app.error(TAG, e.getMessage(), e);
  }

  Gdx.app.log(TAG, "Starting server...");
  server = Gdx.net.newServerSocket(Net.Protocol.TCP, PORT, null);
  buffer = BufferUtils.newByteBuffer(4096);
}
 
Example #8
Source File: FreeType.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
public ByteBuffer getBuffer() {
	if (getRows() == 0)
		// Issue #768 - CheckJNI frowns upon env->NewDirectByteBuffer with NULL buffer or capacity 0
		//                  "JNI WARNING: invalid values for address (0x0) or capacity (0)"
		//              FreeType sets FT_Bitmap::buffer to NULL when the bitmap is empty (e.g. for ' ')
		//              JNICheck is on by default on emulators and might have a point anyway...
		//              So let's avoid this and just return a dummy non-null non-zero buffer
		return BufferUtils.newByteBuffer(1);
	return getBuffer(address);
}
 
Example #9
Source File: Program.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void link() {
	Gdx.gl.glLinkProgram( handle );
	
	IntBuffer status = BufferUtils.newIntBuffer(1);
	Gdx.gl.glGetProgramiv( handle, Gdx.gl.GL_LINK_STATUS, status );
	if (status.get() == Gdx.gl.GL_FALSE) {
		throw new Error( Gdx.gl.glGetProgramInfoLog( handle ) );
	}
}
 
Example #10
Source File: Shader.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void compile() {
	Gdx.gl.glCompileShader( handle );
	
	IntBuffer status = BufferUtils.newIntBuffer(1);
	Gdx.gl.glGetShaderiv( handle, Gdx.gl.GL_COMPILE_STATUS, status);
	if (status.get() == Gdx.gl.GL_FALSE) {
		throw new Error( Gdx.gl.glGetShaderInfoLog( handle ) );
	}
}
 
Example #11
Source File: World.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public void takeScreenshot(String filename, int w) {

		// get viewport
		IntBuffer results = BufferUtils.newIntBuffer(16);
		Gdx.gl20.glGetIntegerv(GL20.GL_VIEWPORT, results);

		int h = (int) (w * getSceneCamera().viewportHeight / getSceneCamera().viewportWidth);

		FrameBuffer fbo = new FrameBuffer(Format.RGB565, w, h, false);

		fbo.begin();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		draw();
		Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, w, h);

		// restore viewport
		fbo.end(results.get(0), results.get(1), results.get(2), results.get(3));

		// Flip the pixmap upside down
		ByteBuffer pixels = pixmap.getPixels();
		int numBytes = w * h * 4;
		byte[] lines = new byte[numBytes];
		int numBytesPerLine = w * 4;
		for (int i = 0; i < h; i++) {
			pixels.position((h - i - 1) * numBytesPerLine);
			pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
		}
		pixels.clear();
		pixels.put(lines);

		PixmapIO.writePNG(EngineAssetManager.getInstance().getUserFile(filename), pixmap);

		fbo.dispose();
	}
 
Example #12
Source File: Main.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  while (!kill.get()) {
    try {
      buffer.clear();
      buffer.mark();
      ReadableByteChannel in = Channels.newChannel(socket.getInputStream());
      if (in.read(buffer) == -1) {
        kill.set(true);
        break;
      }
      buffer.limit(buffer.position());
      buffer.reset();

      byte[] data = com.riiablo.util.BufferUtils.readRemaining(buffer);

      /*
      BNCS packet = BNCS.getRootAsBNCS(buffer);
      Gdx.app.log(TAG, "packet type " + D2GSData.name(packet.dataType()));
      */
      process(id, data);
    } catch (Throwable t) {
      Gdx.app.log(TAG, t.getMessage(), t);
      kill.set(true);
    }
  }

  Gdx.app.log(TAG, "closing socket...");
  if (socket != null) socket.dispose();
  synchronized (clients) {
    clients.removeValue(this, true);
  }
}
 
Example #13
Source File: Program.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public void link() {
	Gdx.gl.glLinkProgram( handle );
	
	IntBuffer status = BufferUtils.newIntBuffer(1);
	Gdx.gl.glGetProgramiv( handle, GL20.GL_LINK_STATUS, status );
	if (status.get() == GL20.GL_FALSE) {
		throw new Error( Gdx.gl.glGetProgramInfoLog( handle ) );
	}
}
 
Example #14
Source File: FreeType.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
@Override
public void dispose() {
	doneFace(address);
	ByteBuffer buffer = library.fontData.get(address);
	if(buffer != null) {
		library.fontData.remove(address);
		if (BufferUtils.isUnsafeByteBuffer(buffer))
			BufferUtils.disposeUnsafeByteBuffer(buffer);
	}
}
 
Example #15
Source File: FreeType.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
public Face newMemoryFace(ByteBuffer buffer, int faceIndex) {
	long face = newMemoryFace(address, buffer, buffer.remaining(), faceIndex);
	if(face == 0) {
		if (BufferUtils.isUnsafeByteBuffer(buffer))
			BufferUtils.disposeUnsafeByteBuffer(buffer);
		throw new GdxRuntimeException("Couldn't load font, FreeType error code: " + getLastErrorCode());
	}
	else {
		fontData.put(face, buffer);
		return new Face(face, this);
	}
}
 
Example #16
Source File: FreeType.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
@Override
public void dispose () {
	doneFreeType(address);
	for(ByteBuffer buffer: fontData.values()) {
		if (BufferUtils.isUnsafeByteBuffer(buffer))
			BufferUtils.disposeUnsafeByteBuffer(buffer);
	}
}
 
Example #17
Source File: Shader.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public void compile() {
	Gdx.gl.glCompileShader( handle );

	IntBuffer status = BufferUtils.newIntBuffer(1);
	Gdx.gl.glGetShaderiv( handle, GL20.GL_COMPILE_STATUS, status);
	if (status.get() == GL20.GL_FALSE) {
		throw new Error( Gdx.gl.glGetShaderInfoLog( handle ) );
	}
}
 
Example #18
Source File: PlatformInfoUtil.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
public static void getGlVersionInfo(JsonObject platformInfoJson, GL30 gl30) {

    BufferUtils.newIntBuffer(16);

    platformInfoJson.addProperty("GL_VERSION", Gdx.gl.glGetString(GL20.GL_VERSION));
    platformInfoJson.addProperty("GL_SHADING_LANGUAGE_VERSION", Gdx.gl.glGetString(GL20.GL_SHADING_LANGUAGE_VERSION));
    platformInfoJson.addProperty("GL_VENDOR", Gdx.gl.glGetString(GL20.GL_VENDOR));
    platformInfoJson.addProperty("GL_RENDERER", Gdx.gl.glGetString(GL20.GL_RENDERER));

    IntBuffer buff = BufferUtils.newIntBuffer(16);

    buff.clear();
    Gdx.gl.glGetIntegerv(GL30.GL_MAJOR_VERSION, buff);
    platformInfoJson.addProperty("GL_MAJOR_VERSION", buff.get(0));

    buff.clear();
    Gdx.gl.glGetIntegerv(GL30.GL_MINOR_VERSION, buff);
    platformInfoJson.addProperty("GL_MINOR_VERSION", buff.get(0));

    JsonArray array = new JsonArray();
    try {
      final int GL_NUM_SHADING_LANGUAGE_VERSIONS = 0x82E9;
      buff.clear();
      Gdx.gl.glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, buff);
      if(Gdx.gl.glGetError() == GL20.GL_NO_ERROR && gl30 != null) {
        int size = buff.get(0);
        for(int i=0; i < size; ++i) {
          array.add(gl30.glGetStringi(GL20.GL_SHADING_LANGUAGE_VERSION, i));
        }
      }
    }
    catch (IllegalStateException e) {
      // The above may not work depending what OpenGL version is supported.
    }
    platformInfoJson.add("Supported_GLSL_versions", array);

  }
 
Example #19
Source File: UniformSetter.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
private static FloatBuffer getFloatBuffer(JsonArray argsJson) {
  float[] floatArgs = getFloatArray(argsJson);
  FloatBuffer floatBuffer = BufferUtils.newFloatBuffer(floatArgs.length);
  floatBuffer.put(floatArgs);
  floatBuffer.position(0);
  return floatBuffer;
}
 
Example #20
Source File: UniformSetter.java    From graphicsfuzz with Apache License 2.0 5 votes vote down vote up
private static IntBuffer getIntBuffer(JsonArray argsJson) {
  int[] intArgs = getIntArray(argsJson);
  IntBuffer intBuffer = BufferUtils.newIntBuffer(intArgs.length);
  intBuffer.put(intArgs);
  intBuffer.position(0);
  return intBuffer;
}
 
Example #21
Source File: PipelineState.java    From RuinsOfRevenge with MIT License 4 votes vote down vote up
protected PipelineState() {
	byteBuffer = BufferUtils.newByteBuffer( 32 );
}
 
Example #22
Source File: Framebuffer.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public Framebuffer() {
	IntBuffer buf = BufferUtils.newIntBuffer(1);
	Gdx.gl.glGenBuffers( 1, buf );
	id = buf.get();
}
 
Example #23
Source File: Framebuffer.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public void delete() {
	IntBuffer buf = BufferUtils.newIntBuffer(1);
	buf.put(id);
	Gdx.gl.glDeleteFramebuffers( 1, buf );
}
 
Example #24
Source File: MetalBasic3DRenderer.java    From robovm-samples with Apache License 2.0 4 votes vote down vote up
public void configure(MetalBasic3DView view) {
    // find a usable Device
    device = view.getDevice();
    
    // setup view with drawable formats
    view.setDepthPixelFormat(MTLPixelFormat.Depth32Float);
    view.setStencilPixelFormat(MTLPixelFormat.Invalid);
    view.setSampleCount(1);
    
    // create a new command queue
    commandQueue = device.newCommandQueue();
    
    defaultLibrary = device.newDefaultLibrary();
    if (defaultLibrary == null) {
        // If the shader libary isn't loading, nothing good will happen
        throw new Error(">> ERROR: Couldnt create a default shader library");
    }
    
    preparePipelineState(view);
    
    MTLDepthStencilDescriptor depthStateDesc = new MTLDepthStencilDescriptor();
    depthStateDesc.setDepthCompareFunction(MTLCompareFunction.Less);
    depthStateDesc.setDepthWriteEnabled(true);
    depthState = device.newDepthStencilState(depthStateDesc);
    
    // allocate a number of buffers in memory that matches the sempahore count so that
    // we always have one self contained memory buffer for each buffered frame.
    // In this case triple buffering is the optimal way to go so we cycle through 3 memory buffers
    for (int i = 0; i < kInFlightCommandBuffers; i++) {
        dynamicConstantBuffer[i] = device.newBuffer(maxBufferBytesPerFrame, MTLResourceOptions.CPUCacheModeDefaultCache);
        dynamicConstantBuffer[i].setLabel("ConstantBuffer" + i);
        
        // write initial color values for both cubes (at each offset).
        // Note, these will get animated during update
        ByteBuffer constant_buffer = dynamicConstantBuffer[i].getContents();
        constant_buffer.order(ByteOrder.nativeOrder());

        for (int j = 0; j < kNumberOfBoxes; j++) {
            if (j % 2 == 0) {
                constant_buffer.position((sizeOfConstantT * j) + ambientColorOffset);
                BufferUtils.copy(kBoxAmbientColors[0], 0, 4, constant_buffer);
                constant_buffer.position((sizeOfConstantT * j) + diffuseColorOffset);
                BufferUtils.copy(kBoxDiffuseColors[0], 0, 4, constant_buffer);
                constant_buffer.putInt((sizeOfConstantT * j) + multiplierOffset, 1);
            } else {
                constant_buffer.position((sizeOfConstantT * j) + ambientColorOffset);
                BufferUtils.copy(kBoxAmbientColors[1], 0, 4, constant_buffer);
                constant_buffer.position((sizeOfConstantT * j) + diffuseColorOffset);
                BufferUtils.copy(kBoxDiffuseColors[1], 0, 4, constant_buffer);
                constant_buffer.putInt((sizeOfConstantT * j) + multiplierOffset, -1);
            }
        }
    }
}
 
Example #25
Source File: Renderbuffer.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public Renderbuffer() {
	IntBuffer buf = BufferUtils.newIntBuffer(1);
	Gdx.gl.glGenRenderbuffers( 1, buf );
	id = buf.get();
}
 
Example #26
Source File: Renderbuffer.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public void delete() {
	IntBuffer buf = BufferUtils.newIntBuffer(1);
	buf.put(id);
	Gdx.gl.glDeleteRenderbuffers( 1, buf );
}
 
Example #27
Source File: VertexBufferObjectExt.java    From libgdx-snippets with MIT License 4 votes vote down vote up
public void setVertices(float[] vertices, int offset, int count) {
	buffer.position(0);
	BufferUtils.copy(vertices, buffer, count, offset);
	buffer.position(buffer.limit());
	buffer.limit(buffer.capacity());
}
 
Example #28
Source File: IndexBufferObjectExt.java    From libgdx-snippets with MIT License 4 votes vote down vote up
public void setIndices(short[] indices, int offset, int count) {
	buffer.position(0);
	BufferUtils.copy(indices, offset, buffer, count);
	buffer.position(buffer.limit());
	buffer.limit(buffer.capacity());
}
 
Example #29
Source File: GLBufferObject.java    From libgdx-snippets with MIT License 4 votes vote down vote up
@Override
public void dispose() {
	gl20.glDeleteBuffer(handle);
	BufferUtils.disposeUnsafeByteBuffer(byteBuffer);
}
 
Example #30
Source File: PaletteIndexedPixmap.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public PaletteIndexedPixmap(int width, int height, byte[] data) {
  this(width, height);
  BufferUtils.copy(data, 0, getPixels(), data.length);
}