r/eli5_programming • u/4r73m190r0s • 3d ago
[ELI5] If source code is platform-agnostic, why computer games are not compiled for both Windows and Linux?
3
u/Technical_Goose_8160 3d ago
Code has to be turned into machine language when we compile it. Different systems speak different languages, even hardware can affect it.
Java is hardware agnostic, so every machine needs a Java virtual machine that will turn it into machine language when the code runs.
This is very useful when developing for disparate hardware, Java is very popular with medical machines. But, compiling when it runs is slower and video games can be pretty heavy.
2
u/Doug2825 3d ago
This misses the question. Linux and Windows are usually in the same hardware. The machine code is the same. The problem is the commands the OS supports are different.
3
u/shadow-battle-crab 3d ago edited 3d ago
Source code isn't totally platform agnostic.
Most of a program is written in a generic way, but the parts that actually communicate to the screen, use the keyboard, etc are written specifically for whatever environment it is designed to run in.
Lets take a video game. So you make a car racing game. Does that game use a game controller? A keyboard? A touch screen? A racing wheel? You, as the game programmer have to design a special connector for your games software for each kind of thing it connects to.
Operating systems are just another kind of connector, because each has its quirks. On windows, you need a different kind of installer than linux, you also need a different kind of way to interface with the windows manager, a different kind of way to accessing the GPU, etc. Where the save game files and temporary files go is different in each environment. How a "Save As" dialog works is different for each environment. How do those screens even work on a game console? How does a switch work differently than a PC?
Modern game engines like unreal and unity do a lot of the heavy lifting of this stuff for you, but not all of it.
Each one of these things requires an extra layer of work, and testing - and if you know your market is going to be mostly people that play the game on Windows, it's easy to decide to not want to do this work.
2
u/wiggle_room1337 3d ago
Even stuff like "what time is it right now" needs some sort of compatibility layer.
3
u/mxldevs 2d ago
Source code isn't platform agnostic.
If your code requires windows libraries to run for example, then it's not going to compile on other platforms unless you provide the required libraries.
You can certainly write code in a way that's more platform agnostic, but it's not the fact that it's code that makes it that way.
2
u/Polyxeno 3d ago
My games are, because I use a cross-platform library: OpenFrameworks.
Pretty much all of the platform-specific work is already done in OpenFrameworks, and wrapped in library functions, so my code uses those functions and does not need to change per platform.
I can build the same game for Linux, Mac, Windows, iOS, or Android, and there are rarely any serious technical issues in doing so . . . BUT the Google Play, Apple, Windows, and Steam platforms all have annoying hurdles to face, and testing to do. So I tend to skip iOS, and Mac, and will probably never do Android again, for those reasons.
2
u/Odds-and-Ns 2d ago
Source code isn’t necessarily platform agnostic in general. It can be, and Id argue it usually should be, but it takes active consideration to make it that way
1
u/Key_River7180 3d ago
Win32, basically we cant agree on how to make a window and put pixels into it with the gpu between platforms.
Read XKCD 927: https://xkcd.com/927/
1
u/mredding 3d ago
Source code CAN be, platform specific APIs aren't inherently.
If you need memory, you need to ask the OS, and how you get memory from an OS depends on THAT OS.
The next best thing you can do is compile the code following some platform agnostic API, and you implement the platform specifics behind the API. That's a lot of what software tries to do.
But video games try to squeeze every last bit of performance out of a machine, and that means you cut the bullshit and get platform specific. You would sacrifice universality for performance.
1
1
u/Frequent-Sir-6942 3d ago
Neither source code nor binaries are platform-agnostic in the general case.
1
u/Lonely-Restaurant986 3d ago
How the computer reads the code is largely the same.
Windows developers at Microsoft say “Let’s write this function to allocate memory”. They create that standard (windows api). When a game dev writes code they use the standards the windows developers created.
Linux developers say “Let’s write this function to write memory”. So all Linux apps use that standard.
So the problem isn’t the code, the issue is how that code interacts with the hardware.
So when you run a windows program, the code says, “Run this windows function in the windows api” but Linux has no idea what that is or how to do it.
This has been largely fixed by compatibility layers like wine. But kernel level code is where the issues lie.
There’s more nuance to it but that’s simply put.
1
u/Technical_Ideal_5439 3d ago
Source code is not platform agnostic, most code is written for a platform with specific parts of it written to deal with platform differences.
Sure you can write some code as platform ignostic but as soon as you start needing hardware, code normally gets locked into the platform unless you are very careful.
1
u/NZAuthor 3d ago
Source code is not fully platform-agnostic. I'd say 99% of it is unless you are playing around with memory on a very technical level.
The other 1% is for edge cases and could be anything from specific libraries being used that aren't universal, very specific low level language commands, or simply how characters work with the file system. It could be anything simple or complex.
But yes, this CAN be easily worked around most of the time. The developer just needs to decide if it's worth doing or not.
More often then not, it's the choice early on to use a windows specific library for drawing graphics that becomes a barrier.
1
1
u/Pale_Height_1251 3d ago
Source code is often platform agnostic but games can be a sore point because GPU shaders are often not platforms agnostic, i.e they are different on Windows, Nintendo, Mac etc. The CPU stuff is much the same, but the GPU stuff often isn't.
1
u/OpeningQuail2483 3d ago
if it is platform agnostic then it compiles and ships for both, if no then no
1
u/bubba-yo 3d ago
I don't think we've had platform agnostic games since Nethack.
But every platform has unique APIs. Linux doesn't have DirectX. Mac uses Metal.
Every platform has different bottlenecks which requires different coding solutions for performance. The Mac has faster single core performance and faster RAM and storage, but slower GPUs. Linux can be 20% faster than Windows for memory read/write. Windows threading is much slower. So a game that is performant on Windows may not be on Linux or Mac without changes, breaking the platform agnosticism.
And every target requires testing, so going from one platform to two doubles your testing overhead, and requires a level of expertise from your team to target it. It's why there were porting houses full of platform experts to figure out how to get that Windows game to run on Linux or Mac.
1
u/grizzlor_ 2d ago
>Linux doesn't have DirectX.
It actually does these days via Proton’s translation layers: DXVK for DirectX 9-11 and vkd3d-proton for DX12. Translates the calls to Vulkan in real time.
It’s pretty amazing honestly — basically every Windows game just runs on Linux now (with the exception of ones using kernel level anticheat).
This also really makes it unnecessary to do Linux ports of any game. Even games that do have native Linux ports tend to work better using their Windows version under Proton.
1
u/bubba-yo 2d ago
No, it doesn't, because that still needs to be tested for. You might say it's good enough, but it's not going to be good enough for Rockstar to promise the $80 game will work.
1
u/grizzlor_ 2d ago
Testing the win32 version under Proton is a lot easier than developing and maintaining a Linux port. Plus there’s already a comprehensive community database of Proton compatibility reports.
Steam runs just fine on Linux — Valve has no problem selling Windows games to Linux users. Their gaming hardware (SteamDeck, SteamMachine) runs Linux.
1
u/bubba-yo 2d ago
Testing costs the same. It might be easier to make it work, but you still need the build chain.
1
u/vitimiti 3d ago
It takes effort. You have to use cross platform libraries that your team may not be used to if they are used to Windows only. Then each platform has particular quirks that may bite you back. To make matters more difficult, Windows is very backwards compatible, while Linux desktops aren't due to the dependence on glib (the equivalent to the Windows Runtime in a way, you need it) which doesn't have a "don't break user space" policy but rather "Welp, you should upgrade your build then" policy. Moreover, each compiler (on Windows you probably use MSVC while on Linux you probably use GCC) has its peculiarities.
All in all, companies just don't consider Linux support profitable enough, especially because Linux users are VERY used to creating bug reports instead of restarting or ignoring problems, so most bug reports will come from the smallest user base (and this has happened before), increasing workload for a tiny market share.
Basically, it's about money, investment Vs benefit
1
u/fiddle_styx 3d ago
Not all source code is platform-agnostic
Games are often compiled for both Windows and Linux (and macos, web, android, etc)
1
u/tecnofauno 3d ago
Source code Is never platform agnostic. It can be portable when simple, and games are not simple. It can be multiplatform, but needs extra effort.
1
u/Quick-Penalty4883 3d ago
A book doesn't care what language it is written in, but for you to understand it it needs to be translated to a language you do understand
1
u/Bo_Jim 2d ago
The core of the programming language is platform independent. System services, like 3D rendering and audio mixing, are very much system dependent. Each operating system has it's own programming interface for accessing those services. A significant portion of any game's code is setting up for those system calls, and those calls are not portable between systems.
Games intended to be ported to multiple platforms usually use an intermediate API that is consistent to the application on every platform, and connects to the platform-specific OS services on each different platform, like OpenGL or Vulkan. They still might require some platform-specific system calls, but these can usually be surrounded by conditionals so that the proper code is compiled on each platform.
Text based applications that use standard libraries are generally fully portable at the source level. A large percentage of the console tools used on Linux are ported directly from sources originally written for Unix.
1
u/fltfathin 2d ago
There is lots of game engine and languages so let's start on what is source code, let's call code for short
Code is like cooking recipes, it can be written in different language, say american or spanish, human understand those
But the one cooking is not human, but some kind of robot, for the recipe to be run correctly by the robot, we need to translate it to robot language. There are many kinds of robots with different cooking appliances available to them.
This is the part that usually get stuck, because the different kind of appliance and tools, the food doesn't always make the same taste around. So the human making the recipe decide that they only support one kind of robot. Because adjusting the taste on multiple robot is expensive and change one affect the other.
Sometimes the are cookbook have the same base cooking flow, imagine the recipe about cakes have common baking recipes, and can work on all robots, sometimes the maker just hate one type of robot and refuse to allow people to translate to that robot, despite there is no adjustments needed just one checklist to click. Oh they also refuse to acknowledge when someone can make one robot acts like the other citing that food is gonna be used for poisoning others.
1
u/henke37 2d ago
One thing the others neglect to mention is hardware differences. While things are relatively same these days with "everything" being derived from "IBM compatible" machines, it was a much bigger mess back in the day. While others have pointed out api differences, sometimes there were fundamental design differences in the hardware itself.
Consider a platform such as the GBA, that thing did not have general purpose graphics hardware with a frame buffer. Instead it had exactly four tilemap layers and 128 "objects". It also didn't have a keyboard, it had a d-pad and some buttons.
Modern operating systems can generally abstract away these differences today, but on more limited systems, which might not even have an OS, it was much more visible to the application programmer.
7
u/pixel293 3d ago
The APIs to access the screen, i.e. the GPU are different. Also the file-system structure between Windows, Linux, and MacOS is different. There might also be 3rd party services like video codecs that are available on one platform by default but not on another platform.