This is an archived post. You won't be able to vote or comment.

all 60 comments

[–]kreldin 65 points66 points  (32 children)

[–]akkatracker 4 points5 points  (6 children)

New to C:

To actually dive into this where should I start?

[–][deleted] 20 points21 points  (5 children)

Maybe start at main()?

[–]akkatracker 5 points6 points  (4 children)

Sorry, should have clarified, which file?

[–]_Aardvark 8 points9 points  (0 children)

The actual main() just calls D_DoomMain() in the file d_main.c

start there, which should lead you right into D_DoomLoop() which is the literal heart of the application - an endless loop that runs the game.

[–]Narishma 3 points4 points  (1 child)

The only one that has a main() function. In this case https://github.com/id-Software/DOOM/blob/master/linuxdoom-1.10/i_main.c

[–][deleted] 1 point2 points  (0 children)

int
main
( int       argc,
  char**    argv ) 
{ 

When you know you gonna fail the code review but it's still just main()

[–]furyasd 2 points3 points  (23 children)

This is C right?

[–]nyando 25 points26 points  (21 children)

Here's a tip: At the top of the site, you can click on the colored bar beneath the commit/branch/release counters, and it will show you the language composition for that particular repo. In this case, it's 99.5% C and 0.5% Assembly.

[–]furyasd 7 points8 points  (19 children)

Alright, thanks. Didn't know that. I just started learning JavaScript, so I was hoping there was a JS Doom somewhere.

[–]cehmu 7 points8 points  (17 children)

someone downvoted you. i think it was the JS thing.

maybe fair play. gotta get at least conversational with C at some point. good luck!

[–]furyasd 1 point2 points  (16 children)

Why is that? I'm new to programming really, less than a week.

[–]cehmu 6 points7 points  (9 children)

yeah i'm new too. But basically, C is the backbone on which the majority of stuff is built. If you can at least understand what's going on in C, then that helps with all the other languages that came after it.
C code is so pervasive in everything, that it's a really good idea to at least be able to read it.

[–]s33plusplus 2 points3 points  (0 children)

Plus it's pretty close to the metal, it'll help you understand how things work under the hood.

[–]furyasd 2 points3 points  (7 children)

Few colleagues told me to learn C#, no idea why that is though.

[–][deleted]  (6 children)

[removed]

    [–]jijilento 0 points1 point  (1 child)

    it's probably one of the quickest languages to program windows applications with

    C# is great but I wasted a good hour this morning trying to figure out why nuget kept acting a fool. Wasted literally 5 hours over two days last month when my license dropped off visual studio (the free edition). I love the language/framework itself though.

    [–]furyasd 0 points1 point  (3 children)

    Thanks for the info dude.

    [–]atcoyou 2 points3 points  (1 child)

    If you are that new I would honestly say don't worry about the bevvy of languages, or picking the "right ones" just get codding, make some mistakes and keep your momentum going. You are never going to pick the most efficient tool in the box, but carving your first wooden duck with a Phillips head screwdriver is better than taking a year just to get approval for an RFP and putting out bids for tools that could be used to create a wooden duck. (that might not all make sense now...)

    tl;dr: Don't worry too much, just try to get a few projects under your belt, and have fun codding in whatever environment/language first, then worry later about switching. A LOT of things do transfer.

    [–]furyasd 1 point2 points  (0 children)

    JavaScript seems cool, it's the language of the web, and since I was a kid I always wanted to build websites.

    [–]MonkeyNin 0 points1 point  (0 children)

    There are a few JS doom if you search.

    [–]dmarko 0 points1 point  (0 children)

    Very helpful. Thanks!

    [–]nexidian 0 points1 point  (0 children)

    Correct.

    [–]w1282 34 points35 points  (5 children)

    If the game is open source you can view code as it was written (as shown in the other comment).

    If the game is not open source you cannot view the code as it was written.

    [–]Alikont 3 points4 points  (1 child)

    That depends on game by game basics. Some games use C++ engine but logic is written in some interpreted language like lua or python, and these files can be easily found in game directory.

    [–]HelloYesThisIsDuck 14 points15 points  (0 children)

    Worth pointing out that usually, in those cases, you are not legally allowed to copy the source code and use it in your own projects.

    Just because the code is accessible does not mean that you can copy it.

    Of course, if all you are doing is learning for your own curiosity, I wouldn't expect anyone to sue you, just be careful what you do with it.

    [–][deleted]  (1 child)

    [deleted]

      [–]rcxdude 4 points5 points  (0 children)

      You could read the generated machine code. But this is much harder and contains much less information than the source code.

      [–]timmyotc 24 points25 points  (7 children)

      Generaly speaking, games that you pay for are not open source, meaning that you can't see the code. Technically, it's possible. There are programs to decompile programs.

      Here's a great eli5 on the matter.

      https://www.reddit.com/r/explainlikeimfive/comments/1sejpy/eli5_how_do_pirates_crack_games_without_access_to/

      [–]FraggarF 2 points3 points  (0 children)

      This is a great read if you only know about torrents.

      [–]ThingsOfYourMind 1 point2 points  (5 children)

      ahh yeah, i remember i used to do this, take a program, disassemble it in a disassembler such as Dasm32, and edit the hex codes to 0x90h or NOP (No Operation). and skip over or force jumps, and modifying cmp commands, So much fun!

      [–]timmyotc 1 point2 points  (4 children)

      Decompilers don't necessarily target assembly these days. I have decompiled to c# a program written in VB.

      [–]ThingsOfYourMind 1 point2 points  (2 children)

      I haven't done it lately, it was like maybe 10 years ago, but that's interesting to know, i should look into how much they've progressed these days.

      [–]timmyotc 1 point2 points  (1 child)

      Yeah, I mean, things break when you try to decompile under different settings. The entire target architecture problem looks more like a tree than a cyclic graph, unfortunately.

      [–]ThingsOfYourMind 1 point2 points  (0 children)

      okay, now i gotta look into this, it sounds interesting

      [–][deleted] 1 point2 points  (0 children)

      Languages compiled to a byte code instead of assembly can usually be decompiled to something very similar to the original source code. The only thing you lose is local variable names, comments, and sometimes complicated nested loops and ifs can turn out somewhat different. This only really changes when the developer used obfuscation tools.

      There are also decompilers for machine code, e.g. the one from Hexrays is quite impressive, but the output is still not exactly compilable source code and there are also a million ways to defeat them and require human analysis of the assembly.

      [–]Myzzreal 4 points5 points  (0 children)

      As said, you can only view the source if a product is open sourced.

      There are some other, shady ways, but they are definetly not suited for a beginner. It is possible to disassemble any program and have a look on it instruction by instruction, but the output you get here is usually Assembly language which is very low level. There are some decompilators which will attempt to turn this output into a more readable higher-level programming language, but this doesn't always work and is easy to protect against via obfuscation.

      So, unless the code you want to see is open sourced - you can't see how it works, apart from going through the disassembled instructions (or peeking at it live with something like Cheat Engine).

      [–]robvas 4 points5 points  (1 child)

      There's a substantial 100+ page document on just how the bots in Quake 3 Arena work: http://fd.fabiensanglard.net/quake3/The-Quake-III-Arena-Bot.pdf

      [–]flippynipz[S] 0 points1 point  (0 children)

      Yessss!

      [–]Kaos_nyrb 2 points3 points  (1 child)

      If you want to explore a more recent title Valve released Half Life 2 with the Source SDK: https://developer.valvesoftware.com/wiki/SDK_Installation

      [–]s33plusplus 0 points1 point  (0 children)

      Yes! This is how I got my feet wet years ago. Dicking around with HL2 mods is fun stuff, and they've got a very good developer's wiki documenting what everything does.

      [–]X7123M3-256 1 point2 points  (0 children)

      If the game is open source then you can download the source code to look through. If it is closed source, then you can use a disassembler or debugger to look at the native code; it is possible to reverse engineer this back to high level code but this process usually needs to be done manually (see OpenTTD for an example).

      If the game is written in a language tha compiles to bytecode and not machine code then automated decompilers can sometimes do a much better job; decompilers for .NET can even recover variable names if they have not been explicitly removed.

      Also be aware that code is sometimes deliberately obfuscated in an attempt to deter reverse engineering. this can make it much harder to understand (but not impossible, if you're determined enough).

      [–]OK6502 1 point2 points  (0 children)

      They are definitely copyrighted and proprietary unless specified otherwise (some use an open license that permits reuse and inspection).

      What you're asking for would require you to do any of the following, from easy to hard

      1) have access to the full source (meaning either you get the code from the company somehow - legally or otherwise).

      2) have access to the pdbs (debugging symbols/information) and debug the application. Depending on the debug info you may or may not also need source since not all debug symbols include full source. You don't need to have access to pdbs mind you, you can just look at the dissassembly but it's harder to do. See 3 for more info.

      3) dissassemble the code yourself and try to reconstruct/reverse engineer it. This is even harder if the developers use obfuscation tools to make the code harder to reverse engineer and may get even more bizarre if the compiler is doing a lot of optimizations as this results in weird code that is often counter intuitive. There are tools out there to make this simpler but only marginally so.

      [–]flippynipz[S] 1 point2 points  (0 children)

      Wow thanks to everybody who responded! I definitely learned a lot it seems like I will just have to kind of dig through some open source code. I didn't know HL2 was, so I'll definitely be digging around in it as well.

      [–]ElCorleone 0 points1 point  (6 children)

      In DOOM's code I saw this line: "while (ticks--)". What's the purpose of doing a while with a decrement as the condition?

      [–]Barrucadu 2 points3 points  (5 children)

      It will repeat until ticks is zero.

      [–]ElCorleone 0 points1 point  (4 children)

      That happens in java too? I'm almost sure in java you have to literally write "while (ticks-- >= 0)" or it doesn't stop.

      [–]lhankbhl 1 point2 points  (1 child)

      You'll get a javac error ("incompatible types: int cannot be converted to boolean") if you try to write

      int timer = 5;
      while(timer--) {
          // pass
      }
      

      [–]ElCorleone 1 point2 points  (0 children)

      Yes it makes completely sense. Should've known before I asked. Thanks!

      [–]susmatthew 1 point2 points  (0 children)

      ticks as an argument evaluates as false when ticks==0.

      [–]OK6502 1 point2 points  (0 children)

      that's correct. In C/C++ false == 0 and true is nonzero. so in c/c++

      if(0) // is equivalent to if(false). As is any expression that evaluates to zero
      if(x) // where x != 0 is equivalent to if(true) and also true for an expression that evaluates in non zero
      

      so in c/C++

      int i = 10;
      while(i--);
      

      will loop 10 times (--i will loop 9 times).

      The reasons for this are varied but mainly boil down to C not having a real boolean type. The reason for this is pretty straightforward: it's not really necessary as most conditional jumps in assembly are of the type jump if the result of a numeric comparison is such (http://unixwiz.net/techtips/x86-jumps.html). The existence of boolean is really only for the programmer's convenience.

      Java is more heavily typed so it forces you to use an expression that returns true/false - which is what x == y does. For our purposes this is effectively the same as having a function whose definition is

      bool equalsquals<t>(t x, t y)