text: #include "Globals.h"
  #include "Application.h"
  #include "ModuleWindow.h"
  #include "ModuleRender.h"
  #include "ModuleSceneIntro.h"
  #include <math.h>
  
  ModuleRender::ModuleRender(Application* app, bool start_enabled) : Module(app, start_enabled)
  {
  	renderer = NULL;
  	camera.x = camera.y = 0;
  	camera.w = SCREEN_WIDTH;
  	camera.h = SCREEN_HEIGHT;
  }
  
  // Destructor
  ModuleRender::~ModuleRender()
  {}
  
  // Called before render is available
  bool ModuleRender::Init()
  {
  	LOG("Creating Renderer context");
  	bool ret = true;
  	Uint32 flags = 0;
  
  	if(VSYNC == true)
  	{
  		flags |= SDL_RENDERER_PRESENTVSYNC;
  	}
  
  	renderer = SDL_CreateRenderer(App->window->window, -1, flags);
  	
  	if(renderer == NULL)
  	{
  		LOG("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }
  
  // PreUpdate: clear buffer
  update_status ModuleRender::PreUpdate()
  {
  	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  	SDL_RenderClear(renderer);
  	return UPDATE_CONTINUE;
  }
  
  // Update: debug camera
  update_status ModuleRender::Update()
  {
  	/*
  	int speed = 3;
  
  	if(App->input->GetKey(SDL_SCANCODE_UP) == KEY_REPEAT)
  		App->renderer->camera.y += speed;
  
  	if(App->input->GetKey(SDL_SCANCODE_DOWN) == KEY_REPEAT)
  		App->renderer->camera.y -= speed;
  
  	if(App->input->GetKey(SDL_SCANCODE_LEFT) == KEY_REPEAT)
  		App->renderer->camera.x += speed;
  
  	if(App->input->GetKey(SDL_SCANCODE_RIGHT) == KEY_REPEAT)
  		App->renderer->camera.x -= speed;
  	*/
  	return UPDATE_CONTINUE;
  }
  
  // PostUpdate present buffer to screen
  update_status ModuleRender::PostUpdate()
  {
  	SDL_RenderPresent(renderer);
  	return UPDATE_CONTINUE;
  }
  
  // Called before quitting
  bool ModuleRender::CleanUp()
  {
  	LOG("Destroying renderer");
  
  	//Destroy window
  	if(renderer != NULL)
  	{
  		SDL_DestroyRenderer(renderer);
  	}
  
  	return true;
  }
  
  // Blit to screen
  bool ModuleRender::Blit(SDL_Texture* texture, int x, int y, SDL_Rect* section, float speed, double angle, int pivot_x, int pivot_y )
  {
  	bool ret = true;
  	SDL_Rect rect;
  	rect.x = (int) (camera.x * speed) + x * SCREEN_SIZE;
  	rect.y = (int) (camera.y * speed) + y * SCREEN_SIZE;
  
  	if(section != NULL)
  	{
  		rect.w = section->w;
  		rect.h = section->h;
  	}
  	else
  	{
  		SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h);
  	}
  
  	rect.w /= 1.25f;
  	rect.h /= 1.25f;
  
  	rect.w *= SCREEN_SIZE;
  	rect.h *= SCREEN_SIZE;
  
  	if (texture == App->scene_intro->lights)
  	{
  		rect.w *= 3;
  		rect.h *= 3;
  	}
  
  	SDL_Point* p = NULL;
  	SDL_Point pivot;
  
  	if(pivot_x != INT_MAX && pivot_y != INT_MAX)
  	{
  		pivot.x = pivot_x;
  		pivot.y = pivot_y;
  		p = &pivot;
  	}
  
  	if(SDL_RenderCopyEx(renderer, texture, section, &rect, angle, p, SDL_FLIP_NONE) != 0)
  	{
  		LOG("Cannot blit to screen. SDL_RenderCopy error: %s", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }
  
  bool ModuleRender::DrawQuad(const SDL_Rect& rect, Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool filled, bool use_camera)
  {
  	bool ret = true;
  
  	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  	SDL_SetRenderDrawColor(renderer, r, g, b, a);
  
  	SDL_Rect rec(rect);
  	if(use_camera)
  	{
  		rec.x = (int)(camera.x + rect.x * SCREEN_SIZE);
  		rec.y = (int)(camera.y + rect.y * SCREEN_SIZE);
  		rec.w *= SCREEN_SIZE;
  		rec.h *= SCREEN_SIZE;
  	}
  
  	int result = (filled) ? SDL_RenderFillRect(renderer, &rec) : SDL_RenderDrawRect(renderer, &rec);
  	
  	if(result != 0)
  	{
  		LOG("Cannot draw quad to screen. SDL_RenderFillRect error: %s", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }
  
  bool ModuleRender::DrawLine(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool use_camera)
  {
  	bool ret = true;
  
  	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  	SDL_SetRenderDrawColor(renderer, r, g, b, a);
  
  	int result = -1;
  
  	if(use_camera)
  		result = SDL_RenderDrawLine(renderer, camera.x + x1 * SCREEN_SIZE, camera.y + y1 * SCREEN_SIZE, camera.x + x2 * SCREEN_SIZE, camera.y + y2 * SCREEN_SIZE);
  	else
  		result = SDL_RenderDrawLine(renderer, x1 * SCREEN_SIZE, y1 * SCREEN_SIZE, x2 * SCREEN_SIZE, y2 * SCREEN_SIZE);
  
  	if(result != 0)
  	{
  		LOG("Cannot draw quad to screen. SDL_RenderFillRect error: %s", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }
  
  bool ModuleRender::DrawCircle(int x, int y, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a, bool use_camera)
  {
  	bool ret = true;
  
  	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  	SDL_SetRenderDrawColor(renderer, r, g, b, a);
  
  	int result = -1;
  	SDL_Point points[360];
  
  	float factor = (float) M_PI / 180.0f;
  
  	for(uint i = 0; i < 360; ++i)
  	{
  		points[i].x = (int) (x + radius * cos( i * factor));
  		points[i].y = (int) (y + radius * sin( i * factor));
  	}
  
  	result = SDL_RenderDrawPoints(renderer, points, 360);
  
  	if(result != 0)
  	{
  		LOG("Cannot draw quad to screen. SDL_RenderFillRect error: %s", SDL_GetError());
  		ret = false;
  	}
  
  	return ret;
  }