Added initial implementation of color_t wrapper class

This commit is contained in:
Charles Dang 2016-11-26 11:52:07 +11:00
parent 55a57d5397
commit 493bd936b6
5 changed files with 162 additions and 0 deletions

View file

@ -974,6 +974,8 @@
<Unit filename="../../src/scripting/plugins/context.hpp" />
<Unit filename="../../src/scripting/plugins/manager.cpp" />
<Unit filename="../../src/scripting/plugins/manager.hpp" />
<Unit filename="../../src/sdl/color.cpp" />
<Unit filename="../../src/sdl/color.hpp" />
<Unit filename="../../src/sdl/exception.cpp" />
<Unit filename="../../src/sdl/exception.hpp" />
<Unit filename="../../src/sdl/rect.cpp" />

View file

@ -363,6 +363,7 @@ set_target_properties(wesnoth-lua
########### Helper libraries ###############
set(wesnoth-sdl_SRC
sdl/color.cpp
sdl/exception.cpp
sdl/rect.cpp
sdl/window.cpp

View file

@ -160,6 +160,7 @@ libcampaignd_sources = Split("""
libcampaignd = env.Library("campaignd", libcampaignd_sources, OBJPREFIX = "campaignd_")
libwesnoth_sdl_sources = Split("""
sdl/color.cpp
sdl/exception.cpp
sdl/rect.cpp
sdl/utils.cpp

101
src/sdl/color.cpp Normal file
View file

@ -0,0 +1,101 @@
/*
Copyright (C) 2003 - 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "serialization/string_utils.hpp"
#include "sdl/color.hpp"
#include "sdl/utils.hpp"
#include <iomanip>
#include <sstream>
color_t::color_t()
: r(255)
, g(255)
, b(255)
, a(255)
{
}
color_t::color_t(const color_t& c)
: r(c.r)
, g(c.g)
, b(c.b)
, a(c.a)
{
}
color_t::color_t(uint8_t r_val, uint8_t g_val , uint8_t b_val, uint8_t a_val)
: r(r_val)
, g(g_val)
, b(b_val)
, a(a_val)
{
}
color_t::color_t(uint32_t c)
: r(static_cast<uint8_t>((SDL_RED_MASK & c) >> 24))
, g(static_cast<uint8_t>((SDL_GREEN_MASK & c) >> 16))
, b(static_cast<uint8_t>((SDL_BLUE_MASK & c) >> 8))
, a(static_cast<uint8_t>( SDL_ALPHA_MASK & c))
{
}
color_t::color_t(const std::string& c)
: r()
, g()
, b()
, a()
{
std::vector<std::string> fields = utils::split(c);
// Make sure we have 4 fields
while(fields.size() < 4) {
fields.push_back("0");
}
r = static_cast<uint8_t>(std::stoul(fields[0]));
g = static_cast<uint8_t>(std::stoul(fields[0]));
b = static_cast<uint8_t>(std::stoul(fields[0]));
a = static_cast<uint8_t>(std::stoul(fields[0]));
}
color_t::color_t(const SDL_Color& c)
: r(c.r)
, g(c.g)
, b(c.b)
, a(c.a)
{
}
uint32_t color_t::to_uint32()
{
return (r << 24) + (g << 16) + (b << 8) + a;
}
std::string color_t::to_pango_markup()
{
std::ostringstream h;
// Must match what pango expects
h << "#"
<< std::hex << std::setfill('0') << std::setw(2) << (r & 0xFF0000)
<< std::hex << std::setfill('0') << std::setw(2) << (g & 0x00FF00)
<< std::hex << std::setfill('0') << std::setw(2) << (b & 0x0000FF);
return h.str();
}
SDL_Color color_t::to_sdl()
{
return {r, g, b, a};
}

57
src/sdl/color.hpp Normal file
View file

@ -0,0 +1,57 @@
/*
Copyright (C) 2003 - 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef COLOR_T_HPP_INCLUDED
#define COLOR_T_HPP_INCLUDED
#include <cstdint>
#include <string>
#include <SDL.h>
struct color_t
{
/**
* Constructors
*/
color_t();
color_t(const color_t& c);
color_t(uint8_t r_val, uint8_t g_val, uint8_t b_val, uint8_t a_val);
color_t(uint32_t c);
color_t(const std::string& c);
color_t(const SDL_Color& c);
/**
* Conversion functions
*/
uint32_t to_uint32();
std::string to_pango_markup();
SDL_Color to_sdl();
/**
* Color members
*/
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
#endif