Random theory - Fish-In-A-Suit/Conquest GitHub Wiki

What does "construct" mean in terms of programming?

First, you need to understand that a constructed language is a language which was developed for human-like communication, instead of having developed naturally.

The English language (a natural language) has tokens 'A-Z/0-9/,;"...' which we use to build WORDS and we use language rules to build sentences out of words. So, in the English language a construct is what we build out of tokens.

Consider this brick-and-mortar example: Imagine if you set out to build a house, the basic materials you might use are: sand, iron, wood, cement, water (just four for simplicity). Anything you build out of these 4 or 5+ items would be CONSTRUCT, which in turn helps you build your house.

A language construct is a piece of language syntax. For example, the following is a language construct in C that lets you control the flow of a program:

if ( condition ) {
  /* when condition is true */
} else {
  /* when condition is false */
}

Difference between a framebuffer and a color buffer

The framebuffer contains (often) the depth, stencil, color, and accumulation buffers. The color buffers are the most important part, but they are only one part. You can create more color buffers, and you can also create more framebuffers in OpenGL.

glBegin() and glEnd() - the old way of performing rendering

glBegin reference

This is the old way of using OpenGL. The modern way to render in OpenGL is to use VBO and shaders. Example of the old way (this code would be put inside the render() method of the Renderer class:

         glBegin(GL_QUADS);
            glColor4f(0, 1, 0, 0);
            glVertex3f(-0.5f, 0.5f, 0.5f);
            
            glColor4f(0, 1, 0, 0);
            glVertex3f(0.5f, 0.5f, 0.5f);
            
            glColor4f(0, 1, 0, 0);
            glVertex3f(0.5f, -0.5f, 0.5f);
            
            glColor4f(0, 1, 0, 0);
            glVertex3f(-0.5f, -0.5f, 0.5f);
        glEnd();
        
        glBegin(GL_TRIANGLES);
            glColor4f(1, 0, 0, 0);
            glVertex3f(-0.7f, -0.7f, 0.6f);
        
            glColor4f(1, 0, 0, 0);
            glVertex3f(0.7f, 0.7f, 0.3f);
        
            glColor4f(1, 0, 0, 0);
            glVertex3f(0.7f, -0.7f, 0.6f);
		glEnd();

Why is the windowHandle of type long?

Shouldn't it be an object? LWJGL is an extension library for OpenGL (which was written in C). There are no objects in C. Instead, you create them as "handles" - which point to a specific memory location. Therefore, the windowHandle field is of type long - it holds numbers which point to a location in memory.

Difference between C and Java methods

C is a function (method) - oriented language, while Java is object oriented. In C you therefore pass an "object" reference as a parameter. This looks like:

glfwMakeContextCurrent(long window)

In Java, on the other hand, you first create an object reference of a particular type/class and then call the type-specific methods of that class through the reference created.

Window window = new Window();

window.makeContextCurrent(); (only as an example)

The ? : operator in Java

The value of a variable often depends on whether a particular boolean expression is or is not true and on nothing else. For instance one common operation is setting the value of a variable to the maximum of two quantities. In Java you might write

if (a > b) {
  max = a;
}
else {
  max = b;
}

Setting a single variable to one of two states based on a single condition is such a common use of if-else that a shortcut has been devised for it, the conditional operator, ?:. Using the conditional operator you can rewrite the above example in a single line like this:

max = (a > b) ? a : b;

(a > b) ? a : b; is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is true the first value, a, is returned. If it is false, the second value, b, is returned. Whichever value is returned is dependent on the conditional test, a > b. The condition can be any expression which returns a boolean value.

Recursion

https://www.youtube.com/watch?v=ttTH_WX-Cbo

A resursive method is a method that calls itself. With each method call, the problem becomes simpler. The recursive method eventually leads to a point where it no longer calls itself (it must have a condition that leads to the method no longer making another method call on itself). Example:

private static void foo() {
    foo();
}

A recursive method is adding itself on the stack again and again (very nice explanation) and if no solution is found, a stack overflow error is thrown (such an error would be thrown by the example above, since there is no condition that would make the method stop calling itself). Therefore, a way is needed to be found for the method to stop calling itself and adding itself on the stack.

main(String[] args) {
    reduceByOne(3);
}

reduceByOne(int n) {
    if (n > 0) {
        reduceByOne(n - 1);
    }

    System.out.println(n);
}

This is the correct way to use a recursive method. First, 3 is passed to reduceByOne from the main method. First, the condition is checked (if input parameter is greater than 0). It is (3 > 0), so the method calls itself, with the expression n - 1 --> 3 - 1 --> 2. 2 is passed to this very same method. Again, first the condition is checked (it's true) and then the method calls itself again: n-1 --> 2 - 1 --> 1. 1 is passed to the method. The condition is checked (it's true), the method calls itself again n-1 --> 1 - 1 --> 0. 0 is passed to the method, but this time the condition isn't met, therefore the method does not perform a further method call on itself.

When the condition is met, the body of the if statement is skipped and current n is printed to console (0). Then, because the method reduceByOne(0) has completed executing and no further recursive call has been made, it is removed from the stack. The method that is waiting for reduceByOne(0) to comeplete is reduceByOne(1). When reduceByOne(0) is completed and removed from the stack, the method reduceByOne(1) goes out of the if statement (since the method reduceByOne(0)) is completed and prints 1 to console. The method waiting for reduceByOne(1) to complete is reduceByOne(2). When reduceByOne(1) is completed, reduceByOne(2) proceeds execution, goes out of the if statement and prints 2 to console. Repeated for reduceByOne(3).

Comparing String literals

When you compare a variable with a string literal, most of people would do that this way:

if (state.equals("OK")) {
  ...
}

However, if state is null, then a NullPointerException is thrown. A better way to do it is to always put the String literal in front:

if ("OK".equals(state)) {
  ...
}

If state variable is now null, a NullPointerException won't be thrown.