diff options
Diffstat (limited to 'src/misc.zig')
-rw-r--r-- | src/misc.zig | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/src/misc.zig b/src/misc.zig index d6063eb..6f1d893 100644 --- a/src/misc.zig +++ b/src/misc.zig @@ -1,4 +1,5 @@ // Miscellaneous functions +// Copyright (C) 2002 David Rosen // Copyright (C) 2021-2023 Nguyễn Gia Phong // // This file is part of Black Shades. @@ -27,6 +28,7 @@ const endsWith = std.mem.endsWith; const eql = std.mem.eql; const free = std.c.free; const join = std.fs.path.joinZ; +const len = std.mem.len; const maxInt = std.math.maxInt; const parseFloat = std.fmt.parseFloat; const parseInt = std.fmt.parseInt; @@ -375,6 +377,77 @@ pub fn saveScores(base_dir: []const u8, current: Scores) !void { try dir.writeFile("scores.ini", data); } +const Text = struct { + texture: u32, + base: u32, +}; + +export fn buildFont(text: *Text) void { + text.base = c.glGenLists(256); + c.glBindTexture(c.GL_TEXTURE_2D, text.texture); + var i = @as(u16, 0); + while (i < 256) : (i += 1) { + // Character coords + const x = @intToFloat(f32, i % 16) / 16.0; + const y = @intToFloat(f32, i / 16) / 16.0; + c.glNewList(text.base + i, c.GL_COMPILE); + // Use A Quad For Each Character + c.glBegin(c.GL_QUADS); + // Bottom left + c.glTexCoord2f(x, 1 - y - 0.0625 + 0.001); + c.glVertex2i(0,0); + // Bottom right + c.glTexCoord2f(x + 0.0625, 1 - y - 0.0625 + 0.001); + c.glVertex2i(16, 0); + // Top right + c.glTexCoord2f(x + 0.0625, 1 - y - 0.001); + c.glVertex2i(16, 16); + // Top left + c.glTexCoord2f(x, 1 - y - 0.001); + c.glVertex2i(0, 16); + c.glEnd(); + // Move to the right of the character + c.glTranslated(10, 0, 0); + c.glEndList(); + } +} + +export fn glPrint(text: *const Text, x: f64, y: f64, str: [*:0]const u8, + set: bool, size: f32, width: f32, height: f32) void { + c.glTexEnvi(c.GL_TEXTURE_ENV, c.GL_TEXTURE_ENV_MODE, c.GL_MODULATE); + c.glBindTexture(c.GL_TEXTURE_2D, text.texture); + c.glDisable(c.GL_DEPTH_TEST); + c.glDisable(c.GL_LIGHTING); + c.glEnable(c.GL_BLEND); + c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA); + c.glMatrixMode(c.GL_PROJECTION); + c.glPushMatrix(); + c.glLoadIdentity(); + c.glOrtho(0, width, 0, height, -100, 100); + c.glMatrixMode(c.GL_MODELVIEW); + c.glPushMatrix(); + c.glLoadIdentity(); + + c.glScalef(size, size, 1); + // Position the text (0,0 - bottom left) + c.glTranslated(x, y, 0); + // Choose the font set (0 or 1) + if (set) + c.glListBase(text.base + 128 - 32) + else + c.glListBase(text.base -% 32); + // Write to screen + c.glCallLists(@intCast(c_int, len(str)), c.GL_BYTE, str); + + // Restore + c.glMatrixMode(c.GL_PROJECTION); + c.glPopMatrix(); + c.glMatrixMode(c.GL_MODELVIEW); + c.glPopMatrix(); + c.glEnable(c.GL_DEPTH_TEST); + c.glTexEnvi(c.GL_TEXTURE_ENV, c.GL_TEXTURE_ENV_MODE, c.GL_MODULATE); +} + /// OpenGL fog state. const Fog = extern struct { color: [4]f32, @@ -394,7 +467,7 @@ export fn setFog(fog: *Fog, r: f32, g: f32, b: f32, } /// Set temporary fog effect. -export fn tempFog(fog: *Fog, r: f32, g: f32, b: f32) void { +export fn tempFog(fog: *const Fog, r: f32, g: f32, b: f32) void { const color = [4]f32{r, g, b, 1.0}; c.glFogi(c.GL_FOG_MODE, c.GL_LINEAR); c.glFogfv(c.GL_FOG_COLOR, &color); @@ -406,7 +479,7 @@ export fn tempFog(fog: *Fog, r: f32, g: f32, b: f32) void { } /// Reset fog effect. -export fn resetFog(fog: *Fog) void { +export fn resetFog(fog: *const Fog) void { c.glFogi(c.GL_FOG_MODE, c.GL_LINEAR); c.glFogfv(c.GL_FOG_COLOR, &fog.color); c.glFogf(c.GL_FOG_DENSITY, fog.density); |