table.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package stats
  2. import (
  3. "github.com/g3n/engine/gls"
  4. "github.com/g3n/engine/gui"
  5. "time"
  6. )
  7. // StatsTable is a gui.Table panel with statistics
  8. type StatsTable struct {
  9. *gui.Table // embedded table panel
  10. fields []*field // array of fields to show
  11. stats *Stats // statistics object
  12. }
  13. type field struct {
  14. id string
  15. row int
  16. }
  17. // NewStatsTable creates and returns a pointer to a new statistics table panel
  18. func NewStatsTable(width, height float32, gs *gls.GLS) *StatsTable {
  19. st := new(StatsTable)
  20. t, err := gui.NewTable(width, height, []gui.TableColumn{
  21. {Id: "f", Header: "Stat", Width: 50, Minwidth: 32, Align: gui.AlignRight, Format: "%s", Resize: true, Expand: 2},
  22. {Id: "v", Header: "Value", Width: 50, Minwidth: 32, Align: gui.AlignRight, Format: "%d", Resize: false, Expand: 1},
  23. })
  24. if err != nil {
  25. panic(err)
  26. }
  27. st.Table = t
  28. st.ShowHeader(false)
  29. st.addRow("shaders", "Shaders:")
  30. st.addRow("vaos", "Vaos:")
  31. st.addRow("buffers", "Buffers:")
  32. st.addRow("textures", "Textures:")
  33. st.addRow("unisets", "Uniforms/frame:")
  34. st.addRow("drawcalls", "Draw calls/frame:")
  35. st.addRow("cgocalls", "CGO calls/frame:")
  36. st.stats = NewStats(gs)
  37. return st
  38. }
  39. // Update should be called normally in the render loop with the desired update interval
  40. func (st *StatsTable) Update(d time.Duration) {
  41. if st.stats.Update(d) {
  42. st.update()
  43. }
  44. }
  45. func (st *StatsTable) update() {
  46. for i := 0; i < len(st.fields); i++ {
  47. f := st.fields[i]
  48. switch f.id {
  49. case "shaders":
  50. st.Table.SetCell(f.row, "v", st.stats.Glstats.Shaders)
  51. case "vaos":
  52. st.Table.SetCell(f.row, "v", st.stats.Glstats.Vaos)
  53. case "buffers":
  54. st.Table.SetCell(f.row, "v", st.stats.Glstats.Buffers)
  55. case "textures":
  56. st.Table.SetCell(f.row, "v", st.stats.Glstats.Textures)
  57. case "unisets":
  58. st.Table.SetCell(f.row, "v", st.stats.Unisets)
  59. case "drawcalls":
  60. st.Table.SetCell(f.row, "v", st.stats.Drawcalls)
  61. case "cgocalls":
  62. st.Table.SetCell(f.row, "v", st.stats.Cgocalls)
  63. }
  64. }
  65. }
  66. func (st *StatsTable) addRow(id, label string) {
  67. f := new(field)
  68. f.id = id
  69. f.row = st.Table.RowCount()
  70. st.Table.AddRow(map[string]interface{}{"f": label, "v": 0})
  71. st.fields = append(st.fields, f)
  72. }