require 'bizzieLibrary' require 'opengl' require 'glut' require 'mathn' # color stuff class Color < Point def initialize(val1, g=-1.0, b=0.0, alpha=1) if (val1.instance_of?(Array)) super(val1) elsif (val1.instance_of?(Point)) if val1.dimension == 4 && g == -1.0 super(val1.coords) else initArray = val1.coords[0..2] # add alpha value initArray << g super(initArray) end else r = val1 g = g b = b alpha = alpha super([r, g, b, alpha]) end end def r coords[0] end def g coords[1] end def b coords[2] end def alpha coords[3] end def a alpha end def setAlpha(newAlpha) @coords[3] = newAlpha return self end end $red = Color.new(1.0, 0.0, 0.0) $yellow = Color.new(1.0, 1.0, 0.0) $green = Color.new(0.0, 1.0, 0.0) $cyan = Color.new(0.0, 1.0, 1.0) $blue = Color.new(0.0, 0.0, 1.0) $magenta = Color.new(1.0, 0.0, 1.0) $black = Color.new(0.0, 0.0, 0.0) $white = Color.new(1.0, 1.0, 1.0) $rainbowPath = CompositePath.new([Line.new($red, $yellow), Line.new($yellow, $green), Line.new($green, $cyan), Line.new($cyan, $blue), Line.new($blue, $magenta), Line.new($magenta, $red)]) $revRainbowPath = $rainbowPath.reverse $redWhitePath = Line.new($red, $white) $redBlackPath = Line.new($red, $black) $blackPath = Line.new($black, $black) $blackRedWhitePath = $redBlackPath.reverse + $redWhitePath $blackRedBlackPath = $redBlackPath.reverse + $redBlackPath $blueCyanPath = Line.new($blue, $cyan) $whiteCyanPath = Line.new($cyan, $white) $cyanBluePath = Line.new($cyan, $blue) $whiteCyanBluePath = $whiteCyanPath + $cyanBluePath $whitePath = Line.new($white, $white) $whiteBlackPath = Line.new($white, $black) #META: Path def setColor(color, g=nil, b = 0, alpha = 1.0) if (color.instance_of?(Color)) if (g != nil) if $graphicsSystem == $useSDL $SDLUsedColor = getPixelValue(color.setAlpha(g)) else GL.Color(color.r, color.g, color.b, g) end else if $graphicsSystem == $useSDL $SDLUsedColor = getPixelValue(color) else GL.Color(color.r, color.g, color.b, color.alpha) end end else if $graphicsSystem == $useSDL $SDLUsedColor == getPixelValue(Color.new(color, g, b, alpha)) else GL.Color(color, g, b, alpha) end end end def setLineWidth(width) if $graphicsSystem == $useSDL else GL.LineWidth(width) end end def rotate(theta) if $graphicsSystem == $useSDL else GL.PushMatrix() GL.Rotate(theta, 0.0, 0.0, 0.1) yield GL.PopMatrix() end end def drawPoint(pt) if $graphicsSystem == $useSDL $SDLscreen.putPixel(pt.x, pt.y, $SDLUsedColor) else drawCircle(pt, 1) end end def drawCircle(pt, radius) end def drawPathColor(path, colorPath, epsilon = nil, getEps=false) pd = PathDrawer.new if epsilon != nil path.doPath(epsilon) {|pt, eps| setColor(colorPath.doPathPoint(eps)) pd.doPoint(pt) if getEps yield eps end } else path.doPath {|pt, eps| setColor(colorPath.doPathPoint(eps)) pd.doPoint(pt) if getEps yield eps end } end end def drawLinesPath(*array) drawPath(getDiscretePathArray(array)) end def drawPath(path, epsilon = nil, getEps=false) pd = PathDrawer.new if epsilon != nil path.doPath(epsilon) {|pt, eps| pd.doPoint(pt) if getEps yield eps end } else path.doPath {|pt, eps| pd.doPoint(pt) if getEps yield eps end } end end def fillPath(path, epsilon) drawPolygon(path.points(epsilon)) end #takes a line or two points or 4 values def drawLine(val1, val2 = 0, val3 = 0, val4 = 0) if (val1.instance_of?(Line2d)) drawLineHelper(val1.p1.x, val1.p1.y, val1.p2.x, val1.p2.y) elsif (val1.instance_of?(Point2d) && val2.instance_of?(Point2d)) drawLineHelper(val1.x, val1.y, val2.x, val2.y) elsif (val1.instance_of?(Point) && val2.instance_of?(Point)) drawLineHelper(val1.coords[0], val1.coords[1], val2.coords[0], val2.coords[1]) elsif (val1.instance_of?(Array)) drawLine(val1[0], val1[1]) else drawLineHelper(val1, val2, val3, val4) end end def drawLineHelper(val1, val2, val3, val4) if $graphicsSystem == $useSDL $SDLscreen.drawLine(val1, val2, val3, val4, $SDLUsedColor) else GL.Begin(GL::LINES) getVertex(val1, val2) getVertex(val3, val4) GL.End() end end def drawPolygon(pts) GL.Begin(GL::POLYGON) pts.each do |pt| getVertex(pt) end GL.End() end def drawRect(x, y, width, height) GL.Begin(GL::POLYGON) GL.Vertex(x, y) GL.Vertex(x + width, y) GL.Vertex(x + width, y + height) GL.Vertex(x, y + height) GL.END() end # helper methods def getVertex(val1, val2 = 0) if (val1.instance_of?(Point2d)) GL.Vertex(val1.x, val1.y) else GL.Vertex(val1, val2) end end $clearColor = color def setClearColor(color) if $graphicsSystem == $useSDL $SDLBGColor = $SDLscreen.format.mapRGB color.r, color.g, color.b else GL.ClearColor(color.r, color.g, color.b, color.alpha) end $clearColor = color end def clearScreen(x = nil, y = nil, width = nil, height = nil) if $graphicsSystem == $useSDL $SDLscreen.fill_rect 0, 0, $screenWidth, $screenHeight, $SDLBGColor else if x == nil else GL.Clear(GL::COLOR_BUFFER_BIT) end end end def flush if $graphicsSystem == $useSDL $SDLscreen.flip else GL.Flush() end end $rotAngle = 10 class Image def initialize(fileName) @surf = SDL::Surface.load(fileName) end def surface @surf end def height @surf.h end def width @surf.w end def setColorKey(color) @surf.setColorKey(SDL::SRCCOLORKEY, getPixelValue(color)) end end #todo: caching! def putImage(image, x, y, angle = nil, scale = nil) if $graphicsSystem == $useSDL if angle == nil && scale == nil SDL.blitSurface(image.surface, 0, 0, 0, 0, $SDLscreen, x, y) else SDL.rotateScaledBlit(image.surface, $SDLscreen, x, y, angle, scale) end end end def putPartialImage(image, srcX, srcY, srcWidth, srcHeight, dstX, dstY) if $graphicsSystem == $useSDL SDL.blitSurface(image.surface, srcX, srcY, srcWidth, srcHeight, $SDLscreen, dstX, dstY) end end # TODO: implement def fillRect(val1, val2, val3 = 0, val4 = 0) if $graphicsSystem == $useSDL x1 = min(val1[0], val2[0]) y1 = min(val1[1], val2[1]) x2 = max(val1[0], val2[0]) y2 = max(val1[1], val2[1]) width = x2 - x1 height = y2 - y1 $SDLscreen.fill_rect(x1, y1, width, height, $SDLUsedColor) end end