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

Added GPUTextEngine #412

Merged
merged 29 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b54f04a
TTF_Draw* functions should return true on success
captain0xff Oct 1, 2024
1621a10
updated .gitignore for clangd
captain0xff Oct 1, 2024
eac1af1
added gputextengine api and example (WIP)
captain0xff Oct 17, 2024
9f80731
Merge remote-tracking branch 'origin/gpu-text-engine' into gpu-text-e…
captain0xff Oct 17, 2024
eac37e1
updated for the latest SDL_ttf changes
captain0xff Oct 17, 2024
90126e2
updated the docs
captain0xff Oct 19, 2024
e5432dc
replaced the math_3d.h with my own custom implementation
captain0xff Oct 20, 2024
32a2e01
added a script to build and load the shaders
captain0xff Oct 22, 2024
8d70098
use SDL_PI_F instead of defining M_PI
captain0xff Oct 22, 2024
6461d91
no need to link libm anymore
captain0xff Oct 22, 2024
4b09066
added perspective and look_at functions
captain0xff Oct 24, 2024
4c8e006
improved the example to look more 3D
captain0xff Oct 24, 2024
4445dbf
added one pixel gap between the glyphs
captain0xff Oct 24, 2024
1bdbb32
moved around some stuff and updated the example
captain0xff Oct 24, 2024
ed26252
added font file and license
captain0xff Oct 25, 2024
f993143
formatted the example
captain0xff Oct 25, 2024
d80f917
Merge branch 'libsdl-org:main' into gpu-text-engine
captain0xff Oct 25, 2024
0267ea1
updated for the latest SDL_ttf API changes
captain0xff Oct 25, 2024
e1bad6c
fix the CI
captain0xff Oct 25, 2024
30e0cb2
more CI fixes
captain0xff Oct 25, 2024
e9dc9b3
add braces around ifs
captain0xff Oct 25, 2024
2976b86
added dx12 and metal shaders
captain0xff Oct 27, 2024
7741273
Add d3d11 shaders
madebr Oct 28, 2024
2b16b72
add shader support for the other backends
captain0xff Oct 30, 2024
b90f8e3
destroy the created text
captain0xff Oct 30, 2024
e25680e
remove the spirv binaries
captain0xff Oct 30, 2024
aec5435
check for issues after doing shady stuff
captain0xff Oct 30, 2024
b585c91
fixed minor issues in the example
captain0xff Nov 9, 2024
2c45250
more fixes
captain0xff Nov 9, 2024
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
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ endif()

add_library(${sdl3_ttf_target_name}
src/SDL_hashtable.c
src/SDL_gpu_textengine.c
src/SDL_renderer_textengine.c
src/SDL_surface_textengine.c
src/SDL_ttf.c
Expand Down Expand Up @@ -364,6 +365,7 @@ if(SDLTTF_SAMPLES)
add_executable(glfont examples/glfont.c)
add_executable(showfont examples/showfont.c examples/editbox.c)
add_executable(testapp examples/testapp.c)
add_executable(testgputext examples/testgputext.c)

set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL)
Expand All @@ -375,10 +377,10 @@ if(SDLTTF_SAMPLES)
target_link_libraries(glfont PRIVATE OpenGL::GL)
endif()

foreach(prog glfont showfont testapp)
foreach(prog glfont showfont testapp testgputext)
sdl_add_warning_options(${prog} WARNING_AS_ERROR ${SDLTTF_WERROR})
target_link_libraries(${prog} PRIVATE SDL3_ttf::${sdl3_ttf_target_name})
target_link_libraries(${prog} PRIVATE ${sdl3_target_name})
target_link_libraries(${prog} PRIVATE SDL3_ttf::${sdl3_ttf_target_name} m)
target_link_libraries(${prog} PRIVATE ${sdl3_target_name} m)

if(SDLTTF_SAMPLES_INSTALL)
install(TARGETS ${prog}
Expand Down
261 changes: 261 additions & 0 deletions examples/SDL_math3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/**
* Define SDL_MATH_3D_IMPLEMENTATION before including this header in any one source (.c) files.
* Angles are in radians unless specified otherwise.
**/

#ifndef SDL_MATH_3D_H
#define SDL_MATH_3D_H

#include <SDL3/SDL_stdinc.h>

// Set up for C function definitions, even when using C++
#ifdef __cplusplus
extern "C" {
#endif

#ifndef M_PI
#define M_PI 3.14159265358979323846
captain0xff marked this conversation as resolved.
Show resolved Hide resolved
#endif

typedef struct
{
float x;
float y;
} SDL_Vec2;

typedef struct
{
float x;
float y;
float z;
} SDL_Vec3;

typedef struct
{
float x;
float y;
float z;
float w;
} SDL_Vec4;

/**
* The matrix is stored in column major format
**/
typedef struct
{
union
{
struct
{
float m00, m01, m02, m03;
float m10, m11, m12, m13;
float m20, m21, m22, m23;
float m30, m31, m32, m33;
} v;
float m[4][4];
};
} SDL_Mat4X4;


// SDL Vector functions
static inline SDL_Vec3 SDL_Vector3(float x, float y, float z);
static inline float SDL_Vec3Magnitude(SDL_Vec3 vec);
static inline SDL_Vec3 SDL_Vec3Normalize(SDL_Vec3 vec);
static inline SDL_Vec3 SDL_Vec3Add(SDL_Vec3 vec1, SDL_Vec3 vec2);
static inline SDL_Vec3 SDL_Vec3Sub(SDL_Vec3 vec1, SDL_Vec3 vec2);
static inline SDL_Vec3 SDL_Vec3MultiplyFloat(SDL_Vec3 vec, float val);
static inline float SDL_Vec3Dot(SDL_Vec3 vec1, SDL_Vec3 vec2);
static inline SDL_Vec3 SDL_Vec3Cross(SDL_Vec3 vec1, SDL_Vec3 vec2);

// SDL Matrix functions
static inline SDL_Mat4X4 SDL_Matrix4X4(
float m00, float m10, float m20, float m30,
float m01, float m11, float m21, float m31,
float m02, float m12, float m22, float m32,
float m03, float m13, float m23, float m33
);
static inline SDL_Mat4X4 SDL_MatrixIdentity(void);
static inline SDL_Mat4X4 SDL_MatrixMultiply(SDL_Mat4X4 M1, SDL_Mat4X4 M2);
static inline SDL_Mat4X4 SDL_MatrixScaling(SDL_Vec3 scale);
static inline SDL_Mat4X4 SDL_MatrixTranslation(SDL_Vec3 offset);
static inline SDL_Mat4X4 SDL_MatrixRotationX(float angle);
static inline SDL_Mat4X4 SDL_MatrixRotationY(float angle);
static inline SDL_Mat4X4 SDL_MatrixRotationZ(float angle);

static inline SDL_Mat4X4 SDL_MatrixOrtho(float left, float right, float bottom, float top, float back, float front);


// Ends C function definitions when using C++
#ifdef __cplusplus
}
#endif
#endif /* SDL_MATH_3D_H */


#ifdef SDL_MATH_3D_IMPLEMENTATION

static inline SDL_Vec3 SDL_Vector3(float x, float y, float z) {
return (SDL_Vec3) {.x = x, .y = y, .z = z};
}

static inline float SDL_Vec3Magnitude(SDL_Vec3 vec)
{
return SDL_sqrtf(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
}


static inline SDL_Vec3 SDL_Vec3Normalize(SDL_Vec3 vec)
{
float mag = SDL_Vec3Magnitude(vec);

if (mag == 0) {
return (SDL_Vec3) {0, 0, 0};
} else {
return (SDL_Vec3) {vec.x/mag, vec.y/mag, vec.z/mag};
}
}

static inline SDL_Vec3 SDL_Vec3Add(SDL_Vec3 vec1, SDL_Vec3 vec2)
{
return SDL_Vector3(vec1.x + vec2.x, vec1.y + vec2.y, vec1.z + vec2.z);
}

static inline SDL_Vec3 SDL_Vec3Sub(SDL_Vec3 vec1, SDL_Vec3 vec2)
{
return SDL_Vector3(vec1.x - vec2.x, vec1.y - vec2.y, vec1.z - vec2.z);
}

static inline SDL_Vec3 SDL_Vec3MultiplyFloat(SDL_Vec3 vec, float val)
{
return SDL_Vector3(vec.x * val, vec.y * val, vec.z * val);
}

static inline float SDL_Vec3Dot(SDL_Vec3 vec1, SDL_Vec3 vec2)
{
return (vec1.x * vec2.x + vec1.y * vec2.y + vec1.y * vec2.y);
}

static inline SDL_Vec3 SDL_Vec3Cross(SDL_Vec3 vec1, SDL_Vec3 vec2) {
return SDL_Vector3(
vec1.y * vec2.z - vec1.z * vec2.y,
vec1.z * vec2.x - vec1.x * vec2.z,
vec1.x * vec2.y - vec1.y * vec2.x
);
}


static inline SDL_Mat4X4 SDL_Matrix4X4(
float m00, float m10, float m20, float m30,
float m01, float m11, float m21, float m31,
float m02, float m12, float m22, float m32,
float m03, float m13, float m23, float m33
) {
return (SDL_Mat4X4) {
.m[0][0] = m00, .m[1][0] = m10, .m[2][0] = m20, .m[3][0] = m30,
.m[0][1] = m01, .m[1][1] = m11, .m[2][1] = m21, .m[3][1] = m31,
.m[0][2] = m02, .m[1][2] = m12, .m[2][2] = m22, .m[3][2] = m32,
.m[0][3] = m03, .m[1][3] = m13, .m[2][3] = m23, .m[3][3] = m33
};
}

static inline SDL_Mat4X4 SDL_MatrixIdentity(void)
{
return SDL_Matrix4X4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}

static inline SDL_Mat4X4 SDL_MatrixMultiply(SDL_Mat4X4 M1, SDL_Mat4X4 M2)
{
SDL_Mat4X4 res;

for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
float sum = 0;
for (int x = 0; x < 4; x++) {
sum += M1.m[x][j] * M2.m[i][x];
}
res.m[i][j] = sum;
}
}

return res;
}

static inline SDL_Mat4X4 SDL_MatrixScaling(SDL_Vec3 scale)
{
float x = scale.x, y = scale.y, z = scale.z;
return SDL_Matrix4X4(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1
);
}

static inline SDL_Mat4X4 SDL_MatrixTranslation(SDL_Vec3 offset)
{
return SDL_Matrix4X4(
1, 0, 0, offset.x,
0, 1, 0, offset.y,
0, 0, 1, offset.z,
0, 0, 0, 1
);
}

static inline SDL_Mat4X4 SDL_MatrixRotationX(float angle)
{
float cos = SDL_cos(angle);
float sin = SDL_sin(angle);

return SDL_Matrix4X4(
cos, 0, sin, 0,
0, 1, 0, 0,
-sin, 0, cos, 0,
0, 0, 0, 1
);
}

static inline SDL_Mat4X4 SDL_MatrixRotationY(float angle)
{
float cos = SDL_cos(angle);
float sin = SDL_sin(angle);

return SDL_Matrix4X4(
cos, 0, sin, 0,
0, 1, 0, 0,
-sin, 0, cos, 0,
0, 0, 0, 1
);
}

static inline SDL_Mat4X4 SDL_MatrixRotationZ(float angle)
{
float cos = SDL_cos(angle);
float sin = SDL_sin(angle);

return SDL_Matrix4X4(
cos, -sin, 0, 0,
sin, cos, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}

static inline SDL_Mat4X4 SDL_MatrixOrtho(float left, float right, float bottom, float top, float near, float far) {
float dx = -(right + left) / (right - left);
float dy = -(top + bottom) / (top - bottom);
float dz = -(far + near) / (far - near);

return SDL_Matrix4X4(
2 / (right - left), 0, 0, dx,
0, 2 / (top - bottom), 0, dy,
0, 0, 2 / (far - near), dz,
0, 0, 0, 1
);
}

#endif /* SDL_MATH_3D_IMPLEMENTATION */
Loading