Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Build-137: Add NanoVG Support(partialy working, random JVM Crash).
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerra24 committed Dec 17, 2015
1 parent 392da72 commit 5ce0652
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 24 deletions.
4 changes: 2 additions & 2 deletions assets/game/settings.conf
@@ -1,5 +1,5 @@
#Voxel Settings
#Wed Dec 16 20:06:48 CST 2015
#Thu Dec 17 17:31:01 CST 2015
UPS=30
VSYNC=false
useMotionBlur=false
Expand All @@ -8,6 +8,6 @@ DrawDistance=2
SettingsVersion=3
useVolumetricLight=true
TESTXMOD=Hello Voxel
useDOF=false
FPS=30
useDOF=false
useShadows=true
@@ -1,9 +1,16 @@
package net.guerra24.voxel.client.core.states;

import java.nio.ByteBuffer;

import org.lwjgl.nanovg.NVGColor;
import org.lwjgl.nanovg.NanoVG;

import net.guerra24.voxel.client.core.GlobalStates;
import net.guerra24.voxel.client.core.GlobalStates.GameState;
import net.guerra24.voxel.client.core.State;
import net.guerra24.voxel.client.core.Voxel;
import net.guerra24.voxel.client.core.GlobalStates.GameState;
import net.guerra24.voxel.client.graphics.MenuRendering;
import net.guerra24.voxel.client.graphics.opengl.Display;
import net.guerra24.voxel.client.resources.GameResources;
import net.guerra24.voxel.universal.util.vector.Vector3f;

Expand All @@ -22,8 +29,12 @@ public MainMenuState() {
@Override
public void render(Voxel voxel, GlobalStates states, float delta) {
GameResources gm = voxel.getGameResources();
Display display = voxel.getDisplay();
gm.getFrustum().calculateFrustum(gm.getRenderer().getProjectionMatrix(), gm.getCamera());
gm.getRenderer().prepare();
display.beingNVGFrame();
gm.getMenuSystem().mainMenu.render();
display.endNVGFrame();
}

@Override
Expand Down
Expand Up @@ -24,18 +24,7 @@

package net.guerra24.voxel.client.graphics;

import static org.lwjgl.opengl.GL11.GL_BACK;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_CULL_FACE;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glCullFace;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -208,7 +197,7 @@ private void processEntity(Entity entity) {
*
*/
public void prepare() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearColor(VoxelVariables.RED, VoxelVariables.GREEN, VoxelVariables.BLUE, 1);
}

Expand Down
@@ -0,0 +1,87 @@
package net.guerra24.voxel.client.graphics;

import static org.lwjgl.nanovg.NanoVG.*;

import org.lwjgl.nanovg.NVGColor;
import org.lwjgl.nanovg.NVGPaint;
import static org.lwjgl.system.MemoryUtil.*;

import java.nio.ByteBuffer;

import net.guerra24.voxel.client.graphics.opengl.Display;

public class MenuRendering {

private static final NVGPaint paintA = NVGPaint.create();
private static final NVGPaint paintB = NVGPaint.create();
private static final NVGPaint paintC = NVGPaint.create();
private static final NVGColor colorA = NVGColor.create();
private static final NVGColor colorB = NVGColor.create();
private static final NVGColor colorC = NVGColor.create();

private static boolean isBlack(NVGColor col) {
return col.r() == 0.0f && col.g() == 0.0f && col.b() == 0.0f && col.a() == 0.0f;
}

public static NVGColor rgba(int r, int g, int b, int a, NVGColor color) {
color.r(r / 255.0f);
color.g(g / 255.0f);
color.b(b / 255.0f);
color.a(a / 255.0f);
return color;
}

public static void renderButton(ByteBuffer preicon, String text, float x, float y, float w, float h,
NVGColor color) {
long vg = Display.getVg();
NVGPaint bg = paintA;
float cornerRadius = 4.0f;
float tw, iw = 0;
nvgLinearGradient(vg, x, y, x, y + h, rgba(255, 255, 255, isBlack(color) ? 16 : 32, colorB),
rgba(0, 0, 0, isBlack(color) ? 16 : 32, colorC), bg);
nvgBeginPath(vg);
nvgRoundedRect(vg, x + 1, y + 1, w - 2, h - 2, cornerRadius - 1);
if (!isBlack(color)) {
nvgFillColor(vg, color);
nvgFill(vg);
}
nvgFillPaint(vg, bg);
nvgFill(vg);

nvgBeginPath(vg);
nvgRoundedRect(vg, x + 0.5f, y + 0.5f, w - 1, h - 1, cornerRadius - 0.5f);
nvgStrokeColor(vg, rgba(0, 0, 0, 48, colorA));
nvgStroke(vg);

ByteBuffer textEncoded = memEncodeASCII(text, BufferAllocator.MALLOC);

ByteBuffer n = null;

nvgFontSize(vg, 20.0f);
nvgFontFace(vg, "sans-bold");
tw = nvgTextBounds(vg, 0, 0, textEncoded, NULL, n);
if (preicon != null) {
nvgFontSize(vg, h * 1.3f);
nvgFontFace(vg, "icons");
iw = nvgTextBounds(vg, 0, 0, preicon, NULL, n);
iw += h * 0.15f;
}
if (preicon != null) {
nvgFontSize(vg, h * 0.3f);
nvgFontFace(vg, "icons");
nvgFillColor(vg, rgba(255, 255, 255, 96, colorA));
nvgText(vg, x + w * 0.5f - tw * 0.5f - iw * 0.75f, y + h * 0.5f, preicon, NULL);
}

nvgFontSize(vg, 20.0f);
nvgFontFace(vg, "sans-bold");
nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgFillColor(vg, rgba(0, 0, 0, 160, colorA));
nvgText(vg, x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f - 1, textEncoded, NULL);
nvgFillColor(vg, rgba(255, 255, 255, 160, colorA));
nvgText(vg, x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f, textEncoded, NULL);

memFree(textEncoded);
}

}
Expand Up @@ -25,6 +25,8 @@
package net.guerra24.voxel.client.graphics.opengl;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.nanovg.NanoVG.*;
import static org.lwjgl.nanovg.NanoVGGL3.*;
import static org.lwjgl.opengl.GL.createCapabilities;
import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL11.GL_TRUE;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class Display {
* LWJGL Window
*/
private static long window;
private static long vg;

/**
* Display VidMode
Expand Down Expand Up @@ -131,6 +134,7 @@ public class Display {
private boolean latestResized = false;
private int latestWidth = 0;
private int latestHeight = 0;
private float pixelRatio;

private static IntBuffer maxVram = BufferUtils.createIntBuffer(1);
private static IntBuffer usedVram = BufferUtils.createIntBuffer(1);
Expand Down Expand Up @@ -320,16 +324,21 @@ public void startUp() {
e.printStackTrace();
}
createCapabilities();
glViewport(0, 0, displayWidth, displayHeight);
vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG);
if (vg == NULL)
throw new RuntimeException("Fail to create NanoVG");
lastLoopTimeUpdate = getTime();
lastLoopTimeRender = getTime();
ByteBuffer w = BufferUtils.createByteBuffer(4);
ByteBuffer h = BufferUtils.createByteBuffer(4);
glfwGetWindowSize(window, w, h);
glfwGetFramebufferSize(window, w, h);
int width = w.getInt(0);
int height = h.getInt(0);
displayWidth = width;
displayHeight = height;
pixelRatio = (float) displayWidth / (float) displayHeight;
glViewport(0, 0, displayWidth, displayHeight);

if (glGetString(GL_VENDOR).contains("NVIDIA"))
nvidia = true;
else if (glGetString(GL_VENDOR).contains("AMD"))
Expand Down Expand Up @@ -358,11 +367,28 @@ public void updateDisplay(int fps, GameResources gm) {
sync(fps);
}

/**
* Call this before any NanoVG call
*
*/
public void beingNVGFrame() {
nvgBeginFrame(vg, displayWidth, displayHeight, pixelRatio);
}

/**
* Ends the actual NVGFrame
*/
public void endNVGFrame() {
nvgEndFrame(vg);
}

/**
* Destroy the display
*
*/
public void closeDisplay() {
nvgDeleteGL3(vg);

glfwDestroyWindow(window);
glfwTerminate();
errorCallback.release();
Expand Down Expand Up @@ -457,6 +483,10 @@ public static long getWindow() {
return window;
}

public static long getVg() {
return vg;
}

/**
* If a close is requested
*
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/net/guerra24/voxel/client/menu/MainMenu.java
Expand Up @@ -24,10 +24,14 @@

package net.guerra24.voxel.client.menu;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.nanovg.NVGColor;

import net.guerra24.voxel.client.core.VoxelVariables;
import net.guerra24.voxel.client.graphics.MenuRendering;
import net.guerra24.voxel.client.resources.GameResources;
import net.guerra24.voxel.client.resources.models.FontType;
import net.guerra24.voxel.client.resources.models.GUIText;
Expand All @@ -40,12 +44,14 @@ public class MainMenu {
private Button optionsButton;
private List<GUIText> texts;

private float xScale, yScale;

public MainMenu(GameResources gm) {
FontType font = gm.getTextHandler().getFont();
float width = VoxelVariables.WIDTH;
float height = VoxelVariables.HEIGHT;
float yScale = height / 720f;
float xScale = width / 1280f;
yScale = height / 720f;
xScale = width / 1280f;
playButton = new Button(new Vector2f(177 * xScale, 532 * yScale), new Vector2f(215, 80));
exitButton = new Button(new Vector2f(177 * xScale, 224 * yScale), new Vector2f(215, 80));
optionsButton = new Button(new Vector2f(177 * xScale, 376 * yScale), new Vector2f(215, 80));
Expand Down Expand Up @@ -76,6 +82,15 @@ public MainMenu(GameResources gm) {
texts.add(textExit);
}

public void render() {
MenuRendering.renderButton((ByteBuffer) null, "Play", 177 * xScale, 100 * yScale, 215, 80,
MenuRendering.rgba(255, 255, 255, 255, NVGColor.create()));
MenuRendering.renderButton((ByteBuffer) null, "Options", 200, 200, 200, 200,
MenuRendering.rgba(255, 255, 255, 255, NVGColor.create()));
MenuRendering.renderButton((ByteBuffer) null, "Exit", 200, 200, 200, 200,
MenuRendering.rgba(255, 255, 255, 255, NVGColor.create()));
}

public void load(GameResources gm) {
gm.getTextHandler().switchTo(texts);
}
Expand Down
Expand Up @@ -141,6 +141,7 @@ public void init() {
UniversalResources.loadUniversalResources(this);
menuSystem = new Menu(this);
loadMusic();
loader.loadNVGFont("Tahoma", "sans-bold");
}

/**
Expand Down

0 comments on commit 5ce0652

Please sign in to comment.