#+TITLE: Theory of game cont.
#+AUTHOR: screwtape
* The tangle
   Here is [[https://orgmode.org/worg/org-contrib/babel/intro.html#literate-programming][A quote from Knuth]]
 #+BEGIN_QUOTE
     Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. \\

         The practitioner of literate programming can be regarded as an essayist, whose main concern is with exposition and excellence of style. Such an author, with thesaurus in hand, chooses the names of variables carefully and explains what each variable means. He or she strives for a program that is comprehensible because its concepts have been introduced in an order that is best for human understanding, using a mixture of formal and informal methods that reinforce each other. \\
 \\
 - Donald Knuth
 #+END_QUOTE
   So organize our code best for ourselves as humans, and let orgmode tangle it together for the computer.
** SDL2 version
*** Paint a rectangle by top-left top-right width height red green blue
**** Setup src
     #+name: setup-rect
#+begin_src C
       SDL_Rect rect;
       int tlx=5, tly=10, w=15, h=20;
       Uint8 red = 50, blue = 100, green=150, alpha=255;
#+end_src

**** Captured variables
***** rect
      An SDL_Rect named rect must be declared first (see setup-rect)
***** tlx
      Top left x value for rectangle
***** tly
      Top left y value for rectangle
***** w
      Width of rectangle
***** h
      Height of rectangle
***** red
      uint8 red value (0 to 255)
***** green
      uint8 green value (0 to 255)
***** blue
      uint8 blue value (0 to 255)
***** alpha
      uint8 alpha value (0 to 255)

**** Source
     #+name: paint-rectangle
#+begin_src C
  rect.x = tlx;
  rect.y = tly;
  rect.w = w;
  rect.h = h;

  SDL_SetRenderDrawColor(renderer, red, green, blue, alpha);

  SDL_RenderFillRect(renderer, &rect);
#+end_src
*** Clear screen
**** setup-clear-screen source
     #+name: setup-clear-screen
#+begin_src C
       Uint8 bgred = 0, bgblue = 0, bggreen = 0;

#+end_src
**** source
     #+name: clear-screen
#+begin_src C
       SDL_SetRenderDrawColor(renderer, bgred, bggreen, bgblue, 255);
       SDL_RenderClear(renderer);
#+end_src

*** Just our logic
    #+name: game-logic
#+begin_src C :noweb yes
      <<setup-clear-screen>>
      <<setup-rect>>

      <<clear-screen>>

      red=255; green = 0; blue = 0;
      alpha=255;
      <<paint-rectangle>>
      red=0; green = 0; blue = 255; alpha=120;
      <<paint-rectangle>>
      SDL_RenderPresent(renderer);
      SDL_Delay(5000);
#+end_src

#+RESULTS: game-logic

*** SDL2 main method

#+begin_src C :includes "/usr/local/include/SDL2/SDL.h" :libs "-L/usr/local/lib -lSDL2" :exports code :results none :noweb yes
    SDL_Window* window;
    SDL_Renderer* renderer;

    /* Initialize SDL. */
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
      return 1;

    /* Create the window where we will draw. */
    window = SDL_CreateWindow("SDL_RenderClear",
			      SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
			      512, 512,
			      0);
    // The wiki misses something here for example.

    /* We must call SDL_CreateRenderer in order for draw calls to affect this window. */
  renderer = SDL_CreateRenderer(window, -1, 0);
  <<game-logic>>
    /* Always be sure to clean up */
    SDL_Quit();
    return 0;
     #+end_src
** Text version
*** Text loop tangle
    The big thing is that we need to name our blocks so we can tangle them.
**** Printing 5 *s
     #+name: print-5-stars
     #+begin_src C
       puts("*****");
     #+end_src
**** for loop
     #+name: 5-stars-5-times
 #+begin_src C :noweb yes
       for (int i = 0; i < 5; i++)
	 <<print-5-stars>>
 #+end_src

 #+RESULTS: 5-stars-5-times
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |
**** Use 5-stars-5-times twice
 #+name: 50-stars
 #+begin_src C :noweb yes
   <<5-stars-5-times>>
   <<5-stars-5-times>>
 #+end_src

 #+RESULTS: 50-stars
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |
 | ***** |

**** Print x stars, y times
     Since this is a kind of macro expansion, we use variable capture.
***** Print x stars
****** Variable x
       should be an integer number of stars, like 4
****** Source
       #+name: print-x-stars
 #+begin_src C
	 for (int i=0; i < x; i++) {
	   printf("*");
	   if (i == x - 1) printf("\n");
	  }
 #+end_src
****** Example
       #+name: print-x-stars-example
      
 #+begin_src C :noweb yes
   int x = 4;
   <<print-x-stars>>
 #+end_src

 #+RESULTS:
 : ****
****** Print x stars y times
       #+name: x-stars-y-times
 #+begin_src C :noweb yes
	 for (int i = 0; i < y; i++)
	   <<print-x-stars>>
 #+end_src
****** Example
       #+name: 12-stars-3-times
 #+begin_src C :noweb yes :results table
	 int x=12, y=3;
	 <<x-stars-y-times>>
 #+end_src

 #+RESULTS: 12-stars-3-times
 | ************ |
 | ************ |
 | ************ |


* Shortcuts I used                                                 :appendix:
** Demarcating a source block
   C-c C-v d "C" <enter>
   Mnemonically, control-verbatim demarcate
   Look demarcate up in a dictionary
** New sub/heading
   C-<enter>
   After which mashing <tab> changes the number of stars in an intelligent way.

** Save
   C-x C-s
   Mnemonically, execute-save

** Jump last heading
   C-c C-p
   Mnemonically, control-previous.

** Jump to next heading
   C-c C-n
   Mnemonically, control-next.

** Jump up a heading level
   C-c C-u
   Mnemonically, control-up.

** Undo last
   C-x u
   Mnemonically, execute undo.

** Export file as html file
   C-c C-e h h
   Mnemonically, control-export html html-file

** Tagging a heading
   C-c C-c "my_tag" <enter>
   Same shortcut as running a source block, but if the cursor is on a heading it tags the heading instead.