Skip to content
Merged
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
16 changes: 0 additions & 16 deletions data/font.fs

This file was deleted.

21 changes: 0 additions & 21 deletions data/font.vs

This file was deleted.

7 changes: 6 additions & 1 deletion src/Box2D.NET.Samples/Box2D.NET.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="\"/>
<!-- Embed shaders so the samples binary has no runtime dependency on data/*.vs and *.fs. -->
<EmbeddedResource Include="../../data/*.vs" LogicalName="Box2D.NET.Samples.Shaders.%(Filename)%(Extension)" />
<EmbeddedResource Include="../../data/*.fs" LogicalName="Box2D.NET.Samples.Shaders.%(Filename)%(Extension)" />
<!-- ImGui.NET 1.90 does not expose ImGui's embedded vector font, so embed the existing font instead. -->
<EmbeddedResource Include="../../data/droid_sans.ttf" LogicalName="Box2D.NET.Samples.Fonts.droid_sans.ttf" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
Expand Down Expand Up @@ -42,4 +47,4 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

</Project>
</Project>
35 changes: 35 additions & 0 deletions src/Box2D.NET.Samples/EmbeddedShaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2026 Erin Catto
// SPDX-FileCopyrightText: 2026 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT

using System.IO;
using System.Reflection;

namespace Box2D.NET.Samples;

// Embedded from data/*.vs and *.fs. Do not edit the shader strings here.
public static class EmbeddedShaders
{
public static readonly string k_background_vs = Load("background.vs");
public static readonly string k_background_fs = Load("background.fs");
public static readonly string k_circle_vs = Load("circle.vs");
public static readonly string k_circle_fs = Load("circle.fs");
public static readonly string k_line_vs = Load("line.vs");
public static readonly string k_line_fs = Load("line.fs");
public static readonly string k_point_vs = Load("point.vs");
public static readonly string k_point_fs = Load("point.fs");
public static readonly string k_solid_capsule_vs = Load("solid_capsule.vs");
public static readonly string k_solid_capsule_fs = Load("solid_capsule.fs");
public static readonly string k_solid_circle_vs = Load("solid_circle.vs");
public static readonly string k_solid_circle_fs = Load("solid_circle.fs");
public static readonly string k_solid_polygon_vs = Load("solid_polygon.vs");
public static readonly string k_solid_polygon_fs = Load("solid_polygon.fs");

private static string Load(string name)
{
Assembly assembly = typeof(EmbeddedShaders).Assembly;
using Stream stream = assembly.GetManifestResourceStream($"Box2D.NET.Samples.Shaders.{name}")!;
using StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
3 changes: 2 additions & 1 deletion src/Box2D.NET.Samples/Graphics/Backgrounds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using Silk.NET.GLFW;
using Silk.NET.OpenGL;
using static Box2D.NET.Samples.EmbeddedShaders;

namespace Box2D.NET.Samples.Graphics;

Expand All @@ -14,7 +15,7 @@ public static Background CreateBackground(GL gl)
{
Background background = new Background();

background.programId = gl.CreateProgramFromFiles("data/background.vs", "data/background.fs");
background.programId = gl.CreateProgramFromStrings(k_background_vs, k_background_fs);
background.timeUniform = gl.GetUniformLocation(background.programId, "time");
background.resolutionUniform = gl.GetUniformLocation(background.programId, "resolution");
background.baseColorUniform = gl.GetUniformLocation(background.programId, "baseColor");
Expand Down
23 changes: 0 additions & 23 deletions src/Box2D.NET.Samples/Graphics/Cameras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,6 @@ public static void BuildProjectionMatrix(Camera camera, Span<float> m, float zBi
m[15] = 1.0f;
}

public static void MakeOrthographicMatrix(Span<float> m, float left, float right, float bottom, float top, float near, float far)
{
m[0] = 2.0f / (right - left);
m[1] = 0.0f;
m[2] = 0.0f;
m[3] = 0.0f;

m[4] = 0.0f;
m[5] = 2.0f / (top - bottom);
m[6] = 0.0f;
m[7] = 0.0f;

m[8] = 0.0f;
m[9] = 0.0f;
m[10] = -2.0f / (far - near);
m[11] = 0.0f;

m[12] = -(right + left) / (right - left);
m[13] = -(top + bottom) / (top - bottom);
m[14] = -(far + near) / (far - near);
m[15] = 1.0f;
}

public static B2AABB GetViewBounds(Camera camera)
{
if (camera.height == 0.0f || camera.width == 0.0f)
Expand Down
5 changes: 3 additions & 2 deletions src/Box2D.NET.Samples/Graphics/Circles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Box2D.NET.Samples.Primitives;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.Samples.Graphics.Cameras;
using static Box2D.NET.Samples.EmbeddedShaders;

namespace Box2D.NET.Samples.Graphics;

Expand All @@ -19,7 +20,7 @@ public static class Circles
public static CircleRender CreateCircles(GL gl)
{
CircleRender render = new CircleRender();
render.programId = gl.CreateProgramFromFiles("data/circle.vs", "data/circle.fs");
render.programId = gl.CreateProgramFromStrings(k_circle_vs, k_circle_fs);
render.projectionUniform = gl.GetUniformLocation(render.programId, "projectionMatrix");
render.pixelScaleUniform = gl.GetUniformLocation(render.programId, "pixelScale");
uint vertexAttribute = 0;
Expand Down Expand Up @@ -144,4 +145,4 @@ public static void FlushCircles(GL gl, ref CircleRender render, Camera camera)

render.circles.Clear();
}
}
}
3 changes: 1 addition & 2 deletions src/Box2D.NET.Samples/Graphics/Draw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public class Draw
public SolidCircleRender circles;
public SolidCapsuleRender capsules;
public SolidPolygonRender polygons;
public Font font;

}
}
35 changes: 35 additions & 0 deletions src/Box2D.NET.Samples/Graphics/DrawText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-FileCopyrightText: 2026 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT

using System.Numerics;
using ImGuiNET;
using static Box2D.NET.Samples.Graphics.Cameras;

namespace Box2D.NET.Samples.Graphics;

public static partial class Draws
{
public static void DrawScreenString(Draw draw, float x, float y, B2HexColor color, string message)
{
message = message.Length > 255 ? message[..255] : message;
uint hex = (uint)color;

// b2HexColor packs as 0xRRGGBB; matches MakeRGBA8's byte order. Force alpha to opaque.
uint col = ImGui.ColorConvertFloat4ToU32(new Vector4(((hex >> 16) & 0xFF) / 255.0f, ((hex >> 8) & 0xFF) / 255.0f,
(hex & 0xFF) / 255.0f, 1.0f));

// Old STB path treated y as the text baseline; ImGui::AddText treats y as the top of the
// glyph. Shift up by the font ascent so existing callers work.
float ascent = ImGui.GetFont().Ascent;
Vector2 viewportPosition = ImGui.GetMainViewport().Pos;
ImGui.GetBackgroundDrawList().AddText(viewportPosition + new Vector2(x, y - ascent), col, message);
}

public static void DrawWorldString(Draw draw, Camera camera, B2Vec2 p, B2HexColor color, string message)
{
message = message.Length > 255 ? message[..255] : message;
B2Vec2 ps = ConvertWorldToScreen(camera, p);
DrawScreenString(draw, ps.X, ps.Y, color, message);
}
}
19 changes: 1 addition & 18 deletions src/Box2D.NET.Samples/Graphics/Draws.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

using System;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.Samples.Graphics.Cameras;
using static Box2D.NET.Samples.Graphics.Backgrounds;
using static Box2D.NET.Samples.Graphics.Points;
using static Box2D.NET.Samples.Graphics.Circles;
using static Box2D.NET.Samples.Graphics.Lines;
using static Box2D.NET.Samples.Graphics.SolidCapsules;
using static Box2D.NET.Samples.Graphics.SolidCircles;
using static Box2D.NET.Samples.Graphics.SolidPolygons;
using static Box2D.NET.Samples.Graphics.Fonts;

namespace Box2D.NET.Samples.Graphics;

public static class Draws
public static partial class Draws
{
public static Draw CreateDraw(SampleContext context)
{
Expand All @@ -30,8 +28,6 @@ public static Draw CreateDraw(SampleContext context)
draw.circles = CreateSolidCircles(context.gl);
draw.capsules = CreateSolidCapsule(context.gl);
draw.polygons = CreateSolidPolygons(context.gl);
draw.font = CreateFont(context.gl, "data/droid_sans.ttf", 18.0f);

return draw;
}

Expand All @@ -44,7 +40,6 @@ public static void DestroyDraw(Draw draw)
DestroySolidCircles(draw.gl, ref draw.circles);
DestroyCapsules(draw.gl, ref draw.capsules);
DestroyPolygons(draw.gl, ref draw.polygons);
DestroyFont(draw.gl, ref draw.font);
}

public static void DrawPolygon(Draw draw, ReadOnlySpan<B2Vec2> vertices, int vertexCount, B2HexColor color)
Expand Down Expand Up @@ -87,17 +82,6 @@ public static void DrawBounds(Draw draw, in B2AABB aabb, B2HexColor c)
AddLine(ref draw.lines, p4, p1, c);
}

public static void DrawScreenString(Draw draw, float x, float y, B2HexColor color, string message)
{
AddText(ref draw.font, x, y, color, message);
}

public static void DrawWorldString(Draw draw, Camera camera, B2Vec2 p, B2HexColor color, string message)
{
B2Vec2 ps = ConvertWorldToScreen(camera, p);
AddText(ref draw.font, ps.X, ps.Y, color, message);
}

public static void DrawCircle(Draw draw, B2Vec2 center, float radius, B2HexColor color)
{
AddCircle(ref draw.hollowCircles, center, radius, color);
Expand Down Expand Up @@ -135,7 +119,6 @@ public static void FlushDraw(Draw draw, Camera camera)
FlushCircles(draw.gl, ref draw.hollowCircles, camera);
FlushLines(draw.gl, ref draw.lines, camera);
FlushPoints(draw.gl, ref draw.points, camera);
FlushText(draw.gl, ref draw.font, camera);
draw.gl.CheckOpenGL();
}
}
38 changes: 0 additions & 38 deletions src/Box2D.NET.Samples/Graphics/Font.cs

This file was deleted.

59 changes: 0 additions & 59 deletions src/Box2D.NET.Samples/Graphics/Fonts.cs

This file was deleted.

Loading
Loading