Skip to content

Commit a6db164

Browse files
committed
added: Demo for C language
1 parent ddbd5bc commit a6db164

49 files changed

Lines changed: 88199 additions & 478 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/linux.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ jobs:
1818
- run: |
1919
sudo apt install xorg-dev libopengl-dev libgl1-mesa-dev
2020
sudo apt install libglfw3 libglfw3-dev libsdl2-dev
21-
cd example
2221
make

.github/workflows/windows.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,4 @@ jobs:
2626
mingw-w64-x86_64-SDL2
2727
- shell: msys2 {0}
2828
run: |
29-
pwd
30-
cd example
3129
make

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all:
2+
$(MAKE) -C demo/c
3+
$(MAKE) -C demo/cpp
4+
5+
.PHONY: clean
6+
7+
clean:
8+
$(MAKE) -C demo/c clean
9+
$(MAKE) -C demo/cpp clean

README.md

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,59 @@
1-
# ImGui Knobs
1+
## CImGui Knobs
22

33

44
![alt](https://github.com/dinau/cimgui-knobs/actions/workflows/windows.yml/badge.svg)
55
![alt](https://github.com/dinau/cimgui-knobs/actions/workflows/linux.yml/badge.svg)
66

7-
This is a port/adaptation of [imgui-rs-knobs](https://github.com/DGriffin91/imgui-rs-knobs), for C++.
7+
This is a port/adaptation of [ImGui-Knobs](https://github.com/altschuler/imgui-knobs), for C language (contributed to [ImGuin](http://github.com/dinau/imguin) project) by [system64MC](https://github.com/system64MC) at [2024/12](https://github.com/dinau/imguin/issues/16#issuecomment-2538066999).
8+
I've just isolated the sources from ImGuin project for maintenance and convenience and added [a simple demo program in C language](demo/c/main.c).
89

9-
![image](https://user-images.githubusercontent.com/956928/164050142-96a8dde4-7d2e-43e4-9afe-14ab48eac243.png)
10+
![alt](img/cimgui-knobs.png)
1011

11-
## Usage
12-
Add `imgui-knobs.cpp` and `imgui-knobs.h` to your project and include `imgui-knobs.h` in some source file.
12+
### Usage
1313

14+
---
1415

15-
```cpp
16-
static float value = 0;
16+
Add
1717

18-
if (ImGuiKnobs::Knob("Volume", &value, -6.0f, 6.0f, 0.1f, "%.1fdB", ImGuiKnobVariant_Tick)) {
19-
// value was changed
20-
}
18+
```txt
19+
cimgui-knobs.cpp
20+
cimgui-knobs.h
21+
imgui-knobs.cpp
22+
imgui-knobs.h
2123
```
2224

23-
Draw knobs using either `Knob` or `KnobInt`. The API is:
25+
to your project and include `cimgui-knobs.h` in some source file.
2426

25-
```
26-
bool ImGuiKnobs::Knob(label, *value, min, max, [speed, format, variant, size, flags, steps, angle_min, angle_max])
27-
bool ImGuiKnobs::KnobInt(label, *value, min, max, [speed, format, variant, size, flags, steps, angle_min, angle_max])
28-
```
27+
### Building and runiing demo in C language
2928

30-
You can implement **double click to reset** using standard imgui functionality:
29+
---
3130

32-
```cpp
33-
ImGuiKnobs::Knob("Volume", &value, -6.0f, 6.0f, 0.1f, "%.1fdB", ImGuiKnobVariant_Tick);
31+
#### Prerequisites
3432

35-
// Double click to reset, must be directly after drawing the knob so the right imgui "item" is used
36-
if (ImGui::IsItemActive() && ImGui::IsMouseDoubleClicked(0)) {
37-
value = 0;
38-
}
39-
```
33+
---
4034

41-
See `example/main.cpp` for a demo.
35+
- Windows10 OS or later
36+
- [Msys2/MinGW](https://www.msys2.org/) tools installed. (gcc, make, etc)
4237

43-
### Variants
44-
`variant` determines the visual look of the knob. Available variants are: `ImGuiKnobVariant_Tick`, `ImGuiKnobVariant_Dot`, `ImGuiKnobVariant_Wiper`, `ImGuiKnobVariant_WiperOnly`, `ImGuiKnobVariant_WiperDot`, `ImGuiKnobVariant_Stepped`, `ImGuiKnobVariant_Space`.
38+
```sh
39+
pacman -S mingw-w64-ucrt-x86_64-{gcc,glfw,SDL2,pkgconf} make
4540

46-
### Flags
47-
- `ImGuiKnobFlags_NoTitle`: Hide the top title.
48-
- `ImGuiKnobFlags_NoInput`: Hide the bottom drag input.
49-
- `ImGuiKnobFlags_ValueTooltip`: Show a tooltip with the current value on hover.
50-
- `ImGuiKnobFlags_DragHorizontal`: Use horizontal dragging only (default is bi-directional).
51-
- `ImGuiKnobFlags_DragVertical`: Use vertical dragging only (default is bi-directional).
41+
```
5242

53-
### Size
54-
You can specify a size given as the width of the knob (will be scaled according to ImGui's `FontGlobalScale`). Default (0) will use 4x line height.
43+
- Linux OS (Debian/ Ubuntu families)
5544

56-
### Colors
57-
By default the knobs are styled using colors from the imgui theme. You can push/pop style colors to change individual colors. The color ids/flags default to button colors, thus:
45+
```sh
46+
$ sudo apt install xorg-dev libopengl-dev libgl1-mesa-dev
47+
$ sudo apt install libglfw3 libglfw3-dev libsdl2-dev
48+
```
5849

59-
| ImGui Color | Knob meaning |
60-
|---|---|
61-
| `ImGuiCol_ButtonActive` | The "filled" part |
62-
| `ImGuiCol_ButtonHovered` | The "filled" part, when hovered |
63-
| `ImGuiCol_Button` | The knob track |
6450

65-
Use `ImGuiCol_FrameBg`/`ImGuiCol_Text` to change the input field colors.
51+
#### Building and running demo
6652

67-
### Steps
68-
Steps determines the number of steps draw, it is only used for the `ImGuiKnobVariant_Stepped` variant.
53+
---
54+
55+
```sh
56+
git clone --recursive https://github.com/dinau/cimgui-knobs
57+
cd cimgui-knobs/demo/c
58+
make run
59+
```

compile_flags.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-I.
2+
-Ilibs/cimgui
3+
-Ilibs/cimgui/imgui
4+
-Ilibs/cimgui/generator/output
5+
-IImGuiFileDialog
6+
7+
-Idemo/utils
8+
-Idemo/utils/fonticon
9+
10+
-DCIMGUI_USE_GLFW
11+
-DCIMGUI_USE_OPENGL3
12+
13+
-Ilibs/imgui-knobs

demo/c/.clang-format

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Generated from CLion C/C++ Code Style settings
2+
BasedOnStyle: LLVM
3+
AccessModifierOffset: -4
4+
AlignAfterOpenBracket: Align
5+
AlignConsecutiveAssignments: false
6+
AlignOperands: true
7+
AllowAllArgumentsOnNextLine: false
8+
AllowAllConstructorInitializersOnNextLine: false
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: Always
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortFunctionsOnASingleLine: All
13+
AllowShortIfStatementsOnASingleLine: Always
14+
AllowShortLambdasOnASingleLine: All
15+
AllowShortLoopsOnASingleLine: true
16+
AlwaysBreakAfterReturnType: None
17+
AlwaysBreakTemplateDeclarations: Yes
18+
BreakBeforeBraces: Custom
19+
BraceWrapping:
20+
AfterCaseLabel: false
21+
AfterClass: false
22+
AfterControlStatement: Never
23+
AfterEnum: false
24+
AfterFunction: false
25+
AfterNamespace: false
26+
AfterUnion: false
27+
BeforeCatch: false
28+
BeforeElse: false
29+
IndentBraces: false
30+
SplitEmptyFunction: false
31+
SplitEmptyRecord: true
32+
BreakBeforeBinaryOperators: None
33+
BreakBeforeTernaryOperators: true
34+
BreakConstructorInitializers: BeforeColon
35+
BreakInheritanceList: BeforeColon
36+
ColumnLimit: 0
37+
CompactNamespaces: false
38+
ContinuationIndentWidth: 8
39+
IndentCaseLabels: true
40+
IndentPPDirectives: None
41+
IndentWidth: 4
42+
KeepEmptyLinesAtTheStartOfBlocks: true
43+
MaxEmptyLinesToKeep: 2
44+
NamespaceIndentation: All
45+
ObjCSpaceAfterProperty: false
46+
ObjCSpaceBeforeProtocolList: true
47+
PointerAlignment: Right
48+
ReflowComments: false
49+
SpaceAfterCStyleCast: true
50+
SpaceAfterLogicalNot: false
51+
SpaceAfterTemplateKeyword: false
52+
SpaceBeforeAssignmentOperators: true
53+
SpaceBeforeCpp11BracedList: false
54+
SpaceBeforeCtorInitializerColon: true
55+
SpaceBeforeInheritanceColon: true
56+
SpaceBeforeParens: ControlStatements
57+
SpaceBeforeRangeBasedForLoopColon: true
58+
SpaceInEmptyParentheses: false
59+
SpacesBeforeTrailingComments: 0
60+
SpacesInAngles: false
61+
SpacesInCStyleCastParentheses: false
62+
SpacesInContainerLiterals: false
63+
SpacesInParentheses: false
64+
SpacesInSquareBrackets: false
65+
TabWidth: 4
66+
UseTab: Never
67+
BinPackArguments: false
68+
BinPackParameters: false

demo/c/Makefile

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#
2+
# Cross Platform Makefile
3+
# Compatible with MSYS2/MINGW, Ubuntu 14.04.1 and Mac OS X
4+
#
5+
# You will need GLFW (http://www.glfw.org):
6+
# Linux:
7+
# apt-get install libglfw-dev
8+
# Mac OS X:
9+
# brew install glfw
10+
# MSYS2:
11+
# pacman -S --noconfirm --needed mingw-w64-x86_64-toolchain mingw-w64-x86_64-glfw
12+
#
13+
14+
#CXX = g++
15+
#CXX = clang++
16+
17+
EXE = cimgui-knobs
18+
19+
20+
CIM_KNOBS_DIR = ../..
21+
LIBS_DIR = $(CIM_KNOBS_DIR)/libs
22+
CIMGUI_DIR = $(LIBS_DIR)/cimgui
23+
IMGUI_DIR = $(CIMGUI_DIR)/imgui
24+
IM_KNOBS_DIR = $(LIBS_DIR)/imgui-knobs
25+
UTILS_DIR = ../utils
26+
STB_DIR = $(LIBS_DIR)/STB
27+
BUILD_DIR = .build
28+
29+
VPATH = $(IM_KNOBS_DIR): $(CIM_KNOBS_DIR) : $(CIMGUI_DIR): $(IMGUI_DIR) : $(UTILS_DIR) : $(IMGUI_DIR)/backends
30+
31+
CSOURCES = main.c setupFonts.c utils.c
32+
SOURCES += imgui_impl_glfw.cpp \
33+
imgui_impl_opengl3.cpp \
34+
cimgui.cpp
35+
SOURCES += cimgui-knobs.cpp
36+
37+
SOURCES += $(notdir $(wildcard $(IMGUI_DIR)/*.cpp))
38+
SOURCES += $(notdir $(wildcard $(IM_KNOBS_DIR)/*.cpp))
39+
SOURCES += $(notdir $(wildcard $(CIM_KNOBS_DIR)/*.cpp))
40+
41+
CXXFLAGS += -DIMKNOBS_DEMO
42+
CXXFLAGS += $(HIDE_CONSOLE)
43+
CXXFLAGS += -MMD -MP
44+
CXXFLAGS += -O1 -Wl,-s -Wno-unused-but-set-variable -Wno-unused-variable -Wno-sign-compare
45+
CXXFLAGS += -I$(STB_DIR)
46+
CXXFLAGS += -I$(IM_KNOBS_DIR) -I$(CIMGUI_DIR) -I$(CIMGUI_DIR)/generator/output
47+
CXXFLAGS += -I$(UTILS_DIR) -I$(CIM_KNOBS_DIR) -I$(UTILS_DIR)/fonticon
48+
CXXFLAGS += -I$(IM_KNOBS_DIR) -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
49+
CXXFLAGS += -DCIMGUI_USE_OPENGL3 -DCIMGUI_USE_GLFW -DIMGUI_IMPL_OPENGL_LOADER_GL3W
50+
51+
OBJS = $(addprefix $(BUILD_DIR)/,$(CSOURCES:.c=.o)) $(addprefix $(BUILD_DIR)/,$(SOURCES:.cpp=.o))
52+
UNAME_S := $(shell uname -s)
53+
LINUX_GL_LIBS = -lGL
54+
55+
56+
CXXFLAGS += -g -Wall -Wformat
57+
LIBS =
58+
59+
##---------------------------------------------------------------------
60+
## OPENGL ES
61+
##---------------------------------------------------------------------
62+
63+
## This assumes a GL ES library available in the system, e.g. libGLESv2.so
64+
# CXXFLAGS += -DIMGUI_IMPL_OPENGL_ES2
65+
# LINUX_GL_LIBS = -lGLESv2
66+
67+
##---------------------------------------------------------------------
68+
## BUILD FLAGS PER PLATFORM
69+
##---------------------------------------------------------------------
70+
71+
ifeq ($(UNAME_S), Linux) #LINUX
72+
ECHO_MESSAGE = "Linux"
73+
LIBS += $(LINUX_GL_LIBS) `pkg-config --static --libs glfw3`
74+
75+
CXXFLAGS += -DIMGUI_IMPL_API="extern \"C\""
76+
CXXFLAGS += `pkg-config --cflags glfw3`
77+
CFLAGS = $(CXXFLAGS)
78+
endif
79+
80+
ifeq ($(UNAME_S), Darwin) #APPLE
81+
ECHO_MESSAGE = "Mac OS X"
82+
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo
83+
LIBS += -L/usr/local/lib -L/opt/local/lib -L/opt/homebrew/lib
84+
#LIBS += -lglfw3
85+
LIBS += -lglfw
86+
87+
CXXFLAGS += -I/usr/local/include -I/opt/local/include -I/opt/homebrew/include
88+
CFLAGS = $(CXXFLAGS)
89+
endif
90+
91+
ifeq ($(OS), Windows_NT)
92+
CXXFLAGS += -static
93+
HIDE_CONSOLE = -mwindows
94+
ECHO_MESSAGE = "MinGW"
95+
LIBS += -lglfw3 -lgdi32 -lopengl32 -limm32
96+
97+
CXXFLAGS += -DIMGUI_IMPL_API="extern \"C\" __declspec(dllexport)"
98+
CXXFLAGS += $(shell pkg-config --cflags glfw3)
99+
CFLAGS = $(CXXFLAGS)
100+
endif
101+
102+
103+
##---------------------------------------------------------------------
104+
## BUILD RULES
105+
##---------------------------------------------------------------------
106+
107+
all: $(BUILD_DIR) $(EXE)
108+
@echo Build complete for $(ECHO_MESSAGE)
109+
110+
$(BUILD_DIR):
111+
@-mkdir -p $@
112+
113+
$(BUILD_DIR)/%.o:%.c Makefile
114+
@echo [$(CC) ] $(notdir $<)
115+
@$(CC) $(CFLAGS) -c -o $@ $<
116+
117+
$(BUILD_DIR)/%.o:%.cpp Makefile
118+
@echo [$(CXX)] $(notdir $<)
119+
@$(CXX) $(CXXFLAGS) -c -o $@ $<
120+
121+
$(EXE): $(OBJS)
122+
@$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)
123+
124+
clean:
125+
@rm -rf $(BUILD_DIR)
126+
@rm -f $(EXE)
127+
128+
run: all
129+
./$(EXE)
130+
131+
gen:
132+
$(MAKE) -C ../.. gen
133+
134+
format:
135+
cp -f main.c main.c.org
136+
clang-format main.c.org > main.c
137+
138+
139+
include $(wildcard $(BUILD_DIR)/*.d)

0 commit comments

Comments
 (0)