Skip to content

Support building with MSVC#95

Open
BlueGradientHorizon wants to merge 1 commit intop2r3:mainfrom
BlueGradientHorizon:msvc-support
Open

Support building with MSVC#95
BlueGradientHorizon wants to merge 1 commit intop2r3:mainfrom
BlueGradientHorizon:msvc-support

Conversation

@BlueGradientHorizon
Copy link
Copy Markdown

@BlueGradientHorizon BlueGradientHorizon commented Sep 15, 2025

Pretty self-explanatory. What I did:

  • removed unistd.h include in all files other than globals.h and wrapped it in #ifndef _MSC_VER;
  • added SSIZE_T typedef for compatibility;
  • added port of POSIX' clock_gettime() - clock_gettime_monotonic();
  • wrote a building instruction in README;
  • wrote a simple powershell build script.

Only 2 warnings of type C4098 (A function declared with return type void has a return statement that returns a value. The compiler assumes the function returns a value of type int.) are currently issued:

} else return getSmeltingOutput(player);

return handleFluidMovement(x, y - 1, z, fluid, fluid);

I'm not much a C dev (or just dev), so sorry if I did somethin' wrong, but it works at least :).

@p2r3
Copy link
Copy Markdown
Owner

p2r3 commented Sep 16, 2025

As the contribution guidelines state, create an issue and discuss before submitting a pull request.

Did we really need a fifth method of compilation for Windows? At this point, what's our primary target - microcontrollers, or desktop machines that require 8GB RAM just to run a browser?

Over half of our pull requests are compiler tweaks and tooling changes. Not actual enhancements, bugfixes, or features. It's starting to piss me off. That said, I'll still look at your commit.

Comment thread src/tools.c
Comment on lines +242 to +274
#ifdef _MSC_VER
// https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows/51974214#51974214
#define MS_PER_SEC 1000ULL // MS = milliseconds
#define US_PER_MS 1000ULL // US = microseconds
#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns)
#define NS_PER_US 1000ULL

#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US)
#define NS_PER_HNS (100ULL) // NS = nanoseconds
#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US)

int clock_gettime_monotonic(struct timespec *tv)
{
static LARGE_INTEGER ticksPerSec;
LARGE_INTEGER ticks;

if (!ticksPerSec.QuadPart) {
QueryPerformanceFrequency(&ticksPerSec);
if (!ticksPerSec.QuadPart) {
errno = ENOTSUP;
return -1;
}
}

QueryPerformanceCounter(&ticks);

tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart);
tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * NS_PER_SEC) / ticksPerSec.QuadPart);

return 0;
}
#endif

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reimplementing clock_gettime specifically, you might as well just provide a Windows-specific get_program_time implementation. clock_gettime on its own isn't used anywhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants