Advertisement
Grant12311

Untitled

Mar 26th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL2/SDL.h>
  3. #include <SDL2/SDL_image.h>
  4. #include <SDL2/SDL_ttf.h>
  5.  
  6. bool init(const char* name, int height, int width, SDL_Window* &window, SDL_Renderer* &renderer, TTF_Font* &font)
  7. {
  8.     bool success = true;
  9.     if(SDL_Init(SDL_INIT_VIDEO) < 0)
  10.     {
  11.         printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
  12.         success = false;
  13.     }else{
  14.         if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
  15.         {
  16.             printf("Warning: Linear texture filtering not enabled!");
  17.         }
  18.         window = SDL_CreateWindow(name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
  19.         if(window == nullptr)
  20.         {
  21.             printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
  22.             success = false;
  23.         }else{
  24.             renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  25.             if(renderer == nullptr)
  26.             {
  27.                 printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
  28.                 success = false;
  29.             }else{
  30.                 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  31.  
  32.                 int imgFlags = IMG_INIT_PNG;
  33.                 if(!(IMG_Init(imgFlags) & imgFlags))
  34.                 {
  35.                     printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
  36.                     success = false;
  37.                 }
  38.  
  39.                 if (TTF_Init() < 0)
  40.                 {
  41.                     printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
  42.                     success = false;
  43.                 }else{
  44.                     font = TTF_OpenFont("assets/fonts/burbank.ttf", 25);
  45.                     if (font == nullptr)
  46.                         printf("Font could not be loaded! SDL_ttf Error: %s\n", TTF_GetError());
  47.                         success = false;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     return success;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement