Skip to content

Commit

Permalink
Replace print/println with write/writeln
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSmith33 committed Aug 19, 2023
1 parent 4bef353 commit cd7d09d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 280 deletions.
17 changes: 17 additions & 0 deletions plugins/core/src/core/format.vx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@ module core.format;

import core.kernel32;

void write[Args...](Args... args) {
FormatSpec spec;
#foreach(i, arg; args) {
alias func = selectPrintFunc(Args[i]);
func(&writeString, arg, spec);
}
}

void writeln[Args...](Args... args) {
FormatSpec spec;
#foreach(i, arg; args) {
alias func = selectPrintFunc(Args[i]);
func(&writeString, arg, spec);
}
writeString("\n");
}

void writefln[Args...](u8[] fmt, Args... args) {
formattedWrite(&writeString, fmt, args);
writeString("\n");
Expand Down
188 changes: 3 additions & 185 deletions plugins/core/src/core/utils.vx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import core.kernel32;
import core.mimalloc;
import core.lz4;
import core.host;
import core.format;

void __init_utils() {
QueryPerformanceFrequency(&_ticksPerSecond);
Expand Down Expand Up @@ -118,7 +119,7 @@ Status readFile(u8[] filename, u8[]* result) {

if (hFile == INVALID_HANDLE_VALUE) {
u8* message = getLastErrorMsg();
print("[ERROR] Failed to read `", filename, "` ", message.fromStringz);
write("[ERROR] Failed to read `", filename, "` ", message.fromStringz);
freeLastErrorMsg(message);
return Status.ERR;
}
Expand All @@ -141,7 +142,7 @@ Status readFile(u8[] filename, u8[]* result) {
if (!ReadFile(hFile, (*result).ptr, cast(u32)size, &bytesRead, null))
{
u8* message = getLastErrorMsg();
print("Failed to read ", filename, " ", message.fromStringz);
write("Failed to read ", filename, " ", message.fromStringz);
freeLastErrorMsg(message);

freeArray[u8](*result);
Expand Down Expand Up @@ -312,192 +313,9 @@ T nextPOT[T](T x)
return x;
}

void printString(u8[] str) {
void* handle = GetStdHandle(STD_OUTPUT_HANDLE);
//SetConsoleOutputCP(65001);
u32 numWritten;
WriteFile(
handle,
str.ptr,
cast(u32)str.length,
&numWritten,
null);
}

void printBool(bool b) #inline {
if (b) printString("true");
else printString("false");
}

$alias selectPrintFunc($type T) {
if ($isInteger(T)) {
if (T == u64) return printUint;
return printInt;
}
if (T == f32) return print_f32;
if (T == f64) return print_f64;
if ($isSlice(T)) return printString;
if ($isPointer(T)) return printPtr;
if (T == bool) return printBool;
$compileError("selectPrintFunc: Invalid type");
}

void print[Args...](Args... args) {
#foreach(i, arg; args) {
alias func = selectPrintFunc(Args[i]);
func(arg);
}
}

void println[Args...](Args... args) {
#foreach(i, arg; args) {
alias func = selectPrintFunc(Args[i]);
func(arg);
}
printString("\n");
}

void printArray[T](T[] array)
{
u64 len = array.length;
print("[");
for (u64 i = 0; i < len; ++i)
{
if (i > 0)
print(", ");
print(array[i]);
}
print("]");
}

void setRangeu8(u8[] slice, u8 value) {
for (u64 i = 0; i < slice.length; ++i) {
slice[i] = value;
}
}

enum INT_BUF_SIZE = 66;
void printInt(i64 i, u32 minSize = 1, u8 base = 10)
{
u8[INT_BUF_SIZE] buf;
u8[] res = formatInt(i, &buf, minSize, base);
printString(res);
}
void printUint(u64 i, u32 minSize = 1, u8 base = 10)
{
u8[INT_BUF_SIZE] buf;
u8[] res = formatUint(i, &buf, minSize, base);
printString(res);
}

void print_f32(f32 val)
{
u8[64] buf;
u8[] res = format_f32(&buf, val);
printString(res);
}

void print_f64(f64 val)
{
u8[64] buf;
u8[] res = format_f64(&buf, val);
printString(res);
}

@extern(module, "host") u8[] format_f32(u8[64]* buf, f32 f);
@extern(module, "host") u8[] format_f64(u8[64]* buf, f64 f);

void printPtr(void* ptr) #inline
{
printString("0x");
printUint(cast(u64)ptr, 16, 16);
}

u8[] formatInt(i64 i, u8[INT_BUF_SIZE]* output, u32 minSize, u8 base)
{
u32 numDigits = 0;
if (i == 0)
{
if (minSize == 0)
minSize = 1;
setRangeu8((*output)[INT_BUF_SIZE - minSize..INT_BUF_SIZE], ' ');
(*output)[INT_BUF_SIZE-1] = '0';
numDigits = minSize;
}
else
{
bool neg = i < 0;
if (neg) {
i = -i;
}
bool overflow = i < 0;
if (overflow)
i = i64.max;

if (base <= 10) {
while (i) {
u8 c = cast(u8)('0' + i % base);
(*output)[INT_BUF_SIZE - ++numDigits] = c;
i /= base;
}
} else {
while (i) {
u8 c = hexDigitsUpper[i % base];
(*output)[INT_BUF_SIZE - ++numDigits] = c;
i /= base;
}
}

while (numDigits < minSize) {
(*output)[INT_BUF_SIZE - ++numDigits] = ' ';
}

if (neg) {
(*output)[INT_BUF_SIZE - ++numDigits] = '-';
}
if (overflow) {
++(*output)[INT_BUF_SIZE-1];
}
}
return (*output)[INT_BUF_SIZE - numDigits..INT_BUF_SIZE];
}

u8[] formatUint(u64 i, u8[INT_BUF_SIZE]* output, u32 minSize, u8 base)
{
u32 numDigits = 0;
if (i == 0)
{
if (minSize == 0)
minSize = 1;
setRangeu8((*output)[INT_BUF_SIZE - minSize..INT_BUF_SIZE], ' ');
(*output)[INT_BUF_SIZE-1] = '0';
numDigits = minSize;
}
else
{
if (base <= 10) {
while (i) {
u8 c = cast(u8)('0' + i % base);
(*output)[INT_BUF_SIZE - ++numDigits] = c;
i /= base;
}
} else {
while (i) {
u8 c = hexDigitsUpper[i % base];
(*output)[INT_BUF_SIZE - ++numDigits] = c;
i /= base;
}
}

while (numDigits < minSize) {
(*output)[INT_BUF_SIZE - ++numDigits] = ' ';
}
}
return (*output)[INT_BUF_SIZE - numDigits..INT_BUF_SIZE];
}

u8[] hexDigitsUpper = "0123456789ABCDEF";

i64 cstrlen(u8* str) {
if (str == null) return 0;

Expand Down
15 changes: 8 additions & 7 deletions plugins/core/src/core/vector.vx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module core.vector;

import core.format;
import core.math;
import core.utils;

Expand Down Expand Up @@ -115,18 +116,18 @@ mat4 identityMatrix()
}

void print_mat4(mat4 m) {
println("(", m[ 0], " ", m[ 1], " ", m[ 2], " ", m[ 3]);
println(" ", m[ 4], " ", m[ 5], " ", m[ 6], " ", m[ 7]);
println(" ", m[ 8], " ", m[ 9], " ", m[10], " ", m[11]);
println(" ", m[12], " ", m[13], " ", m[14], " ", m[15], ")");
writeln("(", m[ 0], " ", m[ 1], " ", m[ 2], " ", m[ 3]);
writeln(" ", m[ 4], " ", m[ 5], " ", m[ 6], " ", m[ 7]);
writeln(" ", m[ 8], " ", m[ 9], " ", m[10], " ", m[11]);
writeln(" ", m[12], " ", m[13], " ", m[14], " ", m[15], ")");
}

void print_vec3(vec3 v) {
println("(", v.x, " ", v.y, " ", v.z, ")");
writeln("(", v.x, " ", v.y, " ", v.z, ")");
}

void print_vec4(vec4 v) {
println("(", v.x, " ", v.y, " ", v.z, " ", v.w, ")");
writeln("(", v.x, " ", v.y, " ", v.z, " ", v.w, ")");
}

// up is the direction of cameras +Y axis
Expand Down Expand Up @@ -168,7 +169,7 @@ mat4 lookAtMatrix(vec3 camera_pos, vec3 target, vec3 up)
//print_vec3(x_axis);
//print_vec3(y_axis);
//print_vec3(z_axis);
//println;
//writeln;
//print_vec3(camera_pos);
//print_vec3(offset);

Expand Down
Loading

0 comments on commit cd7d09d

Please sign in to comment.