/
drawing.cpp
141 lines (117 loc) · 2.87 KB
/
drawing.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "pch.h"
#include "drawing.h"
#include "constants.h"
cairo_t* drawingSink = NULL;
SDL_Texture* NewTexture(SDL_Renderer* renderer, int w, int h, bool transparentAble, bool forCairo)
{
const auto newTexture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ARGB8888,
forCairo ? SDL_TEXTUREACCESS_STREAMING : SDL_TEXTUREACCESS_TARGET,
w,
h
);
if (!newTexture)
{
const auto creationError = SDL_GetError();
ReportError("Failed to create texture", creationError);
}
if (transparentAble)
{
if (SDL_SetTextureBlendMode(newTexture, SDL_BLENDMODE_BLEND) < 0)
{
const auto blendModeError = SDL_GetError();
ReportError("Failed to set texture blend mode", blendModeError);
SDL_DestroyTexture(newTexture);
return NULL;
}
}
return newTexture;
}
cairo_t* BeginTextureDrawing(SDL_Texture* targetTexture, const int canvasWidth, const int canvasHeight)
{
int pitch;
void* pixels;
if (targetTexture)
{
SDL_LockTexture(targetTexture, NULL, &pixels, &pitch);
const auto cairoSurface = cairo_image_surface_create_for_data(
static_cast<unsigned char*>(pixels),
CAIRO_FORMAT_ARGB32,
canvasWidth,
canvasHeight,
pitch
);
const auto sink = cairo_create(cairoSurface);
cairo_set_antialias(sink, CAIRO_ANTIALIAS_SUBPIXEL);
return sink;
}
return NULL;
}
SDL_Texture* BeginRenderDrawing(SDL_Renderer* renderTarget, const int canvasWidth, const int canvasHeight)
{
int pitch;
void* pixels;
const auto targetTexture = SDL_CreateTexture(
renderTarget,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
canvasWidth,
canvasHeight
);
if (targetTexture)
{
SDL_SetTextureBlendMode(targetTexture, SDL_BLENDMODE_BLEND);
SDL_LockTexture(targetTexture, NULL, &pixels, &pitch);
const auto cairoSurface = cairo_image_surface_create_for_data(
static_cast<unsigned char*>(pixels),
CAIRO_FORMAT_ARGB32,
canvasWidth,
canvasHeight,
pitch
);
drawingSink = cairo_create(cairoSurface);
cairo_set_antialias(drawingSink, CAIRO_ANTIALIAS_SUBPIXEL);
}
return targetTexture;
}
cairo_t* GetDrawingSink()
{
return drawingSink;
}
cairo_t* SetSourceColor(cairo_t* context, SDL_Color color)
{
cairo_set_source_rgba
(
context,
color.r / 255.,
color.g / 255.,
color.b / 255.,
color.a / 255.
);
return context;
}
void EndRenderDrawing(SDL_Renderer* renderTarget, SDL_Texture* targetTexture, SDL_Rect* destRect)
{
SDL_UnlockTexture(targetTexture);
SDL_SetTextureBlendMode(targetTexture, SDL_BLENDMODE_BLEND);
SDL_RenderCopy(renderTarget, targetTexture, NULL, destRect);
SDL_DestroyTexture(targetTexture);
cairo_destroy(drawingSink);
}
void EndTextureDrawing(SDL_Texture* targetTexture, cairo_t* drawer)
{
SDL_UnlockTexture(targetTexture);
cairo_destroy(drawer);
}
void DestroyTexture(SDL_Texture** texture)
{
if (texture != NULL)
{
if ((*texture) != NULL)
{
SDL_DestroyTexture(*texture);
}
(*texture) = NULL;
}
}