Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #86

Merged
merged 8 commits into from
Apr 27, 2022
Merged

Dev #86

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/icon.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pixie, windy

when defined(windows):
# Window icon API only currently supported on Windows
when defined(windows) or defined(linux):
# Window icon API only currently supported on Windows and Linux

let window = newWindow("Windy Cursor", ivec2(1280, 800))
let window = newWindow("Windy Icon", ivec2(1280, 800))
window.makeContextCurrent()

let
Expand Down
105 changes: 105 additions & 0 deletions examples/triangle.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import opengl, windy

## This example shows how to get a plain openGL triangle working with windy.

let window = newWindow("Windy Triangle", ivec2(1280, 800))

window.makeContextCurrent()
loadExtensions()

let vertexShaderText = """
#version 410
in vec3 aPos;
out vec3 pos;
void main()
{
pos = aPos;
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
"""

let fragmentShaderText = """
#version 410
in vec3 pos;
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0f, 0.5f, pos.x, 1.0f);
}
"""

proc checkError*(shader: GLuint) =
var code: GLint
glGetShaderiv(shader, GL_COMPILE_STATUS, addr code)
if code.GLboolean == GL_FALSE:
var length: GLint = 0
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, addr length)
var log = newString(length.int)
glGetShaderInfoLog(shader, length, nil, log.cstring)
echo log

proc checkLinkError*(program: GLuint) =
var code: GLint
glGetProgramiv(program, GL_LINK_STATUS, addr code)
if code.GLboolean == GL_FALSE:
var length: GLint = 0
glGetProgramiv(program, GL_INFO_LOG_LENGTH, addr length)
var log = newString(length.int)
glGetProgramInfoLog(program, length, nil, log.cstring)
echo log

var vertexShader = glCreateShader(GL_VERTEX_SHADER)
var vertexShaderTextArr = allocCStringArray([vertexShaderText])
glShaderSource(vertexShader, 1.GLsizei, vertexShaderTextArr, nil)
glCompileShader(vertex_shader)
checkError(vertexShader)

var fragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
var fragmentShaderTextArr = allocCStringArray([fragmentShaderText])
glShaderSource(fragmentShader, 1.GLsizei, fragmentShaderTextArr, nil)
glCompileShader(fragmentShader)
checkError(fragmentShader)

var program = glCreateProgram()
glAttachShader(program, vertexShader)
glAttachShader(program, fragmentShader)
glLinkProgram(program)
checkLinkError(program)

var vboData = @[
vec3(-0.5f, -0.5f, 0.0f),
vec3(0.5f, -0.5f, 0.0f),
vec3(0.0f, 0.5f, 0.0f)
]

var vbo: uint32
glGenBuffers(1, vbo.addr)

var vao: uint32
glGenVertexArrays(1, vao.addr);

glBindVertexArray(vao);

glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, vboData.len*4*3, vboData[0].addr, GL_STATIC_DRAW)

glVertexAttribPointer(0, 3, cGL_FLOAT, GL_FALSE, 3*4, nil)
glEnableVertexAttribArray(0)

proc display() =

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glViewport(0, 0, window.size.x, window.size.y)

glUseProgram(program)
glBindVertexArray(vao)
glDrawArrays(GL_TRIANGLES, 0, 3)

# Your OpenGL display code here
window.swapBuffers()

while not window.closeRequested:
display()
pollEvents()
29 changes: 27 additions & 2 deletions src/windy/platforms/linux/x11.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import ../../common, ../../internal, os, sequtils, sets, strformat, times,
unicode, vmath, x11/glx, x11/keysym, x11/x, x11/xevent, x11/xlib
unicode, vmath, x11/glx, x11/keysym, x11/x, x11/xevent, x11/xlib, pixie
type
XWindow = x.Window

Window* = ref object
onCloseRequest*: Callback
onFrame*: Callback
onMove*: Callback
onResize*: Callback
onFocusChange*: Callback
Expand Down Expand Up @@ -69,6 +70,7 @@ proc initConstants(display: Display) =
xaNetWMStateFullscreen = display.XInternAtom("_NET_WM_STATE_FULLSCREEN", 0)
xaNetWMName = display.XInternAtom("_NET_WM_NAME", 0)
xaUTF8String = display.XInternAtom("UTF8_STRING", 0)
xaNetWMIcon = display.XInternAtom("_NET_WM_ICON", 0)
xaNetWMIconName = display.XInternAtom("_NET_WM_ICON_NAME", 0)
xaWMDeleteWindow = display.XInternAtom("WM_DELETE_WINDOW", 0)
xaNetWMSyncRequest = display.XInternAtom("_NET_WM_SYNC_REQUEST", 0)
Expand Down Expand Up @@ -782,6 +784,8 @@ proc pollEvents(window: Window) =
window.prevSize = ev.configure.size
if window.onResize != nil:
window.onResize()
if window.onFrame != nil:
window.onFrame()

of xeMotion:
window.mousePrevPos = window.mousePos
Expand Down Expand Up @@ -989,6 +993,10 @@ proc setClipboardString*(s: string) =
display.XSetSelectionOwner(xaClipboard, clipboardWindow)

proc pollEvents* =
for window in windows:
if window.onFrame != nil:
window.onFrame()

let ws = windows
windows = @[]

Expand All @@ -1001,7 +1009,7 @@ proc pollEvents* =
if clipboardWindow != 0:
discard processClipboardEvents()
for window in windows:
pollEvents window
pollEvents(window)

proc closeIme*(window: Window) =
discard
Expand All @@ -1011,3 +1019,20 @@ proc imeCursorIndex*(window: Window): int =

proc imeCompositionString*(window: Window): string =
discard

proc `icon=`*(window: Window, icon: Image) =
var data: seq[uint64]
data.add icon.width.uint64
data.add icon.height.uint64
for c in icon.data:
data.add (cast[uint32](c)).uint64

display.XChangeProperty(
window.handle,
xaNetWMIcon,
xaCardinal,
32,
pmReplace,
cast[cstring](data[0].addr),
(data.len).cint
)
1 change: 1 addition & 0 deletions src/windy/platforms/linux/x11/x.nim
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ var
xaNetWMStateMaximizedHorz*: Atom
xaNetWMStateMaximizedVert*: Atom
xaWMState*: Atom
xaNetWMIcon*: Atom
xaNetWMStateHiden*: Atom
xaNetWMStateFullscreen*: Atom
xaNetWMName*: Atom
Expand Down
2 changes: 2 additions & 0 deletions src/windy/platforms/linux/x11/xevent.nim
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,6 @@ proc XSetErrorHandler*(handler: ErrorHandleProc)

proc XLookupKeysym*(e: ptr XKeyEvent, i: cint): KeySym

proc XNextEvent*(d: Display, event: ptr XEvent)

{.pop.}
7 changes: 7 additions & 0 deletions src/windy/platforms/linux/x11/xlib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ proc XCreateWindow*(
): Window
proc XDestroyWindow*(d: Display, window: Window)

proc DefaultScreen*(d: Display): Window
proc RootWindow*(d: Display, window: Window): Window

proc XCreateSimpleWindow*(
d: Display,
root: Window;
Expand Down Expand Up @@ -266,6 +269,10 @@ proc XChangeProperty*(
d: Display, window: Window; property: Atom; kind: Atom;
format: cint; mode: PropMode; data: cstring; len: cint
)
proc XChangeProperty*(
d: Display, window: Window; property: Atom; kind: Atom;
format: cint; mode: PropMode; data: pointer; len: cint
)
proc XDeleteProperty*(d: Display, window: Window; property: Atom)

proc Xutf8SetWMProperties*(
Expand Down