#+TITLE: Revised SDL2 template #+AUTHOR: screwtape #+PROPERTY: header-args :eval never :noweb yes Export C-c C-e h h yes <ret> yes <ret> to build and run while exporting the html version of this documentation. * Gets run first #+name: load-langs #+HEADER: :eval t #+HEADER: :exports both #+begin_src elisp :results none (org-babel-do-load-languages 'org-babel-load-languages '((C . t) (shell . t) (lisp . t))) #+end_src * main function (entrypoint) #+name: main #+HEADER: :eval t #+HEADER: :exports both #+HEADER: :libs "-L/usr/local/lib -lSDL2" #+HEADER: :includes "/usr/local/include/SDL2/SDL.h" #+begin_src C <<define-structures>> <<declare-variables>> <<start-sdl2-etc>> <<after-start>> <<loop-switch>> <<stop-sdl2-etc>> <<after-stop>> return 0; #+end_src #+RESULTS: main * Assets * Tangles ** Structures #+name: define-structures #+begin_src C ; #+end_src ** Variable declaration #+name: declare-variables #+begin_src C SDL_Renderer *renderer; SDL_Window *window; SDL_Event e; int mx, my; Uint32 mdown; /* mouse state */ #+end_src ** SDL2 initialization #+name: start-sdl2-etc #+begin_src C 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(640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)){ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create renderer and window: %s", SDL_GetError()); return 3; } #+end_src ** After initialization #+begin_src C #+end_src #+name: after-start ** Loops #+name: loop-switch #+begin_src C enum mode{BASIC}mode; mode = BASIC; for (;;) switch (mode) { case BASIC: <<basic-event-pump>>; <<basic-action>>; break; default: break; } finish: #+end_src *** Loop cases **** Basic ***** Event pump #+name: basic-event-pump #+begin_src C while(SDL_PollEvent(&e)) if (e.type == SDL_QUIT) quit: goto finish; 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); #+end_src ***** Action #+name: basic-action #+begin_src C SDL_SetRenderDrawColor(renderer, 0, 25, 0, 255); SDL_RenderClear(renderer); SDL_RenderPresent(renderer); SDL_Delay(25); #+end_src ** SDL2 stopping #+name: stop-sdl2-etc #+begin_src C SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); #+end_src ** After stopping (free..) #+name: after-stop #+begin_src C ; #+end_src