Detatch client logic from opengl/openal calls

This commit is contained in:
austin 2023-04-11 21:27:09 -04:00
parent b167062585
commit 2fd8e77a2a
9 changed files with 199 additions and 150 deletions

View File

@ -30,7 +30,7 @@ public class ControlCallback implements GLFWKeyCallbackI {
}
/**
* !!!WARNING!!!, will silently fail if
* !!!WARNING!!!, will silently fail if opengl elementsn ot defined or keycode is outside of key value array size or is not in main rendering thread
* @param keycode
* @return
*/

View File

@ -1167,14 +1167,14 @@ public class ControlHandler {
public void runHandlers(List<Control> controls){
//construct mouse event
glfwGetCursorPos(Globals.window, mouse_X_Buffer, mouse_Y_Buffer);
xpos = mouse_X_Buffer[0];
ypos = mouse_Y_Buffer[0];
//Fills the buffer
getMousePositionInBuffer();
float xoffset = (float) (xpos - mouse_lastX);
float yoffset = (float) (mouse_lastY - ypos);
mouse_lastX = (float) xpos;
mouse_lastY = (float) ypos;
MouseEvent currentMouseEvent = new MouseEvent((int)xpos,(int)ypos,(int)mouse_lastX,(int)mouse_lastY,(int)xoffset,(int)yoffset,Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_1),Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_2));
MouseEvent currentMouseEvent = new MouseEvent((int)xpos,(int)ypos,(int)mouse_lastX,(int)mouse_lastY,(int)xoffset,(int)yoffset,getButton1Raw(),getButton2Raw());
boolean mouseMoveEvent = xoffset != 0 || yoffset != 0;
@ -1238,6 +1238,31 @@ public class ControlHandler {
}
}
void getMousePositionInBuffer(){
//only if not headless, gather position
if(!Globals.HEADLESS){
glfwGetCursorPos(Globals.window, mouse_X_Buffer, mouse_Y_Buffer);
xpos = mouse_X_Buffer[0];
ypos = mouse_Y_Buffer[0];
}
}
boolean getButton1Raw(){
if(Globals.HEADLESS){
return false;
} else {
return Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_1);
}
}
boolean getButton2Raw(){
if(Globals.HEADLESS){
return false;
} else {
return Globals.mouseCallback.getButton(GLFW_MOUSE_BUTTON_2);
}
}
public Control getControl(String controlName){
return controls.get(controlName);
}

View File

@ -23,7 +23,7 @@ public class MouseCallback extends GLFWMouseButtonCallback {
}
/**
* !!!WARNING!!!, will silently fail if
* !!!WARNING!!!, will silently fail if opengl elementsn ot defined or keycode is outside of key value array size or is not in main rendering thread
* @param keycode
* @return
*/

View File

@ -113,8 +113,8 @@ public class Globals {
//
public static ControlHandler controlHandler;
public static boolean updateCamera = true;
public static ControlCallback controlCallback;
public static MouseCallback mouseCallback;
public static ControlCallback controlCallback = new ControlCallback();
public static MouseCallback mouseCallback = new MouseCallback();
//

View File

@ -264,11 +264,11 @@ public class Main {
}
if(Globals.RUN_CLIENT){
if(Globals.RUN_CLIENT && !Globals.HEADLESS){
Globals.renderingEngine.drawScreen();
}
if(Globals.ENGINE_SHUTDOWN_FLAG || (Globals.RUN_CLIENT && glfwWindowShouldClose(Globals.window))){
if(Globals.ENGINE_SHUTDOWN_FLAG || (!Globals.HEADLESS && Globals.RUN_CLIENT && glfwWindowShouldClose(Globals.window))){
running = false;
}
@ -289,7 +289,7 @@ public class Main {
// S H U T D O W N
//
//Terminate the program.
if(Globals.RUN_CLIENT){
if(!Globals.HEADLESS && Globals.RUN_CLIENT){
glfwTerminate();
}
//used to signal threads to stop
@ -299,7 +299,7 @@ public class Main {
Globals.serverThread.interrupt();
}
//shut down audio engine
if(Globals.RUN_CLIENT){
if(!Globals.HEADLESS && Globals.RUN_CLIENT){
Globals.audioEngine.shutdown();
}
}

View File

@ -113,8 +113,11 @@ public class Mesh {
//
// VAO
//
//Check for headless to not call gl functions when not running with gpu
if(!Globals.HEADLESS){
rVal.vertexArrayObject = glGenVertexArrays();
glBindVertexArray(rVal.vertexArrayObject);
}
@ -370,7 +373,7 @@ public class Mesh {
boneIndexDataBuffer.flip();
boneWeightDataBuffer.flip();
if(!Globals.HEADLESS){
rVal.boneWeightBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, rVal.boneWeightBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, boneWeightDataBuffer, GL_STATIC_DRAW);
@ -383,6 +386,7 @@ public class Mesh {
GL15.glBufferData(GL_ARRAY_BUFFER, boneIndexDataBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(3);
}
} else {
rVal.hasBones = false;
}
@ -397,8 +401,9 @@ public class Mesh {
if(!Globals.HEADLESS){
rVal.shader = ShaderProgram.smart_assemble_shader(has_bones, apply_lighting);
}
@ -414,8 +419,9 @@ public class Mesh {
if(!Globals.HEADLESS){
glBindVertexArray(0);
}
@ -435,59 +441,73 @@ public class Mesh {
public void buffer_vertices(FloatBuffer verticies, int vertexDimension){
if(!Globals.HEADLESS){
vertexBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, verticies, GL_STATIC_DRAW);
glVertexAttribPointer(0, vertexDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
}
}
public void buffer_normals(FloatBuffer normals, int normalDimension){
if(!Globals.HEADLESS){
normalBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
glVertexAttribPointer(1, normalDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
}
}
public void buffer_faces(IntBuffer faces){
if(!Globals.HEADLESS){
elementArrayBuffer = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBuffer);
GL15.glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces, GL_STATIC_DRAW);
elementCount = faces.capacity();
}
}
public void buffer_texture_coords(FloatBuffer coords, int textureDimension){
if(!Globals.HEADLESS){
textureCoordBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, textureCoordBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, coords, GL_STATIC_DRAW);
glVertexAttribPointer(4, textureDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(4);
}
}
public void bufferCustomFloatAttribArray(FloatBuffer buffer, int bufferDimension, int attribIndex){
if(!Globals.HEADLESS){
int customBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, customBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glVertexAttribPointer(attribIndex, bufferDimension, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
}
public void bufferCustomIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){
if(!Globals.HEADLESS){
int customBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, customBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glVertexAttribPointer(attribIndex, bufferDimension, GL_INT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
}
public void bufferCustomUIntAttribArray(IntBuffer buffer, int bufferDimension, int attribIndex){
if(!Globals.HEADLESS){
int customBuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, customBuffer);
GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glVertexAttribPointer(attribIndex, bufferDimension, GL_UNSIGNED_INT, false, 0, 0);
glEnableVertexAttribArray(attribIndex);
}
}
public void setTextureMask(ActorTextureMask textureMask){
this.textureMask = textureMask;

View File

@ -231,12 +231,13 @@ public class RenderingEngine {
Globals.WINDOW_WIDTH = bufferWidth;
Globals.WINDOW_HEIGHT = bufferHeight;
//
// Attack controls callbacks
//
//set key callback
Globals.controlCallback = new ControlCallback();
GLFW.glfwSetKeyCallback(Globals.window, Globals.controlCallback);
//set mouse callback
Globals.mouseCallback = new MouseCallback();
GLFW.glfwSetMouseButtonCallback(Globals.window, Globals.mouseCallback);
//get title bar dimensions

View File

@ -5,6 +5,7 @@
*/
package electrosphere.renderer.texture;
import electrosphere.engine.Globals;
import electrosphere.engine.Main;
import electrosphere.util.FileUtils;
import java.awt.Color;
@ -42,6 +43,7 @@ public class Texture {
public Texture(String path){
this.path = path;
if(!Globals.HEADLESS){
//generate the texture object on gpu
texturePointer = glGenTextures();
//bind the new texture
@ -128,7 +130,8 @@ public class Texture {
}
glGenerateMipmap(GL_TEXTURE_2D);
//OPTIONAL free the original image data now that it's on the gpu
System.gc();
// System.gc();
}
}
public void bind(){

View File

@ -15,7 +15,7 @@ public class SpawningCreaturesTest {
@Before
public void initEngine(){
System.out.println("[Test] Spawn many creatures");
Globals.RUN_CLIENT = false;
Globals.RUN_CLIENT = true;
Globals.RUN_SERVER = true;
Globals.HEADLESS = true;
NetUtils.setPort(0);