Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add D example #45

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bindings/D/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### D Language Example

```
dmd main.d clay.c
```
2 changes: 2 additions & 0 deletions bindings/D/clay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CLAY_IMPLEMENTATION
#include "../../clay.h"
88 changes: 88 additions & 0 deletions bindings/D/main.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import clay;

import core.stdc.stdlib;

__gshared:

Clay_LayoutConfig layoutElement = { padding: {5} };

extern(C) void main()
{
ulong totalMemorySize = Clay_MinMemorySize();
Clay_Arena clayMemory = {
label: str("Clay Memory Arena"),
capacity: totalMemorySize,
memory: cast(char*)malloc(totalMemorySize)
};

Clay_Initialize(clayMemory, Clay_Dimensions(1024,768));
Clay_BeginLayout();
if (ClayBegin( Rectangle(color: Clay_Color(255,255,255,0)), Layout(layoutElement)))
{ }
ClayEnd();
}


// helper functions
Clay_String str(string it)
{
return Clay_String(cast(int)it.length, it.ptr);
}

bool ClayBegin(A...)(A configs)
{
Clay__OpenElement();
foreach(config; configs)
{
alias T = typeof(config);
static if (is(T == Clay_ElementId))
{
Clay__AttachId(config);
}
else static if(is(T == Clay_LayoutConfig*))
{
Clay__AttachLayoutConfig(config);
}
else static if(is(T == Clay_ElementConfig))
{
Clay__AttachElementConfig(config.config, config.type);
}
else static assert(0, "unsupported " ~ typeof(config).stringof);
}

Clay__ElementPostConfiguration();
return true;
}

void ClayEnd()
{
Clay__CloseElement();
}

Clay_ElementId Id(string label)
{
return Clay__HashString(str(label), 0, 0);
}

Clay_LayoutConfig* Layout(lazy Clay_Sizing sizing = Clay_Sizing.init)
{
Clay_LayoutConfig config;
config.sizing = sizing;
return Clay__StoreLayoutConfig(config);
}

Clay_LayoutConfig* Layout(Clay_LayoutConfig config)
{
return Clay__StoreLayoutConfig(config);
}

Clay_ElementConfig Rectangle(lazy Clay_Color color = Clay_Color.init)
{
Clay_RectangleElementConfig config;
config.color = color;

Clay_ElementConfig ret;
ret.type = Clay__ElementConfigType.CLAY__ELEMENT_CONFIG_TYPE_RECTANGLE;
ret.config.rectangleElementConfig = Clay__StoreRectangleElementConfig(config);
return ret;
}