<commit_before>/*
  Hume Library
  Copyright (C) 2015 <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
  USA
*/

#include "Graphics.h"

namespace hume
{
	Graphics::Graphics() : window(nullptr)
	{
	}

	Graphics::~Graphics()
	{
		if(window) delete window;
	}

	void Graphics::initialize()
	{
		const int types = IMG_INIT_JPG | IMG_INIT_PNG;
		int result = IMG_Init(types);
		if(result != types) throw SDLException();
    
		result = TTF_Init();
		if(result != 0) throw SDLTTFException();

		return;
	}

	void Graphics::shutdown()
	{
		IMG_Quit();
		TTF_Quit();

		return;
	}

	void Graphics::set_window(Window* const w)
	{
		window = w;
		return;
	}

	Window* Graphics::get_window() const
	{
		return window;
	}

	Image* Graphics::load_image(const std::string& filename)
	{
		Image* image = new Image();
		image->open(filename, window->get_renderer());

		return image;
	}

	Image* Graphics::load_image(const std::string& filename, const Uint8 r, const Uint8 g, const Uint8 b)
	{
		Image* image = new Image(r, g, b);
		image->open(filename, window->get_renderer());

		return image;
	}

	void Graphics::draw(const Blittable* const b, const Properties& p)
	{
		window->draw(b, p);
		return;
	}

	void Graphics::draw_rect(const Properties& p, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
	{
		draw_rect(p.x, p.y, p.w, p.h, r, g, b, a);
		return;
	}

	void Graphics::draw_rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
	{
		SDL_Renderer* const renderer = window->get_renderer();

		// We'll be restoring the blend mode and colors back to what they were.
		SDL_BlendMode previous;
		uint8_t rgba[4];
		SDL_GetRenderDrawBlendMode(renderer, &previous);
		SDL_GetRenderDrawColor(renderer, &rgba[0], &rgba[1], &rgba[2], &rgba[3]);
		SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
		SDL_SetRenderDrawColor(renderer, r, g, b, a);

		SDL_Rect rect;
		rect.x = x;
		rect.y = y;
		rect.w = w;
		rect.h = h;
		SDL_RenderFillRect(renderer, &rect);

		// Restore blend mode.
		SDL_SetRenderDrawBlendMode(renderer, previous);
		SDL_SetRenderDrawColor(renderer, rgba[0], rgba[1], rgba[2], rgba[3]);

		return;
	}

	void Graphics::clear()
	{
		window->clear();
		return;
	}

	void Graphics::refresh()
	{
		window->present();
		return;
	}
}
<commit_msg>Catch SDL errors as exceptions.<commit_after>/*
  Hume Library
  Copyright (C) 2015 <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
  USA
*/

#include "Graphics.h"

namespace hume
{
	Graphics::Graphics() : window(nullptr)
	{
	}

	Graphics::~Graphics()
	{
		if(window) delete window;
	}

	void Graphics::initialize()
	{
		const int types = IMG_INIT_JPG | IMG_INIT_PNG;
		int result = IMG_Init(types);
		if(result != types) throw SDLException();
    
		result = TTF_Init();
		if(result != 0) throw SDLTTFException();

		return;
	}

	void Graphics::shutdown()
	{
		IMG_Quit();
		TTF_Quit();

		return;
	}

	void Graphics::set_window(Window* const w)
	{
		window = w;
		return;
	}

	Window* Graphics::get_window() const
	{
		return window;
	}

	Image* Graphics::load_image(const std::string& filename)
	{
		Image* image = new Image();
		image->open(filename, window->get_renderer());

		return image;
	}

	Image* Graphics::load_image(const std::string& filename, const Uint8 r, const Uint8 g, const Uint8 b)
	{
		Image* image = new Image(r, g, b);
		image->open(filename, window->get_renderer());

		return image;
	}

	void Graphics::draw(const Blittable* const b, const Properties& p)
	{
		window->draw(b, p);
		return;
	}

	void Graphics::draw_rect(const Properties& p, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
	{
		draw_rect(p.x, p.y, p.w, p.h, r, g, b, a);
		return;
	}

	void Graphics::draw_rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
	{
		SDL_Renderer* const renderer = window->get_renderer();

		// We'll be restoring the blend mode and colors back to what they were.
		SDL_BlendMode previous;
		uint8_t rgba[4];
		if(SDL_GetRenderDrawBlendMode(renderer, &previous) != 0) throw SDLException();
		if(SDL_GetRenderDrawColor(renderer, &rgba[0], &rgba[1], &rgba[2], &rgba[3]) != 0) throw SDLException();
		if(SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) != 0) throw SDLException();
		if(SDL_SetRenderDrawColor(renderer, r, g, b, a) != 0) throw SDLException();

		SDL_Rect rect;
		rect.x = x;
		rect.y = y;
		rect.w = w;
		rect.h = h;
		if(SDL_RenderFillRect(renderer, &rect) != 0) throw SDLException();

		// Restore blend mode.
		if(SDL_SetRenderDrawBlendMode(renderer, previous) != 0) throw SDLException();
		if(SDL_SetRenderDrawColor(renderer, rgba[0], rgba[1], rgba[2], rgba[3]) != 0) throw SDLException();

		return;
	}

	void Graphics::clear()
	{
		window->clear();
		return;
	}

	void Graphics::refresh()
	{
		window->present();
		return;
	}
}

{'fraction_non_alphanumeric': 0.1194102397926118, 'fraction_numerical': 0.016364225534672715, 'mean_word_length': 4.101652892561983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '571965', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2152, 'n_words': 667}