#+TITLE: sdl2 org src
#+AUTHOR: screwtape
* Rich media sdl2 C applications
(skip past window settings in your reading; they are just better at the top)
** Window settings
#+NAME: w-sizing
#+HEADER: :includes "/usr/local/include/SDL2/SDL.h"
#+begin_src C
SDL_WINDOW_RESIZABLE
#+end_src

#+NAME: w-height
#+begin_src C
768
#+end_src

#+NAME: w-width
#+begin_src C
1280
#+end_src

** template
#+BEGIN_EXAMPLE
  This template does seem a little bit  goto cleanup rather than some clunky
  long, but that is because it handles  quit flag;  but then,  it was easier
  keyboard  and mouse events and mouse  to goto quit which gotos cleanup. At
  position  in the general case.  I do  least the loop isn't goto based!
#+END_EXAMPLE

includes and libs might need to be customized. I found that  my  sdl2-config
flags on arm64 openbsd didn't actually work!
#+name: template
#+HEADER: :includes "/usr/local/include/SDL2/SDL.h"
#+HEADER: :libs "-L/usr/local/lib -lSDL2"
#+HEADER: :exports code
#+HEADER: :results none
#+HEADER: :noweb yes
#+begin_src C
  SDL_Event e;
  int mx, my;
  Uint32 mdown;

  /* spinning animation */
  int ticks = 0;
  int tox, toy, centerx, centery;
  float xoff=0.0, yoff=0.0, l;
  /*/spinning animation */

  <<sdlstart>>

  for(;;){
    while(SDL_PollEvent(&e))
      if (e.type == SDL_QUIT)
      quit:
	goto cleanup;
      else if (e.type == SDL_MOUSEBUTTONDOWN)
	switch (1){
	default:
	  break;
	}
      else if (e.type == SDL_MOUSEBUTTONUP)
	switch (1){
	default:
	  break;
	}
      else if (e.type == SDL_KEYDOWN)
	switch (e.key.keysym.sym){
	case SDLK_q:
	  goto quit;
	default:
	  break;
	}
      else if (e.type == SDL_KEYUP)
	switch (1){
	default:
	  break;
	}

    mdown = SDL_GetMouseState(&mx, &my);

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);      
    SDL_RenderClear(renderer);
    SDL_SetRenderDrawColor(renderer, 100, 200, 200, 255);


    /* spinning animation */
    ticks++;
    centerx = 635; centery = 384; l = 100.0;
    xoff= l * SDL_sin((float)ticks / (2.0 * M_PI));
    tox = centerx + (int)xoff;
    yoff= l * SDL_cos((float)ticks / (2.0 * M_PI));
    toy = centery + (int)yoff;

    SDL_RenderDrawLine(renderer, centerx, centery, tox, toy);
    /*/spinning animation */


    SDL_RenderPresent(renderer);
    SDL_Delay(25);
   }
  cleanup:
  <<sdlcleanup>>
#+end_src

** Simple SDL2, window, renderer init
#+NAME: sdlstart
#+HEADER: :noweb yes
#+begin_src C
  SDL_Window *window;
  SDL_Renderer *renderer;

  if (SDL_Init(SDL_INIT_VIDEO) < 0){
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to init SDL: %s",
		 SDL_GetError());
    return 3;
   }

  if (SDL_CreateWindowAndRenderer(<<w-width>>, <<w-height>>,
				  <<w-sizing>>, &window, &renderer)){
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create renderer and window: %s",
		 SDL_GetError());
    return 3;
   }
#+end_src

** Simple SDL2 cleanup
#+NAME: sdlcleanup
#+begin_src C
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
#+end_src