yess gimme all the fonts

This commit is contained in:
vfs committed 2026-01-22 19:12:30 +01:00
1 parent 063b46cec4
commit 7aa05887d5
132 files changed
+860 -538

No files matched your search

+71 -165
View File
@@ -1,13 +1,11 @@
const std = @import("std");
const c = @import("c.zig").c;
// const rl = @import("raylib");
const rl = @import("raylib");
const kb = @import("kb");
const ft = @import("freetype");
const rp = @import("stb_rect_pack");
pub var ft_lib : ft.Library = undefined;
pub var shader : c.Shader = undefined;
// pub var shader : rl.Shader = undefined;
pub var shader : rl.Shader = undefined;
const ATLAS_SIZE = 4096;
@@ -20,47 +18,38 @@ const Glyph = struct {
height : i32,
dpi_width : f32,
dpi_height : f32,
// rwidth : i16,
// rheight : i16,
bearing_x : i32,
bearing_y : i32,
dpi_bearing_x : f32,
dpi_bearing_y : f32,
// y_max : i16,
// y_min : i16,
descent : i32,
ascent : i32,
dpi_descent : f32,
dpi_ascent : f32,
// advance : i16,
st0 : c.Vector2,
st1 : c.Vector2,
// st0 : rl.Vector2,
// st1 : rl.Vector2,
st0 : rl.Vector2,
st1 : rl.Vector2,
};
pub const Font = struct {
face : ft.Face,
glyphs : std.AutoHashMap(u32, Glyph),
kb : kb.kbts_font,
texture : c.Texture2D,
// texture : rl.Texture2D,
// texture : c.Texture2D,
texture : rl.Texture2D,
file_data : []u8,
pub fn init(filename : []const u8, size : i32, allocator: std.mem.Allocator) !Font {
const font_data = try std.fs.cwd().readFileAlloc(allocator, filename, 10 * 1024 * 1024);
const file_data = try std.fs.cwd().readFileAlloc(allocator, filename, 10 * 1024 * 1024);
errdefer allocator.free(file_data);
var face = try ft_lib.createFaceMemory(font_data, 0);
var face = try ft_lib.createFaceMemory(file_data, 0);
var glyphs = std.AutoHashMap(u32, Glyph).init(allocator);
try face.setCharSize(0, size * 64, 0, 96 * @as(u16, @intFromFloat(c.GetWindowScaleDPI().y)));
// try face.setPixelSizes(0, @intCast(size * @as(u16, @intFromFloat(c.GetWindowScaleDPI().y))));
// try face.setCharSize(0, size * 64, 0, 72 * @as(u16, @intFromFloat(rl.getWindowScaleDPI().y)));
// try face.setCharSize(0, size * 64, 0, 0);
try face.setCharSize(0, size * 64, 0, 96 * @as(u16, @intFromFloat(rl.getWindowScaleDPI().y)));
try face.selectCharmap(.unicode);
var rects = try std.ArrayList(rp.stbrp_rect).initCapacity(allocator, 1024);
@@ -75,21 +64,15 @@ pub const Font = struct {
defer allocator.free(_atlas);
@memset(_atlas, 0);
const atlas : c.Image = .{
// const atlas : rl.Image = .{
const atlas : rl.Image = .{
.data = _atlas.ptr,
.width = ATLAS_SIZE,
.height = ATLAS_SIZE,
.mipmaps = 1,
.format = c.PIXELFORMAT_UNCOMPRESSED_GRAYSCALE,
.format = rl.PixelFormat.uncompressed_grayscale,
};
// var iter = face.iterateCharmap();
// while (iter.next()) |char| {
for (0..face.numGlyphs()) |index| {
// const i = iter.index;
// for (0..face.numGlyphs()) |i| {
try face.loadGlyph(@intCast(index), .{});
const bmp = face.glyph().bitmap();
@@ -130,31 +113,17 @@ pub const Font = struct {
}
}
// std.log.info("{}", .{bmp.width()});
const width : i32 = @intCast(bmp.width());
// std.log.info("{}", .{bmp.rows()});
const height : i32 = @intCast(bmp.rows());
// const dpi_width : i32 = @intFromFloat(@as(f32, @floatFromInt(width)) / rl.getWindowScaleDPI().x);
// const dpi_height : i32 = @intFromFloat(@as(f32, @floatFromInt(height)) / rl.getWindowScaleDPI().y);
// std.log.info("{}", .{@as(f32, @floatFromInt(width)) / c.GetWindowScaleDPI().x});
const dpi_width : f32 = @as(f32, @floatFromInt(width)) / c.GetWindowScaleDPI().x;
// std.log.info("{}", .{@as(f32, @floatFromInt(height)) / c.GetWindowScaleDPI().y});
const dpi_height : f32 = @as(f32, @floatFromInt(height)) / c.GetWindowScaleDPI().y;
// std.log.info("{}", .{face.glyph().bitmapLeft()});
const dpi_width : f32 = @as(f32, @floatFromInt(width)) / rl.getWindowScaleDPI().x;
const dpi_height : f32 = @as(f32, @floatFromInt(height)) / rl.getWindowScaleDPI().y;
const bearing_x : i32 = face.glyph().bitmapLeft();
// std.log.info("{}", .{face.glyph().bitmapTop()});
const bearing_y : i32 = face.glyph().bitmapTop();
// std.log.info("{}", .{@as(f32, @floatFromInt(bearing_x)) / c.GetWindowScaleDPI().x});
const dpi_bearing_x : f32 = @as(f32, @floatFromInt(bearing_x)) / c.GetWindowScaleDPI().x;
// std.log.info("{}", .{@as(f32, @floatFromInt(bearing_y)) / c.GetWindowScaleDPI().y});
const dpi_bearing_y : f32 = @as(f32, @floatFromInt(bearing_y)) / c.GetWindowScaleDPI().y;
// std.log.info("{}", .{height - bearing_y});
const dpi_bearing_x : f32 = @as(f32, @floatFromInt(bearing_x)) / rl.getWindowScaleDPI().x;
const dpi_bearing_y : f32 = @as(f32, @floatFromInt(bearing_y)) / rl.getWindowScaleDPI().y;
const descent : i32 = height - bearing_y;
// std.log.info("{}", .{height - descent});
const ascent : i32 = height - descent;
// std.log.info("{}", .{dpi_height - dpi_bearing_y});
const dpi_descent : f32 = dpi_height - dpi_bearing_y;
// std.log.info("{}", .{dpi_height - dpi_descent});
const dpi_ascent : f32 = dpi_height - dpi_descent;
const x : i16 = @intCast(rect.x);
@@ -178,13 +147,10 @@ pub const Font = struct {
.height = height,
.dpi_width = dpi_width,
.dpi_height = dpi_height,
// .rwidth = @intCast(face.glyph().metrics().width >> 6),
// .rheight = @intCast(face.glyph().metrics().height >> 6),
.descent = descent,
.ascent = ascent,
.dpi_descent = dpi_descent,
.dpi_ascent = dpi_ascent,
// .advance = @intCast(face.glyph().advance().x >> 6),
.st0 = .{.x = (fx + 0.5) / fs, .y = (fy - fh + 0.5) / fs},
.st1 = .{.x = (fx + fw - 0.5) / fs, .y = (fy - 0.5) / fs},
};
@@ -192,47 +158,38 @@ pub const Font = struct {
try glyphs.put(glyph.index, glyph);
}
const texture = c.LoadTextureFromImage(atlas);
// const texture = try rl.Texture.fromImage(atlas);
const texture = try rl.loadTextureFromImage(atlas);
var _kb = kb.kbts_FontFromMemory(font_data.ptr, @intCast(font_data.len), 0, null, null);
var _kb = kb.kbts_FontFromMemory(file_data.ptr, @intCast(file_data.len), 0, null, null);
if (kb.kbts_FontIsValid(&_kb) != 0) {
if (kb.kbts_FontIsValid(&_kb) == 0) {
std.log.info("[Error] [kb_text_shape] Failed to load the font.", .{});
}
allocator.free(font_data);
return .{
.face = face,
.glyphs = glyphs,
.kb = _kb,
.texture = texture,
.file_data = file_data,
};
}
pub fn deinit(self: *Font) void {
// var it = self.glyphs.valueIterator();
// while (it.next()) |g| {
// allocator.free(g.bitmap);
// }
pub fn deinit(self: *Font, allocator : std.mem.Allocator) void {
self.glyphs.deinit();
self.face.deinit();
kb.kbts_FreeFont(&self.kb);
c.UnloadTexture(self.texture);
// self.texture.unload();
self.texture.unload();
allocator.free(self.file_data);
}
pub fn render_text(
self: *Font,
text: []const u8,
pos: c.Vector2,
// pos: rl.Vector2,
pos: rl.Vector2,
window_space: bool,
colour: c.Color,
background: c.Color,
// colour: rl.Color,
// background: rl.Color,
colour: rl.Color,
background: rl.Color,
nice_background: bool,
count_descent: bool
) void {
@@ -242,13 +199,10 @@ pub const Font = struct {
if (nice_background) {
self.render_text(
text,
c.Vector2Add(pos, .{.x = 3.0, .y = -3.0}),
// pos.add(.{.x = 3.0, .y = -3.0}),
pos.add(.{.x = 3.0, .y = -3.0}),
window_space,
c.Color{ .r = 0, .g = 0, .b = 0, .a = 255 },
c.Color{ .r = 0, .g = 0, .b = 0, .a = 0 },
// rl.Color.init(0, 0, 0, 255),
// rl.Color.init(0, 0, 0, 0),
rl.Color{ .r = 0, .g = 0, .b = 0, .a = 255 },
rl.Color{ .r = 0, .g = 0, .b = 0, .a = 0 },
false,
false
);
@@ -256,19 +210,12 @@ pub const Font = struct {
var render_pos = pos;
_ = count_descent;
//_ = colour;
_ = background;
// const draw_size, const max_ascent, const max_descent = self.size_row(text, 0, 0);
// _ = max_ascent;
// _ = max_descent;
const font_ascent = @as(f32, @floatFromInt(self.face.size().metrics().ascender >> 6));
// const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
const font_ascent : f32 = @floatFromInt(self.face.size().metrics().ascender >> 6);
const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
render_pos.y += font_ascent;
// c.DrawLine(@intFromFloat(render_pos.x), @intFromFloat(render_pos.y), c.GetScreenWidth(), @intFromFloat(render_pos.y), c.RED);
// rl.drawLine(@intFromFloat(render_pos.x), @intFromFloat(render_pos.y), rl.getScreenWidth(), @intFromFloat(render_pos.y), .red);
render_pos.y += dpi_font_ascent;
const Context = kb.kbts_CreateShapeContext(null, null);
const kb_font = kb.kbts_ShapePushFont(Context, &self.kb);
@@ -277,21 +224,17 @@ pub const Font = struct {
return;
}
kb.kbts_ShapeBegin(Context, kb.KBTS_DIRECTION_DONT_KNOW, kb.KBTS_LANGUAGE_DONT_KNOW);
kb.kbts_ShapeBegin(Context, kb.KBTS_DIRECTION_DONT_KNOW, kb.KBTS_LANGUAGE_ENGLISH);
kb.kbts_ShapeUtf8(Context, text.ptr, @intCast(text.len), kb.KBTS_USER_ID_GENERATION_MODE_CODEPOINT_INDEX);
kb.kbts_ShapeEnd(Context);
var x_offset : f32 = 0;
var y_offset : f32 = 0;
c.BeginShaderMode(shader);
c.rlSetTexture(self.texture.id);
c.rlBegin(c.RL_QUADS);
// rl.beginShaderMode(shader);
// rl.gl.rlSetTexture(self.texture.id);
// rl.gl.rlBegin(rl.gl.rl_quads);
rl.beginShaderMode(shader);
rl.gl.rlSetTexture(self.texture.id);
rl.gl.rlBegin(rl.gl.rl_quads);
// Layout runs naively left to right.
var Run = kb.kbts_run{};
while (kb.kbts_ShapeRun(Context, &Run) != 0) {
if ((Run.Flags & kb.KBTS_BREAK_FLAG_LINE_HARD) != 0) {
@@ -307,73 +250,38 @@ pub const Font = struct {
std.log.info("aah it's wrong!! idk how to handle the error rn!!", .{});
}
const advance_x = @as(f32, @floatFromInt(ft.mulFix(RunGlyph.AdvanceX, @intCast(self.face.size().metrics().x_scale)) >> 6));
const advance_y = @as(f32, @floatFromInt(ft.mulFix(RunGlyph.AdvanceY, @intCast(self.face.size().metrics().y_scale)) >> 6));
const dpi_advance_x = advance_x / c.GetWindowScaleDPI().x;
const dpi_advance_y = advance_y / c.GetWindowScaleDPI().y;
// const dpi_advance_x = advance_x / rl.getWindowScaleDPI().x;
// const dpi_advance_y = advance_y / rl.getWindowScaleDPI().y;
const advance_x : f32 = @floatFromInt(ft.mulFix(RunGlyph.AdvanceX, @intCast(self.face.size().metrics().x_scale)) >> 6);
const advance_y : f32 = @floatFromInt(ft.mulFix(RunGlyph.AdvanceY, @intCast(self.face.size().metrics().y_scale)) >> 6);
const dpi_advance_x = advance_x / rl.getWindowScaleDPI().x;
const dpi_advance_y = advance_y / rl.getWindowScaleDPI().y;
// glyph : *Glyph = table_find_pointer(*text.font.glyphs, RunGlyph.Id);
if (self.glyphs.getPtr(RunGlyph.Id)) |glyph| {
var v0 = c.Vector2{};
var v1 = c.Vector2{};
var v0 = rl.Vector2.zero();
var v1 = rl.Vector2.zero();
// const bx = @as(f32, @floatFromInt(glyph.bearing_x));
const by = glyph.dpi_bearing_y;
// const height = @as(f32, @floatFromInt(glyph.height));
// const descent = @as(f32, @floatFromInt(glyph.descent));
// const ascent = @as(f32, @floatFromInt(glyph.ascent));
// if (count_descent) {
v0 = c.Vector2Add(render_pos, .{ .x = x_offset,// + glyph.bearing_x,
.y = y_offset - by });// + draw_size.y });// /*- max_descent*/};
// v0 = render_pos.add(.{ .x = x_offset,// + glyph.bearing_x,
// .y = y_offset - by });// + draw_size.y });// /*- max_descent*/};
// } else {
// v0 = render_pos.add(.{
// .x = x_offset,// + glyph.bearing_x,
// .y = y_offset - @as(f32, @floatFromInt(glyph.height - glyph.bearing_y))
// });//* - glyph.height + draw_size.y*/};
// }
v0 = render_pos.add(.{ .x = x_offset + glyph.dpi_bearing_x,
.y = y_offset - by });
v1 = c.Vector2Add(v0, c.Vector2{ .x = glyph.dpi_width, .y = glyph.dpi_height });
// v1 = v0.add(rl.Vector2{ .x = @floatFromInt(glyph.width), .y = @floatFromInt(glyph.height) });
const p0 : c.Vector4 = .{ .x = v0.x, .y = v0.y, .z = 0.0, .w = 1.0 };
const p1 : c.Vector4 = .{ .x = v1.x, .y = v1.y, .z = 0.0, .w = 1.0 };
// const p0 : rl.Vector4 = .{ .x = v0.x, .y = v0.y, .z = 0.0, .w = 1.0 };
// const p1 : rl.Vector4 = .{ .x = v1.x, .y = v1.y, .z = 0.0, .w = 1.0 };
v1 = v0.add(rl.Vector2{ .x = glyph.dpi_width, .y = glyph.dpi_height });
const p0 : rl.Vector4 = .{ .x = v0.x, .y = v0.y, .z = 0.0, .w = 1.0 };
const p1 : rl.Vector4 = .{ .x = v1.x, .y = v1.y, .z = 0.0, .w = 1.0 };
x_offset += dpi_advance_x;
y_offset += dpi_advance_y;
// #if Y_IS_UP {
// t0 := Vector2.{
// cast(float, glyph.x) / cast(float, ATLAS_SIZE),
// cast(float, glyph.y) / cast(float, ATLAS_SIZE)
// };
// t1 := t0 + Vector2.{
// cast(float, glyph.width) / cast(float, ATLAS_SIZE),
// -cast(float, glyph.height) / cast(float, ATLAS_SIZE)
// };
const st0 = glyph.st0;
const st1 = glyph.st1;
// } else {
// t0 := Vector2.{cast(float, glyph.x / ATLAS_SIZE), cast(float, glyph.y / ATLAS_SIZE)};
// t1 := t0 + .{cast(float, glyph.width / ATLAS_SIZE), cast(float, glyph.height / ATLAS_SIZE)};
// }
const st0 = glyph.st0;
const st1 = glyph.st1;
c.rlColor4ub(colour.r, colour.g, colour.b, colour.a);
c.rlNormal3f(0.0, 0.0, 1.0);
rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a);
rl.gl.rlNormal3f(0.0, 0.0, 1.0);
c.rlTexCoord2f(st0.x, st0.y); c.rlVertex2f(p0.x, p0.y);
c.rlTexCoord2f(st0.x, st1.y); c.rlVertex2f(p0.x, p1.y);
c.rlTexCoord2f(st1.x, st1.y); c.rlVertex2f(p1.x, p1.y);
c.rlTexCoord2f(st1.x, st0.y); c.rlVertex2f(p1.x, p0.y);
// rl.gl.rlColor4ub(colour.r, colour.g, colour.b, colour.a);
// rl.gl.rlNormal3f(0.0, 0.0, 1.0);
rl.gl.rlTexCoord2f(st0.x, st0.y); rl.gl.rlVertex2f(p0.x, p0.y);
rl.gl.rlTexCoord2f(st0.x, st1.y); rl.gl.rlVertex2f(p0.x, p1.y);
rl.gl.rlTexCoord2f(st1.x, st1.y); rl.gl.rlVertex2f(p1.x, p1.y);
rl.gl.rlTexCoord2f(st1.x, st0.y); rl.gl.rlVertex2f(p1.x, p0.y);
// rl.gl.rlTexCoord2f(st0.x, st0.y); rl.gl.rlVertex2f(p0.x, p0.y);
// rl.gl.rlTexCoord2f(st0.x, st1.y); rl.gl.rlVertex2f(p0.x, p1.y);
// rl.gl.rlTexCoord2f(st1.x, st1.y); rl.gl.rlVertex2f(p1.x, p1.y);
// rl.gl.rlTexCoord2f(st1.x, st0.y); rl.gl.rlVertex2f(p1.x, p0.y);
} else {
std.log.warn("kb_text_shape found the glyph, but we didn't load it from the font. index {}", .{RunGlyph.Id});
std.log.warn("advance_x {}", .{advance_x});
@@ -384,32 +292,28 @@ pub const Font = struct {
}
}
c.rlEnd();
c.rlSetTexture(0);
c.EndShaderMode();
// rl.gl.rlEnd();
// rl.gl.rlSetTexture(0);
// rl.endShaderMode();
rl.gl.rlEnd();
rl.gl.rlSetTexture(0);
rl.endShaderMode();
}
pub fn size_row(self: *Font, str: []const u8, n: i32, max_width: f32) struct {c.Vector2, f32, i32} {
pub fn size_row(self: *Font, str: []const u8, n: i32, max_width: f32) struct {rl.Vector2, f32, i32} {
_ = max_width;
_ = n;
const Context = kb.kbts_CreateShapeContext(null, null);
const kb_font = kb.kbts_ShapePushFont(Context, &self.kb);
if (kb_font == null) {
std.log.info("Could not open font!", .{});
return .{ c.Vector2.zero(), 0, 0 };
return .{ rl.Vector2.zero(), 0, 0 };
}
kb.kbts_ShapeBegin(Context, kb.KBTS_DIRECTION_DONT_KNOW, kb.KBTS_LANGUAGE_DONT_KNOW);
kb.kbts_ShapeUtf8(Context, str.ptr, @intCast(str.len), kb.KBTS_USER_ID_GENERATION_MODE_CODEPOINT_INDEX);
kb.kbts_ShapeEnd(Context);
var size = c.Vector2.zero();
var size = rl.Vector2.zero();
var max_descent : f32 = 0;
// Layout runs naively left to right.
var Run = kb.kbts_run{};
while(kb.kbts_ShapeRun(Context, &Run) != 0) {
if ((Run.Flags & kb.KBTS_BREAK_FLAG_LINE_HARD) != 0) {
@@ -418,7 +322,7 @@ pub const Font = struct {
var g : [*c]kb.kbts_glyph = null;
while (kb.kbts_GlyphIteratorNext(&Run.Glyphs, &g) != 0) {
const RunGlyph : *kb.kbts_glyph = g.?; // assert non-null
const RunGlyph : *kb.kbts_glyph = g.?;
const CodepointIndex : i32 = RunGlyph.UserIdOrCodepointIndex;
var ShapeCodepoint : kb.kbts_shape_codepoint = undefined;
if (kb.kbts_ShapeGetShapeCodepoint(Context, CodepointIndex, &ShapeCodepoint) == 0) {
@@ -426,10 +330,12 @@ pub const Font = struct {
}
if (self.glyphs.getPtr(@intCast(RunGlyph.Id))) |glyph| {
size.y = @max(size.y, @as(f32, @floatFromInt(glyph.dpi_height)));
// size.x += (ft.mulFix(RunGlyph.AdvanceX, self.face.size().metrics().x_scale) >> 6);
size.x += @floatFromInt(ft.mulFix(RunGlyph.AdvanceX, @intCast(self.face.size().metrics().x_scale)) >> 6);
max_descent = @max(max_descent, @as(f32, @floatFromInt(glyph.descent)));
const advance_x = @as(f32, @floatFromInt(ft.mulFix(RunGlyph.AdvanceX, @intCast(self.face.size().metrics().x_scale)) >> 6));
const dpi_advance_x = advance_x / rl.getWindowScaleDPI().x;
size.y = @max(size.y, glyph.dpi_height);
size.x += dpi_advance_x;
max_descent = @max(max_descent, glyph.dpi_descent);
}
}
}
+179 -346
View File
@@ -1,12 +1,10 @@
const std = @import("std");
const zm = @import("zmath");
const znet = @import("znet");
// const rl = @import("raylib");
const rl = @import("raylib");
const bufzilla = @import("bufzilla");
const ft = @import("freetype");
const c = @import("c.zig").c;
const shared = @import("shared");
const client = @import("client.zig");
@@ -40,26 +38,37 @@ pub fn main() !void {
defer _ = dbg_allocator.deinit();
try znet.init();
defer znet.deinit();
c.SetConfigFlags(c.FLAG_WINDOW_HIGHDPI);
// rl.setConfigFlags(.{ .window_highdpi = true });
c.InitWindow(screen_width, screen_height, "zzz");
// rl.initWindow(screen_width, screen_height, "zzz");
defer c.CloseWindow();
// defer rl.closeWindow();
rl.setConfigFlags(.{ .window_highdpi = true });
rl.initWindow(screen_width, screen_height, "zzz");
defer rl.closeWindow();
font.ft_lib = try ft.Library.init();
// std.log.debug("screen ? {}", .{rl.getScreenWidth()});
// std.log.debug("screen ? {}", .{rl.getScreenHeight()});
// std.log.debug("render ? {}", .{rl.getRenderWidth()});
// std.log.debug("render ? {}", .{rl.getRenderHeight()});
const sizes = [_]u32{
12, 13, 14, 15, 16,
18, 20, 22,
};
// std.log.debug("what's being used for viewport ? {}", .{rl.RLGL.State.framebufferWidth});
var inconsolata: [sizes.len]font.Font = undefined;
var vollkorn: [sizes.len]font.Font = undefined;
var arabic: [sizes.len]font.Font = undefined;
var japanese: [sizes.len]font.Font = undefined;
var f = try font.Font.init("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", 7, allocator);
defer f.deinit();
for (sizes, 0..) |pt, i| {
inconsolata[i] = try font.Font.init("assets/fonts/Inconsolata/static/Inconsolata-Regular.ttf", @intCast(pt), allocator);
vollkorn[i] = try font.Font.init("assets/fonts/Vollkorn/static/Vollkorn-Regular.ttf", @intCast(pt), allocator);
arabic[i] = try font.Font.init("assets/fonts/Noto_Sans_Arabic/static/NotoSansArabic-Regular.ttf", @intCast(pt), allocator);
japanese[i] = try font.Font.init("assets/fonts/Noto_Sans_JP/static/NotoSansJP-Regular.ttf", @intCast(pt), allocator);
}
font.shader = c.LoadShader(null, "assets/text.frag");
defer {
for (&inconsolata) |*f| f.deinit(allocator);
for (&vollkorn) |*f| f.deinit(allocator);
for (&arabic) |*f| f.deinit(allocator);
for (&japanese) |*f| f.deinit(allocator);
}
font.shader = try rl.loadShader(null, "assets/text.frag");
// const test_shader1 = try c.LoadShader(null, "assets/test_1.frag");
// const test_shader2 = try c.LoadShader(null, "assets/test_2.frag");
@@ -133,12 +142,12 @@ pub fn main() !void {
// rl.playMusicStream(music);
// std.log.info("is music playing? {}", .{rl.isMusicStreamPlaying(music)});
var old_time : f32 = @floatCast(c.GetTime());
var old_time : f32 = @floatCast(rl.getTime());
// var old_time : f32 = @floatCast(rl.getTime());
while (!c.WindowShouldClose()) {
while (!rl.windowShouldClose()) {
// while (!rl.windowShouldClose()) { // Detect window close button or ESC key
const new_time : f32 = @floatCast(c.GetTime());
const new_time : f32 = @floatCast(rl.getTime());
// const new_time : f32 = @floatCast(rl.getTime());
var frame_time = new_time - old_time;
old_time = new_time;
@@ -173,47 +182,94 @@ pub fn main() !void {
},
};
c.PollInputEvents();
// rl.pollInputEvents();
rl.pollInputEvents();
// if (peer.state() == .connected) {
// const packet = try znet.Packet.init("Hello, Server!", 0, .reliable);
// try peer.send(packet);
// }
// // _ = peer;
// if (peer.state() == .connected) {
// const packet = try znet.Packet.init("Hello, Server!", 0, .reliable);
// try peer.send(packet);
// }
// // _ = peer;
// // Update
// //----------------------------------------------------------------------------------
// // TODO: Update your variables here
// //----------------------------------------------------------------------------------
// rl.updateMusicStream(music);
// rl.updateMusicStream(music);
while (accumulator > dt * k) {
// update(dt * cast(float) k);
accumulator -= dt * k;
gt += dt * k;
}
// while (accumulator > dt * k) {
// // update(dt * cast(float) k);
// accumulator -= dt * k;
// gt += dt * k;
// }
rl.beginDrawing();
rl.clearBackground(.sky_blue);
// // Draw
// //----------------------------------------------------------------------------------
c.BeginDrawing();
c.ClearBackground(c.SKYBLUE);
// rl.clearBackground(.black);
var y : f32 = 0;
// rl.beginDrawing();
for (&vollkorn) |*f| {
f.render_text(
"Whereas, disregard and contempt for human rights have resulted!fi",
rl.Vector2{ .x = 0, .y = y},
true,
.white,
.blank,
false,
true
);
// // rl.drawRectangle(screen_width - 100, 0, 100, 100, .red);
const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
f.render_text(
"Whereas, disregard and contempt for human rights have resulted!fi",
c.Vector2{ .x = 0, .y = 0},
true,
c.WHITE,
c.BLANK,
false,
true
);
y += dpi_font_ascent;
}
for (&inconsolata) |*f| {
f.render_text(
"Whereas, disregard and contempt for human rights have resulted!fi",
rl.Vector2{ .x = 0, .y = y},
true,
.white,
.blank,
false,
true
);
const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
const dpi_font_ascent = font_ascent / rl.getWindowScaleDPI().y;
y += dpi_font_ascent;
}
// for (&japanese) |*f| {
// f.render_text(
// "外は夜が長くて寒い",
// c.Vector2{ .x = 0, .y = y},
// true,
// c.WHITE,
// c.BLANK,
// false,
// true
// );
// const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
// const dpi_font_ascent = font_ascent / c.GetWindowScaleDPI().y;
// y += dpi_font_ascent;
// }
// for (&arabic) |*f| {
// f.render_text(
// "الليل طويل وبارد في الخارج",
// c.Vector2{ .x = 0, .y = y},
// true,
// c.WHITE,
// c.BLANK,
// false,
// true
// );
// const font_ascent : f32 = @floatFromInt(f.face.size().metrics().ascender >> 6);
// const dpi_font_ascent = font_ascent / c.GetWindowScaleDPI().y;
// y += dpi_font_ascent;
// }
// f.render_text(
// "Whereas, disregard and contempt for human rights have resulted!",
// rl.Vector2.init(0, 0),
@@ -224,107 +280,103 @@ pub fn main() !void {
// true
// );
// // rl.beginShaderMode(test_shader1);
// // rl.gl.rlBegin(rl.gl.rl_quads);
// rl.beginShaderMode(test_shader1);
// rl.gl.rlBegin(rl.gl.rl_quads);
// // {
// // const topLeft : rl.Vector2 = .{ .x = 0.0, .y = 0.0 };
// // const bottomRight : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = @as(f32, @floatFromInt(rl.getScreenHeight())) };
// {
// const topLeft : rl.Vector2 = .{ .x = 0.0, .y = 0.0 };
// const bottomRight : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = @as(f32, @floatFromInt(rl.getScreenHeight())) };
// // rl.gl.rlVertex2f(topLeft.x, topLeft.y);
// // rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
// // rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
// // rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
// rl.gl.rlVertex2f(topLeft.x, topLeft.y);
// rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
// rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
// rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
// // }
// }
// // rl.gl.rlEnd();
// // rl.endShaderMode();
// rl.gl.rlEnd();
// rl.endShaderMode();
// // rl.beginShaderMode(test_shader2);
// // rl.gl.rlBegin(rl.gl.rl_quads);
// rl.beginShaderMode(test_shader2);
// rl.gl.rlBegin(rl.gl.rl_quads);
// // {
// // const topLeft : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = 0 };
// // const bottomRight : rl.Vector2 = .{ .x = @floatFromInt(rl.getScreenWidth()), .y = @floatFromInt(rl.getScreenHeight()) };
// {
// const topLeft : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = 0 };
// const bottomRight : rl.Vector2 = .{ .x = @floatFromInt(rl.getScreenWidth()), .y = @floatFromInt(rl.getScreenHeight()) };
// // rl.gl.rlVertex2f(topLeft.x, topLeft.y);
// // rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
// // rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
// // rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
// // }
// rl.gl.rlVertex2f(topLeft.x, topLeft.y);
// rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
// rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
// rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
// }
// // rl.gl.rlEnd();
// // rl.endShaderMode();
// rl.gl.rlEnd();
// rl.endShaderMode();
// // rl.beginShaderMode(test_shader2);
// // rl.gl.rlBegin(rl.gl.rl_quads);
// rl.beginShaderMode(test_shader2);
// rl.gl.rlBegin(rl.gl.rl_quads);
// // {
// // const topLeft : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = 0 };
// // const bottomRight : rl.Vector2 = .{ .x = @floatFromInt(rl.getScreenWidth()), .y = @floatFromInt(rl.getScreenHeight()) };
// {
// const topLeft : rl.Vector2 = .{ .x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0, .y = 0 };
// const bottomRight : rl.Vector2 = .{ .x = @floatFromInt(rl.getScreenWidth()), .y = @floatFromInt(rl.getScreenHeight()) };
// // rl.gl.rlVertex2f(topLeft.x, topLeft.y);
// // rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
// // rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
// // rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
// // }
// rl.gl.rlVertex2f(topLeft.x, topLeft.y);
// rl.gl.rlVertex2f(topLeft.x, bottomRight.y);
// rl.gl.rlVertex2f(bottomRight.x, bottomRight.y);
// rl.gl.rlVertex2f(bottomRight.x, topLeft.y);
// }
// // rl.gl.rlEnd();
// // rl.endShaderMode();
// rl.gl.rlEnd();
// rl.endShaderMode();
// // rl.drawRectangle(400, 0, 400, 450, rl.Color{ .r = 54, .g = 54, .b = 54, .a = 255 });
// rl.drawRectangle(400, 0, 400, 450, rl.Color{ .r = 54, .g = 54, .b = 54, .a = 255 });
// // f.texture.drawPro(
// // .{.x = 0, .y = 0, .width = 4096, .height = 4096},
// // .{.x = 0, .y = 0, .width = 512, .height = 512 },
// // .zero(), 0, .white);
// f.texture.drawPro(
// .{.x = 0, .y = 0, .width = 4096, .height = 4096},
// .{.x = 0, .y = 0, .width = 512, .height = 512 },
// .zero(), 0, .white);
// // const connected_text = "Connected";
// //const not_connected_text = "Not Connected";
// const connected_text = "Connected";
//const not_connected_text = "Not Connected";
// // switch (peer.state()) {
// // .connected => rl.drawText(connected_text, @divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2), 50, 20, .white),
// // else => rl.drawText(not_connected_text, @divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(not_connected_text, f.baseSize), 1), 50, 20, .white),
// // }
// //@divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2), 50
// switch (peer.state()) {
// .connected => rl.drawText(connected_text, @divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2), 50, 20, .white),
// else => rl.drawText(not_connected_text, @divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(not_connected_text, f.baseSize), 1), 50, 20, .white),
// }
//@divFloor(rl.getScreenWidth(), 2) - @divFloor(rl.measureText(connected_text, f.baseSize), 2), 50
// // // const font_size : i32 = 180;
// // // const text_size = rl.Vector2{.x = @floatFromInt(rl.measureText(connected_text, font_size)), .y = font_size};//rl.measureTextEx(try rl.getFontDefault(), connected_text, font_size, font_size / 10);
// // // const pos = rl.Vector2{.x = 0, .y = 0};
// // // rl.drawText(connected_text, pos.x, pos.y, font_size, .white);
// // // rl.drawRectangleLines(pos.x, pos.y, @intFromFloat(text_size.x), @intFromFloat(text_size.y), .red);
// // // rl.drawRectangle(pos.x, pos.y, rl.getScreenWidth(), rl.getScreenHeight(), .white);
// // rl.drawTexturePro(tx,
// // rl.Rectangle{.x = 0, .y = 0, .width = 32, .height = 32},
// // rl.Rectangle{.x = 100, .y = 100, .width = 500, .height = 500},
// // .{.x = 0, .y = 0},
// // 0,
// // .white);
// const font_size : i32 = 180;
// const text_size = rl.Vector2{.x = @floatFromInt(rl.measureText(connected_text, font_size)), .y = font_size};//rl.measureTextEx(try rl.getFontDefault(), connected_text, font_size, font_size / 10);
// const pos = rl.Vector2{.x = 0, .y = 0};
// rl.drawText(connected_text, pos.x, pos.y, font_size, .white);
// rl.drawRectangleLines(pos.x, pos.y, @intFromFloat(text_size.x), @intFromFloat(text_size.y), .red);
// rl.drawRectangle(pos.x, pos.y, rl.getScreenWidth(), rl.getScreenHeight(), .white);
// rl.drawTexturePro(tx,
// rl.Rectangle{.x = 0, .y = 0, .width = 32, .height = 32},
// rl.Rectangle{.x = 100, .y = 100, .width = 500, .height = 500},
// .{.x = 0, .y = 0},
// 0,
// .white);
// //rl.drawLineV(.{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = 0}, .{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = @floatFromInt(rl.getScreenHeight())}, .red);
//rl.drawLineV(.{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = 0}, .{.x = @floatFromInt(@divFloor(rl.getScreenWidth(), 2)), .y = @floatFromInt(rl.getScreenHeight())}, .red);
// //rl.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white);
// //rl.drawRectangleLines(0, 0, 100, 100, .red);
// // misc.drawFPS(0, 0, frame_time, frame);
//rl.drawText("Congrats! You created your first window!", rl.getMouseX(), rl.getMouseY(), 20, .white);
//rl.drawRectangleLines(0, 0, 100, 100, .red);
// misc.drawFPS(0, 0, frame_time, frame);
// //elf.draw();
//elf.draw();
// // rl.beginMode3D(camera);
// // rl.drawSphere(.{ .x = 0, .y = 0, .z = 0 }, 1, .red);
// // rl.endMode3D();
// rl.beginMode3D(camera);
// rl.drawSphere(.{ .x = 0, .y = 0, .z = 0 }, 1, .red);
// rl.endMode3D();
c.EndDrawing();
// rl.endDrawing();
rl.endDrawing();
// //----------------------------------------------------------------------------------
c.SwapScreenBuffer();
// rl.swapScreenBuffer();
rl.swapScreenBuffer();
}
}
@@ -365,222 +417,3 @@ fn connect() !void {
// };
// try std.testing.fuzz(Context{}, Context.testOne, .{});
// }
// const c = @cImport({
// @cInclude("raylib.h");
// });
// //------------------------------------------------------------------------------------
// // Program main entry point
// //------------------------------------------------------------------------------------
// pub fn main() !void {
// // Initialization
// //--------------------------------------------------------------------------------------
// const screenWidth = 800;
// const screenHeight = 450;
// c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_WINDOW_HIGHDPI);
// c.InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi testbed");
// var scaleDpi = c.GetWindowScaleDPI();
// var mousePos = c.GetMousePosition();
// var currentMonitor = c.GetCurrentMonitor();
// var windowPos = c.GetWindowPosition();
// const gridSpacing = 40; // Grid spacing in pixels
// c.SetTargetFPS(60);
// //--------------------------------------------------------------------------------------
// // Main game loop
// while (!c.WindowShouldClose()) // Detect window close button or ESC key
// {
// // Update
// //----------------------------------------------------------------------------------
// mousePos = c.GetMousePosition();
// currentMonitor = c.GetCurrentMonitor();
// scaleDpi = c.GetWindowScaleDPI();
// windowPos = c.GetWindowPosition();
// if (c.IsKeyPressed(c.KEY_SPACE)) c.ToggleBorderlessWindowed();
// if (c.IsKeyPressed(c.KEY_F)) c.ToggleFullscreen();
// //----------------------------------------------------------------------------------
// // Draw
// //----------------------------------------------------------------------------------
// c.BeginDrawing();
// c.ClearBackground(c.RAYWHITE);
// // Draw grid
// const screen_h : usize = @intCast(c.GetScreenHeight());
// const spacing : usize = @intCast(gridSpacing);
// var limit = screen_h / spacing + 1;
// for (0..limit) |h| {
// const y : i32 = @intCast(h * gridSpacing);
// c.DrawText(c.TextFormat("%02i", y), 4, y - 4, 10, c.GRAY);
// c.DrawLine(24, y, c.GetScreenWidth(), y, c.LIGHTGRAY);
// }
// const screen_v : usize = @intCast(c.GetScreenWidth());
// limit = screen_v / spacing + 1;
// for (0..limit) |v| {
// const x : i32 = @intCast(v * gridSpacing);
// c.DrawText(c.TextFormat("%02i", x), x - 10, 4, 10, c.GRAY);
// c.DrawLine(x, 20, x, c.GetScreenHeight(), c.LIGHTGRAY);
// }
// // Draw UI info
// c.DrawText(c.TextFormat("CURRENT MONITOR: %i/%i (%ix%i)", currentMonitor + 1, c.GetMonitorCount(),
// c.GetMonitorWidth(currentMonitor), c.GetMonitorHeight(currentMonitor)), 50, 50, 20, c.DARKGRAY);
// c.DrawText(c.TextFormat("WINDOW POSITION: %ix%i", windowPos.x, windowPos.y), 50, 90, 20, c.DARKGRAY);
// c.DrawText(c.TextFormat("SCREEN SIZE: %ix%i", c.GetScreenWidth(), c.GetScreenHeight()), 50, 130, 20, c.DARKGRAY);
// c.DrawText(c.TextFormat("RENDER SIZE: %ix%i", c.GetRenderWidth(), c.GetRenderHeight()), 50, 170, 20, c.DARKGRAY);
// c.DrawText(c.TextFormat("SCALE FACTOR: %.1fx%.1f", scaleDpi.x, scaleDpi.y), 50, 210, 20, c.GRAY);
// // Draw reference rectangles, top-left and bottom-right corners
// c.DrawRectangle(0, 0, 30, 60, c.RED);
// c.DrawRectangle(c.GetScreenWidth() - 30, c.GetScreenHeight() - 60, 30, 60, c.BLUE);
// // Draw mouse position
// c.DrawCircleV(c.GetMousePosition(), 20, c.MAROON);
// c.DrawRectangle(@intFromFloat(mousePos.x - 25), @intFromFloat(mousePos.y), 50, 2, c.BLACK);
// c.DrawRectangle(@intFromFloat(mousePos.x), @intFromFloat(mousePos.y - 25), 2, 50, c.BLACK);
// c.DrawText(c.TextFormat("[%i,%i]", c.GetMouseX(), c.GetMouseY()), @intFromFloat(mousePos.x - 44),
// if (mousePos.y > @as(f32, @floatFromInt(c.GetScreenHeight())) - 60) @intFromFloat(mousePos.y - 46) else @intFromFloat(mousePos.y + 30), 20, c.BLACK);
// c.EndDrawing();
// //----------------------------------------------------------------------------------
// }
// // De-Initialization
// //--------------------------------------------------------------------------------------
// // TODO: Unload all loaded resources at this point
// c.CloseWindow(); // Close window and OpenGL context
// //--------------------------------------------------------------------------------------
// return;
// }
// const c = @cImport({
// @cInclude("raylib.h");
// });
// //------------------------------------------------------------------------------------
// // Program main entry point
// //------------------------------------------------------------------------------------
// pub fn main() void {
// // Initialization
// //--------------------------------------------------------------------------------------
// const screenWidth = 800;
// const screenHeight = 450;
// c.SetConfigFlags(c.FLAG_WINDOW_HIGHDPI | c.FLAG_WINDOW_RESIZABLE);
// c.InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi demo");
// c.SetWindowMinSize(450, 450);
// const logicalGridDescY = 120;
// const logicalGridLabelY = logicalGridDescY + 30;
// const logicalGridTop = logicalGridLabelY + 30;
// const logicalGridBottom = logicalGridTop + 80;
// const pixelGridTop = logicalGridBottom - 20;
// const pixelGridBottom = pixelGridTop + 80;
// const pixelGridLabelY = pixelGridBottom + 30;
// const pixelGridDescY = pixelGridLabelY + 30;
// const cellSize = 50;
// var cellSizePx : f32 = @floatFromInt(cellSize);
// c.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
// //--------------------------------------------------------------------------------------
// // Main game loop
// while (!c.WindowShouldClose()) // Detect window close button or ESC key
// {
// // Update
// //----------------------------------------------------------------------------------
// const monitorCount = c.GetMonitorCount();
// if ((monitorCount > 1) and c.IsKeyPressed(c.KEY_N)) {
// c.SetWindowMonitor(@mod((c.GetCurrentMonitor() + 1), monitorCount));
// }
// const currentMonitor = c.GetCurrentMonitor();
// const dpiScale = c.GetWindowScaleDPI();
// cellSizePx = (@as(f32, @floatFromInt(cellSize)))/dpiScale.x;
// //----------------------------------------------------------------------------------
// // Draw
// //----------------------------------------------------------------------------------
// c.BeginDrawing();
// c.ClearBackground(c.RAYWHITE);
// const windowCenter = @divFloor(c.GetScreenWidth(), 2);
// DrawTextCenter(c.TextFormat("Dpi Scale: %f", dpiScale.x), windowCenter, 30, 40, c.DARKGRAY);
// DrawTextCenter(c.TextFormat("Monitor: %d/%d ([N] next monitor)", currentMonitor+1, monitorCount), windowCenter, 70, 20, c.LIGHTGRAY);
// DrawTextCenter(c.TextFormat("Window is %d \"logical points\" wide", c.GetScreenWidth()), windowCenter, logicalGridDescY, 20, c.ORANGE);
// var odd = true;
// var i : i32 = cellSize;
// while (i < c.GetScreenWidth()) {
// // for (int i = cellSize; i < GetScreenWidth(); i += cellSize, odd = !odd) {
// if (odd) c.DrawRectangle(i, logicalGridTop, cellSize, logicalGridBottom-logicalGridTop, c.ORANGE);
// DrawTextCenter(c.TextFormat("%d", i), i, logicalGridLabelY, 10, c.LIGHTGRAY);
// c.DrawLine(i, logicalGridLabelY + 10, i, logicalGridBottom, c.GRAY);
// i += cellSize;
// odd = !odd;
// }
// odd = true;
// const minTextSpace = 30;
// var lastTextX : i32 = -minTextSpace;
// i = cellSize;
// while (i < c.GetRenderWidth()) {
// // for (int i = cellSize; i < GetRenderWidth(); i += cellSize, odd = !odd) {
// const x : i32 = @intFromFloat(@as(f32, @floatFromInt(i)) / dpiScale.x);
// if (odd) c.DrawRectangle(x, pixelGridTop, @intFromFloat(cellSizePx), pixelGridBottom - pixelGridTop, c.Color{ .r = 0, .g = 121, .b = 241, .a = 100 });
// c.DrawLine(x, pixelGridTop, @intFromFloat(@as(f32, @floatFromInt(i)) / dpiScale.x), pixelGridLabelY - 10, c.GRAY);
// if ((x - lastTextX) >= minTextSpace)
// {
// DrawTextCenter(c.TextFormat("%d", i), x, pixelGridLabelY, 10, c.LIGHTGRAY);
// lastTextX = x;
// }
// i += cellSize;
// odd = !odd;
// }
// DrawTextCenter(c.TextFormat("Window is %d \"physical pixels\" wide", c.GetRenderWidth()), windowCenter, pixelGridDescY, 20, c.BLUE);
// const text = "Can you see this?";
// const size = c.MeasureTextEx(c.GetFontDefault(), text, 20, 3);
// const pos = c.Vector2{ .x = @as(f32, @floatFromInt(c.GetScreenWidth())) - size.x - 5, .y = @as(f32, @floatFromInt(c.GetScreenHeight())) - size.y - 5 };
// c.DrawTextEx(c.GetFontDefault(), text, pos, 20, 3, c.LIGHTGRAY);
// c.EndDrawing();
// //----------------------------------------------------------------------------------
// }
// // De-Initialization
// //--------------------------------------------------------------------------------------
// c.CloseWindow(); // Close window and OpenGL context
// //--------------------------------------------------------------------------------------
// }
// //------------------------------------------------------------------------------------
// // Module Functions Definition
// //------------------------------------------------------------------------------------
// fn DrawTextCenter(text : [*:0]const u8, x : i32, y: i32, fontSize : i32, color: c.Color) void {
// // fn DrawTextCenter(const char *text, int x, int y, int fontSize, Color color) void {
// const size = c.MeasureTextEx(c.GetFontDefault(), text, @floatFromInt(fontSize), 3);
// const pos = c.Vector2{ .x = @as(f32, @floatFromInt(x)) - size.x/2, .y = @as(f32, @floatFromInt(y)) - size.y/2 };
// c.DrawTextEx(c.GetFontDefault(), text, pos, @as(f32, @floatFromInt(fontSize)), 3, color);
// }
+9 -7
View File
@@ -1,4 +1,5 @@
const rl = @import("raylib");
const c = @import("c.zig").c;
// const rl = @import("raylib");
const FPS_CAPTURE_FRAMES_COUNT : i32 = 30; // 30 captures
const FPS_AVERAGE_TIME_SECONDS : f32 = 0.5; // 500 milliseconds
@@ -10,13 +11,14 @@ var average : f32 = 0;
var last : f32 = 0;
pub fn drawFPS(posX : i32, posY : i32, fpsFrame : f32, frame : i32) void {
var color : rl.Color = .lime; // Good FPS
var color : c.Color = c.LIME;
// var color : rl.Color = .lime; // Good FPS
const fps : i32 = getFPS(fpsFrame, frame);
if ((fps < 30) and (fps >= 15)) {color = .orange;} // Warning FPS
else if (fps < 15) color = .red; // Low FPS
if ((fps < 30) and (fps >= 15)) {color = c.ORANGE;} // Warning FPS
else if (fps < 15) color = c.RED; // Low FPS
rl.drawText(rl.textFormat("%2i FPS", .{fps}), posX, posY, 20, color);
c.DrawText(c.TextFormat("%2i FPS", fps), posX, posY, 20, color);
}
pub fn getFPS(fpsFrame : f32, frame : i32) i32 {
@@ -34,9 +36,9 @@ pub fn getFPS(fpsFrame : f32, frame : i32) i32 {
if (fpsFrame == 0) return 0;
if ((rl.getTime() - last) > FPS_STEP)
if ((c.GetTime() - last) > FPS_STEP)
{
last = @floatCast(rl.getTime());
last = @floatCast(c.GetTime());
index = (index + 1) % FPS_CAPTURE_FRAMES_COUNT;
average -= history[index];
history[index] = fpsFrame / FPS_CAPTURE_FRAMES_COUNT;