Skip to content
This repository was archived by the owner on Dec 31, 2022. It is now read-only.

Commit 0d7a7db

Browse files
authored
Merge pull request #4 from inkyblackness/feature/linter-action
Feature/linter action
2 parents 5fb94de + 107722a commit 0d7a7db

File tree

10 files changed

+86
-42
lines changed

10 files changed

+86
-42
lines changed

.github/workflows/golangci-lint.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: golangci-lint
2+
on: [push, pull_request]
3+
jobs:
4+
golangci:
5+
name: lint
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
- name: Install libraries
10+
run: |
11+
sudo apt-get update
12+
sudo apt-get install -y mesa-utils mesa-common-dev libsdl2-dev libglfw3-dev
13+
14+
- name: golangci-lint
15+
uses: golangci/golangci-lint-action@v1
16+
with:
17+
version: v1.27

.golangci.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ linters:
1414
- goimports # disabled because of so many false-positives with "imgui-go"
1515
- gosec
1616
- maligned
17+
- gomnd # disabled as most OpenGL stuff are direct constants.
1718
- funlen # disabled because of example code. While people will probably tend to duplicate this, they'll handle it on their own.
18-
- godox # for the deliberate TODO tags
1919
- wsl # this one became too pedantic and with a different style than I would.
2020

2121
issues:
@@ -29,12 +29,3 @@ issues:
2929
linters-settings:
3030
lll:
3131
line-length: 160
32-
33-
service:
34-
golangci-lint-version: 1.21.x
35-
prepare:
36-
- apt-get update && apt-get install -y mesa-utils mesa-common-dev libsdl2-dev libglfw3-dev
37-
# the following a fix as per https://github.com/golangci/golangci/issues/29#issuecomment-460649402 -- missing vendor libraries
38-
- GO111MODULE=on go mod vendor
39-
# the following a fix as per https://github.com/esimov/diagram/issues/7#issuecomment-443186458 -- glfw library broken through vendoring
40-
- GO111MODULE=off go get -u github.com/go-gl/glfw/v3.2/glfw/glfw; cp -R $GOPATH/src/github.com/go-gl/glfw/v3.2/glfw/glfw vendor/github.com/go-gl/glfw/v3.2/glfw/glfw

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Examples of Dear ImGui for Go
22

33
[![Go Report Card](https://goreportcard.com/badge/github.com/inkyblackness/imgui-go-examples)](https://goreportcard.com/report/github.com/inkyblackness/imgui-go-examples)
4-
[![GolangCI](https://golangci.com/badges/github.com/inkyblackness/imgui-go-examples.svg)](https://golangci.com)
4+
[![Lint Status](https://github.com/inkyblackness/imgui-go-examples/workflows/golangci-lint/badge.svg)](https://github.com/inkyblackness/imgui-go-examples/actions)
55

66
This project contains a set of [Go](https://www.golang.org) examples for [imgui-go](https://github.com/inkyblackness/imgui-go), which is a wrapper for [**Dear ImGui**](https://github.com/ocornut/imgui).
77

internal/demo/Run.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ type Renderer interface {
4747
Render(displaySize [2]float32, framebufferSize [2]float32, drawData imgui.DrawData)
4848
}
4949

50+
const (
51+
millisPerSecond = 1000
52+
sleepDuration = time.Millisecond * 25
53+
)
54+
5055
// Run implements the main program loop of the demo. It returns when the platform signals to stop.
5156
// This demo application shows some basic features of ImGui, as well as exposing the standard demo window.
5257
func Run(p Platform, r Renderer) {
5358
imgui.CurrentIO().SetClipboard(clipboard{platform: p})
5459

5560
showDemoWindow := false
5661
clearColor := [3]float32{0.0, 0.0, 0.0}
57-
f := float32(0.0)
62+
f := float32(0)
5863
counter := 0
5964
showAnotherWindow := false
6065

@@ -83,7 +88,7 @@ func Run(p Platform, r Renderer) {
8388
imgui.Text(fmt.Sprintf("counter = %d", counter))
8489

8590
imgui.Text(fmt.Sprintf("Application average %.3f ms/frame (%.1f FPS)",
86-
1000.0/imgui.CurrentIO().Framerate(), imgui.CurrentIO().Framerate()))
91+
millisPerSecond/imgui.CurrentIO().Framerate(), imgui.CurrentIO().Framerate()))
8792
}
8893

8994
// 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
@@ -102,7 +107,9 @@ func Run(p Platform, r Renderer) {
102107
if showDemoWindow {
103108
// Normally user code doesn't need/want to call this because positions are saved in .ini file anyway.
104109
// Here we just want to make the demo initial state a bit more friendly!
105-
imgui.SetNextWindowPosV(imgui.Vec2{X: 650, Y: 20}, imgui.ConditionFirstUseEver, imgui.Vec2{})
110+
const demoX = 650
111+
const demoY = 20
112+
imgui.SetNextWindowPosV(imgui.Vec2{X: demoX, Y: demoY}, imgui.ConditionFirstUseEver, imgui.Vec2{})
106113

107114
imgui.ShowDemoWindow(&showDemoWindow)
108115
}
@@ -118,6 +125,6 @@ func Run(p Platform, r Renderer) {
118125
p.PostRender()
119126

120127
// sleep to avoid 100% CPU usage for this demo
121-
<-time.After(time.Millisecond * 25)
128+
<-time.After(sleepDuration)
122129
}
123130
}

internal/platforms/constants.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package platforms
2+
3+
const (
4+
windowWidth = 1280
5+
windowHeight = 720
6+
7+
mouseButtonPrimary = 0
8+
mouseButtonSecondary = 1
9+
mouseButtonTertiary = 2
10+
mouseButtonCount = 3
11+
)

internal/platforms/errors.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package platforms
2+
3+
// StringError describes a basic error with static information.
4+
type StringError string
5+
6+
// Error returns the string itself.
7+
func (err StringError) Error() string {
8+
return string(err)
9+
}
10+
11+
const (
12+
// ErrUnsupportedClientAPI is used in case the API is not available by the platform.
13+
ErrUnsupportedClientAPI = StringError("unsupported ClientAPI")
14+
)

internal/platforms/glfw.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// GLFWClientAPI identifies the render system that shall be initialized.
1515
type GLFWClientAPI string
1616

17-
// GLFWClientAPI constants
17+
// This is a list of GLFWClientAPI constants.
1818
const (
1919
GLFWClientAPIOpenGL2 GLFWClientAPI = "OpenGL2"
2020
GLFWClientAPIOpenGL3 GLFWClientAPI = "OpenGL3"
@@ -36,7 +36,7 @@ func NewGLFW(io imgui.IO, clientAPI GLFWClientAPI) (*GLFW, error) {
3636

3737
err := glfw.Init()
3838
if err != nil {
39-
return nil, fmt.Errorf("failed to initialize glfw: %v", err)
39+
return nil, fmt.Errorf("failed to initialize glfw: %w", err)
4040
}
4141

4242
switch clientAPI {
@@ -50,13 +50,13 @@ func NewGLFW(io imgui.IO, clientAPI GLFWClientAPI) (*GLFW, error) {
5050
glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
5151
default:
5252
glfw.Terminate()
53-
return nil, fmt.Errorf("unsupported ClientAPI: <%s>", clientAPI)
53+
return nil, ErrUnsupportedClientAPI
5454
}
5555

56-
window, err := glfw.CreateWindow(1280, 720, "ImGui-Go GLFW+"+string(clientAPI)+" example", nil, nil)
56+
window, err := glfw.CreateWindow(windowWidth, windowHeight, "ImGui-Go GLFW+"+string(clientAPI)+" example", nil, nil)
5757
if err != nil {
5858
glfw.Terminate()
59-
return nil, fmt.Errorf("failed to create window: %v", err)
59+
return nil, fmt.Errorf("failed to create window: %w", err)
6060
}
6161
window.MakeContextCurrent()
6262
glfw.SwapInterval(1)
@@ -165,15 +165,15 @@ func (platform *GLFW) installCallbacks() {
165165
}
166166

167167
var glfwButtonIndexByID = map[glfw.MouseButton]int{
168-
glfw.MouseButton1: 0,
169-
glfw.MouseButton2: 1,
170-
glfw.MouseButton3: 2,
168+
glfw.MouseButton1: mouseButtonPrimary,
169+
glfw.MouseButton2: mouseButtonSecondary,
170+
glfw.MouseButton3: mouseButtonTertiary,
171171
}
172172

173173
var glfwButtonIDByIndex = map[int]glfw.MouseButton{
174-
0: glfw.MouseButton1,
175-
1: glfw.MouseButton2,
176-
2: glfw.MouseButton3,
174+
mouseButtonPrimary: glfw.MouseButton1,
175+
mouseButtonSecondary: glfw.MouseButton2,
176+
mouseButtonTertiary: glfw.MouseButton3,
177177
}
178178

179179
func (platform *GLFW) mouseButtonChange(window *glfw.Window, rawButton glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {

internal/platforms/sdl.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// SDLClientAPI identifies the render system that shall be initialized.
1414
type SDLClientAPI string
1515

16-
// SDLClientAPI constants
16+
// This is a list of SDLClientAPI constants.
1717
const (
1818
SDLClientAPIOpenGL2 SDLClientAPI = "OpenGL2"
1919
SDLClientAPIOpenGL3 SDLClientAPI = "OpenGL3"
@@ -27,7 +27,7 @@ type SDL struct {
2727
shouldStop bool
2828

2929
time uint64
30-
buttonsDown [3]bool
30+
buttonsDown [mouseButtonCount]bool
3131
}
3232

3333
// NewSDL attempts to initialize an SDL context.
@@ -36,13 +36,14 @@ func NewSDL(io imgui.IO, clientAPI SDLClientAPI) (*SDL, error) {
3636

3737
err := sdl.Init(sdl.INIT_VIDEO)
3838
if err != nil {
39-
return nil, fmt.Errorf("failed to initialize SDL2: %v", err)
39+
return nil, fmt.Errorf("failed to initialize SDL2: %w", err)
4040
}
4141

42-
window, err := sdl.CreateWindow("ImGui-Go SDL2+"+string(clientAPI)+" example", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, 1280, 720, sdl.WINDOW_OPENGL)
42+
window, err := sdl.CreateWindow("ImGui-Go SDL2+"+string(clientAPI)+" example",
43+
sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, windowWidth, windowHeight, sdl.WINDOW_OPENGL)
4344
if err != nil {
4445
sdl.Quit()
45-
return nil, fmt.Errorf("failed to create window: %v", err)
46+
return nil, fmt.Errorf("failed to create window: %w", err)
4647
}
4748

4849
platform := &SDL{
@@ -62,7 +63,7 @@ func NewSDL(io imgui.IO, clientAPI SDLClientAPI) (*SDL, error) {
6263
_ = sdl.GLSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE)
6364
default:
6465
platform.Dispose()
65-
return nil, fmt.Errorf("unsupported ClientAPI: <%s>", clientAPI)
66+
return nil, ErrUnsupportedClientAPI
6667
}
6768
_ = sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1)
6869
_ = sdl.GLSetAttribute(sdl.GL_DEPTH_SIZE, 24)
@@ -71,12 +72,12 @@ func NewSDL(io imgui.IO, clientAPI SDLClientAPI) (*SDL, error) {
7172
glContext, err := window.GLCreateContext()
7273
if err != nil {
7374
platform.Dispose()
74-
return nil, fmt.Errorf("failed to create OpenGL context: %v", err)
75+
return nil, fmt.Errorf("failed to create OpenGL context: %w", err)
7576
}
7677
err = window.GLMakeCurrent(glContext)
7778
if err != nil {
7879
platform.Dispose()
79-
return nil, fmt.Errorf("failed to set current OpenGL context: %v", err)
80+
return nil, fmt.Errorf("failed to set current OpenGL context: %w", err)
8081
}
8182

8283
_ = sdl.GLSetSwapInterval(1)
@@ -129,7 +130,8 @@ func (platform *SDL) NewFrame() {
129130
if platform.time > 0 {
130131
platform.imguiIO.SetDeltaTime(float32(currentTime-platform.time) / float32(frequency))
131132
} else {
132-
platform.imguiIO.SetDeltaTime(1.0 / 60.0)
133+
const fallbackDelta = 1.0 / 60.0
134+
platform.imguiIO.SetDeltaTime(fallbackDelta)
133135
}
134136
platform.time = currentTime
135137

@@ -200,11 +202,11 @@ func (platform *SDL) processEvent(event sdl.Event) {
200202
buttonEvent := event.(*sdl.MouseButtonEvent)
201203
switch buttonEvent.Button {
202204
case sdl.BUTTON_LEFT:
203-
platform.buttonsDown[0] = true
205+
platform.buttonsDown[mouseButtonPrimary] = true
204206
case sdl.BUTTON_RIGHT:
205-
platform.buttonsDown[1] = true
207+
platform.buttonsDown[mouseButtonSecondary] = true
206208
case sdl.BUTTON_MIDDLE:
207-
platform.buttonsDown[2] = true
209+
platform.buttonsDown[mouseButtonTertiary] = true
208210
}
209211
case sdl.TEXTINPUT:
210212
inputEvent := event.(*sdl.TextInputEvent)

internal/renderers/OpenGL2.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type OpenGL2 struct {
2020
func NewOpenGL2(io imgui.IO) (*OpenGL2, error) {
2121
err := gl.Init()
2222
if err != nil {
23-
return nil, fmt.Errorf("failed to initialize OpenGL: %v", err)
23+
return nil, fmt.Errorf("failed to initialize OpenGL: %w", err)
2424
}
2525

2626
renderer := &OpenGL2{
@@ -96,7 +96,8 @@ func (renderer *OpenGL2) Render(displaySize [2]float32, framebufferSize [2]float
9696
indexSize := imgui.IndexBufferLayout()
9797

9898
drawType := gl.UNSIGNED_SHORT
99-
if indexSize == 4 {
99+
const bytesPerUint32 = 4
100+
if indexSize == bytesPerUint32 {
100101
drawType = gl.UNSIGNED_INT
101102
}
102103

internal/renderers/OpenGL3.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type OpenGL3 struct {
3131
func NewOpenGL3(io imgui.IO) (*OpenGL3, error) {
3232
err := gl.Init()
3333
if err != nil {
34-
return nil, fmt.Errorf("failed to initialize OpenGL: %v", err)
34+
return nil, fmt.Errorf("failed to initialize OpenGL: %w", err)
3535
}
3636

3737
renderer := &OpenGL3{
@@ -145,7 +145,8 @@ func (renderer *OpenGL3) Render(displaySize [2]float32, framebufferSize [2]float
145145
gl.VertexAttribPointer(uint32(renderer.attribLocationColor), 4, gl.UNSIGNED_BYTE, true, int32(vertexSize), unsafe.Pointer(uintptr(vertexOffsetCol)))
146146
indexSize := imgui.IndexBufferLayout()
147147
drawType := gl.UNSIGNED_SHORT
148-
if indexSize == 4 {
148+
const bytesPerUint32 = 4
149+
if indexSize == bytesPerUint32 {
149150
drawType = gl.UNSIGNED_INT
150151
}
151152

0 commit comments

Comments
 (0)