Advertisement
Guest User

Untitled

a guest
Apr 9th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.35 KB | None | 0 0
  1. class Window(
  2.     val title: String,
  3.     var width: Int,
  4.     var height: Int
  5. ) {
  6.  
  7.     private val FOV = Math.toRadians(60.0).toFloat()
  8.     private val Z_NEAR = 0.01f
  9.     private val Z_FAR = 1000f
  10.  
  11.     var vSync: Boolean = false
  12.     var fullscreen: Boolean = false
  13.     var resized = false
  14.     var debug: Boolean = false
  15.     var clearColor: Vector4f = Vector4f(0f, 0f, 0f, 0f)
  16.     set(value) {
  17.         field = value
  18.         updateClearColor()
  19.     }
  20.     var projectionMatrix: Matrix4f = Matrix4f()
  21.     get() {
  22.         //TODO Ha senso aggiornare ad ogni chiamata? Quanto mi costa?
  23.         val aspectRatio = width.toFloat() / height.toFloat()
  24.         field = field.setPerspective(FOV, aspectRatio, Z_NEAR, Z_FAR)
  25.         return field
  26.     }
  27.  
  28.     var windowId: Long = -1
  29.         private set
  30.  
  31.     init {
  32.         createPrint(System.err).set();
  33.         if (!glfwInit()) {
  34.             throw IllegalStateException("Unable to initialize GLFW")
  35.         }
  36.         setupWindowHint()
  37.         windowId = glfwCreateWindow(width, height, title, 0, 0)
  38.  
  39.         glfwSetFramebufferSizeCallback(windowId, object : GLFWFramebufferSizeCallback() {
  40.             override fun invoke(window: Long, width: Int, height: Int) {
  41.                 val windowResizedEvent = WindowResizedEvent(width,height)
  42.                 GameEngine.eventBus.dispatchEvent(windowResizedEvent)
  43.                 resized = true
  44.             }
  45.         })
  46.         glfwSetKeyCallback(windowId,object : GLFWKeyCallback(){
  47.             override fun invoke(window: Long, key: Int, scancode: Int, action: Int, mods: Int) {
  48.                 when (action) {
  49.                     GLFW_PRESS -> GameEngine.eventBus.dispatchEvent(KeyPressedEvent(key))
  50.                     GLFW_RELEASE -> GameEngine.eventBus.dispatchEvent(KeyReleasedEvent(key))
  51.                     GLFW_REPEAT -> GameEngine.eventBus.dispatchEvent(KeyHoldEvent(key))
  52.                     else -> GameEngine.eventBus.dispatchEvent(KeyEvent(key, action))
  53.                 }
  54.             }
  55.         })
  56.         glfwMakeContextCurrent(windowId)
  57.         GL.createCapabilities()
  58.         updateClearColor()
  59.     }
  60.  
  61.     fun enableFullScreen(flag: Boolean) {
  62.         fullscreen = flag
  63.         if (fullscreen) glfwMaximizeWindow(windowId) else {
  64.             val monitor = glfwGetVideoMode(glfwGetPrimaryMonitor())
  65.             Objects.requireNonNull(monitor)
  66.             if (monitor != null) {
  67.                 glfwSetWindowPos(
  68.                     windowId,
  69.                     (monitor.width() - width) / 2,
  70.                     (monitor.height() - height) / 2
  71.                 )
  72.             }
  73.         }
  74.     }
  75.  
  76.     fun updateClearColor() {
  77.         glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w)
  78.     }
  79.  
  80.     fun enableVsync(flag: Boolean) {
  81.         if (flag) glfwSwapInterval(1) else glfwSwapInterval(0)
  82.     }
  83.  
  84.     fun enableDepthTest(flag: Boolean) {
  85.         if (flag) glEnable(GL_DEPTH_TEST) else glDisable(GL_DEPTH_TEST)
  86.     }
  87.  
  88.     fun enableBlending(flag: Boolean) {
  89.         if (flag) glEnable(GL_BLEND) else glDisable(GL_BLEND)
  90.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  91.     }
  92.  
  93.     fun enableCullFace(flag: Boolean) {
  94.         if (flag) glEnable(GL_CULL_FACE) else glDisable(GL_CULL_FACE)
  95.     }
  96.  
  97.     fun setCullFace(face: Int) {
  98.         glCullFace(face)
  99.     }
  100.  
  101.     fun showWindow(flag: Boolean) {
  102.         if (flag) glfwShowWindow(windowId) else glfwHideWindow(windowId)
  103.     }
  104.  
  105.     fun enableDebugMode(flag: Boolean) {
  106.         debug = flag
  107.         if (debug) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
  108.     }
  109.  
  110.     fun restoreState() {
  111.         enableDepthTest(true)
  112.         enableBlending(true)
  113.         enableCullFace(true)
  114.         setCullFace(GL_BACK)
  115.     }
  116.  
  117.     private fun setupWindowHint() {
  118.         glfwDefaultWindowHints()
  119.         glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
  120.         glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE)
  121.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
  122.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2)
  123.         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
  124.         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
  125.     }
  126.  
  127.     fun update() {
  128.         if (resized) {
  129.             glViewport(0, 0, width, height)
  130.             resized = false
  131.         }
  132.         glfwSwapBuffers(windowId)
  133.         glfwPollEvents()
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement