|
| 1 | +package org.opengame.engine; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import lombok.Setter; |
| 5 | +import lombok.extern.java.Log; |
| 6 | +import lombok.extern.log4j.Log4j2; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.joml.Vector2f; |
| 9 | +import org.lwjgl.PointerBuffer; |
| 10 | +import org.lwjgl.bgfx.BGFXInit; |
| 11 | +import org.lwjgl.glfw.GLFWErrorCallback; |
| 12 | +import org.lwjgl.glfw.GLFWNativeCocoa; |
| 13 | +import org.lwjgl.glfw.GLFWNativeWin32; |
| 14 | +import org.lwjgl.glfw.GLFWNativeX11; |
| 15 | +import org.lwjgl.system.MemoryStack; |
| 16 | +import org.lwjgl.system.Platform; |
| 17 | +import org.opengame.engine.app.AppConfig; |
| 18 | +import org.opengame.engine.event.EventBus; |
| 19 | +import org.opengame.engine.event.EventType; |
| 20 | +import org.opengame.engine.event.KeyEventData; |
| 21 | +import org.opengame.engine.event.MouseEventData; |
| 22 | +import org.opengame.engine.scene.Scene; |
| 23 | + |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +import static org.lwjgl.bgfx.BGFX.*; |
| 28 | +import static org.lwjgl.glfw.GLFW.*; |
| 29 | +import static org.lwjgl.system.MemoryUtil.NULL; |
| 30 | + |
| 31 | +/** |
| 32 | + * Entry point for engine, init here |
| 33 | + */ |
| 34 | +@Log |
| 35 | +public class Engine { |
| 36 | + private static Engine instance; |
| 37 | + private long windowHandle; |
| 38 | + @Getter |
| 39 | + private AppConfig config; |
| 40 | + @Getter |
| 41 | + @Setter |
| 42 | + private Scene currentScene; |
| 43 | + |
| 44 | + private float tickTime = 1000; |
| 45 | + private int msPerUpdate = 1000 / 20; |
| 46 | + |
| 47 | + /** |
| 48 | + * Init Vulkan context and window |
| 49 | + */ |
| 50 | + public void Init(AppConfig config) { |
| 51 | + log.info("Init"); |
| 52 | + log.info("Working directory: " + config.getWorkingDirectory()); |
| 53 | + instance = this; |
| 54 | + this.config = config; |
| 55 | + initWindow(config); |
| 56 | + } |
| 57 | + |
| 58 | + private void initWindow(AppConfig config) { |
| 59 | + if (!glfwInit()) { |
| 60 | + throw new RuntimeException("Cannot initialize GLFW"); |
| 61 | + } |
| 62 | + |
| 63 | + GLFWErrorCallback.createPrint().set(); |
| 64 | + |
| 65 | + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); |
| 66 | + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); |
| 67 | + |
| 68 | + windowHandle = glfwCreateWindow(config.getWindowWidth(), config.getWindowHeight(), |
| 69 | + config.getAppName(), NULL, NULL); |
| 70 | + |
| 71 | + setupInputCallbacks(); |
| 72 | + |
| 73 | + try (MemoryStack stack = MemoryStack.stackPush()) { |
| 74 | + BGFXInit init = BGFXInit.malloc(); |
| 75 | + bgfx_init_ctor(init); |
| 76 | + init.type(BGFX_RENDERER_TYPE_OPENGL); |
| 77 | + init.resolution(it -> it |
| 78 | + .width(config.getWindowWidth()) |
| 79 | + .height(config.getWindowHeight()) |
| 80 | + .reset(BGFX_RESET_VSYNC)); |
| 81 | + |
| 82 | + switch (Platform.get()) { |
| 83 | + case LINUX: |
| 84 | + init.platformData() |
| 85 | + .ndt(GLFWNativeX11.glfwGetX11Display()) |
| 86 | + .nwh(GLFWNativeX11.glfwGetX11Window(windowHandle)); |
| 87 | + break; |
| 88 | + case MACOSX: |
| 89 | + init.platformData() |
| 90 | + .nwh(GLFWNativeCocoa.glfwGetCocoaWindow(windowHandle)); |
| 91 | + break; |
| 92 | + case WINDOWS: |
| 93 | + init.platformData() |
| 94 | + .nwh(GLFWNativeWin32.glfwGetWin32Window(windowHandle)); |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + if (!bgfx_init(init)) { |
| 99 | + throw new RuntimeException("Error initializing bgfx renderer"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + logAvailableDevices(); |
| 105 | + logAvailableRenderers(); |
| 106 | + |
| 107 | + log.info("bgfx renderer: " + bgfx_get_renderer_name(bgfx_get_renderer_type())); |
| 108 | + |
| 109 | + bgfx_set_debug(BGFX_DEBUG_TEXT); |
| 110 | + bgfx_set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0); |
| 111 | + } |
| 112 | + |
| 113 | + private void logAvailableRenderers() { |
| 114 | + var types = new int[BGFX_RENDERER_TYPE_COUNT]; |
| 115 | + |
| 116 | + var rendererCount = bgfx_get_supported_renderers(types); |
| 117 | + |
| 118 | + log.info("Supported renderers:"); |
| 119 | + for (int i = 0; i < rendererCount; i++) { |
| 120 | + log.info(bgfx_get_renderer_name(types[i])); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void logAvailableDevices() { |
| 125 | + } |
| 126 | + |
| 127 | + private void setupInputCallbacks() { |
| 128 | + glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> { |
| 129 | + if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) { |
| 130 | + glfwSetWindowShouldClose(window, true); |
| 131 | + } |
| 132 | + if (action == GLFW_PRESS || action == GLFW_RELEASE) { |
| 133 | + EventBus.broadcastEvent(EventType.KEY_PRESSED, new KeyEventData(key, action == GLFW_PRESS)); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + glfwSetMouseButtonCallback(windowHandle, (window, button, action, mods) -> { |
| 138 | + EventBus.broadcastEvent(EventType.MOUSE_BUTTON_EVENT, new KeyEventData(button, action == GLFW_PRESS)); |
| 139 | + }); |
| 140 | + |
| 141 | + glfwSetCursorPosCallback(windowHandle, (window, xpos, ypos) -> { |
| 142 | + EventBus.broadcastEvent(EventType.MOUSE_MOVED, new MouseEventData(xpos, ypos)); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + public static void setCursorPos(Vector2f cursorPos) { |
| 147 | + glfwSetCursorPos(instance.windowHandle, cursorPos.x, cursorPos.y); |
| 148 | + } |
| 149 | + |
| 150 | + public void startLoop() { |
| 151 | + long lastTime; |
| 152 | + long startTime = lastTime = glfwGetTimerValue(); |
| 153 | + while (!glfwWindowShouldClose(windowHandle)) { |
| 154 | + glfwPollEvents(); |
| 155 | + |
| 156 | + long now = glfwGetTimerValue(); |
| 157 | + long frameTime = now - lastTime; |
| 158 | + lastTime = now; |
| 159 | + |
| 160 | + double freq = glfwGetTimerFrequency(); |
| 161 | + double toMs = 1000.0 / freq; |
| 162 | + |
| 163 | + double time = (now - startTime) / freq; |
| 164 | + |
| 165 | + bgfx_set_view_rect(0, 0, 0, config.getWindowWidth(), config.getWindowHeight()); |
| 166 | + bgfx_touch(0); |
| 167 | + |
| 168 | + bgfx_dbg_text_clear(0, false); |
| 169 | + bgfx_dbg_text_printf(0, 1, 0x1f, "Hello bgfx!"); |
| 170 | + |
| 171 | + var frameMs = (float) toMs * frameTime; |
| 172 | + if (currentScene != null) { |
| 173 | + if (tickTime >= msPerUpdate) { |
| 174 | + currentScene.update(); |
| 175 | + tickTime = 0; |
| 176 | + } |
| 177 | + tickTime += frameMs; |
| 178 | + currentScene.render((float) time, frameMs); |
| 179 | + } |
| 180 | + |
| 181 | + bgfx_frame(false); |
| 182 | + } |
| 183 | + |
| 184 | + bgfx_shutdown(); |
| 185 | + glfwDestroyWindow(windowHandle); |
| 186 | + glfwTerminate(); |
| 187 | + } |
| 188 | + |
| 189 | + public static int getRenderer() { |
| 190 | + return bgfx_get_renderer_type(); |
| 191 | + } |
| 192 | + |
| 193 | + public static String getWorkingDirectory() { |
| 194 | + return instance.config.getWorkingDirectory(); |
| 195 | + } |
| 196 | + |
| 197 | + public static Scene getCurrentScene() { |
| 198 | + return instance.currentScene; |
| 199 | + } |
| 200 | + |
| 201 | + public static void setScene(Scene scene) { |
| 202 | + instance.currentScene = scene; |
| 203 | + } |
| 204 | + |
| 205 | + public static int getScreenWidth() { |
| 206 | + return instance.config.getWindowWidth(); |
| 207 | + } |
| 208 | + |
| 209 | + public static int getScreenHeight() { |
| 210 | + return instance.getConfig().getWindowHeight(); |
| 211 | + } |
| 212 | +} |
0 commit comments