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

Add dynamic and static text examples. #29

Merged
merged 4 commits into from
Mar 22, 2022
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
2 changes: 1 addition & 1 deletion boxy.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT"
srcDir = "src"

requires "nim >= 1.2.2"
requires "pixie >= 3.0.0"
requires "pixie >= 4.1.0"
requires "vmath >= 1.1.0"
requires "opengl >= 1.2.3"
requires "bitty >= 0.1.4"
Binary file added examples/data/IBMPlexMono-Bold.ttf
Binary file not shown.
Binary file added examples/data/PinyonScript.ttf
Binary file not shown.
75 changes: 75 additions & 0 deletions examples/text_dynamic.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import boxy, opengl, windy, times

let windowSize = ivec2(1280, 800)

let window = newWindow("Windy + Boxy", windowSize)
makeContextCurrent(window)

loadExtensions()

let bxy = newBoxy()

# Load the images.
bxy.addImage("bg", readImage("examples/data/bg.png"))

var frame: int

let typeface = readTypeface("examples/data/IBMPlexMono-Bold.ttf")

proc drawText(
bxy: Boxy,
imageKey: string,
transform: Mat3,
typeface: Typeface,
text: string,
size: float32,
color: Color
) =
var font = newFont(typeface)
font.size = size
font.paint = color
let
arrangement = typeset(@[newSpan(text, font)], bounds = vec2(1280, 800))
globalBounds = arrangement.computeBounds(transform).snapToPixels()
textImage = newImage(globalBounds.w.int, globalBounds.h.int)
imageSpace = translate(-globalBounds.xy) * transform
textImage.fillText(arrangement, imageSpace)

bxy.addImage(imageKey, textImage)
bxy.drawImage(imageKey, globalBounds.xy)

# Called when it is time to draw a new frame.
proc display() =
# Clear the screen and begin a new frame.
bxy.beginFrame(windowSize)

# Draw the bg.
bxy.drawImage("bg", rect = rect(vec2(0, 0), windowSize.vec2))

bxy.drawText(
"main-image",
translate(vec2(100, 100)),
typeface,
"Current time:",
80,
color(1, 1, 1, 1)
)

bxy.drawText(
"main-image2",
translate(vec2(100, 200)),
typeface,
now().format("hh:mm:ss"),
80,
color(1, 1, 1, 1)
)

# End this frame, flushing the draw commands.
bxy.endFrame()
# Swap buffers displaying the new Boxy frame.
window.swapBuffers()
inc frame

while not window.closeRequested:
display()
pollEvents()
67 changes: 67 additions & 0 deletions examples/text_static.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import boxy, opengl, windy

let windowSize = ivec2(1280, 800)

let window = newWindow("Windy + Boxy", windowSize)
makeContextCurrent(window)

loadExtensions()

let bxy = newBoxy()

# Load the images.
bxy.addImage("bg", readImage("examples/data/bg.png"))

var frame: int

let
typeface1 = readTypeface("examples/data/PinyonScript.ttf")
font1 = newFont(typeface1)
font1.size = 40
font1.paint = "#FFFFFF"

let poem = """
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
“’Tis some visitor,” I muttered, “tapping at my chamber door—
Only this and nothing more.”

Ah, distinctly I remember it was in the bleak December;
And each separate dying ember wrought its ghost upon the floor.
Eagerly I wished the morrow;—vainly I had sought to borrow
From my books surcease of sorrow—sorrow for the lost Lenore—
For the rare and radiant maiden whom the angels name Lenore—
Nameless here for evermore.
"""

let
arrangement = typeset(@[newSpan(poem, font1)], bounds = vec2(1280, 800))
snappedBounds = arrangement.computeBounds().snapToPixels()
textImage = newImage(snappedBounds.w.int, snappedBounds.h.int)

textImage.fillText(arrangement, translate(-snappedBounds.xy))

bxy.addImage("text", textImage)

# Called when it is time to draw a new frame.
proc display() =
# Clear the screen and begin a new frame.
bxy.beginFrame(windowSize)

# Draw the bg.
bxy.drawImage("bg", rect = rect(vec2(0, 0), windowSize.vec2))

# Draw big static text:
bxy.drawImage("text", snappedBounds.xy + vec2(100, 100))

# End this frame, flushing the draw commands.
bxy.endFrame()
# Swap buffers displaying the new Boxy frame.
window.swapBuffers()
inc frame

while not window.closeRequested:
display()
pollEvents()
21 changes: 19 additions & 2 deletions src/boxy.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bitty, boxy/buffers, boxy/shaders, boxy/textures, bumpy, chroma, hashes,
opengl, os, pixie, strutils, tables, vmath
opengl, os, pixie, strutils, tables, vmath, sets

export pixie

Expand Down Expand Up @@ -37,6 +37,7 @@ type
mat: Mat4 ## The current matrix.
mats: seq[Mat4] ## The matrix stack.
entries: Table[string, ImageInfo]
entriesBuffered: HashSet[string] ## Entires used by not flushed yet.
tileSize: int
maxTiles: int
tileRun: int
Expand Down Expand Up @@ -94,7 +95,7 @@ proc draw(boxy: Boxy) =
## Flips - draws current buffer and starts a new one.
if boxy.quadCount == 0:
return

boxy.entriesBuffered.clear()
boxy.upload()

glUseProgram(boxy.activeShader.programId)
Expand Down Expand Up @@ -352,6 +353,13 @@ proc takeFreeTile(boxy: Boxy): int =

proc removeImage*(boxy: Boxy, key: string) =
## Removes an image, does nothing if the image has not been added.

if key in boxy.entriesBuffered:
raise newException(
BoxyError,
"Attempting to remove an image that is set to be drawn"
)

if key in boxy.entries:
for tileLevel in boxy.entries[key].tiles:
for tile in tileLevel:
Expand All @@ -360,8 +368,17 @@ proc removeImage*(boxy: Boxy, key: string) =
boxy.entries.del(key)

proc addImage*(boxy: Boxy, key: string, image: Image, genMipmaps = true) =

if key in boxy.entriesBuffered:
raise newException(
BoxyError,
"Attempting to modify an image that is already set to be drawn " &
"(try using a unique key?)"
)
boxy.removeImage(key)

boxy.entriesBuffered.incl(key)

var imageInfo: ImageInfo
imageInfo.size = ivec2(image.width.int32, image.height.int32)

Expand Down