Skip to content

Commit

Permalink
Merge pull request nimgl#11 from slugspace/master
Browse files Browse the repository at this point in the history
GLFW callback events
  • Loading branch information
Leonardo Mariscal authored Jan 5, 2019
2 parents 54fcc49 + 8b1a56f commit bf3803f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ 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.
. Remember to make the Pull Request to the `develop` branch

=== License Note

Expand Down
8 changes: 8 additions & 0 deletions CONTRIBUTORS.adoc
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion nimgl.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.3.0"
version = "0.3.1"
author = "Leonardo Mariscal"
description = "Nim Game Library"
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions src/nimgl/glfw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions src/nimgl/imgui/impl_glfw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,37 @@ var
gMouseJustPressed: array[5, bool]
gMouseCursors: array[ImGuiMouseCursor_COUNT, GLFWCursor]

# 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()

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 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 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 gPrevKeyCallback != nil:
gPrevKeyCallback(window, key, scancode, action, mods)

let io = igGetIO()
if key.ord < 511 and key.ord >= 0:
if action == kaPress:
Expand All @@ -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 gPrevCharCallback != nil:
gPrevCharCallback(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.
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
Expand Down
2 changes: 1 addition & 1 deletion src/nimgl/imgui/impl_opengl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
21 changes: 12 additions & 9 deletions src/nimgl/stb_image.nim → src/nimgl/stb/image.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

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
ImageData* = object
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
Expand All @@ -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
Expand All @@ -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

0 comments on commit bf3803f

Please sign in to comment.