mirror of
https://github.com/nicbarker/clay.git
synced 2025-01-23 01:46:02 +00:00
Compare commits
6 Commits
29ac80189c
...
9680a7bdae
Author | SHA1 | Date | |
---|---|---|---|
|
9680a7bdae | ||
|
9d3fba39be | ||
|
4961f2153e | ||
|
ea3e29be5c | ||
|
134beca09c | ||
|
b3cdf90d39 |
5
bindings/D/README.md
Normal file
5
bindings/D/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
### D Language Example
|
||||
|
||||
```
|
||||
dmd main.d clay.c
|
||||
```
|
2
bindings/D/clay.c
Normal file
2
bindings/D/clay.c
Normal file
@ -0,0 +1,2 @@
|
||||
#define CLAY_IMPLEMENTATION
|
||||
#include "../../clay.h"
|
88
bindings/D/main.d
Normal file
88
bindings/D/main.d
Normal file
@ -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;
|
||||
}
|
2
bindings/zig/README
Normal file
2
bindings/zig/README
Normal file
@ -0,0 +1,2 @@
|
||||
https://codeberg.org/Zettexe/clay-zig
|
||||
https://github.com/johan0A/clay-zig-bindings
|
@ -23,6 +23,15 @@ FetchContent_Declare(
|
||||
)
|
||||
FetchContent_MakeAvailable(SDL2_ttf)
|
||||
|
||||
FetchContent_Declare(
|
||||
SDL2_image
|
||||
GIT_REPOSITORY "https://github.com/libsdl-org/SDL_image.git"
|
||||
GIT_TAG "release-2.8.4"
|
||||
GIT_PROGRESS TRUE
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(SDL2_image)
|
||||
|
||||
add_executable(SDL2_video_demo main.c)
|
||||
|
||||
target_compile_options(SDL2_video_demo PUBLIC)
|
||||
@ -32,6 +41,7 @@ target_link_libraries(SDL2_video_demo PUBLIC
|
||||
SDL2::SDL2main
|
||||
SDL2::SDL2-static
|
||||
SDL2_ttf::SDL2_ttf-static
|
||||
SDL2_image::SDL2_image-static
|
||||
)
|
||||
|
||||
if(MSVC)
|
||||
|
@ -13,6 +13,8 @@
|
||||
const int FONT_ID_BODY_16 = 0;
|
||||
Clay_Color COLOR_WHITE = { 255, 255, 255, 255};
|
||||
|
||||
SDL_Surface *sample_image;
|
||||
|
||||
void RenderHeaderButton(Clay_String text) {
|
||||
CLAY(
|
||||
CLAY_LAYOUT({ .padding = { 16, 16, 8, 8 }}),
|
||||
@ -111,9 +113,18 @@ static Clay_RenderCommandArray CreateLayout() {
|
||||
})
|
||||
) {
|
||||
// Header buttons go here
|
||||
CLAY(
|
||||
CLAY_LAYOUT({ .padding = { 16, 16, 8, 8 }}),
|
||||
CLAY_BORDER_ALL({ 2, COLOR_WHITE })
|
||||
) {
|
||||
CLAY(
|
||||
CLAY_LAYOUT({ .padding = { 8, 8, 8, 8 }}),
|
||||
CLAY_IMAGE({ sample_image, { 23, 42 } })
|
||||
) {}
|
||||
}
|
||||
CLAY(
|
||||
CLAY_ID("FileButton"),
|
||||
CLAY_LAYOUT({ .padding = { 16, 8 }}),
|
||||
CLAY_LAYOUT({ .padding = { 16, 16, 8, 8 }}),
|
||||
CLAY_RECTANGLE({
|
||||
.color = { 140, 140, 140, 255 },
|
||||
.cornerRadius = 5
|
||||
@ -278,6 +289,10 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr, "Error: could not initialize TTF: %s\n", TTF_GetError());
|
||||
return 1;
|
||||
}
|
||||
if (IMG_Init(IMG_INIT_PNG) < 0) {
|
||||
fprintf(stderr, "Error: could not initialize IMG: %s\n", IMG_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
TTF_Font *font = TTF_OpenFont("resources/Roboto-Regular.ttf", 16);
|
||||
if (!font) {
|
||||
@ -289,6 +304,8 @@ int main(int argc, char *argv[]) {
|
||||
.font = font,
|
||||
};
|
||||
|
||||
sample_image = IMG_Load("resources/sample.png");
|
||||
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
if (SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_RESIZABLE, &window, &renderer) < 0) {
|
||||
@ -352,6 +369,7 @@ int main(int argc, char *argv[]) {
|
||||
quit:
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
IMG_Quit();
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
|
BIN
examples/SDL2-video-demo/resources/sample.png
Normal file
BIN
examples/SDL2-video-demo/resources/sample.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 850 B |
@ -1,7 +1,5 @@
|
||||
Please note, the SDL2 renderer is not 100% feature complete. It is currently missing:
|
||||
|
||||
- Border rendering
|
||||
- Image rendering
|
||||
- Rounded rectangle corners
|
||||
|
||||
Note: on Mac OSX, SDL2 for some reason decides to automatically disable momentum scrolling on macbook trackpads.
|
||||
@ -10,4 +8,4 @@ You can re enable it in objective C using:
|
||||
```C
|
||||
[[NSUserDefaults standardUserDefaults] setBool: YES
|
||||
forKey: @"AppleMomentumScrollSupported"];
|
||||
```
|
||||
```
|
||||
|
@ -1,8 +1,11 @@
|
||||
#include "../../clay.h"
|
||||
#include <SDL.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <SDL_image.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define CLAY_COLOR_TO_SDL_COLOR_ARGS(color) color.r, color.g, color.b, color.a
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t fontId;
|
||||
@ -93,10 +96,55 @@ static void Clay_SDL2_Render(SDL_Renderer *renderer, Clay_RenderCommandArray ren
|
||||
SDL_RenderSetClipRect(renderer, NULL);
|
||||
break;
|
||||
}
|
||||
case CLAY_RENDER_COMMAND_TYPE_IMAGE: {
|
||||
SDL_Surface *image = (SDL_Surface *)renderCommand->config.imageElementConfig->imageData;
|
||||
|
||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
|
||||
|
||||
SDL_Rect destination = (SDL_Rect){
|
||||
.x = boundingBox.x,
|
||||
.y = boundingBox.y,
|
||||
.w = boundingBox.width,
|
||||
.h = boundingBox.height,
|
||||
};
|
||||
|
||||
SDL_RenderCopy(renderer, texture, NULL, &destination);
|
||||
break;
|
||||
}
|
||||
case CLAY_RENDER_COMMAND_TYPE_BORDER: {
|
||||
Clay_BorderElementConfig *config = renderCommand->config.borderElementConfig;
|
||||
|
||||
if (config->left.width > 0) {
|
||||
SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->left.color));
|
||||
SDL_RenderFillRectF(renderer, &(SDL_FRect){ boundingBox.x, boundingBox.y + config->cornerRadius.topLeft, config->left.width, boundingBox.height - config->cornerRadius.topLeft - config->cornerRadius.bottomLeft });
|
||||
}
|
||||
|
||||
if (config->right.width > 0) {
|
||||
SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->right.color));
|
||||
SDL_RenderFillRectF(renderer, &(SDL_FRect){ boundingBox.x + boundingBox.width - config->right.width, boundingBox.y + config->cornerRadius.topRight, config->right.width, boundingBox.height - config->cornerRadius.topRight - config->cornerRadius.bottomRight });
|
||||
}
|
||||
|
||||
if (config->right.width > 0) {
|
||||
SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->right.color));
|
||||
SDL_RenderFillRectF(renderer, &(SDL_FRect){ boundingBox.x + boundingBox.width - config->right.width, boundingBox.y + config->cornerRadius.topRight, config->right.width, boundingBox.height - config->cornerRadius.topRight - config->cornerRadius.bottomRight });
|
||||
}
|
||||
|
||||
if (config->top.width > 0) {
|
||||
SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->right.color));
|
||||
SDL_RenderFillRectF(renderer, &(SDL_FRect){ boundingBox.x + config->cornerRadius.topLeft, boundingBox.y, boundingBox.width - config->cornerRadius.topLeft - config->cornerRadius.topRight, config->top.width });
|
||||
}
|
||||
|
||||
if (config->bottom.width > 0) {
|
||||
SDL_SetRenderDrawColor(renderer, CLAY_COLOR_TO_SDL_COLOR_ARGS(config->bottom.color));
|
||||
SDL_RenderFillRectF(renderer, &(SDL_FRect){ boundingBox.x + config->cornerRadius.bottomLeft, boundingBox.y + boundingBox.height - config->bottom.width, boundingBox.width - config->cornerRadius.bottomLeft - config->cornerRadius.bottomRight, config->bottom.width });
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
fprintf(stderr, "Error: unhandled render command: %d\n", renderCommand->commandType);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user