Skip to content

DarknessFX Zig templates, projects, programs, libs and tools.

License

Notifications You must be signed in to change notification settings

DarknessFX/zig_workbench

Repository files navigation

 .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || |  ____  ____  | |
| | |_   ___ `.  | || | |_   ___  |  | || | |_  _||_  _| | |
| |   | |   `. \ | || |   | |_  \_|  | || |   \ \  / /   | |
| |   | |    | | | || |   |  _|      | || |    > `' <    | |
| |  _| |___.' / | || |  _| |_       | || |  _/ /'`\ \_  | |
| | |________.'  | || | |_____|      | || | |____||____| | |
| |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------' 

       DarknessFX @ https://dfx.lv | Twitter: @DrkFX

About

I'm studying and learning Zig Language (started Nov 19, 2023), sharing here my Zig projects, templates, libs and tools.

Using Windows 10, Zig x86_64 Version : 0.13.0

Note

This is a student project, code will run and build without errors (mostly because I just throw away errors), it is not a reference of "best coding practices". Suggestions or contributions changing the code to the "right way and best practices" are welcome.

Templates

Folder Description /Subsystem
Base Template for a console program. Console
BaseEx Template for a console program that hide the console window. Console
BaseWin Template for a Windows program. Windows
BaseWinEx Template for a Windows program, Windows API as submodule. Windows
BaseImGui Template with Dear ImGui via Dear Bindings. Extra: ImGui_Memory_Editor. Renderers: OpenGL2, OpenGL3, DirectX11, SDL3 OpenGL3, SDL2 OpenGL2, SDL3_Renderer, SDL2_Renderer Both
BaseLVGL Template with LVGL . Console
Basemicroui Template with microui. Renderers: SDL2, Windows GDI. Windows
BaseRayLib Template with RayLib and RayGUI. Console
BaseSDL2 Template with SDL2. Windows
BaseSDL3 Template with SDL3 Preview. Windows
BaseOpenGL Template with OpenGL (GL.h). Windows
BaseGLFW Template with GLFW and GLAD. Console
BaseDX11 Template with DirectX Direct3D 11. Windows
BaseWebGPU Template with WebGPU. Windows + Web
Usage
Steps Path example
Duplicate the template folder. C:\zig_workbench\BaseWin Copy\
Rename copy folder to your project name. C:\zig_workbench\MyZigProgram\
Copy tools/updateProjectName.bat to your project Tools folder. C:\zig_workbench\MyZigProgram\Tools\
Run updateProjectName.bat. C:\zig_workbench\MyZigProgram\Tools\updateProjectName.bat
Open YourProject VSCode Workspace. C:\zig_workbench\MyZigProgram\MyZigProgram VSCode Workspace.lnk

[!WARNING]
Current VSCode + ZLS extension do not accept @cInclude relative to project folder and will break builds.
After open your new project, remember to edit .zig files @cInclude including your full path and using / folder separator.

Zig have a useful built in feature: zig init that creates a basic project. I customized this basic project to fit my use cases, mostly to output to bin folder instead of zig-out\bin, have main.zig in the project root instead of src folder and use my VSCode Setup.

About Dear ImGui
Using Dear ImGui Docking 1.91.5 and Dear Bindings (20241108)
All necessary libraries are inside the template.

Note:

  • When changing renderers, make sure to rename all files (Main.zig, Build.zig, .vscode/Tasks.json).
  • Check tools/RunAll.bat to get a list of Zig Run commands to launch rendereres without renaming files.

ImGui_Memory_Editor: Edited from Dear Bindings output. Sample inside all ImGui templates and usage details at cimgui_memory_editor.h

About LVGL
Using LVGL from source (20231105, 9.0 Preview).
Used parts of code from lv_port_pc_visual_studio (lv_conf and main source).
All necessary libraries are inside the template.
Download Demos and Examples folders from the GitHub source
(and don't forget to add all .C files necessary to build).
About microui
microui.c and microui.h are inside the project folder.
Normally I would recommend to download from the official repository 
but sadly microui is outdated (last update 3 years ago) and I applied 
community pull requests to the source code.
It was necessary because the original code crashed with runtime 
error: member access within misaligned address and without the 
fix this project would not work.
About RayLib
Using RayLib from source (v5.0 from 20231118).
Using RayGUI from source (v4.1.0-dev from 20240704).
Rebuild raylib.dll because the original was compiled with 
/MT (Multi-thread) and Zig is allergic of this kind of DLL, 
changing the compile option to /MD solved the problem.
Raygui.h edited to fix a problem with Zig cImport where C 
sizeof(int) is treated as [*c]int instead of usize.
About SDL2
  Using SDL2 v2.28.4.
  Download SDL2 from: GitHub SDL2 Releases Page.
  For Windows devs: SDL2-devel-2.28.4-VC.zip 2.57 MB.
  Check BaseSDL2/lib/SDL2/filelist.txt for a description 
  of the folder structure and expected files path location.
About SDL3 Preview
  Built from source in 20240624, version 3.1.2.
About GLFW and GLAD
GLFW 3.3.8 (Win64 Static).
GLAD 2.0 (OpenGL 3.3 Compatibility).
All necessary libraries are inside the template.
About WebGPU
SDL2 and Dawn Native.
All necessary libraries are inside the template.
 
Requirements:
. [Emscripten](https://emscripten.org/) installed.
. Change a few hard-coded paths to reflect your local emscripten paths.

Programs

Folder Description /Subsystem
ToSystray Give other Windows programs ability to "Minimize to Systray". Windows
zTime Similar to Linux TIME command, add zTime in front of your command to get the time it took to execute.
Binary version ready to use is available to download at Releases Page - zTime v1.0.1.
Console
ToSystray Usage
Usage:
ToSystray.exe "Application.exe" "Application Name"
  
Example: ToSystray.exe "C:\Windows\System32\Notepad.exe" "Notepad"
zTime Usage
  Examples, run in your Command Prompt, Windows Terminal or Powershell:
    C:\>zTime zig build
    C:\>zTime dir
    C:\>zTime bin\ReleaseFast\YourProject.exe
  Suggestion:   Copy zTime.exe to your Zig folder, this way the application will   share the Environment Path and can be executed from anywhere.

Projects

Folder Description
ModernOpenGL Mike Shah ModernOpenGL Youtube Tutorials ported to Zig + SDL3.1.2 OpenGL 4.6.
ModernOpenGL Info
All files at Lib/SDL3 are the original ones from SDL Github, 
GLAD generated for 4.6 Core. For this project I did not use any 
zig binds or wrappers, just plain cImport.
A copy of SDL.h and glad.h exist at Lib root just replacing <> with "",
this change made easier for VSCode and ZLS display auto-complete.
I tried to @cImport GLM OpenGL Mathematics "C" version cGML, @import ziglm
and glm-zig, but each have their own quirks and styles while I'm wanted to 
keep the source code similar to the episodes, for this reason I built my 
own GLM.ZIG library with just a handful of used functions.
There are some small changes implemented from the original tutorial code, 
mostly adding full Translate, Rotate, Scale, Keyboard and Mouse Movement.
The Window Caption have a brief instruction of the keyboard settings and 
also, as my default, I used SHIFT+ESC to close the program.

Libraries

Folder Description
dos_color.zig Helper to output colors to console (std.debug.print) or debug console (OutputDebugString).
string.zig WIP String Type.
Libraries usage
  Create a /lib/ folder in your project folder.
  Copy the library file to /lib/ .
  Add const libname = @Import("lib/lib_name.zig"); to your source code.

Tools

Important

All tools should be run from YourProjectFolder\Tools\ folder,
do not run it directly in the main folder.

Folder Description
updateProjectName.bat Read parent folder name as your ProjectName and replace template references to ProjectName.
buildReleaseStrip.bat Call "zig build-exe" with additional options (ReleaseSmall, strip, single-thread), emit assembly (.s), llvm bitcode (.ll, .bc), C header, zig build report.
clean_zig-cache.bat Remove zig-cache from all sub folders.

Tools_ContextMenu

Folder Description
zig.ico Zig logo Icon file (.ico). (Resolutions 64p, 32p, 16p)
zig_256p.ico Zig logo Icon file (.ico) with higher resolutions . (Resolutions 256p, 128p, 64p, 32p, 16p)
zig_contextmenu.bat Launcher used by Windows Explorer context menu, copy to Zig folder PATH.
zig_icon.reg Associate an icon for .Zig files, add Build, Run, Test to Windows Explorer context menu. Read more details in the file comments.
zig_icon_cascade.reg Alternative of zig_icon.reg, groups all options inside a Zig submenu. Read more details in the file comments.

Tools to help setup Windows Explorer to apply icons to .ZIG files and add context menu short-cuts to Build, Run and Test.

📷zig_icon.reg - screenshot
After run zig_icon.reg, Windows Explorer will look like:
📷zig_icon_cascade.reg - screenshot
After run zig_icon_cascade.reg, Windows Explorer will look like:

About VSCode (Tips and Tricks)

I'm using VSCode to program in Zig and using Zig Language extension from ZLS - Zig Language Server.

Extensions that I use and recommend
  C/C++ from Microsoft. (**essential to enable Debug mode**.)
  C/C++ Extension Pack from Microsoft. (non-essential)
  C/C++ Themes. (non-essential)
  Hex Editor from Microsoft. (**essential in Debug mode**)
  OverType from DrMerfy. (non-essential? Add Insert key mode)
  Material Icon Theme from Philipp Kief. (non-essential, but make VSCode looks better)

Ctrl+R is the new F5

I changed a few VSCode keybindings for better use, mostly because Zig offer multiple options for Build, Run, Test, Generate Docs, and I setup VSCode Tasks.json with all available options.

The most important key binding change is CTRL+T to open TASKS menu, because VSCode keep the last task as first menu item, just pressing ENTER will: save current file and run the last ask.

Zig Build is fast and Template/.vscode/launch.json is already setup so VSCode F5 key (Start with Debugger) will activate Zig Build and start debug, it works great and fast. But even better is Zig Run Main, the way zig run compile and start (without debugger) is a lot faster and helps a lot to iterate and productivity. CTRL+T, Enter became one of my most used keyboard shortcut inside VSCode and CTRL+R to repeat the last task.

📷Task menu screenshot
VSCode Keybindings details
VSCode Keybindings file location at %APPDATA%\Code\User\keybindings.json

CTRL+T : Removed showAllSymbols and added runTask.
Reason : Easy access to Tasks menu and repeatable action to run last action.

CTRL+R : Removed all bindings.
Reason: Because this key binding try to reload the current document or display a different menu that also will try to close the current document... If I need I can go to menu File > Open Recent File instead of this shortcut that risk to close what I'm working.

[
  {
    "key": "ctrl+t",
    "command": "-workbench.action.showAllSymbols"
  },
  {
    "key": "ctrl+t",
    "command": "workbench.action.tasks.runTask"
  }
  {
    "key": "ctrl+r",
    "command": "-workbench.action.reloadWindow",
    "when": "isDevelopment"
  },
  {
    "key": "ctrl+r",
    "command": "-workbench.action.quickOpenNavigateNextInRecentFilesPicker",
    "when": "inQuickOpen && inRecentFilesPicker"
  },
  {
    "key": "ctrl+r",
    "command": "-workbench.action.openRecent"
  },
  {
    "key": "ctrl+t",
    "command": "workbench.action.tasks.runTask"
  },
  {
    "key": "ctrl+r",
    "command": "workbench.action.tasks.reRunTask"
  }
]

Copy your libraries DLL to Zig folder

When using libraries that have .DLL (for example SDL2_ttf.dll) the task Zig Run Main will fail because it cannot find the DLL and the exe was built somewhere in zig-cache. The easier way to fix is to copy the library DLL to your Zig folder.

Personal observation about VSCode

I have a Love/Hate relationship with VSCode, I only used it to code for Arduino and ESP32 with Platform.io and the hate is always when the editor try to be "smart and helpful".

Yellow lightbulbs sometimes show up to notify "There are no fix", JSON files organized to easier read key items are reorder because "that is how JSON should be ordered", at least 10% of keys typed are wasted deleting things that VSCode put there to help me. And my favorite gripe: You select a function name in the Intellisense combo, it prints at your source code "YourFunction([cursor here])" BUT it don't display the arguments list, you need to backspace to delete the ( opening parenthesis, type ( and now the tooltip show up with the arguments list.

Credits

Zig Language from ZigLang.org .
SDL2, SDL3 from libSDL.org .
GLFW from GLFW.org .
GLAD from Dav1dde .
microui from rxi .
Dear ImGui from Omar Cornut .
Dear Bindings from Ben Carter .
LVGL from LVGL Kft .
ModernOpenGL from Mike Shah .
RayLib and RayGUI from Ramon Santamaria (@raysan5) .
WebGPU from World Wide Web Consortium . .
Dawn from Google .

License

MIT - Free for everyone and any use.

DarknessFX @ https://dfx.lv | Twitter: @DrkFX
https://github.com/DarknessFX/zig_workbench

SEO Helper
Giving Google a little help pairing Zig + LIB words, because it find my twitter posts easier than this repo:
BaseWinEx   = Zig Windows program template with Windows API as submodule.
BaseImGui   = Zig ImGui Windows program template with renderers: OpenGL3, DirectX11, SDL3 OpenGL3, SDL2 OpenGL2, SDL3_Renderer, SDL2_Renderer.
BaseLVGL    = Zig LVGL Windows program template.
Basemicroui = Zig microui Windows program template with renderers: SDL2, Windows GDI.
BaseRayLib  = Zig RayLib and RayGUI Windows program template.
BaseSDL2    = Zig SDL2 Windows program template.
BaseSDL3    = Zig SDL3 Windows program template.
BaseOpenGL  = Zig OpenGL GL.h Windows program template.
BaseGLFW    = Zig GLFW GLAD Windows program template.
BaseDX11    = Zig DirectX Direct3D 11 DX11 Windows program template.
BaseWebGPU  = Zig WebGPU WASM program template.