SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include <SDL2/SDL.h>
  5.  
  6. /**
  7.  * I finally find out how this SDL should work on my machine.
  8.  * In the past, I just so dumb to not initialize its renderer.
  9.  *
  10.  * On this case, I create renderer and let it render the black screen with `SDL_SetRenderDrawColor`.
  11.  *
  12.  * Important notes for me:
  13.  * 1. The window would show nothing if it has no renderer.
  14.  * 2. We MUST use one (at least) infinite loop on main thread, which is also handling triggered event.
  15.  * 3. RenderClear here is only for debugging purpose, this help me to see the black screen that i've declared before.
  16.  * 4. RenderPresent helps to updating the screen.
  17.  * 5. DestroyWindow && Quit at the end.
  18.  * 6. SDL Blocking the KDE's compositor, don't be panic if your GUI being messed up (sacrifice the appearance to the performance).
  19.  */
  20. int main(int argc, char **argv)
  21. {
  22.     int init_status = SDL_Init(SDL_INIT_EVERYTHING);
  23.     if (init_status != 0) {
  24.         fprintf(stderr, "Error on initialize the SDL.\n==> Error message: %s\n==> Error code: %d\n", SDL_GetError(), init_status);
  25.         return EXIT_FAILURE;
  26.     }
  27.  
  28.     SDL_Window *window = SDL_CreateWindow(
  29.         "ChaerEngine - A Simple game engine written by @KennFatt",
  30.         100, 100,
  31.         640, 480,
  32.         SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE
  33.     );
  34.     if (window == NULL) {
  35.         fprintf(stderr, "Error on create window. Error message: %s\n", SDL_GetError());
  36.         return EXIT_FAILURE;
  37.     }
  38.  
  39.     SDL_Renderer *renderer = SDL_CreateRenderer(
  40.         window,
  41.         -1,
  42.         SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
  43.     );
  44.     if (renderer == NULL) {
  45.         SDL_DestroyWindow(window);
  46.         fprintf(stderr, "Error on create renderer. Error message: %s\n", SDL_GetError());
  47.         return EXIT_FAILURE;
  48.     }
  49.  
  50.     SDL_Event ev;
  51.     int err = 0;
  52.     while (!err) {
  53.         while (SDL_PollEvent(&ev)) {
  54.             switch (ev.type) {
  55.                 case SDL_QUIT:
  56.                     err = 1;
  57.                 break;
  58.  
  59.                 default:
  60.                     fprintf(stdout, "[Debug] Event type: %d\n", ev.type);
  61.                 break;
  62.             }
  63.         }
  64.        
  65.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  66.         SDL_RenderClear(renderer);
  67.         SDL_RenderPresent(renderer);
  68.     }
  69.  
  70.     SDL_DestroyWindow(window);
  71.     SDL_Quit();
  72.  
  73.     return EXIT_SUCCESS;
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top