forked from albertz/planet_wars-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx.h
80 lines (67 loc) · 3.34 KB
/
gfx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* gfx.h
* PlanetWars
*
* Created by Albert Zeyer on 13.09.10.
* code under GPLv3
*
*/
#ifndef __PW__GFX_H__
#define __PW__GFX_H__
#include <SDL.h>
#include <cassert>
#include "utils.h"
#include "vec.h"
inline SDL_PixelFormat* getMainPixelFormat() { return SDL_GetVideoSurface()->format; }
struct Color {
Color() : r(0), g(0), b(0), a(SDL_ALPHA_OPAQUE) {}
Color(Uint8 _r, Uint8 _g, Uint8 _b) : r(_r), g(_g), b(_b), a(SDL_ALPHA_OPAQUE) {}
Color(Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a) : r(_r), g(_g), b(_b), a(_a) {}
Color(SDL_PixelFormat *f, Uint32 cl) { SDL_GetRGBA(cl, f, &r, &g, &b, &a); }
explicit Color(Uint32 cl) { set(getMainPixelFormat(), cl); }
Color(const SDL_Color& cl) : r(cl.r), g(cl.g), b(cl.b), a(SDL_ALPHA_OPAQUE) {}
static Color fromDefault(Uint32 cl) { return Color(Uint8(cl >> 24), Uint8(cl >> 16), Uint8(cl >> 8), Uint8(cl)); }
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
Uint32 get() const { return get(getMainPixelFormat()); }
Uint32 get(SDL_PixelFormat *f) const { return SDL_MapRGBA(f, r, g, b, a); }
Uint32 getDefault() const { return (Uint32(r) << 24) | (Uint32(g) << 16) | (Uint32(b) << 8) | Uint32(a); }
Color derived(Sint16 _r, Sint16 _g, Sint16 _b, Sint16 _a) const {
return Color(
Uint8(CLAMP(_r + r, 0, 255)),
Uint8(CLAMP(_g + g, 0, 255)),
Uint8(CLAMP(_b + b, 0, 255)),
Uint8(CLAMP(_a + a, 0, 255)));
}
void set(SDL_PixelFormat *f, Uint32 cl) { SDL_GetRGBA(cl, f, &r, &g, &b, &a); }
bool operator == ( const Color & c ) const { return r == c.r && g == c.g && b == c.b && a == c.a; };
bool operator != ( const Color & c ) const { return ! ( *this == c ); };
Color operator * ( float f ) const { return Color( Uint8(CLAMP(r*f,0.0f,255.0f)), Uint8(CLAMP(g*f,0.0f,255.0f)), Uint8(CLAMP(b*f,0.0f,255.0f)), a ); };
Color operator + ( const Color & c ) const { return Color( (Uint8)CLAMP(Uint16(r)+c.r,0,255), (Uint8)CLAMP(Uint16(g)+c.g,0,255), (Uint8)CLAMP(Uint16(b)+c.b,0,255), (Uint8)CLAMP(Uint16(a)+c.a,0,255)); };
bool operator<(const Color& c) const {
if(r != c.r) return r < c.r;
if(g != c.g) return g < c.g;
if(b != c.b) return b < c.b;
return a < c.a;
}
Uint8& operator[](int i) { switch(i) { case 0: return r; case 1: return g; case 2: return b; case 3: return a; default: assert(false); } return *((Uint8*)NULL); }
Uint8 operator[](int i) const { switch(i) { case 0: return r; case 1: return g; case 2: return b; case 3: return a; default: assert(false); } return 0; }
};
inline void FillSurface(SDL_Surface* surf, Color col) {
SDL_Rect r = {0, 0, surf->w, surf->h};
SDL_FillRect(surf, &r, col.get(surf->format));
}
void DrawText(SDL_Surface* surf, const std::string& txt, Color col, int x, int y, bool center = false);
void DrawRectFill(SDL_Surface * bmpDest, int x, int y, int x2, int y2, Color color);
void DrawCircleFilled(SDL_Surface* bmpDest, int x, int y, int rx, int ry, Color color);
void DrawCircle(SDL_Surface* bmpDest, int x, int y, int rx, int ry, Color color);
void DrawHLine(SDL_Surface * bmpDest, int x, int x2, int y, Color colour);
void DrawVLine(SDL_Surface * bmpDest, int y, int y2, int x, Color colour);
void DrawLine(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Color color);
inline void DrawLine(SDL_Surface* dst, VectorD2<Sint16> p1, VectorD2<Sint16> p2, Color col) {
DrawLine(dst, p1.x, p1.y, p2.x, p2.y, col);
}
Vec TextGetSize(const std::string& txt);
#endif