Skip to content
Merged
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
84 changes: 17 additions & 67 deletions src/pixie/demo.nim
Original file line number Diff line number Diff line change
@@ -1,91 +1,41 @@
import opengl, pixie, pixie/contexts
import boxy, opengl, pixie, windy

export pixie

import staticglfw except Image

export staticglfw except Image
export pixie, windy

var
dpi: float32 = 1.0
window*: Window
screen*: Image
ctx*: Context
window*: Window

proc getMousePos*(): Vec2 =
## Get the mouse position.
var xpos, ypos: float64
getCursorPos(window, xpos.addr, ypos.addr)
vec2(xpos, ypos) / dpi

proc isMouseDown*(): bool =
## Get if the left mouse button is down.
getMouseButton(window, MOUSE_BUTTON_LEFT) == PRESS

proc isKeyDown*(keyCode: int): bool =
## Get if the key is currently being held down.
## See key codes: https://www.glfw.org/docs/3.3/group__keys.html
## Examples: KEY_SPACE, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
getKey(window, keyCode.cint) == PRESS
bxy: Boxy

proc tick*() =
## Called this every frame in a while loop.

# Update texture with new pixels from surface.
var dataPtr = screen.data[0].addr
glTexSubImage2D(
GL_TEXTURE_2D, 0, 0, 0,
screen.width.GLsizei, screen.height.GLsizei,
GL_RGBA, GL_UNSIGNED_BYTE, dataPtr
)
bxy.addImage("screen", screen, genMipmaps = false)

# Draw a quad over the whole screen.
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_QUADS)
glTexCoord2d(0.0, 0.0); glVertex2d(-1.0, +1.0)
glTexCoord2d(1.0, 0.0); glVertex2d(+1.0, +1.0)
glTexCoord2d(1.0, 1.0); glVertex2d(+1.0, -1.0)
glTexCoord2d(0.0, 1.0); glVertex2d(-1.0, -1.0)
glEnd()
bxy.beginFrame(window.framebufferSize)
bxy.drawRect(rect(vec2(0, 0), window.framebufferSize.vec2), color(1, 1, 1, 1))
bxy.drawImage("screen", vec2(0, 0))
bxy.endFrame()

swapBuffers(window)

pollEvents()

if windowShouldClose(window) == 1:
if window.closeRequested:
quit()

ctx.setTransform(scale(vec2(dpi, dpi)))
ctx.setTransform(scale(vec2(window.contentScale, window.contentScale)))

proc start*(title = "Demo", width = 800, height = 600) =
proc start*(title = "Demo", windowSize = ivec2(800, 600)) =
## Start the demo.
if init() == 0:
quit("Failed to Initialize GLFW.")
window = newWindow(title, windowSize)
window.style = Decorated

windowHint(RESIZABLE, false.cint)
window = createWindow(width.cint, height.cint, title, nil, nil)
if window == nil:
quit("Failed to create window.")
makeContextCurrent(window)
loadExtensions()

var xscale, yscale: cfloat
window.getWindowContentScale(xscale.addr, yscale.addr)
dpi = xscale
screen = newImage(int(width.float32 * dpi), int(height.float32 * dpi))
window.setWindowSize(screen.width.cint, screen.height.cint)
glViewport(0, 0, screen.width.cint, screen.height.cint)
let pixelSize = windowSize.vec2 * window.contentScale
screen = newImage(pixelSize.x.int, pixelSize.y.int)
ctx = newContext(screen)

# Allocate a texture and bind it.
var dataPtr = screen.data[0].addr
glTexImage2D(
GL_TEXTURE_2D, 0, 3,
screen.width.GLsizei, screen.height.GLsizei,
0, GL_RGBA, GL_UNSIGNED_BYTE, dataPtr
)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glEnable(GL_TEXTURE_2D)
bxy = newBoxy()
3 changes: 3 additions & 0 deletions src/pixie/paths.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,9 @@ proc fillShapes(
pathHeight = min(image.height, (bounds.y + bounds.h).int)
partitioning = partitionSegments(segments, startY, pathHeight - startY)

if pathWidth == 0:
return

var
coverages = newSeq[uint8](pathWidth)
hits = newSeq[(float32, int16)](partitioning.maxEntryCount)
Expand Down