org.lwjgl.system.MemoryStack Java Examples

The following examples show how to use org.lwjgl.system.MemoryStack. 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: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(String fileName) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(fileName, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + fileName  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #2
Source File: LmdbLwjgl.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
@Benchmark
public void readKey(final Reader r, final Blackhole bh) {
  try (MemoryStack stack = stackPush()) {
    final MDBVal rwKey = mallocStack(stack);
    final MDBVal rwVal = mallocStack(stack);

    for (final int key : r.keys) {
      stack.push();
      if (r.intKey) {
        rwKey.mv_data(stack.malloc(4).putInt(0, key));
      } else {
        rwKey.mv_data(stack.ASCII(r.padKey(key), false));
      }
      bh.consume(mdb_cursor_get(r.c, rwKey, rwVal, MDB_SET_KEY));
      bh.consume(rwVal.mv_data());
      stack.pop();
    }
  }
}
 
Example #3
Source File: VorbisTrack.java    From ure with MIT License 6 votes vote down vote up
VorbisTrack(String filePath, AtomicInteger sampleIndex) {
    try {
        encodedAudio = ioResourceToByteBuffer(filePath, 256*1024);
    } catch (IOException e) { e.printStackTrace(); }

    try (MemoryStack stack = stackPush()) {
        IntBuffer error = stack.mallocInt(1);
        handle = stb_vorbis_open_memory(encodedAudio,error,null);
        if (handle == NULL)
            throw new RuntimeException("Failed to open OGG file.  Error: " + error.get(0));
        STBVorbisInfo info = STBVorbisInfo.mallocStack(stack);
        stb_vorbis_get_info(handle, info);
        this.channels = info.channels();
        this.sampleRate = info.sample_rate();
        log.debug("vorbis file detected " + Integer.toString(channels) + " channel " + Integer.toString(sampleRate) + " samplerate");
    }
    this.samplesLength = stb_vorbis_stream_length_in_samples(handle);
    this.samplesSec = stb_vorbis_stream_length_in_seconds(handle);
    this.sampleIndex = sampleIndex;
    sampleIndex.set(0);
}
 
Example #4
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #5
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #6
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #7
Source File: LmdbLwjgl.java    From benchmarks with Apache License 2.0 6 votes vote down vote up
@Benchmark
public void readCrc(final Reader r, final Blackhole bh) {
  try (MemoryStack stack = stackPush()) {
    final MDBVal rwKey = mallocStack(stack);
    final MDBVal rwVal = mallocStack(stack);

    r.crc.reset();
    int status = mdb_cursor_get(r.c, rwKey, rwVal, MDB_FIRST);
    while (status != MDB_NOTFOUND) {
      r.crc.update(rwKey.mv_data());
      r.crc.update(rwVal.mv_data());
      status = mdb_cursor_get(r.c, rwKey, rwVal, MDB_NEXT);
    }
    bh.consume(r.crc.getValue());
  }
}
 
Example #8
Source File: TabPane.java    From LWJGUI with MIT License 6 votes vote down vote up
@Override
public void render(Context context) {
	if ( !isVisible() )
		return;
	
	// Render children
	super.render(context);
	
	this.clip(context);
	
	// Dropshadow
	long vg = context.getNVG();
	float x = (float) getX();
	float y = (float) getY();
	float w = (float) getWidth();
	try (MemoryStack stack = stackPush()) {
		NVGPaint bg = NanoVG.nvgLinearGradient(vg, x, y-16, x, y+6, Theme.current().getShadow().getNVG(), Color.TRANSPARENT.getNVG(), NVGPaint.callocStack(stack));
		NanoVG.nvgBeginPath(vg);
		NanoVG.nvgRect(vg, x, y, w, 6);
		NanoVG.nvgFillPaint(vg, bg);
		NanoVG.nvgFill(vg);
	}
}
 
Example #9
Source File: FontTexture.java    From ure with MIT License 6 votes vote down vote up
private void readFontInfo(ByteBuffer ttfData) {
    STBTTFontinfo fontInfo = STBTTFontinfo.create();
    if (!stbtt_InitFont(fontInfo, ttfData)) {
        throw new IllegalStateException("Failed to initialize font information.");
    }

    try (MemoryStack stack = stackPush()) {
        IntBuffer pAscent  = stack.mallocInt(1);
        IntBuffer pDescent = stack.mallocInt(1);
        IntBuffer pLineGap = stack.mallocInt(1);

        stbtt_GetFontVMetrics(fontInfo, pAscent, pDescent, pLineGap);

        ascent = pAscent.get(0);
        descent = pDescent.get(0);
        lineGap = pLineGap.get(0);

        float scaleFactor = stbtt_ScaleForPixelHeight(fontInfo, fontSize);
        ascent *= scaleFactor;
        descent *= scaleFactor;
        lineGap *= scaleFactor;
    }
}
 
Example #10
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #11
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example #12
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(String fileName) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(fileName, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + fileName  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #13
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(String fileName) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(fileName, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + fileName  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #14
Source File: MenuBar.java    From LWJGUI with MIT License 6 votes vote down vote up
@Override
public void render(Context context) {
	if ( !isVisible() )
		return;
	
	clip(context);
	long vg = context.getNVG();
	
	// Gradient
	try (MemoryStack stack = stackPush()) {
		NVGPaint bg = NanoVG.nvgLinearGradient(vg, 0, 0, 0, (float)getHeight(), Theme.current().getPane().getNVG(), Theme.current().getPaneAlt().getNVG(), NVGPaint.callocStack(stack));
		NanoVG.nvgBeginPath(vg);
		NanoVG.nvgRect(vg, (int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());
		NanoVG.nvgFillPaint(vg, bg);
		NanoVG.nvgFill(vg);
	}
	
	// Divider line
	NanoVG.nvgBeginPath(vg);
	NanoVG.nvgRect(vg, (int)getX(), (int)(getY()+getHeight()-1), (int)getWidth(), 1);
	NanoVG.nvgFillColor(vg, Theme.current().getControlOutline().getNVG());
	NanoVG.nvgFill(vg);
	
	// Render internal box
	this.internalBox.render(context);
}
 
Example #15
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #16
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #17
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #18
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #19
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(String fileName) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(fileName, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + fileName  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #20
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #21
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #22
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #23
Source File: PlatformWin32GLCanvas.java    From lwjgl3-swt with MIT License 6 votes vote down vote up
public long create(GLCanvas canvas, GLData attribs, GLData effective) {
    Canvas dummycanvas = new Canvas(canvas.getParent(), checkStyle(canvas.getParent(), canvas.getStyle()));
    long context = 0L;
    MemoryStack stack = MemoryStack.stackGet(); int ptr = stack.getPointer();
    try {
        context = create(canvas.handle, dummycanvas.handle, attribs, effective);
    } catch (SWTException e) {
        stack.setPointer(ptr);
        SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH, e);
    }
    final long finalContext = context;
    dummycanvas.dispose();
    Listener listener = new Listener() {
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.Dispose:
                deleteContext(canvas, finalContext);
                break;
            }
        }
    };
    canvas.addListener(SWT.Dispose, listener);
    return context;
}
 
Example #24
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #25
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #26
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #27
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example #28
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}
 
Example #29
Source File: ToolsUI.java    From recast4j with zlib License 5 votes vote down vote up
@Override
public boolean layout(NkContext ctx, int x, int y, int width, int height, int mouseX, int mouseY) {
    boolean mouseInside = false;
    nk_rgb(255, 255, 255, white);
    try (MemoryStack stack = stackPush()) {
        NkRect rect = NkRect.mallocStack(stack);
        if (nk_begin(ctx, "Tools", nk_rect(5, 5, 250, height - 10, rect), NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_TITLE)) {
            if (enabled) {
                for (Tool tool : tools) {
                    nk_layout_row_dynamic(ctx, 20, 1);
                    if (nk_option_label(ctx, tool.getName(), tool == currentTool)) {
                        currentTool = tool;
                    }
                }
                nk_layout_row_dynamic(ctx, 5, 1);
                nk_spacing(ctx, 1);
                if (currentTool != null) {
                    currentTool.layout(ctx);
                }
            }
            nk_window_get_bounds(ctx, rect);
            if (mouseX >= rect.x() && mouseX <= rect.x() + rect.w() && mouseY >= rect.y() && mouseY <= rect.y() + rect.h()) {
                mouseInside = true;
            }
        }
        nk_end(ctx);
    }
    return mouseInside;
}
 
Example #30
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}