Skip to content

Commit

Permalink
fmt and betterC compat
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Dec 19, 2023
1 parent 01019c5 commit f2e42dd
Show file tree
Hide file tree
Showing 15 changed files with 2,227 additions and 1,094 deletions.
5 changes: 4 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ pub fn build(b: *Builder) !void {
config.force_egl = b.option(bool, "egl", "Use EGL instead of GLX if possible (default: false)") orelse false;

var target = b.standardTargetOptions(.{});
if (target.isWindows())

// ldc2 w/ druntime + phobos2 works on MSVC
if (target.isWindows() and target.isNative())
target.abi = .msvc;

const optimize = b.standardOptimizeOption(.{});
Expand Down Expand Up @@ -217,6 +219,7 @@ pub fn build(b: *Builder) !void {
// handmade math
fmt.comptimePrint("{s}/src/examples/math.d", .{rootPath()}),
},
// .betterC = .on,
.dflags = &.{
"--wi", // warnings only (no error)
// "-w", // warnings as error
Expand Down
22 changes: 15 additions & 7 deletions src/examples/clear.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
//------------------------------------------------------------------------------
module examples.clear;

import std.stdio;
import sg = sokol.gfx;
import sgapp = sokol.glue;
import sapp = sokol.app;
import log = sokol.log;

static sg.PassAction pass_action;

void init()
extern (C) void init()
{
sg.Desc cd;
cd.context = sgapp.context();
Expand All @@ -26,10 +25,19 @@ void init()
pass_action.colors[0].clear_value.b = 0;
pass_action.colors[0].clear_value.a = 1;

writeln("Backend: ", sg.queryBackend());
version (D_BetterC)
{
import core.stdc.stdio: printf;
printf("Backend: %d\n", sg.queryBackend());
}
else
{
import std.stdio;
writeln("Backend: ", sg.queryBackend());
}
}

void frame()
extern (C) void frame()
{
auto g = pass_action.colors[0].clear_value.g + 0.01;
pass_action.colors[0].clear_value.g = g > 1.0 ? 0.0 : g;
Expand All @@ -38,12 +46,12 @@ void frame()
sg.endPass();
}

void cleanup()
extern (C) void cleanup()
{
sg.shutdown();
}

void main()
extern(C) void main()
{
sapp.IconDesc icon = {sokol_default: true};
sapp.Desc runner = {
Expand All @@ -53,7 +61,7 @@ void main()
cleanup_cb: &cleanup,
width: 640,
height: 480,
win32_console_attach: true
win32_console_attach: true,
};
runner.icon = icon;
runner.logger.func = &log.func;
Expand Down
22 changes: 9 additions & 13 deletions src/examples/cube.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ struct State

static Mat4 view()
{
return Mat4.lookAt(
Vec3(0.0f, 1.5f, 6.0f),
Vec3.zero(),
Vec3.up()
);
return Mat4.lookAt(Vec3(0.0f, 1.5f, 6.0f), Vec3.zero(), Vec3.up());
}
}

Expand Down Expand Up @@ -102,15 +98,15 @@ State state;
// sg.commit();
// }

// void cleanup()
// {
// sg.shutdown();
// }
extern (C) void cleanup()
{
sg.shutdown();
}

// void main()
// {
// // TODO
// }
void main()
{
// TODO
}

// TODO: missing cube.glsl
// VsParams computeVsParams(float rx, float ry)
Expand Down
138 changes: 78 additions & 60 deletions src/examples/debugtext_print.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,119 @@
//------------------------------------------------------------------------------
module examples.debugtext_print;

import std.stdio;
import stm = sokol.time;
import sg = sokol.gfx;
import sgapp = sokol.glue;
import sapp = sokol.app;
import sdtx = sokol.debugtext;
import log = sokol.log;

// Font slots
enum KC854 = 0;
enum C64 = 1;
enum ORIC = 2;

extern(C)
struct Color
{
ubyte r, g, b;
@disable this();
this(ubyte r, ubyte g, ubyte b){
this.r = r;
this.g = g;
this.b = b;
}
ubyte r, g, b;
}

extern(C)
struct State
{
sg.PassAction passAction;
uint frameCount;
ulong timeStamp;

immutable Color[3] colors = [
Color(0xF4, 0x43, 0x36),
Color(0x21, 0x96, 0xF3),
Color(0x4C, 0xAF, 0x50)
];
@disable this();
sg.PassAction passAction;
uint frameCount;
ulong timeStamp;

immutable Color[3] colors = [
Color(0xF4, 0x43, 0x36),
Color(0x21, 0x96, 0xF3),
Color(0x4C, 0xAF, 0x50)
];
}

static State state;
__gshared State state = {};

void init()
extern (C) void init()
{
stm.setup();
sg.Desc cd;
cd.context = sgapp.context();
sg.setup(cd);

sdtx.Desc desc;
desc.fonts[0] = sdtx.fontKc854();
desc.fonts[1] = sdtx.fontC64();
desc.fonts[3] = sdtx.fontOric();
sdtx.setup(desc);
stm.setup();
sg.Desc cd;
cd.context = sgapp.context();
cd.logger.func = &log.func;
sg.setup(cd);

sdtx.Desc desc;
desc.logger.func = &log.func;
desc.fonts[0] = sdtx.fontKc854();
desc.fonts[1] = sdtx.fontC64();
desc.fonts[3] = sdtx.fontOric();
sdtx.setup(desc);

state.passAction.colors[0].load_action = sg.LoadAction.CLEAR;
state.passAction.colors[0].clear_value.r = 0;
state.passAction.colors[0].clear_value.g = 0.125;
state.passAction.colors[0].clear_value.b = 0.25;
state.passAction.colors[0].clear_value.a = 1;
}

void frame()
extern (C) void frame()
{
state.frameCount++;
state.frameCount++;

auto frameTime = stm.ms(stm.laptime(&state.timeStamp));
const frameTime = stm.ms(stm.laptime(&state.timeStamp));

sdtx.canvas(sapp.widthf() / 2, sapp.heightf() / 2);
sdtx.origin(3, 3);
sdtx.canvas(sapp.widthf() / 2, sapp.heightf() / 2);
sdtx.origin(3, 3);

foreach (font; [KC854, C64, ORIC])
{
auto color = state.colors[font];
sdtx.font(font);
sdtx.color3b(color.r, color.g, color.b);
foreach (font; [KC854, C64, ORIC])
{
const color = state.colors[font];
sdtx.font(font);
sdtx.color3b(color.r, color.g, color.b);

auto worldStr = (state.frameCount & (1 << 7)) ? "Welt" : "World";
const worldStr = (state.frameCount & (1 << 7)) ? "Welt" : "World";

writeln("Hello ", worldStr," !");
writeln("\tFrame Time:\t\t", frameTime,"ms");
writef("\tFrame Count:\t%d\t%0.4f\n", state.frameCount, state.frameCount);
sdtx.moveY(2);
}
sdtx.print("Hello '%s'!\n", worldStr);
sdtx.print("\tFrame Time:\t\t %0.3f ms", frameTime);
sdtx.print("\tFrame Count:\t%d\t%x\n", state.frameCount, state.frameCount);
sdtx.moveY(2);
}

sdtx.font(KC854);
sdtx.color3b(255, 128, 0);
sdtx.font(KC854);
sdtx.color3b(255, 128, 0);

// writeln("using std.format directly ", state.frameCount);

sg.beginDefaultPass(state.passAction, sapp.width, sapp.height);
sdtx.draw();
sg.endPass();
sg.commit();
sg.beginDefaultPass(state.passAction, sapp.width, sapp.height);
sdtx.draw();
sg.endPass();
sg.commit();
}

void cleanup()
extern (C) void cleanup()
{
sdtx.shutdown();
sg.shutdown();
sdtx.shutdown();
sg.shutdown();
}

extern(C) void main()
void main()
{
sapp.IconDesc icon = {sokol_default: true};
sapp.Desc runner = {window_title: "debugtext_print.d"};
runner.init_cb = &init;
runner.frame_cb = &frame;
runner.cleanup_cb = &cleanup;
runner.width = 640;
runner.height = 480;
runner.icon = icon;
sapp.run(runner);
sapp.IconDesc icon = {sokol_default: true};
sapp.Desc runner = {
window_title: "debugtext_print.d",
init_cb: &init,
frame_cb: &frame,
cleanup_cb: &cleanup,
width: 640,
height: 480
};
runner.icon = icon;
runner.logger.func = &log.func;
sapp.run(runner);
}
29 changes: 7 additions & 22 deletions src/examples/math.d
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@ struct Vec3

static Vec3 cross(Vec3 v0, Vec3 v1)
{
return Vec3(
(v0.y * v1.z) - (v0.z * v1.y),
(v0.z * v1.x) - (v0.x * v1.z),
(v0.x * v1.y) - (v0.y * v1.x)
);
return Vec3((v0.y * v1.z) - (v0.z * v1.y), (v0.z * v1.x) - (v0.x * v1.z),
(v0.x * v1.y) - (v0.y * v1.x));
}

static float dot(Vec3 v0, Vec3 v1)
Expand All @@ -101,22 +98,12 @@ struct Mat4

static Mat4 identity()
{
return Mat4([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]);
return Mat4([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]);
}

static Mat4 zero()
{
return Mat4([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]);
return Mat4([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]);
}

static Mat4 mul(Mat4 left, Mat4 right)
Expand All @@ -127,11 +114,9 @@ struct Mat4
{
foreach (col; 0 .. 4)
{
result.m[row][col] =
left.m[row][0] * right.m[0][col] +
left.m[row][1] * right.m[1][col] +
left.m[row][2] * right.m[2][col] +
left.m[row][3] * right.m[3][col];
result.m[row][col] = left.m[row][0] * right.m[0][col]
+ left.m[row][1] * right.m[1][col] + left.m[row][2]
* right.m[2][col] + left.m[row][3] * right.m[3][col];
}
}

Expand Down
Loading

0 comments on commit f2e42dd

Please sign in to comment.