Guest User

Untitled

a guest
Apr 14th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 4.00 KB | None | 0 0
  1. #ERROR in winRefreshCb
  2. # Traceback (most recent call last)
  3. # events.nim(126)          events
  4. # glfw.nim(826)            update
  5. # glfw.nim(695)            :anonymous
  6. # events.nim(32)           winRefreshCb
  7. # opengl.nim(2393)         glClear
  8. # SIGSEGV: Illegal storage access. (Attempt to read from nil?)
  9. # Error: execution of an external program failed
  10.  
  11. from strutils import formatFloat, ffDecimal
  12. from unicode import toUTF8, Rune
  13. import glfw/glfw
  14. import glfw/wrapper
  15. import opengl
  16.  
  17. proc winPosCb(o: Win, pos: tuple[x, y: int]) =
  18.   echo "Window position: ", pos
  19.  
  20. proc mouseBtnCb(
  21.     o: Win, btn: MouseBtn, pressed: bool, modKeys: ModifierKeySet) =
  22.   echo($btn, ": ", if pressed: "down" else: "up")
  23.  
  24. proc winSizeCb(o: Win not nil, res: tuple[w, h: int]) =
  25.   echo "Window size: ", res
  26.  
  27. proc winFramebufSizeCb(o: Win, res: tuple[w, h: int]) =
  28.   #glViewport(0.GLint, 0.GLint, res.w.GLsizei, res.h.GLsizei)
  29.   echo "Window framebuffer size: ", res
  30.  
  31. proc winCloseCb(o: Win) =
  32.   echo "Window close event"
  33.  
  34. var nWinRefreshes = 0
  35.  
  36. proc winRefreshCb(o: Win) =
  37.   nWinRefreshes += 1
  38.   let
  39.     (w, h) = o.framebufSize
  40.     ratio = w.float / h.float
  41.   glViewport(0.GLint, 0.GLint, w.GLsizei, h.GLsizei)
  42.   glClear(GL_COLOR_BUFFER_BIT)
  43.   glMatrixMode(GL_PROJECTION)
  44.   glLoadIdentity()
  45.   glOrtho(-ratio, ratio, -1, 1, 1, -1)
  46.   glMatrixMode(GL_MODELVIEW)
  47.   glLoadIdentity()
  48.   glRotatef(wrapper.getTime().float * 50, 0, 0, 1)
  49.   glBegin(GL_TRIANGLES)
  50.   glColor3f(1, 0, 0)
  51.   glVertex3f(-0.6, -0.4, 0)
  52.   glColor3f(0, 1, 0)
  53.   glVertex3f(0.6, -0.4, 0)
  54.   glColor3f(0, 0, 1)
  55.   glVertex3f(0, 0.6, 0)
  56.   glEnd()
  57.  
  58. proc cursorPosCb(o: Win, pos: tuple[x, y: float64]) =
  59.   let xy = (x: formatFloat(pos.x, ffDecimal, 2),
  60.             y: formatFloat(pos.y, ffDecimal, 2))
  61.   echo "Cursor pos: ", xy
  62.  
  63. proc cursorEnterCb(o: Win, entered: bool) =
  64.   echo "The cursor ", if entered: "entered" else: "left", " the window area"
  65.  
  66. proc winFocusCb(o: Win; focused: bool) =
  67.   echo if focused: "Gained" else: "Lost", " window focus"
  68.  
  69. proc winIconifyCb(o: Win; iconified: bool) =
  70.   echo "Window ", if iconified: "iconified" else: "un-iconified"
  71.  
  72. proc scrollCb(o: Win, offset: tuple[x, y: float64]) =
  73.   let xy = (x: formatFloat(offset.x, ffDecimal, 2),
  74.             y: formatFloat(offset.y, ffDecimal, 2))
  75.   echo "Mouse scroll: ", xy
  76.  
  77. proc charCb(o: Win, codePoint: Rune) =
  78.   echo "Unicode char: ", codePoint.toUTF8()
  79.  
  80. proc keyCb(o: Win, key: Key, scanCode: int, action: KeyAction,
  81.     modKeys: ModifierKeySet) =
  82.   echo("Key: ", key, " (scan code: ", scanCode, "): ", action)
  83.  
  84.   if action != kaUp:
  85.     if key == keyEscape or key == keyF4 and mkAlt in modKeys:
  86.       o.shouldClose = true
  87.  
  88. glfw.init()
  89.  
  90. var done = false
  91.  
  92. # All arguments are optional. For OpenGL ES, use newGlEsWin.
  93. var win = newGlWin(
  94.   dim = (w: 640, h: 480),
  95.   title = "Event example",
  96.   fullscreen = nilMonitor(), # No monitor specified; don't go fullscreen.
  97.   shareResourcesWith = nilWin(), # Don't share resources.
  98.   visible = true,
  99.   decorated = true,
  100.   resizable = true,
  101.   stereo = false,
  102.   srgbCapableFramebuf = false,
  103.   bits = (r: 8, g: 8, b: 8, a: 8, stencil: 8, depth: 24),
  104.   accumBufBits = (r: 0, g: 0, b: 0, a: 0),
  105.   nAuxBufs = 0,
  106.   nMultiSamples = 0,
  107.   refreshRate = 0, # Use the current refresh rate.
  108.   version = glv12,
  109.   forwardCompat = false,
  110.   debugContext = false,
  111.   profile = glpAny,
  112.   robustness = glrNone
  113. )
  114.  
  115. win.makeContextCurrent
  116. swapInterval(1.Natural)
  117. setControlCHook(proc() {.noconv.} = done = true)
  118.  
  119. # Set up event handlers.
  120. win.winPosCb = winPosCb
  121. win.mouseBtnCb = mouseBtnCb
  122. win.winSizeCb = winSizeCb
  123. win.keyCb = keyCb
  124. win.framebufSizeCb = winFramebufSizeCb
  125. win.winCloseCb = winCloseCb
  126. win.winRefreshCb = winRefreshCb
  127. win.cursorPosCb = cursorPosCb
  128. win.cursorEnterCb = cursorEnterCb
  129. win.winFocusCb = winFocusCb
  130. win.winIconifyCb = winIconifyCb
  131. win.scrollCb = scrollCb
  132. win.charCb = charCb
  133. win.keyCb = keyCb
  134.  
  135. while not done and not win.shouldClose:
  136.   win.update() # Buffer swap + event poll.
  137.  
  138. win.destroy()
  139. glfw.terminate()
Advertisement
Add Comment
Please, Sign In to add comment