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

Extract Clay Renderer API into a separate header file #65

Open
wants to merge 7 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
10 changes: 5 additions & 5 deletions examples/cairo-pdf-rendering/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <stdlib.h>

// The renderer includes clay.h while also providing the
// CLAY_IMPLEMENTATION
#include "../../renderers/cairo/clay_renderer_cairo.c"
#define CLAY_IMPLEMENTATION
#include "../../renderers/clay_renderer.h"

// cairo-pdf, though this is optional and not required if you,
// e.g. render PNGs.
Expand Down Expand Up @@ -128,11 +128,11 @@ int main(void) {
// We require some kind of global reference to a valid
// cairo instance to properly measure text.
// Note that due to this, this interface is not thread-safe!
Clay_Cairo_Initialize(cr);
Clay_Renderer_Initialize((struct Clay_Renderer_Data *)cr);

uint64_t totalMemorySize = Clay_MinMemorySize();
Clay_Arena clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize };
Clay_SetMeasureTextFunction(Clay_Cairo_MeasureText);
Clay_SetMeasureTextFunction(Clay_Renderer_MeasureText);

// We initialize Clay with the same size
Clay_Initialize(clayMemory, (Clay_Dimensions) { width, height });
Expand All @@ -145,7 +145,7 @@ int main(void) {

Clay_RenderCommandArray commands = Clay_EndLayout();
// Pass our layout to the cairo backend
Clay_Cairo_Render(commands);
Clay_Renderer_Render(commands);

// To keep this example short, we will not emit a second page in the PDF.
// But to do so, you have to
Expand Down
22 changes: 18 additions & 4 deletions examples/raylib-sidebar-scrolling-container/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#define CLAY_IMPLEMENTATION
#include "../../clay.h"
#include "../../renderers/raylib/clay_renderer_raylib.c"
#include <stdio.h>
#include <stdlib.h>
#include "../../renderers/raylib/raylib.h"
#include "../../renderers/clay_renderer.h"

const uint32_t FONT_ID_BODY_24 = 0;
const uint32_t FONT_ID_BODY_16 = 1;
Expand Down Expand Up @@ -195,7 +198,7 @@ void UpdateDrawFrame(void)
// currentTime = GetTime();
BeginDrawing();
ClearBackground(BLACK);
Clay_Raylib_Render(renderCommands);
Clay_Renderer_Render(renderCommands);
EndDrawing();
// printf("render time: %f ms\n", (GetTime() - currentTime) * 1000);

Expand All @@ -205,9 +208,20 @@ void UpdateDrawFrame(void)
int main(void) {
uint64_t totalMemorySize = Clay_MinMemorySize();
Clay_Arena clayMemory = (Clay_Arena) { .label = CLAY_STRING("Clay Memory Arena"), .memory = malloc(totalMemorySize), .capacity = totalMemorySize };
Clay_SetMeasureTextFunction(Raylib_MeasureText);
Clay_SetMeasureTextFunction(Clay_Renderer_MeasureText);
Clay_Initialize(clayMemory, (Clay_Dimensions) { (float)GetScreenWidth(), (float)GetScreenHeight() });
Clay_Raylib_Initialize(1024, 768, "Clay - Raylib Renderer Example", FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT);

Clay_Renderer_Initialize((struct Clay_Renderer_Data)&(struct {
int width;
int height;
const char *title;
int flags;
}){
.width = 1024,
.height = 768,
.title = "Clay - Raylib Renderer Example",
.flags = FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI | FLAG_MSAA_4X_HINT
});
profilePicture = LoadTextureFromImage(LoadImage("resources/profile-picture.png"));
Raylib_fonts[FONT_ID_BODY_24] = (Raylib_Font) {
.font = LoadFontEx("resources/Roboto-Regular.ttf", 48, 0, 400),
Expand Down
23 changes: 12 additions & 11 deletions renderers/cairo/clay_renderer_cairo.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@

// TODO: We should use the given `uint16_t fontId` instead of doing this.
#define CLAY_EXTEND_CONFIG_TEXT Clay_String fontFamily; // Font family
#define CLAY_IMPLEMENTATION
#include "../../clay.h"
#include "../clay_renderer.h"

#include <cairo/cairo.h>

typedef struct caito_t Clay_Renderer_Data;

////////////////////////////////
//
// Public API
// Public API (defined in clay_renderer.h)
//

// Initialize the internal cairo pointer with the user provided instance.
// This is REQUIRED before calling Clay_Cairo_Render.
void Clay_Cairo_Initialize(cairo_t *cairo);
// This is REQUIRED before calling Clay_Renderer_Render.
// void Clay_Renderer_Initialize(struct Clay_Renderer_Data *cairo);

// Render the command queue to the `cairo_t*` instance you called
// `Clay_Cairo_Initialize` on.
void Clay_Cairo_Render(Clay_RenderCommandArray commands);
// `Clay_Renderer_Initialize` on.
// void Clay_Renderer_Render(Clay_RenderCommandArray commands);
////////////////////////////////


Expand Down Expand Up @@ -83,7 +84,7 @@ static inline char *Clay_Cairo__NullTerminate(Clay_String *str) {
}

// Measure text using cairo's *toy* text API.
static inline Clay_Dimensions Clay_Cairo_MeasureText(Clay_String *str, Clay_TextElementConfig *config) {
Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *str, Clay_TextElementConfig *config) {
// Edge case: Clay computes the width of a whitespace character
// once. Cairo does not factor in whitespaces when computing text
// extents, this edge-case serves as a short-circuit to introduce
Expand Down Expand Up @@ -159,8 +160,8 @@ static inline Clay_Dimensions Clay_Cairo_MeasureText(Clay_String *str, Clay_Text
}


void Clay_Cairo_Initialize(cairo_t *cairo) {
Clay__Cairo = cairo;
void Clay_Renderer_Initialize(struct Clay_Renderer_Data *cairo) {
Clay__Cairo = (cairo_t *)cairo;
}

// Internally used to copy images onto our document/active workspace.
Expand Down Expand Up @@ -191,7 +192,7 @@ void Clay_Cairo__Blit_Surface(cairo_surface_t *src_surface, cairo_surface_t *des
cairo_destroy(cr);
}

void Clay_Cairo_Render(Clay_RenderCommandArray commands) {
void Clay_Renderer_Render(Clay_RenderCommandArray commands) {
cairo_t *cr = Clay__Cairo;
for(size_t i = 0; i < commands.length; i++) {
Clay_RenderCommand *command = Clay_RenderCommandArray_Get(&commands, i);
Expand Down
8 changes: 8 additions & 0 deletions renderers/clay_renderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "../clay.h"

struct Clay_Renderer_Data;
void Clay_Renderer_Initialize(struct Clay_Renderer_Data *data);
void Clay_Renderer_Render(Clay_RenderCommandArray renderCommands);
Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *text, Clay_TextElementConfig *config);
39 changes: 30 additions & 9 deletions renderers/raylib/clay_renderer_raylib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "../clay_renderer.h"
#include "raylib.h"
#include "raymath.h"
#include "stdint.h"
Expand All @@ -8,8 +9,21 @@
#include "signal.h"
#endif

#define CLAY_RECTANGLE_TO_RAYLIB_RECTANGLE(rectangle) (Rectangle) { .x = rectangle.x, .y = rectangle.y, .width = rectangle.width, .height = rectangle.height }
#define CLAY_COLOR_TO_RAYLIB_COLOR(color) (Color) { .r = (unsigned char)roundf(color.r), .g = (unsigned char)roundf(color.g), .b = (unsigned char)roundf(color.b), .a = (unsigned char)roundf(color.a) }
#define CLAY_RECTANGLE_TO_RAYLIB_RECTANGLE(rectangle) \
(Rectangle) { \
.x = rectangle.x, \
.y = rectangle.y, \
.width = rectangle.width, \
.height = rectangle.height \
}

#define CLAY_COLOR_TO_RAYLIB_COLOR(color) \
(Color) { \
.r = (unsigned char)roundf(color.r), \
.g = (unsigned char)roundf(color.g), \
.b = (unsigned char)roundf(color.b), \
.a = (unsigned char)roundf(color.a) \
}

typedef struct
{
Expand Down Expand Up @@ -42,7 +56,7 @@ typedef struct
} CustomLayoutElement;

// Get a ray trace from the screen position (i.e mouse) within a specific section of the screen
Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int screenWidth, int screenHeight, float zDistance)
static Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int screenWidth, int screenHeight, float zDistance)
{
Ray ray = { 0 };

Expand Down Expand Up @@ -92,7 +106,7 @@ Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int scre

uint32_t measureCalls = 0;

static inline Clay_Dimensions Raylib_MeasureText(Clay_String *text, Clay_TextElementConfig *config) {
inline Clay_Dimensions Clay_Renderer_MeasureText(Clay_String *text, Clay_TextElementConfig *config) {
measureCalls++;
// Measure string size for Font
Clay_Dimensions textSize = { 0 };
Expand Down Expand Up @@ -124,13 +138,20 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_String *text, Clay_TextEle
return textSize;
}

void Clay_Raylib_Initialize(int width, int height, const char *title, unsigned int flags) {
SetConfigFlags(flags);
InitWindow(width, height, title);
// EnableEventWaiting();
typedef struct Clay_Renderer_Data {
int width;
int height;
const char *title;
unsigned int flags;
} Clay_Raylib_Data;

void Clay_Renderer_Initialize(struct Clay_Renderer_Data *data) {
SetConfigFlags(data->flags);
InitWindow(data->width, data->height, data->title);
// EnableEventWaiting();
}

void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands)
void Clay_Renderer_Render(Clay_RenderCommandArray renderCommands)
{
measureCalls = 0;
for (int j = 0; j < renderCommands.length; j++)
Expand Down
Loading