From 9acdf94cc04726cb80e3012849d8f56f06615017 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 24 Jul 2013 23:07:28 +0200 Subject: fixes #531 --- tests/manyloc/named_argument_bug/gui.nim | 44 ++++++ tests/manyloc/named_argument_bug/main.nim | 23 +++ tests/manyloc/named_argument_bug/main.nimrod.cfg | 2 + .../named_argument_bug/tri_engine/config.nim | 6 + .../named_argument_bug/tri_engine/gfx/color.nim | 57 ++++++++ .../named_argument_bug/tri_engine/gfx/gl/gl.nim | 61 ++++++++ .../tri_engine/gfx/gl/primitive.nim | 157 +++++++++++++++++++++ .../tri_engine/gfx/gl/shader.nim | 103 ++++++++++++++ .../named_argument_bug/tri_engine/gfx/tex.nim | 31 ++++ .../named_argument_bug/tri_engine/math/circle.nim | 9 ++ .../named_argument_bug/tri_engine/math/rect.nim | 8 ++ .../named_argument_bug/tri_engine/math/vec.nim | 55 ++++++++ .../named_argument_bug/tri_engine/tri_engine.nim | 106 ++++++++++++++ 13 files changed, 662 insertions(+) create mode 100644 tests/manyloc/named_argument_bug/gui.nim create mode 100644 tests/manyloc/named_argument_bug/main.nim create mode 100644 tests/manyloc/named_argument_bug/main.nimrod.cfg create mode 100644 tests/manyloc/named_argument_bug/tri_engine/config.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/gfx/color.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/gfx/gl/gl.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/gfx/gl/primitive.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/gfx/gl/shader.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/gfx/tex.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/math/circle.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/math/rect.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/math/vec.nim create mode 100644 tests/manyloc/named_argument_bug/tri_engine/tri_engine.nim (limited to 'tests/manyloc') diff --git a/tests/manyloc/named_argument_bug/gui.nim b/tests/manyloc/named_argument_bug/gui.nim new file mode 100644 index 000000000..1e0bc6ffd --- /dev/null +++ b/tests/manyloc/named_argument_bug/gui.nim @@ -0,0 +1,44 @@ +import + tri_engine/gfx/gl/primitive, + tri_engine/gfx/tex, + tri_engine/gfx/color, + tri_engine/math/rect, + tri_engine/math/vec + +type + TWidgetLayer* = enum + wlBg = 100, + wlOverlap = 200, + wlMain = 300, + wlOverlay = 400, + wlCursor = 500 + TWidgetLayerType = TWidgetLayer|int + TWidgetType* = enum + wtImg + PWidget* = ref object + `type`* : TWidgetType + layer* : TWidgetLayer + rect* : TRect + prim* : PPrimitive + +const + baseZ = 5000 + +proc newWidget*(`type`: TWidgetType, layer: TWidgetLayerType, rect: TRect): PWidget = + new(result) + result.`type` = `type` + result.layer = layer + result.rect = rect + + var verts = newVert(rect) + + # This works because z is accessible at this scope. + #var z = baseZ + layer.int + #result.prim = newPrimitive(verts, z=z) + + # Doesn't work, because the compiler looks for a symbol called z in this scope, + # but it should only check that it is the name of one of the params. + #result.prim = newPrimitive(verts, z=baseZ + layer.int) + + # This doesn't work either. + result.prim = newPrimitive(verts, z=0) diff --git a/tests/manyloc/named_argument_bug/main.nim b/tests/manyloc/named_argument_bug/main.nim new file mode 100644 index 000000000..767674428 --- /dev/null +++ b/tests/manyloc/named_argument_bug/main.nim @@ -0,0 +1,23 @@ +import + tri_engine/config, + tri_engine/math/vec, + tri_engine/math/circle, + tri_engine/gfx/gl/primitive, + tri_engine/gfx/tex, + tri_engine/gfx/color, + tri_engine/tri_engine, + gui + +var isRunning = true + +block: + var renderer = newRenderer(w=10, h=10) + + var primitive = newPrimitiveCircle(0.3.TR, color=white(0.5, 0.8), z=15) + renderer.addPrimitive(primitive) + + var verts = newVert((min: newV2xy(-0.4), size: newV2xy(0.3))) + var primitive2 = newPrimitive(verts, color=red(0.5, 0.8), z=10) + renderer.addPrimitive(primitive2) + + var mainMenuWidget = newWidget(wtImg, wlBg, rect=(newV2xy(-1.0), newV2xy(2.0))) diff --git a/tests/manyloc/named_argument_bug/main.nimrod.cfg b/tests/manyloc/named_argument_bug/main.nimrod.cfg new file mode 100644 index 000000000..27cf8e688 --- /dev/null +++ b/tests/manyloc/named_argument_bug/main.nimrod.cfg @@ -0,0 +1,2 @@ +# this file only exists to mark 'main.nim' as the main file + diff --git a/tests/manyloc/named_argument_bug/tri_engine/config.nim b/tests/manyloc/named_argument_bug/tri_engine/config.nim new file mode 100644 index 000000000..b90dc0b26 --- /dev/null +++ b/tests/manyloc/named_argument_bug/tri_engine/config.nim @@ -0,0 +1,6 @@ +when defined(doublePrecision): + type + TR* = float64 +else: + type + TR* = float32 diff --git a/tests/manyloc/named_argument_bug/tri_engine/gfx/color.nim b/tests/manyloc/named_argument_bug/tri_engine/gfx/color.nim new file mode 100644 index 000000000..8e47c1f2f --- /dev/null +++ b/tests/manyloc/named_argument_bug/tri_engine/gfx/color.nim @@ -0,0 +1,57 @@ +import + tri_engine/config, + tri_engine/math/vec + +from strutils import + formatFloat, + TFloatFormat, + `%` + +from unsigned import + `shr`, + `and` + +type + TColor* = tuple[r, g, b, a: TR] + +converter toColor*(o: uint32): TColor = + ## Convert an integer to a color. This is mostly useful when the integer is specified as a hex + ## literal such as 0xFF00007F, which is 100% red, with 50% alpha. + ## TODO: turn this into a template that can take either 4 or 8 characters? + ((((o and 0xff000000'u32) shr 24).TR / 255.0).TR, + (((o and 0xff0000'u32) shr 16).TR / 255.0).TR, + (((o and 0xff00'u32) shr 8).TR / 255.0).TR, + (((o and 0xff'u32)).TR / 255.0).TR) + +converter toV4*(o: TColor): TV4[TR] = + cast[TV4[TR]](o) + +proc newColor*(r, g, b: TR=0.0, a: TR=1.0): TColor = + (r, g, b, a) + +proc white*(rgb, a: TR=1.0): TColor = + (rgb, rgb, rgb, a) + +proc red*(r, a: TR=1.0): TColor = + newColor(r=r, a=a) + +proc green*(g, a: TR=1.0): TColor = + newColor(g=g, a=a) + +proc yellow*(rg, a: TR=1.0): TColor = + newColor(r=rg, g=rg, a=a) + +proc blue*(b, a: TR=1.0): TColor = + newColor(b=b, a=a) + +proc cyan*(gb, a: TR=1.0): TColor = + newColor(g=gb, b=gb, a=a) + +proc purple*(rb, a: TR=1.0): TColor = + newColor(r=rb, b=rb, a=a) + +proc `$`*(o: TColor): string = + proc f(f: float): string = + f.formatFloat(precision=2, format=ffDecimal) + + "(r: $#, g: $#, b: $#, s: $#)" % [f(o.r), f(o.g), f(o.b), f(o.a)] diff --git a/tests/manyloc/named_argument_bug/tri_engine/gfx/gl/gl.nim b/tests/manyloc/named_argument_bug/tri_engine/gfx/gl/gl.nim new file mode 100644 index 000000000..e731969c1 --- /dev/null +++ b/tests/manyloc/named_argument_bug/tri_engine/gfx/gl/gl.nim @@ -0,0 +1,61 @@ +import + opengl, + tri_engine/math/vec + +export + opengl + +type + EGL* = object of E_Base + EGL_code* = object of EGL + code*: EGL_err + EGL_err {.pure.} = enum + none = GL_NO_ERROR + invalidEnum = GL_INVALID_ENUM + invalidVal = GL_INVALID_VALUE + invalidOp = GL_INVALID_OPERATION + stackOverflow = GL_STACK_OVERFLOW + stackUnderflow = GL_STACK_UNDERFLOW + outOfMem = GL_OUT_OF_MEMORY + invalidFramebufferOp = GL_INVALID_FRAMEBUFFER_OPERATION + unknown + +proc newGL_codeException*(msg: string, code: EGL_err): ref EGL_code = + result = newException(EGL_code, $code) + result.code = code + +proc getErr*(): EGL_err = + result = glGetError().EGL_err + if result notin {EGL_err.none, + EGL_err.invalidEnum, + EGL_err.invalidVal, + EGL_err.invalidOp, + EGL_err.invalidFramebufferOp, + EGL_err.outOfMem, + EGL_err.stackUnderflow, + EGL_err.stackOverflow}: + return EGL_err.unknown + +proc errCheck*() = + let err = getErr() + if err != EGL_err.none: + raise newGL_codeException($err, err) + +macro `?`*(call: expr{nkCall}): expr = + result = call + # Can't yet reference foreign symbols in macros. + #errCheck() + +when defined(doublePrecision): + const + glRealType* = cGLdouble +else: + const + glRealType* = cGLfloat + +proc setUniformV4*[T](loc: GLint, vecs: var openarray[TV4[T]]) = + glUniform4fv(loc, vecs.len.GLsizei, cast[PGLfloat](vecs[0].addr)) + +proc setUniformV4*[T](loc: GLint, vec: TV4[T]) = + var vecs = [vec] + setUniformV4(loc, vecs) diff --git a/tests/manyloc/named_argument_bug/tri_engine/gfx/gl/primitive.nim b/tests/manyloc/named_argument_bug/tri_engine/gfx/gl/primitive.nim new file mode 100644 index 000000000..c67748967 --- /dev/null +++ b/tests/manyloc/named_argument_bug/tri_engine/gfx/gl/primitive.nim @@ -0,0 +1,157 @@ +import + math, + tri_engine/config, + tri_engine/gfx/gl/gl, + tri_engine/gfx/tex, + tri_engine/gfx/color, + tri_engine/math/vec, + tri_engine/math/rect, + tri_engine/math/circle + +import strutils + +type + TVert* = tuple[pos: TV2[TR], texCoord: TV2[TR]] + TVertAttrib* = object + i* : GLuint + size* : GLint + stride* : GLsizei + offset* : PGLvoid + TVertMode* = enum + vmTriStrip = GLtriangleStrip, + vmTriFan = GLtriangleFan + TZ_range* = range[-100_000..100_000] + PPrimitive* = ref object + verts* : seq[TVert] + indices* : seq[GLushort] + arrBufId* : GLuint + elemArrBufId* : GLuint + tex* : TTex + color* : TColor + vertMode* : TVertMode + z* : int + +proc newVert*(pos, texCoord: TV2): TVert = + (pos, texCoord) + +proc newVertQuad*(min, minRight, maxLeft, max: TV2[TR]): seq[TVert] = + @[newVert(min, newV2()), + newVert(minRight, newV2(x=1.0)), + newVert(maxLeft, newV2(y=1.0)), + newVert(max, newV2xy(1.0)) + ] + +proc newVert*(rect: rect.TRect): seq[TVert] = + newVertQuad(rect.min, newV2(rect.max.x, rect.min.y), newV2(rect.min.x, rect.max.y), rect.max) + +proc newVertAttrib(i: GLuint, size: GLint, stride: GLsizei, offset: PGLvoid): TVertAttrib = + TVertAttrib(i: i, size: size, stride: stride, offset: offset) + +proc genBuf*[T](vboTarget, objUsage: GLenum, data: var openarray[T]): GLuint = + result = 0.GLuint + ?glGenBuffers(1, result.addr) + ?glBindBuffer(vboTarget, result) + + let size = (data.len * T.sizeof).GLsizeiptr + ?glBufferData(vboTarget, size, data[0].addr, objUsage) + +proc newPrimitive*(verts: var seq[TVert], + vertMode=vmTriStrip, + tex=whiteTex(), + color=white(), + z: TZ_range=0): PPrimitive = + var indices = newSeq[GLushort](verts.len) + for i in 0 .. y.z: + 1 + else: + 0) + + for x in zSortedPrimitives: + o.draw(x) + + disableVertAttribArrs() + +proc addPrimitive*(o: PRenderer, p: PPrimitive) = + o.primitives.add(p) -- cgit 1.4.1-2-gfad0