diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/impure/graphics.nim | 107 | ||||
-rwxr-xr-x | lib/posix/posix.nim | 4 | ||||
-rwxr-xr-x | lib/system.nim | 31 |
3 files changed, 90 insertions, 52 deletions
diff --git a/lib/impure/graphics.nim b/lib/impure/graphics.nim index aa4d08dd8..9ddb69768 100755 --- a/lib/impure/graphics.nim +++ b/lib/impure/graphics.nim @@ -406,20 +406,22 @@ proc drawEllipse*(sur: PSurface, CX, CY, XRadius, YRadius: Natural, inc(YChange,TwoASquare) -proc plotAA(sur: PSurface, x, y, c: float, color: TColor) = - if (x.toInt() > 0 and x.toInt() < sur.s.w) and (y.toInt() > 0 and - y.toInt() < sur.s.h): +proc plotAA(sur: PSurface, x, y: int, c: float, color: TColor) = + if (x > 0 and x < sur.s.w) and (y > 0 and + y < sur.s.h): var video = cast[PPixels](sur.s.pixels) var pitch = sur.s.pitch div ColSize - var pixColor = getPix(video, pitch, x.toInt, y.toInt) + var pixColor = getPix(video, pitch, x, y) - setPix(video, pitch, x.toInt(), y.toInt(), + setPix(video, pitch, x, y, pixColor.intensity(1.0 - c) + color.intensity(c)) - -proc ipart(x: float): float = return x.trunc() -proc fpart(x: float): float = return x - ipart(x) -proc rfpart(x: float): float = return 1.0 - fpart(x) + + +template ipart(x: expr): expr = floor(x) +template cround(x: expr): expr = ipart(x + 0.5) +template fpart(x: expr): expr = x - ipart(x) +template rfpart(x: expr): expr = 1.0 - fpart(x) proc drawLineAA*(sur: PSurface, p1, p2: TPoint, color: TColor) = ## Draws a anti-aliased line from ``p1`` to ``p2``, using Xiaolin Wu's @@ -428,36 +430,56 @@ proc drawLineAA*(sur: PSurface, p1, p2: TPoint, color: TColor) = p1.y.toFloat(), p2.y.toFloat()) var dx = x2 - x1 var dy = y2 - y1 - if abs(dx) < abs(dy): + + var ax = dx + if ax < 0'f64: + ax = 0'f64 - ax + var ay = dy + if ay < 0'f64: + ay = 0'f64 - ay + + if ax < ay: swap(x1, y1) swap(x2, y2) + swap(dx, dy) + + template doPlot(x, y: int, c: float, color: TColor): stmt = + if ax < ay: + sur.PlotAA(y, x, c, color) + else: + sur.PlotAA(x, y, c, color) + if x2 < x1: swap(x1, x2) swap(y1, y2) - + var gradient = dy / dx # handle first endpoint - var xend = x1 # Should be round(x1), but since this is an int anyway.. + var xend = cround(x1) var yend = y1 + gradient * (xend - x1) var xgap = rfpart(x1 + 0.5) - var xpxl1 = xend # this will be used in the main loop - var ypxl1 = ipart(yend) - sur.plotAA(xpxl1, ypxl1, rfpart(yend) * xgap, color) - sur.plotAA(xpxl1, ypxl1 + 1.0, fpart(yend) * xgap, color) + var xpxl1 = int(xend) # this will be used in the main loop + var ypxl1 = int(ipart(yend)) + doPlot(xpxl1, ypxl1, rfpart(yend)*xgap, color) + doPlot(xpxl1, ypxl1 + 1, fpart(yend)*xgap, color) var intery = yend + gradient # first y-intersection for the main loop + # handle second endpoint - xend = x2 # Should be round(x1), but since this is an int anyway.. + xend = cround(x2) yend = y2 + gradient * (xend - x2) xgap = fpart(x2 + 0.5) - var xpxl2 = xend # this will be used in the main loop - var ypxl2 = ipart(yend) - sur.plotAA(xpxl2, ypxl2, rfpart(yend) * xgap, color) - sur.plotAA(xpxl2, ypxl2 + 1.0, fpart(yend) * xgap, color) + var xpxl2 = int(xend) # this will be used in the main loop + var ypxl2 = int(ipart(yend)) + doPlot(xpxl2, ypxl2, rfpart(yend) * xgap, color) + doPlot(xpxl2, ypxl2 + 1, fpart(yend) * xgap, color) + # main loop - for x in xpxl1.toInt + 1..xpxl2.toInt - 1: - sur.plotAA(x.toFloat(), ipart(intery), rfpart(intery), color) - sur.plotAA(x.toFloat(), ipart(intery) + 1.0, fpart(intery), color) + var x = xpxl1 + 1 + while x <= xpxl2-1: + doPlot(x, int(ipart(intery)), rfpart(intery), color) + doPlot(x, int(ipart(intery)) + 1, fpart(intery), color) intery = intery + gradient + inc(x) proc fillSurface*(sur: PSurface, color: TColor) = ## Fills the entire surface with ``color``. @@ -469,7 +491,7 @@ template withEvents*(surf: PSurface, event: expr, actions: stmt): stmt = ## variable containing the TEvent object. while True: var event: SDL.TEvent - if SDL.PollEvent(addr(event)) == 1: + if SDL.WaitEvent(addr(event)) == 1: actions if sdl.Init(sdl.INIT_VIDEO) < 0: raiseEGraphics() @@ -478,9 +500,9 @@ if sdl_ttf.Init() < 0: raiseEGraphics() when isMainModule: var surf = newScreenSurface(800, 600) surf.fillSurface(colWhite) - + # Draw the shapes - surf.drawLineAA((100, 170), (400, 471), colTan) + surf.drawLineAA((150, 170), (400, 471), colTan) surf.drawLine((100, 170), (400, 471), colRed) surf.drawEllipse(200, 300, 200, 30, colSeaGreen) @@ -496,14 +518,16 @@ when isMainModule: surf.drawCircle((600, 500), 60, colRed) surf.fillRect((50, 50, 100, 100), colFuchsia) + + surf.drawLineAA((592, 160), (592, 280), colPurple) #surf.drawText((300, 300), "TEST", colMidnightBlue) #var textSize = textBounds("TEST") #surf.drawText((300, 300 + textSize.height), $textSize.width & ", " & # $textSize.height, colDarkGreen) - var mouseStartX = 0 - var mouseStartY = 0 + var mouseStartX = -1 + var mouseStartY = -1 withEvents(surf, event): var eventp = addr(event) case event.kind: @@ -518,22 +542,19 @@ when isMainModule: echo(evk.keysym.sym) of SDL.MOUSEBUTTONDOWN: var mbd = sdl.EvMouseButton(eventp) - mouseStartX = mbd.x - mouseStartY = mbd.y - - of SDL.MOUSEBUTTONUP: - var mbu = sdl.EvMouseButton(eventp) - if mouseStartX != 0 and mouseStartY != 0: - echo(mouseStartX, "x->", mbu.x) - echo(mouseStartY, "y->", mbu.y) - surf.drawLineAA((mouseStartX, MouseStartY), - (int(mbu.x), int(mbu.y)), colRed) - mouseStartX = 0 - mouseStartY = 0 - + if mouseStartX == -1 or mouseStartY == -1: + mouseStartX = int(mbd.x) + mouseStartY = int(mbd.y) + else: + surf.drawLineAA((mouseStartX, mouseStartY), (int(mbd.x), int(mbd.y)), colPurple) + mouseStartX = -1 + mouseStartY = -1 + of SDL.MouseMotion: var mm = sdl.EvMouseMotion(eventp) - echo(mm.x, " ", mm.y, " ", mm.yrel) + if mouseStartX != -1 and mouseStartY != -1: + surf.drawLineAA((mouseStartX, mouseStartY), (int(mm.x), int(mm.y)), colPurple) + #echo(mm.x, " ", mm.y, " ", mm.yrel) else: #echo(event.kind) diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index f8d49ddb4..ca29f5fcc 100755 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -1889,7 +1889,7 @@ proc setpwent*() {.importc, header: "<pwd.h>".} proc uname*(a1: var Tutsname): cint {.importc, header: "<sys/utsname.h>".} -proc pthread_atfork*(a1, a2, a3: proc {.noconv.}): cint {. +proc pthread_atfork*(a1, a2, a3: proc () {.noconv.}): cint {. importc, header: "<pthread.h>".} proc pthread_attr_destroy*(a1: ptr Tpthread_attr): cint {. importc, header: "<pthread.h>".} @@ -2015,7 +2015,7 @@ proc pthread_mutexattr_setprotocol*(a1: ptr Tpthread_mutexattr, a2: cint): cint proc pthread_mutexattr_setpshared*(a1: ptr Tpthread_mutexattr, a2: cint): cint {.importc, header: "<pthread.h>".} proc pthread_mutexattr_settype*(a1: ptr Tpthread_mutexattr, a2: cint): cint {.importc, header: "<pthread.h>".} -proc pthread_once*(a1: ptr Tpthread_once, a2: proc {.noconv.}): cint {.importc, header: "<pthread.h>".} +proc pthread_once*(a1: ptr Tpthread_once, a2: proc () {.noconv.}): cint {.importc, header: "<pthread.h>".} proc pthread_rwlock_destroy*(a1: ptr Tpthread_rwlock): cint {.importc, header: "<pthread.h>".} proc pthread_rwlock_init*(a1: ptr Tpthread_rwlock, diff --git a/lib/system.nim b/lib/system.nim index 9a00979d5..128ec921c 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -48,8 +48,14 @@ type typeDesc* {.magic: TypeDesc.} ## meta type to denote ## a type description (for templates) void* {.magic: "VoidType".} ## meta type to denote the absense of any type + + TInteger* = int|char|int8|int16|int32|int64|bool|enum + ## type class matching all integer types + + TNumber* = TInteger|float|float32|float64 + ## type class matching all number types -proc defined*[T](x: T): bool {.magic: "Defined", noSideEffect.} +proc defined*(x: expr): bool {.magic: "Defined", noSideEffect.} ## Special compile-time procedure that checks whether `x` is ## defined. `x` has to be an identifier or a qualified identifier. ## This can be used to check whether a library provides a certain @@ -60,7 +66,7 @@ proc defined*[T](x: T): bool {.magic: "Defined", noSideEffect.} ## # provide our own toUpper proc here, because strutils is ## # missing it. -proc definedInScope*[T](x: T): bool {. +proc definedInScope*(x: expr): bool {. magic: "DefinedInScope", noSideEffect.} ## Special compile-time procedure that checks whether `x` is ## defined in the current scope. `x` has to be an identifier. @@ -971,7 +977,7 @@ proc toBiggestInt*(f: biggestfloat): biggestint {. ## rounds `f` if it does not contain an integer value. If the conversion ## fails (because `f` is infinite for example), `EInvalidValue` is raised. -proc addQuitProc*(QuitProc: proc {.noconv.}) {.importc: "atexit", nodecl.} +proc addQuitProc*(QuitProc: proc() {.noconv.}) {.importc: "atexit", nodecl.} ## adds/registers a quit procedure. Each call to ``addQuitProc`` ## registers another quit procedure. Up to 30 procedures can be ## registered. They are executed on a last-in, first-out basis @@ -1214,6 +1220,11 @@ proc max*[T](x: openarray[T]): T = result = x[0] for i in 1..high(x): result = max(result, x[i]) +proc clamp*[T](x, a, b: T): T = + ## limits the value ``x`` within the interval [a, b] + if x > a: return a + if x < b: return b + return x iterator items*[T](a: openarray[T]): T {.inline.} = ## iterates over each item of `a`. @@ -1263,6 +1274,10 @@ iterator items*(a: cstring): char {.inline.} = yield a[i] inc(i) +iterator items*(E: typedesc{enum}): E = + ## iterates over the values of the enum ``E``. + for v in low(E)..high(E): + yield v iterator pairs*[T](a: openarray[T]): tuple[key: int, val: T] {.inline.} = ## iterates over each item of `a`. Yields ``(index, a[index])`` pairs. @@ -1374,7 +1389,8 @@ proc each*[T](data: var openArray[T], op: proc (x: var T)) = ## `op` to every item in `data`. for i in 0..data.len-1: op(data[i]) -iterator fields*[T: tuple](x: T): expr {.magic: "Fields", noSideEffect.} +iterator fields*[T: tuple](x: T): TObject {. + magic: "Fields", noSideEffect.} ## iterates over every field of `x`. Warning: This really transforms ## the 'for' and unrolls the loop. The current implementation also has a bug ## that affects symbol binding in the loop body. @@ -1384,7 +1400,8 @@ iterator fields*[S: tuple, T: tuple](x: S, y: T): tuple[a, b: expr] {. ## Warning: This is really transforms the 'for' and unrolls the loop. ## The current implementation also has a bug that affects symbol binding ## in the loop body. -iterator fieldPairs*[T: tuple](x: T): expr {.magic: "FieldPairs", noSideEffect.} +iterator fieldPairs*[T: tuple](x: T): TObject {. + magic: "FieldPairs", noSideEffect.} ## iterates over every field of `x`. Warning: This really transforms ## the 'for' and unrolls the loop. The current implementation also has a bug ## that affects symbol binding in the loop body. @@ -1511,7 +1528,7 @@ const nimrodStackTrace = compileOption("stacktrace") # of the code var - dbgLineHook*: proc + dbgLineHook*: proc () ## set this variable to provide a procedure that should be called before ## each executed instruction. This should only be used by debuggers! ## Only code compiled with the ``debugger:on`` switch calls this hook. @@ -1531,7 +1548,7 @@ var ## do when setting this. If ``localRaiseHook`` returns false, the exception ## is caught and does not propagate further through the call stack. - outOfMemHook*: proc + outOfMemHook*: proc () ## set this variable to provide a procedure that should be called ## in case of an `out of memory`:idx: event. The standard handler ## writes an error message and terminates the program. `outOfMemHook` can |