func drawHex(x, y, kind int, alpha float32) { if kind == 6 { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) starTex.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(x, y) gl.TexCoord2f(0, 1) gl.Vertex2i(x, y+HEX_HEIGHT) gl.TexCoord2f(1, 1) gl.Vertex2i(x+HEX_WIDTH, y+HEX_HEIGHT) gl.TexCoord2f(1, 0) gl.Vertex2i(x+HEX_WIDTH, y) gl.End() } else { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) var r, g, b float32 switch kind { case 0: r = 1 case 1: g = 1 case 2: b = 1 case 3: r = 1 g = 1 case 4: r = 1 b = 1 case 5: g = 1 - 222/255 b = 1 } hexTex.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) if alpha < 1 { gl.Color4f(r, g, b, alpha) } else { gl.Color3f(r, g, b) } gl.TexCoord2f(0, 0) gl.Vertex2i(x, y) gl.TexCoord2f(0, 1) gl.Vertex2i(x, y+HEX_HEIGHT) gl.TexCoord2f(1, 1) gl.Vertex2i(x+HEX_WIDTH, y+HEX_HEIGHT) gl.TexCoord2f(1, 0) gl.Vertex2i(x+HEX_WIDTH, y) gl.End() } }
func (h *Hex) Render(alpha float32, drawFromCenter bool) { gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) if h.Kind == HexFlower { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) starTex.Bind(gl.TEXTURE_2D) } else { gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) hexTex.Bind(gl.TEXTURE_2D) gl.GetError() var r, g, b uint8 r = uint8(colors.Colors[h.Kind-1][0]) g = uint8(colors.Colors[h.Kind-1][1]) b = uint8(colors.Colors[h.Kind-1][2]) if alpha < 1 { gl.Color4ub(r, g, b, uint8(alpha*255)) } else { gl.Color3ub(r, g, b) } } gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) if drawFromCenter { gl.Vertex2i(HEX_WIDTH/2, HEX_HEIGHT/2) } else { gl.Vertex2i(HEX_WIDTH, HEX_HEIGHT) } gl.TexCoord2f(0, 1) if drawFromCenter { gl.Vertex2i(HEX_WIDTH/2, -HEX_HEIGHT/2) } else { gl.Vertex2i(HEX_WIDTH, 0) } gl.TexCoord2f(1, 1) if drawFromCenter { gl.Vertex2i(-HEX_WIDTH/2, -HEX_HEIGHT/2) } else { gl.Vertex2i(0, 0) } gl.TexCoord2f(1, 0) if drawFromCenter { gl.Vertex2i(-HEX_WIDTH/2, HEX_HEIGHT/2) } else { gl.Vertex2i(0, HEX_HEIGHT) } gl.End() }
func initTexture2(filename string) gl.Texture { img, err := glfw.ReadImage(filename+".tga", glfw.NoRescaleBit) if err != nil { panic(err) } rt := gl.GenTexture() gl.Enable(gl.TEXTURE_2D) rt.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) // gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) // gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, img.Width(), img.Height(), 0, gl.RGBA, gl.UNSIGNED_BYTE, img.Data()) fmt.Println(filename, img.Width(), img.Height()) return rt }
func initTexture(filename string, width, height int) gl.Texture { file, err := os.Open(filename + ".png") if err != nil { panic(err) } defer file.Close() img, _, err := image.Decode(file) if err != nil { panic(err) } t := reflect.ValueOf(img) fmt.Println(t.Elem().Type().Name()) canvas := image.NewRGBA(image.Rect(0, 0, width, height)) for y := 0; y < height; y++ { for x := 0; x < width; x++ { r, g, b, a := img.At(x, y).RGBA() if (filename == "hex4v" || filename == "hexstar2" || filename == "hexborder") && r == 0 && g == 0 && b == 0 { a = 0 } // if filename == "hex5k" { // fmt.Println(r, g, b, a) // } base := 4*x + canvas.Stride*y canvas.Pix[base] = uint8(r) canvas.Pix[base+1] = uint8(g) canvas.Pix[base+2] = uint8(b) canvas.Pix[base+3] = uint8(a) } } rt := gl.GenTexture() gl.Enable(gl.TEXTURE_2D) rt.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) // gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT) // gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, canvas.Pix) return rt }
func drawBorderAtXY(x, y float32, reverse int) { if x <= 80 || y <= 80 { return } gl.PushMatrix() gl.Translatef(x, y, 0) if reverse == 1 { gl.Rotatef(60, 0, 0, 1) } gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) borderTex.Bind(gl.TEXTURE_2D) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(-38, -38) gl.TexCoord2f(0, 1) gl.Vertex2i(-38, 38) gl.TexCoord2f(1, 1) gl.Vertex2i(38, 38) gl.TexCoord2f(1, 0) gl.Vertex2i(38, -38) gl.End() gl.PopMatrix() }
func main() { sys := Make() sys.Startup() defer sys.Shutdown() // InitQueue() sys.CreateWindow(1024, 768, "Gexic") gl.ClearColor(1, 1, 1, 0.) initGL() prevSelectPos = []int{0, 0, 0} // PurgeQueue() // genHexMap() hexMap2 = GenHexMap() // for matches := checkHexMap(); len(matches) > 0; matches = checkHexMap() { // removeHexAndGenNew(matches) // } glfw.SetMouseButtonCallback(MouseButtonCallback) glfw.SetCharCallback(charCallback) glfw.SetMousePosCallback(MousePosCallback) currExX = -1 currExY = -1 for sys.CheckExitMainLoop() { start := glfw.Time() wait := float64(1) / float64(30) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.Disable(gl.DEPTH_TEST) // renderHexMap() wallpaper.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(0, 0) gl.TexCoord2f(0, 1) gl.Vertex2i(0, 768) gl.TexCoord2f(1, 1) gl.Vertex2i(1024, 768) gl.TexCoord2f(1, 0) gl.Vertex2i(1024, 0) gl.End() hexMap2.Render() hexRotate.AnimateAndExecute() hexShrink.AnimateAndExecute() hexFall.AnimateAndExecute() if !mouse.locked { x, y := mouse.GetXY() hexMap2.CalcClosestCenter(x, y) } gl.Flush() gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.BLEND) sys.Refresh() diff := glfw.Time() - start if diff < wait { glfw.Sleep(wait - diff) } } }
func renderHexMap() { gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) gl.Disable(gl.DEPTH_TEST) wallpaper.Bind(gl.TEXTURE_2D) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.REPLACE) gl.Begin(gl.QUADS) gl.TexCoord2f(0, 0) gl.Vertex2i(0, 0) gl.TexCoord2f(0, 1) gl.Vertex2i(0, 768) gl.TexCoord2f(1, 1) gl.Vertex2i(1024, 768) gl.TexCoord2f(1, 0) gl.Vertex2i(1024, 0) gl.End() gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.DECAL) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.PushMatrix() gl.Translatef(80, 80, 0) for x := 0; x < 10; x++ { maxy := 8 topy := 19 if x%2 == 1 { maxy = 9 topy = 0 } starty := 0 for y := starty; y < maxy; y++ { if currExX > -1 && currExY > -1 && starRotate { if y == currExY && x == currExX || y == currExY+1 && x == currExX || y == currExY-1 && x == currExX || currExX%2 == 0 && ((currExX == x-1 || currExX == x+1) && currExY == y-1 || (currExX == x-1 || currExX == x+1) && currExY == y) || currExX%2 == 1 && ((currExX == x-1 || currExX == x+1) && currExY == y || (currExX == x-1 || currExX == x+1) && currExY == y+1) { continue } } else if timesToRotate > 0 { // if y == currExY && x == currExX || currExX%2 == 0 && (x == currExX+1 && y == currExY || x == currExX+1 && y == currExY+1) || currExX%2 == 1 && (x == currExX+1 && y == currExY || x == currExX+1 && y == currExY-1) { // continue // } found := false for _, v := range selectedHex { if y == v[1] && x == v[0] { found = true break } } if found { continue } } found := false for _, v := range currentMatches { if scale > 0 && v[0] == x && v[1] == y || scale <= 0 && v[0] == x && v[1] >= y { found = true break } } for _, v := range starMatches { if starAlpha > 0 && v[0] == x && v[1] == y || starAlpha <= 0 && v[0] == x && v[1] >= y { found = true break } } if found || len(currStarCenter) > 0 && currStarCenter[0] == x && currStarCenter[1] == y { continue } gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(x*33, topy+y*38, hexMap[x][y], 1) } } gl.PopMatrix() if len(currentMatches) > 0 || len(starMatches) > 0 { mouseLock = true if len(currentMatches) > 0 && scale > 0 { scale -= 0.1 for _, v := range currentMatches { gl.PushMatrix() topy := 19 if v[0]%2 == 1 { topy = 0 } gl.Translatef(float32(v[0]*33+102), float32(v[1]*38+topy+94), 0) gl.Scalef(scale, scale, 1) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[v[0]][v[1]], 1) gl.PopMatrix() } } else if len(starMatches) > 0 && starAlpha > 0 { starAlpha -= 0.05 // starAlpha = 0.7 starScale += 0.05 // starScale = 1.4 gl.PushMatrix() topy := 19 pm := 0 if currStarCenter[0]%2 == 1 { topy = 0 pm = -1 } gl.Translatef(float32(currStarCenter[0]*33+102), float32(currStarCenter[1]*38+topy+94), 0) drawHex(-22, -14, 6, 1) gl.PopMatrix() gl.PushMatrix() gl.Translatef(float32(currStarCenter[0]*33+102), float32(currStarCenter[1]*38+topy+94), 0) gl.Scalef(starScale, starScale, 1) drawHex(-22, -14-HEX_HEIGHT, hexMap[currStarCenter[0]][currStarCenter[1]-1], starAlpha) drawHex(-22, -20+HEX_HEIGHT, hexMap[currStarCenter[0]][currStarCenter[1]+1], starAlpha) drawHex(-52, -36, hexMap[currStarCenter[0]-1][currStarCenter[1]+pm], starAlpha) drawHex(-52, -40+HEX_HEIGHT, hexMap[currStarCenter[0]-1][currStarCenter[1]+pm+1], starAlpha) drawHex(8, -36, hexMap[currStarCenter[0]+1][currStarCenter[1]+pm], starAlpha) drawHex(8, -40+HEX_HEIGHT, hexMap[currStarCenter[0]+1][currStarCenter[1]+pm+1], starAlpha) gl.PopMatrix() } else { if fallticks == 0 { animateFall = make([]*freefall, 0) for x := 0; x < 10; x++ { topy := 19 if x%2 == 1 { topy = 0 } fromy := -1 for _, v := range currentMatches { if v[0] != x { continue } if v[1] > fromy { fromy = v[1] } } for _, v := range starMatches { if v[0] != x { continue } if v[1] > fromy { fromy = v[1] } } if fromy == -1 { continue } for y := fromy; y >= 0; y-- { found := false for _, v := range currentMatches { if v[0] == x && v[1] == y { found = true break } } for _, v := range starMatches { if v[0] == x && v[1] == y { found = true break } } if found { continue } animateFall = append(animateFall, &freefall{x, y, getFallTargetY(x, y), math.Pow(float64(y), 2)/16 + 0.5}) gl.PushMatrix() gl.Translatef(float32(x*33+102), float32(y*38+topy+94), 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[x][y], 1) gl.PopMatrix() } } fallticks++ } else { stillFalling := 0 for _, v := range animateFall { topy := 19 if v.x%2 == 1 { topy = 0 } newy := v.accel * math.Pow(float64(fallticks), 2) / 2 gl.PushMatrix() gl.Translatef(float32(v.x*33+102), float32(math.Min(float64(v.y*38+topy+94)+newy, float64(v.targetY*38+topy+94))), 0) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) drawHex(-22, -14, hexMap[v.x][v.y], 1) gl.PopMatrix() if float64(v.y*38+topy+94)+newy < float64(v.targetY*38+topy+94) { stillFalling++ } } fallticks++ if stillFalling == 0 { starScale = 1 starAlpha = 0 removeHexAndGenNew(currentMatches) makeStarAndGenNew(starMatches) currentMatches = checkHexMap() starMatches = checkHexStar() currStarCenter = []int{} scale = 1 fallticks = 0 mouseLock = false fmt.Println("Mouse unlocked 1") animateFall = nil } } } } if currExX > -1 && currExY > -1 { gl.PushMatrix() topy := 19 if currExX%2 == 1 { topy = 0 } if starRotate { timesToRotate = 0 gl.Translatef(float32(currExX*33+102), float32(currExY*38+topy+94), 0) gl.Scalef(1.3, 1.3, 1) gl.Rotatef(rotate, 0, 0, 1) drawHex(-22, -14, 6, 1) drawHex(-22, -14-HEX_HEIGHT, hexMap[currExX][currExY-1], 1) drawHex(-22, -20+HEX_HEIGHT, hexMap[currExX][currExY+1], 1) pm := 0 if currExX%2 == 1 { pm = -1 } drawHex(-52, -36, hexMap[currExX-1][currExY+pm], 1) drawHex(-52, -40+HEX_HEIGHT, hexMap[currExX-1][currExY+pm+1], 1) drawHex(8, -36, hexMap[currExX+1][currExY+pm], 1) drawHex(8, -40+HEX_HEIGHT, hexMap[currExX+1][currExY+pm+1], 1) } else { // gl.Translatef(float32(currExX*33+HEX_WIDTH+80), float32(currExxY*38+topy+20+80), 0) gl.Translatef(float32(prevSelectPos[0]), float32(prevSelectPos[1]), 0) gl.Scalef(1.3, 1.3, 1) gl.Rotatef(rotate, 0, 0, 1) for _, v := range selectedHex { switch v[2] { case 1: drawHex(-32, -34, hexMap[v[0]][v[1]], 1) case 2: drawHex(0, -17, hexMap[v[0]][v[1]], 1) case 3: drawHex(-32, 0, hexMap[v[0]][v[1]], 1) case 4: drawHex(-44, -19, hexMap[v[0]][v[1]], 1) case 5: drawHex(-12, -36, hexMap[v[0]][v[1]], 1) case 6: drawHex(-12, -2, hexMap[v[0]][v[1]], 1) } } // if currExX%2 == 0 { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -36, hexMap[currExX+1][currExY], 1) // } else { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -36, hexMap[currExX+1][currExY-1], 1) // } // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-44, -19, hexMap[currExX][currExY], 1) // if currExX%2 == 0 { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -2, hexMap[currExX+1][currExY+1], 1) // } else { // gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) // drawHex(-12, -2, hexMap[currExX+1][currExY], 1) // } } gl.PopMatrix() if !starRotate && rotate < 120 { rotate += 15 // rotate = 15 } else if starRotate && rotate < 60 { rotate += 6 } else { if starRotate { if currExX%2 == 0 { hexMap[currExX][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX+1][currExY+1], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY+1], hexMap[currExX-1][currExY] = hexMap[currExX-1][currExY], hexMap[currExX][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX+1][currExY+1], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY+1] } else { hexMap[currExX][currExY-1], hexMap[currExX+1][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY], hexMap[currExX-1][currExY-1] = hexMap[currExX-1][currExY-1], hexMap[currExX][currExY-1], hexMap[currExX+1][currExY-1], hexMap[currExX+1][currExY], hexMap[currExX][currExY+1], hexMap[currExX-1][currExY] } } else { v2 := make([][]int, 3) for _, v := range selectedHex { idx := 0 switch v[2] { case 1, 4: idx = 0 case 2, 5: idx = 1 case 3, 6: idx = 2 } v2[idx] = []int{v[0], v[1]} fmt.Println(idx, v[0], v[1]) } hexMap[v2[0][0]][v2[0][1]], hexMap[v2[1][0]][v2[1][1]], hexMap[v2[2][0]][v2[2][1]] = hexMap[v2[2][0]][v2[2][1]], hexMap[v2[0][0]][v2[0][1]], hexMap[v2[1][0]][v2[1][1]] } starMatches = checkHexStar() if len(starMatches) > 6 { fmt.Println(starMatches) } if len(starMatches) >= 6 { timesToRotate = 0 rotate = 0 currExX = -1 currExY = -1 starScale = 1 starAlpha = 1 starRotate = false currStarCenter = getStarCenter(starMatches) hexMap[currStarCenter[0]][currStarCenter[1]] = 6 // makeStarAndGenNew(starMatches) } else { matches := checkHexMap() if len(matches) > 0 { currentMatches = matches scale = 1 currExX = -1 currExY = -1 rotate = 0 timesToRotate = 0 starRotate = false } else { if timesToRotate == 0 { currExX = -1 currExY = -1 rotate = 0 timesToRotate = 0 starRotate = false mouseLock = false fmt.Println("Mouse unlocked 3") } rotate = 0 timesToRotate-- } } } } if !mouseLock { prevSelectPos = calcClosestCenter(posX, posY) drawBorderAtXY(float32(prevSelectPos[0]), float32(prevSelectPos[1]), prevSelectPos[2]) } gl.Flush() gl.Disable(gl.TEXTURE_2D) gl.Disable(gl.BLEND) }
// Printf draws the given string at the specified coordinates. // It expects the string to be a single line. Line breaks are not // handled as line breaks and are rendered as glyphs. // // In order to render multi-line text, it is up to the caller to split // the text up into individual lines of adequate length and then call // this method for each line seperately. func (f *Font) Printf(x, y float32, fs string, argv ...interface{}) error { indices := []rune(fmt.Sprintf(fs, argv...)) if len(indices) == 0 { return nil } // Runes form display list indices. // For this purpose, they need to be offset by -FontConfig.Low low := f.Config.Low for i := range indices { indices[i] -= low } var vp [4]int32 gl.GetIntegerv(gl.VIEWPORT, vp[:]) gl.PushAttrib(gl.TRANSFORM_BIT) gl.MatrixMode(gl.PROJECTION) gl.PushMatrix() gl.LoadIdentity() gl.Ortho(float64(vp[0]), float64(vp[2]), float64(vp[1]), float64(vp[3]), 0, 1) gl.PopAttrib() gl.PushAttrib(gl.LIST_BIT | gl.CURRENT_BIT | gl.ENABLE_BIT | gl.TRANSFORM_BIT) { gl.MatrixMode(gl.MODELVIEW) gl.Disable(gl.LIGHTING) gl.Disable(gl.DEPTH_TEST) gl.Enable(gl.BLEND) gl.Enable(gl.TEXTURE_2D) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.TexEnvf(gl.TEXTURE_ENV, gl.TEXTURE_ENV_MODE, gl.MODULATE) f.Texture.Bind(gl.TEXTURE_2D) var mv [16]float32 gl.GetFloatv(gl.MODELVIEW_MATRIX, mv[:]) gl.PushMatrix() { gl.LoadIdentity() mgw := float32(f.maxGlyphWidth) mgh := float32(f.maxGlyphHeight) switch f.Config.Dir { case LeftToRight, TopToBottom: gl.Translatef(x, float32(vp[3])-y-mgh, 0) case RightToLeft: gl.Translatef(x-mgw, float32(vp[3])-y-mgh, 0) } gl.MultMatrixf(&mv) gl.CallLists(len(indices), gl.UNSIGNED_INT, indices) } gl.PopMatrix() f.Texture.Unbind(gl.TEXTURE_2D) } gl.PopAttrib() gl.PushAttrib(gl.TRANSFORM_BIT) gl.MatrixMode(gl.PROJECTION) gl.PopMatrix() gl.PopAttrib() return glh.CheckGLError() }