From 2c95de7e1357cfefe90fba3117a702156850a696 Mon Sep 17 00:00:00 2001 From: Leonardo Mariscal Date: Sat, 10 Nov 2018 22:40:32 -0600 Subject: [PATCH 1/4] moved stb library to itws own folder and made proper changes to keep a style to the library --- .github/stale.yml | 2 +- CONTRIBUTING.adoc | 1 + src/nimgl/glfw.nim | 6 +++--- src/nimgl/{stb_image.nim => stb/image.nim} | 21 ++++++++++++--------- 4 files changed, 17 insertions(+), 13 deletions(-) rename src/nimgl/{stb_image.nim => stb/image.nim} (69%) diff --git a/.github/stale.yml b/.github/stale.yml index 1ee2842..ab5dbd9 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -6,7 +6,7 @@ daysUntilClose: 1 exemptLabels: - important -staleLabel: wontfix +staleLabel: "Status: Abandoned" markComment: > Thanks for contributing to this issue. As it has been 61 days since the last activity, we are automatically closing the issue in 24 hours. This is often because the request was already solved in some way and it just wasn't updated or diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc index 354b80f..cf5ab8e 100644 --- a/CONTRIBUTING.adoc +++ b/CONTRIBUTING.adoc @@ -43,6 +43,7 @@ find them at http://editorconfig.org/. . Run `nimble build` . Modify, commit and push. . Create Pull Request with squashed commits and changelog. +. Remember to make the Pull Request to the `develop` branch === License Note diff --git a/src/nimgl/glfw.nim b/src/nimgl/glfw.nim index 345bc15..18f74c9 100644 --- a/src/nimgl/glfw.nim +++ b/src/nimgl/glfw.nim @@ -7,7 +7,7 @@ ## Or continue reading to get the documentation shown here. import private/logo -import stb_image +import stb/image when defined(glfwDLL): when defined(windows): @@ -621,10 +621,10 @@ proc glfwCreateWindow*(width: int32, height: int32, title: cstring = "NimGL", mo ## Utility to create the window with a proper icon. result = glfwCreateWindowC(width, height, title, monitor, share) if not icon: return result - let data: ImageData = stbi_load_from_memory(cast[ptr char](nimgl_logo[0].addr), nimgl_logo.len.int32) + let data: ImageData = stbiLoadFromMemory(cast[ptr char](nimgl_logo[0].addr), nimgl_logo.len.int32) var image: GLFWImage = GLFWImage(pixels: data.data, width: data.width, height: data.height) result.setWindowIcon(1, image.addr) - image.pixels.stbi_image_free() + image.pixels.stbiImageFree() proc glfwInit*(): bool {.glfw_lib, importc: "glfwInit".} ## Initializes the GLFW library. Before most GLFW functions can diff --git a/src/nimgl/stb_image.nim b/src/nimgl/stb/image.nim similarity index 69% rename from src/nimgl/stb_image.nim rename to src/nimgl/stb/image.nim index cc07e0c..183bf7a 100644 --- a/src/nimgl/stb_image.nim +++ b/src/nimgl/stb/image.nim @@ -12,8 +12,8 @@ from os import splitPath -{.passC: "-DSTB_IMAGE_IMPLEMENTATION -I" & currentSourcePath().splitPath.head & "/private/stb", - compile: "private/stb/stb_image.c"} +{.passC: "-DSTB_IMAGE_IMPLEMENTATION -I" & currentSourcePath().splitPath.head & "/../private/stb", + compile: "../private/stb/stb_image.c"} {.pragma: stb_image, cdecl, importc.} type @@ -21,9 +21,9 @@ type width*: int32 height*: int32 channels*: int32 - data*: ptr char + data*: ptr cuchar -proc stbi_load*(filename: cstring, width: ptr int32, height: ptr int32, channels: ptr int32, components: int32 = 0): ptr char {.stb_image, importc: "stbi_load".} +proc stbiLoad*(filename: cstring, width: ptr int32, height: ptr int32, channels: ptr int32, components: int32 = 0): ptr cuchar {.stb_image, importc: "stbi_load".} ## returns a pointer to the image requested, nil if nothind found. ## width and height as you imagine are from the image ## channels, how many channels the image has @@ -34,12 +34,12 @@ proc stbi_load*(filename: cstring, width: ptr int32, height: ptr int32, channels ## components, define if you require some especific number of channels. If 0 ## uses the number of channels the image has. -proc stbi_load*(filename: cstring, components: int32 = 0): ImageData = +proc stbiLoad*(filename: cstring, components: int32 = 0): ImageData = ## a utility to only give the filename and get a tupple with all the data ## more info in the original proc result.data = stbi_load(filename, result.width.addr, result.height.addr, result.channels.addr, components) -proc stbi_load_from_memory*(buffer: ptr char, len: int32, width: ptr int32, height: ptr int32, channels: ptr int32, components: int32 = 0): ptr char {.stb_image, importc: "stbi_load_from_memory".} +proc stbiLoadFromMemory*(buffer: ptr cuchar, len: int32, width: ptr int32, height: ptr int32, channels: ptr int32, components: int32 = 0): ptr cuchar {.stb_image, importc: "stbi_load_from_memory".} ## returns a pointer to the image loaded from the buffer ## width and height as you imagine are from the image ## channels, how many channels the image has @@ -50,13 +50,16 @@ proc stbi_load_from_memory*(buffer: ptr char, len: int32, width: ptr int32, heig ## components, define if you require some especific number of channels. If 0 ## uses the number of channels the image has. -proc stbi_load_from_memory*(buffer: ptr char, len: int32, components: int32 = 0): ImageData = +proc stbiLoadFromMemory*(buffer: ptr cuchar, len: int32, components: int32 = 0): ImageData = ## a utility to only give the filename and get a tupple with all the data ## more info in the original proc result.data = stbi_load_from_memory(buffer, len, result.width.addr, result.height.addr, result.channels.addr, components) -proc stbi_image_free*(data: ptr char): void {.stb_image, importc: "stbi_image_free".} +proc stbiImageFree*(data: ptr cuchar): void {.stb_image, importc: "stbi_image_free".} ## frees the data, loaded from stbi_load -proc stbi_set_flip_vertically_on_load*(state: bool): void {.stb_image, importc: "stbi_set_flip_vertically_on_load".} +proc imageFree*(image: ImageData): void = + image.data.stbiImageFree() + +proc stbiSetFlipVerticallyOnLoad*(state: bool): void {.stb_image, importc: "stbi_set_flip_vertically_on_load".} ## flip the image vertically, so the first pixel in the output array is the bottom left From b0d524e3f21e4fdbe51172b3a711990fb1d4cf1d Mon Sep 17 00:00:00 2001 From: slugspace Date: Fri, 4 Jan 2019 16:12:26 +0000 Subject: [PATCH 2/4] GLFW callback events Previous callbacks are stored so they can be chain called. --- src/nimgl/imgui/impl_glfw.nim | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/nimgl/imgui/impl_glfw.nim b/src/nimgl/imgui/impl_glfw.nim index 78856b2..c3667a9 100644 --- a/src/nimgl/imgui/impl_glfw.nim +++ b/src/nimgl/imgui/impl_glfw.nim @@ -20,6 +20,12 @@ var gMouseJustPressed: array[5, bool] gMouseCursors: array[ImGuiMouseCursor_COUNT, GLFWCursor] + #Store previous callbacks so they can be chained + prevMouseButtonCallback: glfwMouseButtonProc + prevScrollCallback: glfwScrollProc + prevKeyCallback: glfwKeyProc + prevCharCallback: glfwCharProc + proc igGlfwGetClipboardText(user_data: pointer): cstring {.cdecl.} = cast[GLFWwindow](user_data).getClipboardString() @@ -27,15 +33,24 @@ proc igGlfwSetClipboardText(user_data: pointer, text: cstring): void {.cdecl.} = cast[GLFWwindow](user_data).setClipboardString(text) proc igGlfwMouseCallback*(window: GLFWWindow, button: GLFWMouseButton, action: GLFWMouseAction, mods: GLFWKeyMod): void {.cdecl.} = + if prevMouseButtonCallback != nil: + prevMouseButtonCallback(window, button, action, mods) + if action == maPress and button.ord >= 0 and button.ord < gMouseJustPressed.len: gMouseJustPressed[button.ord] = true proc igGlfwScrollCallback*(window: GLFWWindow, xoff: float64, yoff: float64): void {.cdecl.} = + if prevScrollCallback != nil: + prevScrollCallback(window, xoff, yoff) + let io = igGetIO() io.mouseWheelH += xoff.float32 io.mouseWheel += yoff.float32 proc igGlfwKeyCallback*(window: GLFWWindow, key: GLFWKey, scancode: int32, action: GLFWKeyAction, mods: GLFWKeyMod): void {.cdecl.} = + if prevKeyCallback != nil: + prevKeyCallback(window, key, scancode, action, mods) + let io = igGetIO() if key.ord < 511 and key.ord >= 0: if action == kaPress: @@ -49,15 +64,19 @@ proc igGlfwKeyCallback*(window: GLFWWindow, key: GLFWKey, scancode: int32, actio io.keySuper = io.keysDown[keyLeftSuper.ord] or io.keysDown[keyRightSuper.ord] proc igGlfwCharCallback*(window: GLFWWindow, code: uint32): void {.cdecl.} = + if prevCharCallback != nil: + prevCharCallback(window, code) + let io = igGetIO() if code > 0'u32 and code < 0x10000'u32: io.addInputCharacter(cast[ImWchar](code)) proc igGlfwInstallCallbacks(window: GLFWwindow) = - discard gWindow.setMouseButtonCallback(igGlfwMouseCallback) - discard gWindow.setScrollCallback(igGlfwScrollCallback) - discard gWindow.setKeyCallback(igGlfwKeyCallback) - discard gWindow.setCharCallback(igGlfwCharCallback) + # The already set callback proc should be returned. Store these and and chain callbacks. + prevMouseButtonCallback = gWindow.setMouseButtonCallback(igGlfwMouseCallback) + prevScrollCallback = gWindow.setScrollCallback(igGlfwScrollCallback) + prevKeyCallback = gWindow.setKeyCallback(igGlfwKeyCallback) + prevCharCallback = gWindow.setCharCallback(igGlfwCharCallback) proc igGlfwInit(window: GLFWwindow, install_callbacks: bool, client_api: GlfwClientApi): bool = gWindow = window From e171bdc2e4a9922a9d20b364f9cb8f2157ddf796 Mon Sep 17 00:00:00 2001 From: slugspace Date: Fri, 4 Jan 2019 17:18:56 +0000 Subject: [PATCH 3/4] Fix restoring modified GL state. --- src/nimgl/imgui/impl_opengl.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nimgl/imgui/impl_opengl.nim b/src/nimgl/imgui/impl_opengl.nim index 74f73c0..21db5a8 100644 --- a/src/nimgl/imgui/impl_opengl.nim +++ b/src/nimgl/imgui/impl_opengl.nim @@ -263,7 +263,7 @@ proc igOpenGL3RenderDrawData*(data: ptr ImDrawData) = if last_enable_blend: glEnable(GL_BLEND) else: glDisable(GL_BLEND) if last_enable_cull_face: glEnable(GL_CULL_FACE) else: glDisable(GL_CULL_FACE) if last_enable_depth_test: glEnable(GL_DEPTH_TEST) else: glDisable(GL_DEPTH_TEST) - if last_enable_scissor_test: glEnable(GL_SCISSOR_TEST) else: glDisable(GL_SCISSOR_BOX) + if last_enable_scissor_test: glEnable(GL_SCISSOR_TEST) else: glDisable(GL_SCISSOR_TEST) glViewport(last_viewport[0], last_viewport[1], last_viewport[2], last_viewport[3]) glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2], last_scissor_box[3]) From b081cf8fcc4c975fded7fef0d7717023e1dffda0 Mon Sep 17 00:00:00 2001 From: Leonardo Mariscal Date: Fri, 4 Jan 2019 15:43:53 -0600 Subject: [PATCH 4/4] fixed prefix to global variables in impl_glfw, pushed version by one and created CONTRIBUTORS list --- CONTRIBUTING.adoc | 1 + CONTRIBUTORS.adoc | 8 ++++++++ nimgl.nimble | 2 +- src/nimgl/imgui/impl_glfw.nim | 34 +++++++++++++++++----------------- 4 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 CONTRIBUTORS.adoc diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc index 354b80f..4981db7 100644 --- a/CONTRIBUTING.adoc +++ b/CONTRIBUTING.adoc @@ -42,6 +42,7 @@ find them at http://editorconfig.org/. . Clone the repo with git. . Run `nimble build` . Modify, commit and push. +. Put yourself on the link:CONTRIBUTORS.adoc[CONTRIBUTORS] file! . Create Pull Request with squashed commits and changelog. === License Note diff --git a/CONTRIBUTORS.adoc b/CONTRIBUTORS.adoc new file mode 100644 index 0000000..44505dd --- /dev/null +++ b/CONTRIBUTORS.adoc @@ -0,0 +1,8 @@ +== Contributors + +Wanted to create a file with all the contributors. + +To thank all of the amazing people investing their free time in this project. + +Does not matter the size or impact, matters the effort! + + +- link:https://github.com/zacharycarter[@zacharycarter] +- link:https://github.com/slugspace[@slugspace] diff --git a/nimgl.nimble b/nimgl.nimble index 5280c08..4bd41b0 100644 --- a/nimgl.nimble +++ b/nimgl.nimble @@ -1,6 +1,6 @@ # Package -version = "0.3.0" +version = "0.3.1" author = "Leonardo Mariscal" description = "Nim Game Library" license = "MIT" diff --git a/src/nimgl/imgui/impl_glfw.nim b/src/nimgl/imgui/impl_glfw.nim index c3667a9..10f8df9 100644 --- a/src/nimgl/imgui/impl_glfw.nim +++ b/src/nimgl/imgui/impl_glfw.nim @@ -20,11 +20,11 @@ var gMouseJustPressed: array[5, bool] gMouseCursors: array[ImGuiMouseCursor_COUNT, GLFWCursor] - #Store previous callbacks so they can be chained - prevMouseButtonCallback: glfwMouseButtonProc - prevScrollCallback: glfwScrollProc - prevKeyCallback: glfwKeyProc - prevCharCallback: glfwCharProc + # Store previous callbacks so they can be chained + gPrevMouseButtonCallback: glfwMouseButtonProc = nil + gPrevScrollCallback: glfwScrollProc = nil + gPrevKeyCallback: glfwKeyProc = nil + gPrevCharCallback: glfwCharProc = nil proc igGlfwGetClipboardText(user_data: pointer): cstring {.cdecl.} = cast[GLFWwindow](user_data).getClipboardString() @@ -33,23 +33,23 @@ proc igGlfwSetClipboardText(user_data: pointer, text: cstring): void {.cdecl.} = cast[GLFWwindow](user_data).setClipboardString(text) proc igGlfwMouseCallback*(window: GLFWWindow, button: GLFWMouseButton, action: GLFWMouseAction, mods: GLFWKeyMod): void {.cdecl.} = - if prevMouseButtonCallback != nil: - prevMouseButtonCallback(window, button, action, mods) + if gPrevMouseButtonCallback != nil: + gPrevMouseButtonCallback(window, button, action, mods) if action == maPress and button.ord >= 0 and button.ord < gMouseJustPressed.len: gMouseJustPressed[button.ord] = true proc igGlfwScrollCallback*(window: GLFWWindow, xoff: float64, yoff: float64): void {.cdecl.} = - if prevScrollCallback != nil: - prevScrollCallback(window, xoff, yoff) + if gPrevScrollCallback != nil: + gPrevScrollCallback(window, xoff, yoff) let io = igGetIO() io.mouseWheelH += xoff.float32 io.mouseWheel += yoff.float32 proc igGlfwKeyCallback*(window: GLFWWindow, key: GLFWKey, scancode: int32, action: GLFWKeyAction, mods: GLFWKeyMod): void {.cdecl.} = - if prevKeyCallback != nil: - prevKeyCallback(window, key, scancode, action, mods) + if gPrevKeyCallback != nil: + gPrevKeyCallback(window, key, scancode, action, mods) let io = igGetIO() if key.ord < 511 and key.ord >= 0: @@ -64,8 +64,8 @@ proc igGlfwKeyCallback*(window: GLFWWindow, key: GLFWKey, scancode: int32, actio io.keySuper = io.keysDown[keyLeftSuper.ord] or io.keysDown[keyRightSuper.ord] proc igGlfwCharCallback*(window: GLFWWindow, code: uint32): void {.cdecl.} = - if prevCharCallback != nil: - prevCharCallback(window, code) + if gPrevCharCallback != nil: + gPrevCharCallback(window, code) let io = igGetIO() if code > 0'u32 and code < 0x10000'u32: @@ -73,10 +73,10 @@ proc igGlfwCharCallback*(window: GLFWWindow, code: uint32): void {.cdecl.} = proc igGlfwInstallCallbacks(window: GLFWwindow) = # The already set callback proc should be returned. Store these and and chain callbacks. - prevMouseButtonCallback = gWindow.setMouseButtonCallback(igGlfwMouseCallback) - prevScrollCallback = gWindow.setScrollCallback(igGlfwScrollCallback) - prevKeyCallback = gWindow.setKeyCallback(igGlfwKeyCallback) - prevCharCallback = gWindow.setCharCallback(igGlfwCharCallback) + gPrevMouseButtonCallback = gWindow.setMouseButtonCallback(igGlfwMouseCallback) + gPrevScrollCallback = gWindow.setScrollCallback(igGlfwScrollCallback) + gPrevKeyCallback = gWindow.setKeyCallback(igGlfwKeyCallback) + gPrevCharCallback = gWindow.setCharCallback(igGlfwCharCallback) proc igGlfwInit(window: GLFWwindow, install_callbacks: bool, client_api: GlfwClientApi): bool = gWindow = window