text: /*This source code copyrighted by Lazy Foo' Productions (2004-2020)
  and may not be redistributed without written permission.*/
  
  //Using SDL and standard IO
  #include <SDL.h>
  #include <SDL_image.h>
  #include <stdio.h>
  
  //Screen dimension constants
  const int SCREEN_WIDTH = 640;
  const int SCREEN_HEIGHT = 480;
  
  // Some global variables
  // The Window we'll be rendering to
  SDL_Window* gWindow = NULL;
  
  // The Window Renderer
  SDL_Renderer* gRenderer = NULL;
  
  // Current displayed texture
  SDL_Texture* gTexture = NULL;
  
  // Starts up SDL and creates the window
  bool Init();
  
  // Loads media
  bool LoadMedia();
  
  // Free media files and shut down SDL
  void Close();
  
  // Loads individual image
  SDL_Texture* loadTexture(const char* pPath);
  
  int main(int argc, char* args[])
  {
  	if (!Init())
  	{
  		printf("Failed to initialize!\n");
  	}
  	else
  	{
  		// Load media
  		if (LoadMedia())
  		{
  			// Main loop flag
  			bool quit = false;
  
  			// Event Handler 
  			SDL_Event e;
  
  			// Handle events on queue
  			while (!quit)
  			{
  				while (SDL_PollEvent(&e) != 0)
  				{
  					// User requests to quit
  					if (e.type == SDL_QUIT)
  					{
  						quit = true;
  					}
  				}
  
  				// Clear Screen
  				SDL_SetRenderDrawColor(gRenderer, 0xff, 0xff, 0xff, 0xff);
  				SDL_RenderClear(gRenderer);
  
  				// Top left corner viewport
  				SDL_Rect topLeftViewport;
  				topLeftViewport.x = topLeftViewport.y = 0;
  				topLeftViewport.w = SCREEN_WIDTH / 2;
  				topLeftViewport.h = SCREEN_HEIGHT / 2;
  				SDL_RenderSetViewport(gRenderer, &topLeftViewport);
  
  				// Render texture to screen
  				SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
  
  				// Top right corner viewport
  				SDL_Rect topRightViewport;
  				topRightViewport.x = SCREEN_WIDTH / 2;
  				topRightViewport.y = 0;
  				topRightViewport.w = SCREEN_WIDTH / 2;
  				topRightViewport.h = SCREEN_HEIGHT / 2;
  				SDL_RenderSetViewport(gRenderer, &topRightViewport);
  
  				// Render texture to screen
  				SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
  
  				// Bottom viewport
  				SDL_Rect bottomViewport;
  				bottomViewport.x = 0;
  				bottomViewport.y = SCREEN_HEIGHT / 2;
  				bottomViewport.w = SCREEN_WIDTH;
  				bottomViewport.h = SCREEN_HEIGHT / 2;
  				SDL_RenderSetViewport(gRenderer, &bottomViewport);
  
  				// Render texture to screen
  				SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
  
  				// Update screen
  				SDL_RenderPresent(gRenderer);
  			}
  		}
  	}
  
  	// Free resources and close SDL
  	Close();
  
  	return 0;
  }
  
  bool Init()
  {
  	// Initialization flag
  
  	bool success = true;
  	//Initialize SDL
  	if (SDL_Init(SDL_INIT_VIDEO) < 0)
  	{
  		printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  		success = false;
  	}
  	else
  	{
  		//Create window
  		gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  		if (gWindow == NULL)
  		{
  			printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  			success = false;
  		}
  		else
  		{
  			gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
  			if (!gRenderer)
  			{
  				printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
  				success = false;
  			}
  			else
  			{
  				// Initialize renderer color
  				SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  
  				// Initialize PNG loading
  				int imgFlags = IMG_INIT_PNG;
  				if (!(IMG_Init(imgFlags) & imgFlags))
  				{
  					printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
  					success = false;
  				}
  			}
  		}
  	}
  
  	return success;
  }
  
  bool LoadMedia(void)
  {
  	// Loading success flag
  	bool success = true;
  
  	gTexture = loadTexture("viewport.png");
  	if (gTexture == NULL)
  	{
  		printf("Could not load the texture: %s! SDL Error: %s\n", "viewport.png", SDL_GetError());
  		success = false;
  	}
  
  	// Nothing to load
  	return success;
  }
  
  void Close()
  {
  	// Deallocate surface
  	SDL_DestroyTexture(gTexture);
  
  	//Destroy window
  	SDL_DestroyRenderer(gRenderer);
  	SDL_DestroyWindow(gWindow);
  	gWindow = NULL;
  
  	//Quit SDL subsystems
  	IMG_Quit();
  	SDL_Quit();
  }
  
  SDL_Texture* loadTexture(const char* pPath)
  {
  	// The final texture
  	SDL_Texture* newTexture = NULL;
  
  	// Load image at specified path
  	SDL_Surface* loadedSurface = IMG_Load(pPath);
  	if (!loadedSurface)
  	{
  		printf("Unable to load image %s! SDL Error: %s\n", pPath, SDL_GetError());
  	}
  	else
  	{
  		// Convert surface to screen format
  		newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
  		if (!newTexture)
  		{
  			printf("Unable to create texture from %s!: SDL Error: %s\n", pPath, SDL_GetError());
  		}
  
  		// Get rid of old loaded surface
  		SDL_FreeSurface(loadedSurface);
  	}
  	return newTexture;
  }
  