Skip to content

Instantly share code, notes, and snippets.

@haiitch
Created October 14, 2017 12:52
Show Gist options
  • Save haiitch/8614b05e86893b1a09a14557ae9a4e70 to your computer and use it in GitHub Desktop.
Save haiitch/8614b05e86893b1a09a14557ae9a4e70 to your computer and use it in GitHub Desktop.
A raylib-go test that crashes randomly
package main
import (
"fmt"
"image"
"math"
"time"
"math/rand"
"github.com/fogleman/gg"
"github.com/fogleman/ease"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Application struct {
Title string
Init func()
OnUpdate func(*Application)
OnDraw func(*Application)
Close func()
SystemFont *FontHandle
SansFont FontHandle
SerifFont FontHandle
MonoFont FontHandle
DingbatsFont FontHandle
Color rl.Color
px int32
py int32
}
const (
IconSEARCH = 0x1F50D
IconCIRCLEDCROSS = 0x2716
IconCHEVRONRIGHT = 0xE75E
IconCHECK = 0x2713
IconLOGIN = 0xE740
IconTRASH = 0xE729
)
func RenderImage() image.Image {
const S = 4096
const N = 4096
dc := gg.NewContext(S, N)
dc.SetRGB(1, 1, 1)
dc.Clear()
for i := 0; i <= N; i=i+2 {
t := float64(i) / N
d := (t*S*0.6 + 20)
a := t * math.Pi * 2 * 600
x := S/2 + math.Cos(a)*d
y := S/2 + math.Sin(a)*d
r := t * 50
r1 := rand.Float64() * 0.4
if r1 < 0 {
r1 = 0
}
g1 := rand.Float64() + r1
if r1 < 0 {
g1 = 0
}
b1 := rand.Float64() + r1
if b1 < 0 {
b1 = 0
}
r1, g1, b1 = 0.50, 0.90, 0.95
dc.SetRGBA(r1, g1, b1, b1)
dc.DrawCircle(x, y, r)
dc.Fill()
}
img := dc.Image()
return img
}
func RLImage(img image.Image) *rl.Image {
rgba := gg.ImageToRGBA(img)
b := img.Bounds()
rli := rl.LoadImagePro( rgba.Pix, int32(b.Max.X), int32(b.Max.Y), rl.UncompressedR8g8b8a8)
return rli
}
func NewApplication(title string) *Application {
var app Application
app.Title = title
return &app
}
type FontHandle struct {
Font rl.SpriteFont
FontChars int32
FontSize int32
TextSize *rl.Vector2
CurrentFontFilter int32
LineHeight int32
}
func (a *Application) LoadFont(f *FontHandle, filename string, size int32, lineheight int32) {
//f = &a.SystemFont
f.FontChars = int32(0)
f.Font = rl.LoadSpriteFontTTF(filename, size, 0, &f.FontChars)
rl.GenTextureMipmaps(&f.Font.Texture) // mostly relevant for 3d views only
f.FontSize = f.Font.BaseSize
f.TextSize = &rl.Vector2{} // TODO: empty, lookup what goes here later
rl.SetTextureFilter(f.Font.Texture, rl.FilterPoint)
f.CurrentFontFilter = 0 // FilterPoint
f.LineHeight = lineheight // TODO: this should be computed fron the font's reported size
}
func (a *Application) FontSetup() {
a.LoadFont(&a.DingbatsFont, "../fonts/dings/halflings.ttf", 36, 30)
fmt.Printf("Dingbats: %+v\n\n", a.DingbatsFont)
// a.LoadFont(&a.MonoFont, "../fonts/profont/profont.ttf", 20, 17)
a.LoadFont(&a.MonoFont, "../fonts/proggy/proggytiny.ttf", 18, 18)
fmt.Printf("Monofont: %+v\n\n", a.MonoFont)
a.LoadFont(&a.SerifFont, "../fonts/lato/Lato-Medium.ttf", 26, 21)
fmt.Printf("Serifont: %+v\n\n", a.SerifFont)
a.LoadFont(&a.SansFont, "../fonts/texgyre/texgyreheros-regular-xheightfix.ttf", 24, 20)
fmt.Printf("Sansfont: %+v\n\n", a.SansFont)
a.SystemFont = &a.MonoFont
}
func (a *Application) SelectFont(f *FontHandle) {
a.SystemFont = f
}
func (a *Application) SetFontColor(c rl.Color) {
a.Color = c
}
func (a *Application) PrintXY(x int32, y int32, text string) {
var sf rl.SpriteFont
sf = a.SystemFont.Font
fontpos := rl.NewVector2(float32(x), float32(y))
rl.DrawTextEx(sf, text, fontpos, float32(a.SystemFont.FontSize), 0, a.Color)
}
func (a *Application) Print(text string) {
a.PrintXY(a.px, a.py, text)
a.py = a.py + a.SystemFont.LineHeight
}
func cpToUTF8(cp int) string {
return string([]rune{rune(cp)})
}
func (a *Application) Run() {
screenWidth := int32(1024)
screenHeight := int32(800)
// rl.SetDebug(false) // no effect on general logs
rl.SetConfigFlags(rl.FlagVsyncHint + rl.FlagWindowDecorated + rl.FlagWindowResizable)
rl.InitWindow(screenWidth, screenHeight, a.Title)
rl.SetTargetFPS(62)
a.FontSetup()
var rli *rl.Image
var img image.Image
st := time.Now()
img = RenderImage()
rli = RLImage(img)
texture := rl.LoadTextureFromImage(rli)
et := time.Now()
elapsed := et.Sub(st)
fmt.Printf("Render time: %+v\n", elapsed)
fmt.Printf("\nrl.image: %+v\n", rli)
fmt.Printf("rl.texture: %+v\n", texture)
var i float64 = 0
var ir float32 = 0
var s float32 = 0.5
s = 0.5
rotation := float32(0.0)
for !rl.WindowShouldClose() {
a.px = 20; a.py = 40
mousepos := rl.GetMousePosition()
mousex := int32(mousepos.X)
mousey := int32(mousepos.Y)
a.OnUpdate(a) // this is meant to provide a callback to the library user for custom drawing
rl.BeginDrawing()
windowheight := rl.GetScreenHeight()
windowwidth := rl.GetScreenWidth()
framewidth := texture.Width
frameheight := texture.Height
scale := float32(ease.OutSine(float64(s)))
srect := rl.NewRectangle(0, 0, texture.Width, int32(texture.Height))
drect := rl.NewRectangle(windowwidth/2, windowheight/2,
int32(float32(framewidth)*scale), int32(float32(frameheight)*scale) )
orig := rl.NewVector2(float32(framewidth)*scale/2, float32(frameheight)*scale/2)
rl.ClearBackground(rl.White)
rl.DrawTexturePro( texture, srect, drect, orig, -rotation, rl.White )
_ , _, _ = srect, drect, orig
//////////////////////////////////////////////////////////
// buggy, use DrawRectangleLines instead
// rl.DrawLine( mousex, 0, mousex, windowheight-1, rl.Red)
// rl.DrawLine( 0, mousey, windowwidth-1, mousey, rl.Red)
//////////////////////////////////////////////////////////
// draw crosshairs above animation, but below text
rl.DrawRectangleLines( mousex, 0, 1, windowheight-1, rl.Orange)
rl.DrawRectangleLines( 0, mousey, windowwidth, 1, rl.Orange )
rl.DrawRectangleLines(mousex-10, mousey-10, 20, 20, rl.Orange)
/////////////////////////////////////////////////////////
a.OnDraw(a) // this is meant to provide a callback to the library user for custom drawing
a.SetFontColor(rl.Black)
a.Print(fmt.Sprintf("Window dimensions: %+v x %+v", windowwidth, windowheight ) )
a.Print(fmt.Sprintf("Mouse position: %+v", mousepos))
rl.EndDrawing()
// if i<3 {i = i+0.1}
rotation = rotation + 0.03
// s = s + 0.0001
if s >=7 {s = 0.2}
_ = i
_ = ir
}
}
func updatethings(a *Application) {
// removed for simplification
}
func drawthings(a *Application) {
a.SelectFont(&a.MonoFont)
a.SetFontColor(rl.Blue)
a.Print(fmt.Sprintf("FPS: %+v", rl.GetFPS()))
a.SetFontColor(rl.Black)
a.SelectFont(&a.SerifFont)
a.Print("")
a.Print("")
a.Print("If all places on Earth are in The Aleph,")
a.Print("all luminaries will be there, all the torches,")
a.Print("all the sources of light.")
a.Print("")
a.Print("-- JLB")
a.Print("")
a.SelectFont(&a.MonoFont)
a.Print("Twingback Twingon Ontomap [ipfsave] [discard]")
}
func main() {
Myapp := NewApplication("simplified, removed all networking and widgetry code")
Myapp.OnUpdate = updatethings
Myapp.OnDraw = drawthings
Myapp.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment