Skip to content

Commit

Permalink
Move code from xrMisc to related modules. No new runtime dependences.…
Browse files Browse the repository at this point in the history
… Some code reorganizations and small cleanup.
  • Loading branch information
intorr committed Jan 27, 2018
1 parent ff4b7c0 commit b9a6593
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 620 deletions.
5 changes: 4 additions & 1 deletion src/editors/xrWeatherEditor/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

#pragma once

// Конфликт при использовании встроенного в движок аллокатора
#define NO_XRNEW

#pragma managed(push, off)
#include "Common/Platform.hpp"
#include "Common/Common.hpp"
#include "xrCommon/inlining_macros.h"
#include "xrCore/xrMemory.h"
#include "xrCore/xrstring.h"
#pragma managed(pop)

Expand Down
173 changes: 136 additions & 37 deletions src/xrCore/_color.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
#pragma once
#ifndef XRCORE_COLOR_H
#define XRCORE_COLOR_H

#include "_types.h"
#include "xrCommon/inlining_macros.h"
#include "xrCore/math_constants.h"
#include "_std_extensions.h"
#include "math_constants.h"

IC s32 clamp_to_8bit(const s32 val) throw()
{
if (val < 0)
return 0;
if (val > 255)
return 255;
return val;
}

// maps unsigned 8 bits/channel to D3DCOLOR
ICF u32 color_argb(u32 a, u32 r, u32 g, u32 b) throw()
{ return ((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); }
ICF u32 color_rgba(u32 r, u32 g, u32 b, u32 a) throw()
{ return color_argb(a, r, g, b); }
u32 color_argb_f(f32 a, f32 r, f32 g, f32 b) throw();
ICF u32 color_argb_f(f32 a, f32 r, f32 g, f32 b) throw()
{
#if 0
s32 _r = clampr(iFloor(r*255.f), 0, 255);
s32 _g = clampr(iFloor(g*255.f), 0, 255);
s32 _b = clampr(iFloor(b*255.f), 0, 255);
s32 _a = clampr(iFloor(a*255.f), 0, 255);
#else
s32 _r = clamp_to_8bit(iFloor(r*255.f));
s32 _g = clamp_to_8bit(iFloor(g*255.f));
s32 _b = clamp_to_8bit(iFloor(b*255.f));
s32 _a = clamp_to_8bit(iFloor(a*255.f));
#endif
return color_argb(_a, _r, _g, _b);
}
ICF u32 color_rgba_f(f32 r, f32 g, f32 b, f32 a) throw()
{ return color_argb_f(a, r, g, b); }
ICF u32 color_xrgb(u32 r, u32 g, u32 b) { return color_argb(0xff, r, g, b); }
Expand All @@ -26,7 +48,15 @@ struct Fcolor
{
float r, g, b, a;

Fcolor& set(u32 dw) throw();
Fcolor& set(u32 dw) throw()
{
const float f = float(1.0) / float(255.0);
a = f * float((dw >> 24) & 0xff);
r = f * float((dw >> 16) & 0xff);
g = f * float((dw >> 8) & 0xff);
b = f * float((dw >> 0) & 0xff);
return *this;
}
Fcolor& set(float _r, float _g, float _b, float _a)
{
r = _r;
Expand All @@ -35,40 +65,109 @@ struct Fcolor
a = _a;
return *this;
};
Fcolor& set(const Fcolor& rhs) throw();
u32 get() const throw();
u32 get_windows() const throw(); // Get color as a Windows DWORD value.
Fcolor& set_windows(u32 dw) throw(); // Set color from a Windows DWORD color value.
Fcolor& adjust_contrast(float f) throw(); // >1 - contrast will be increased
Fcolor& adjust_contrast(const Fcolor& in, float f) throw(); // >1 - contrast will be increased
Fcolor& adjust_saturation(float s) throw();
Fcolor& adjust_saturation(const Fcolor& in, float s) throw();
Fcolor& modulate(Fcolor& in) throw();
Fcolor& modulate(const Fcolor& in1, const Fcolor& in2) throw();
Fcolor& negative(const Fcolor& in) throw();
Fcolor& negative() throw();
Fcolor& sub_rgb(float s) throw();
Fcolor& add_rgb(float s) throw();
Fcolor& add_rgba(float s) throw();
Fcolor& mul_rgba(float s) throw();
Fcolor& mul_rgb(float s) throw();
Fcolor& mul_rgba(const Fcolor& c, float s) throw();
Fcolor& mul_rgb(const Fcolor& c, float s) throw();
Fcolor& set(const Fcolor& rhs) throw()
{
r = rhs.r;
g = rhs.g;
b = rhs.b;
a = rhs.a;
return *this;
}
u32 get() const throw() { return color_rgba_f(r, g, b, a); }
u32 get_windows() const throw() // Get color as a Windows DWORD value.
{
u8 _a, _r, _g, _b;
_a = u8(a*255.f);
_r = u8(r*255.f);
_g = u8(g*255.f);
_b = u8(b*255.f);
return (u32)(_a << 24) | (_b << 16) | (_g << 8) | _r;
}
Fcolor& set_windows(u32 dw) throw() // Set color from a Windows DWORD color value.
{
const float f = 1.0f / 255.0f;
a = f * (float)(u8)(dw >> 24);
b = f * (float)(u8)(dw >> 16);
g = f * (float)(u8)(dw >> 8);
r = f * (float)(u8)(dw >> 0);
return *this;
}
Fcolor& adjust_contrast(float f) throw() // >1 - contrast will be increased
{
r = 0.5f + f * (r - 0.5f);
g = 0.5f + f * (g - 0.5f);
b = 0.5f + f * (b - 0.5f);
return *this;
}
Fcolor& adjust_contrast(const Fcolor& in, float f) throw() // >1 - contrast will be increased
{
r = 0.5f + f * (in.r - 0.5f);
g = 0.5f + f * (in.g - 0.5f);
b = 0.5f + f * (in.b - 0.5f);
return *this;
}
Fcolor& adjust_saturation(float s) throw()
{
// Approximate values for each component's contribution to luminance.
// Based upon the NTSC standard described in ITU-R Recommendation BT.709.
float grey = r * 0.2125f + g * 0.7154f + b * 0.0721f;
r = grey + s * (r - grey);
g = grey + s * (g - grey);
b = grey + s * (b - grey);
return *this;
}
Fcolor& adjust_saturation(const Fcolor& in, float s) throw()
{
// Approximate values for each component's contribution to luminance.
// Based upon the NTSC standard described in ITU-R Recommendation BT.709.
float grey = in.r*0.2125f + in.g*0.7154f + in.b*0.0721f;
r = grey + s * (in.r - grey);
g = grey + s * (in.g - grey);
b = grey + s * (in.b - grey);
return *this;
}
Fcolor& modulate(Fcolor& in) throw() { r *= in.r; g *= in.g; b *= in.b; a *= in.a; return *this; }
Fcolor& modulate(const Fcolor& in1, const Fcolor& in2) throw() { r = in1.r*in2.r; g = in1.g*in2.g; b = in1.b*in2.b; a = in1.a*in2.a; return *this; }
Fcolor& negative(const Fcolor& in) throw() { r = 1.0f - in.r; g = 1.0f - in.g; b = 1.0f - in.b; a = 1.0f - in.a; return *this; }
Fcolor& negative() throw() { r = 1.0f - r; g = 1.0f - g; b = 1.0f - b; a = 1.0f - a; return *this; }
Fcolor& sub_rgb(float s) throw() { r -= s; g -= s; b -= s; return *this; }
Fcolor& add_rgb(float s) throw() { r += s; g += s; b += s; return *this; }
Fcolor& add_rgba(float s) throw() { r += s; g += s; b += s; a += s; return *this; }
Fcolor& mul_rgba(float s) throw() { r *= s; g *= s; b *= s; a *= s; return *this; }
Fcolor& mul_rgb(float s) throw() { r *= s; g *= s; b *= s; return *this; }
Fcolor& mul_rgba(const Fcolor& c, float s) throw() { r = c.r*s; g = c.g*s; b = c.b*s; a = c.a*s; return *this; }
Fcolor& mul_rgb(const Fcolor& c, float s) throw() { r = c.r*s; g = c.g*s; b = c.b*s; return *this; }

// SQ magnitude
float magnitude_sqr_rgb() const throw();
float magnitude_sqr_rgb() const throw() { return r * r + g * g + b * b;}
// magnitude
float magnitude_rgb() const throw();
float intensity() const throw();
float magnitude_rgb() const throw() { return _sqrt(magnitude_sqr_rgb()); }
float intensity() const throw()
{
// XXX: Use the component percentages from adjust_saturation()?
return (r + g + b) / 3.f;
}
// Normalize
Fcolor& normalize_rgb();
Fcolor& normalize_rgb(const Fcolor& c);
Fcolor& lerp(const Fcolor& c1, const Fcolor& c2, float t) throw();
Fcolor& lerp(const Fcolor& c1, const Fcolor& c2, const Fcolor& c3, float t);
bool similar_rgba(const Fcolor& v, float E = EPS_L) const throw();
bool similar_rgb(const Fcolor& v, float E = EPS_L) const throw();
Fcolor& normalize_rgb() throw() { VERIFY( magnitude_sqr_rgb() > EPS_S); return mul_rgb( 1.f / magnitude_rgb()); }
Fcolor& normalize_rgb(const Fcolor& c) throw() { VERIFY(c.magnitude_sqr_rgb() > EPS_S); return mul_rgb(c, 1.f / c.magnitude_rgb()); }
Fcolor& lerp(const Fcolor& c1, const Fcolor& c2, float t) throw()
{
float invt = 1.f - t;
r = c1.r*invt + c2.r*t;
g = c1.g*invt + c2.g*t;
b = c1.b*invt + c2.b*t;
a = c1.a*invt + c2.a*t;
return *this;
}
Fcolor& lerp(const Fcolor& c1, const Fcolor& c2, const Fcolor& c3, float t) throw()
{
if (t>.5f)
return lerp(c2, c3, t*2.f - 1.f);
else
return lerp(c1, c2, t*2.f);
}
bool similar_rgba(const Fcolor& v, float E = EPS_L) const throw() { return _abs(r - v.r) < E && _abs(g - v.g) < E && _abs(b - v.b) < E && _abs(a - v.a) < E; }
bool similar_rgb (const Fcolor& v, float E = EPS_L) const throw() { return _abs(r - v.r) < E && _abs(g - v.g) < E && _abs(b - v.b) < E; }
};

bool _valid(const Fcolor& c);

#endif // XRCORE_COLOR_H
IC bool _valid(const Fcolor& c) throw() { return _valid(c.r) && _valid(c.g) && _valid(c.b) && _valid(c.a); }
25 changes: 22 additions & 3 deletions src/xrCore/xrstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,18 @@ class shared_str
rhs.p_ = tmp;
}
bool equal(const shared_str& rhs) const { return (p_ == rhs.p_); }
shared_str& __cdecl printf(const char* format, ...);
shared_str& __cdecl printf(const char* format, ...)
{
string4096 buf;
va_list p;
va_start(p, format);
int vs_sz = vsnprintf(buf, sizeof(buf) - 1, format, p);
buf[sizeof(buf) - 1] = 0;
va_end(p);
if (vs_sz)
_set(buf);
return (shared_str&)*this;
}
};

// res_ptr == res_ptr
Expand Down Expand Up @@ -160,7 +171,15 @@ IC int xr_strcmp(const shared_str& a, const shared_str& b) throw()
return xr_strcmp(*a, *b);
}

// void xr_strlwr(xr_string& src) // in xrCommon/xr_string.h, since it depends on xr_string defined there.
void xr_strlwr(shared_str& src);
IC void xr_strlwr(shared_str& src)
{
if (*src)
{
char* lp = xr_strdup(*src);
xr_strlwr(lp);
src = lp;
xr_free(lp);
}
}

#pragma pack(pop)
Loading

0 comments on commit b9a6593

Please sign in to comment.