Merge branch 'master' of github.com:Araq/Nimrod into upstream

This commit is contained in:
Zahary Karadjov 2012-04-21 04:08:29 +03:00
commit a05f0aa6b5

View file

@ -406,20 +406,22 @@ proc drawEllipse*(sur: PSurface, CX, CY, XRadius, YRadius: Natural,
inc(YChange,TwoASquare) inc(YChange,TwoASquare)
proc plotAA(sur: PSurface, x, y, c: float, color: TColor) = proc plotAA(sur: PSurface, x, y: int, c: float, color: TColor) =
if (x.toInt() > 0 and x.toInt() < sur.s.w) and (y.toInt() > 0 and if (x > 0 and x < sur.s.w) and (y > 0 and
y.toInt() < sur.s.h): y < sur.s.h):
var video = cast[PPixels](sur.s.pixels) var video = cast[PPixels](sur.s.pixels)
var pitch = sur.s.pitch div ColSize 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)) 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) template ipart(x: expr): expr = floor(x)
proc rfpart(x: float): float = return 1.0 - fpart(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) = proc drawLineAA*(sur: PSurface, p1, p2: TPoint, color: TColor) =
## Draws a anti-aliased line from ``p1`` to ``p2``, using Xiaolin Wu's ## 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()) p1.y.toFloat(), p2.y.toFloat())
var dx = x2 - x1 var dx = x2 - x1
var dy = y2 - y1 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(x1, y1)
swap(x2, y2) 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: if x2 < x1:
swap(x1, x2) swap(x1, x2)
swap(y1, y2) swap(y1, y2)
var gradient = dy / dx var gradient = dy / dx
# handle first endpoint # 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 yend = y1 + gradient * (xend - x1)
var xgap = rfpart(x1 + 0.5) var xgap = rfpart(x1 + 0.5)
var xpxl1 = xend # this will be used in the main loop var xpxl1 = int(xend) # this will be used in the main loop
var ypxl1 = ipart(yend) var ypxl1 = int(ipart(yend))
sur.plotAA(xpxl1, ypxl1, rfpart(yend) * xgap, color) doPlot(xpxl1, ypxl1, rfpart(yend)*xgap, color)
sur.plotAA(xpxl1, ypxl1 + 1.0, fpart(yend) * xgap, color) doPlot(xpxl1, ypxl1 + 1, fpart(yend)*xgap, color)
var intery = yend + gradient # first y-intersection for the main loop var intery = yend + gradient # first y-intersection for the main loop
# handle second endpoint # handle second endpoint
xend = x2 # Should be round(x1), but since this is an int anyway.. xend = cround(x2)
yend = y2 + gradient * (xend - x2) yend = y2 + gradient * (xend - x2)
xgap = fpart(x2 + 0.5) xgap = fpart(x2 + 0.5)
var xpxl2 = xend # this will be used in the main loop var xpxl2 = int(xend) # this will be used in the main loop
var ypxl2 = ipart(yend) var ypxl2 = int(ipart(yend))
sur.plotAA(xpxl2, ypxl2, rfpart(yend) * xgap, color) doPlot(xpxl2, ypxl2, rfpart(yend) * xgap, color)
sur.plotAA(xpxl2, ypxl2 + 1.0, fpart(yend) * xgap, color) doPlot(xpxl2, ypxl2 + 1, fpart(yend) * xgap, color)
# main loop # main loop
for x in xpxl1.toInt + 1..xpxl2.toInt - 1: var x = xpxl1 + 1
sur.plotAA(x.toFloat(), ipart(intery), rfpart(intery), color) while x <= xpxl2-1:
sur.plotAA(x.toFloat(), ipart(intery) + 1.0, fpart(intery), color) doPlot(x, int(ipart(intery)), rfpart(intery), color)
doPlot(x, int(ipart(intery)) + 1, fpart(intery), color)
intery = intery + gradient intery = intery + gradient
inc(x)
proc fillSurface*(sur: PSurface, color: TColor) = proc fillSurface*(sur: PSurface, color: TColor) =
## Fills the entire surface with ``color``. ## Fills the entire surface with ``color``.
@ -469,7 +491,7 @@ template withEvents*(surf: PSurface, event: expr, actions: stmt): stmt =
## variable containing the TEvent object. ## variable containing the TEvent object.
while True: while True:
var event: SDL.TEvent var event: SDL.TEvent
if SDL.PollEvent(addr(event)) == 1: if SDL.WaitEvent(addr(event)) == 1:
actions actions
if sdl.Init(sdl.INIT_VIDEO) < 0: raiseEGraphics() if sdl.Init(sdl.INIT_VIDEO) < 0: raiseEGraphics()
@ -478,9 +500,9 @@ if sdl_ttf.Init() < 0: raiseEGraphics()
when isMainModule: when isMainModule:
var surf = newScreenSurface(800, 600) var surf = newScreenSurface(800, 600)
surf.fillSurface(colWhite) surf.fillSurface(colWhite)
# Draw the shapes # 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.drawLine((100, 170), (400, 471), colRed)
surf.drawEllipse(200, 300, 200, 30, colSeaGreen) surf.drawEllipse(200, 300, 200, 30, colSeaGreen)
@ -496,14 +518,16 @@ when isMainModule:
surf.drawCircle((600, 500), 60, colRed) surf.drawCircle((600, 500), 60, colRed)
surf.fillRect((50, 50, 100, 100), colFuchsia) surf.fillRect((50, 50, 100, 100), colFuchsia)
surf.drawLineAA((592, 160), (592, 280), colPurple)
#surf.drawText((300, 300), "TEST", colMidnightBlue) #surf.drawText((300, 300), "TEST", colMidnightBlue)
#var textSize = textBounds("TEST") #var textSize = textBounds("TEST")
#surf.drawText((300, 300 + textSize.height), $textSize.width & ", " & #surf.drawText((300, 300 + textSize.height), $textSize.width & ", " &
# $textSize.height, colDarkGreen) # $textSize.height, colDarkGreen)
var mouseStartX = 0 var mouseStartX = -1
var mouseStartY = 0 var mouseStartY = -1
withEvents(surf, event): withEvents(surf, event):
var eventp = addr(event) var eventp = addr(event)
case event.kind: case event.kind:
@ -518,22 +542,19 @@ when isMainModule:
echo(evk.keysym.sym) echo(evk.keysym.sym)
of SDL.MOUSEBUTTONDOWN: of SDL.MOUSEBUTTONDOWN:
var mbd = sdl.EvMouseButton(eventp) var mbd = sdl.EvMouseButton(eventp)
mouseStartX = mbd.x if mouseStartX == -1 or mouseStartY == -1:
mouseStartY = mbd.y mouseStartX = int(mbd.x)
mouseStartY = int(mbd.y)
of SDL.MOUSEBUTTONUP: else:
var mbu = sdl.EvMouseButton(eventp) surf.drawLineAA((mouseStartX, mouseStartY), (int(mbd.x), int(mbd.y)), colPurple)
if mouseStartX != 0 and mouseStartY != 0: mouseStartX = -1
echo(mouseStartX, "x->", mbu.x) mouseStartY = -1
echo(mouseStartY, "y->", mbu.y)
surf.drawLineAA((mouseStartX, MouseStartY),
(int(mbu.x), int(mbu.y)), colRed)
mouseStartX = 0
mouseStartY = 0
of SDL.MouseMotion: of SDL.MouseMotion:
var mm = sdl.EvMouseMotion(eventp) 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: else:
#echo(event.kind) #echo(event.kind)