Use RenderGeometry to implement RenderCopy and RenderCopyEx at higher level
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 6a59bb7..017fd9f 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -572,7 +572,6 @@ QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
return retval;
}
-#if SDL_HAVE_RENDER_GEOMETRY
static int
QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
const float *xy, int xy_stride,
@@ -600,7 +599,6 @@ QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
}
return retval;
}
-#endif
static int UpdateLogicalSize(SDL_Renderer *renderer);
@@ -845,7 +843,7 @@ void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
SDL_assert(renderer->QueueDrawPoints != NULL);
SDL_assert(renderer->QueueDrawLines != NULL);
SDL_assert(renderer->QueueFillRects != NULL);
- SDL_assert(renderer->QueueCopy != NULL);
+ SDL_assert(renderer->QueueCopy != NULL || renderer->QueueGeometry != NULL);
SDL_assert(renderer->RunCommandQueue != NULL);
}
@@ -3245,14 +3243,72 @@ SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
texture = texture->native;
}
- real_dstrect.x *= renderer->scale.x;
- real_dstrect.y *= renderer->scale.y;
- real_dstrect.w *= renderer->scale.x;
- real_dstrect.h *= renderer->scale.y;
-
texture->last_command_generation = renderer->render_command_generation;
- retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
+ if (renderer->QueueGeometry && renderer->QueueCopy == NULL) {
+ float xy[8];
+ const int xy_stride = 2 * sizeof (float);
+ SDL_Color col, color[4];
+ const int color_stride = sizeof (SDL_Color);
+ float uv[8];
+ const int uv_stride = 2 * sizeof (float);
+ const int num_vertices = 4;
+ const int indices[6] = {0, 1, 2, 0, 2, 3};
+ const int num_indices = 6;
+ const int size_indices = 4;
+ float minu, minv, maxu, maxv;
+ float minx, miny, maxx, maxy;
+
+ SDL_GetTextureColorMod(texture, &col.r, &col.g, &col.b);
+ SDL_GetTextureAlphaMod(texture, &col.a);
+
+ color[0] = col;
+ color[1] = col;
+ color[2] = col;
+ color[3] = col;
+
+ minu = (float) (real_srcrect.x) / (float) texture->w;
+ minv = (float) (real_srcrect.y) / (float) texture->h;
+ maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
+ maxv = (float) (real_srcrect.y + real_srcrect.h) / (float) texture->h;
+
+ minx = real_dstrect.x;
+ miny = real_dstrect.y;
+ maxx = real_dstrect.x + real_dstrect.w;
+ maxy = real_dstrect.y + real_dstrect.h;
+
+ uv[0] = minu;
+ uv[1] = minv;
+ uv[2] = maxu;
+ uv[3] = minv;
+ uv[4] = maxu;
+ uv[5] = maxv;
+ uv[6] = minu;
+ uv[7] = maxv;
+
+ xy[0] = minx;
+ xy[1] = miny;
+ xy[2] = maxx;
+ xy[3] = miny;
+ xy[4] = maxx;
+ xy[5] = maxy;
+ xy[6] = minx;
+ xy[7] = maxy;
+
+ retval = QueueCmdGeometry(renderer, texture,
+ xy, xy_stride, (int *)color, color_stride, uv, uv_stride,
+ num_vertices,
+ indices, num_indices, size_indices,
+ renderer->scale.x, renderer->scale.y);
+ } else {
+
+ real_dstrect.x *= renderer->scale.x;
+ real_dstrect.y *= renderer->scale.y;
+ real_dstrect.w *= renderer->scale.x;
+ real_dstrect.h *= renderer->scale.y;
+
+ retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
+ }
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
}
@@ -3303,7 +3359,7 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
if (renderer != texture->renderer) {
return SDL_SetError("Texture was not created with this renderer");
}
- if (!renderer->QueueCopyEx) {
+ if (!renderer->QueueCopyEx && !renderer->QueueGeometry) {
return SDL_SetError("Renderer does not support RenderCopyEx");
}
@@ -3342,17 +3398,112 @@ SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
real_center.y = real_dstrect.h / 2.0f;
}
- real_dstrect.x *= renderer->scale.x;
- real_dstrect.y *= renderer->scale.y;
- real_dstrect.w *= renderer->scale.x;
- real_dstrect.h *= renderer->scale.y;
+ texture->last_command_generation = renderer->render_command_generation;
- real_center.x *= renderer->scale.x;
- real_center.y *= renderer->scale.y;
+ if (renderer->QueueGeometry && renderer->QueueCopyEx == NULL) {
+ float xy[8];
+ const int xy_stride = 2 * sizeof (float);
+ SDL_Color col, color[4];
+ const int color_stride = sizeof (SDL_Color);
+ float uv[8];
+ const int uv_stride = 2 * sizeof (float);
+ const int num_vertices = 4;
+ const int indices[6] = {0, 1, 2, 0, 2, 3};
+ const int num_indices = 6;
+ const int size_indices = 4;
+ float minu, minv, maxu, maxv;
+ float minx, miny, maxx, maxy;
+ float centerx, centery;
+
+ float s_minx, s_miny, s_maxx, s_maxy;
+ float c_minx, c_miny, c_maxx, c_maxy;
+
+ const float radian_angle = (float)((M_PI * angle) / 180.0);
+ const float s = SDL_sin(radian_angle);
+ const float c = SDL_cos(radian_angle);
+
+ SDL_GetTextureColorMod(texture, &col.r, &col.g, &col.b);
+ SDL_GetTextureAlphaMod(texture, &col.a);
+
+ color[0] = col;
+ color[1] = col;
+ color[2] = col;
+ color[3] = col;
+
+ minu = (float) (real_srcrect.x) / (float) texture->w;
+ minv = (float) (real_srcrect.y) / (float) texture->h;
+ maxu = (float) (real_srcrect.x + real_srcrect.w) / (float) texture->w;
+ maxv = (float) (real_srcrect.y + real_srcrect.h) / (float) texture->h;
+
+ centerx = real_center.x + real_dstrect.x;
+ centery = real_center.y + real_dstrect.y;
+
+ if (flip & SDL_FLIP_HORIZONTAL) {
+ minx = real_dstrect.x + real_dstrect.w;
+ maxx = real_dstrect.x;
+ } else {
+ minx = real_dstrect.x;
+ maxx = real_dstrect.x + real_dstrect.w;
+ }
- texture->last_command_generation = renderer->render_command_generation;
+ if (flip & SDL_FLIP_VERTICAL) {
+ miny = real_dstrect.y + real_dstrect.h;
+ maxy = real_dstrect.y;
+ } else {
+ miny = real_dstrect.y;
+ maxy = real_dstrect.y + real_dstrect.h;
+ }
+
+ uv[0] = minu;
+ uv[1] = minv;
+ uv[2] = maxu;
+ uv[3] = minv;
+ uv[4] = maxu;
+ uv[5] = maxv;
+ uv[6] = minu;
+ uv[7] = maxv;
+
+ /* apply rotation with 2x2 matrix ( c -s )
+ * ( s c ) */
+ s_minx = s * (minx - centerx);
+ s_miny = s * (miny - centery);
+ s_maxx = s * (maxx - centerx);
+ s_maxy = s * (maxy - centery);
+ c_minx = c * (minx - centerx);
+ c_miny = c * (miny - centery);
+ c_maxx = c * (maxx - centerx);
+ c_maxy = c * (maxy - centery);
+
+ /* (minx, miny) */
+ xy[0] = (c_minx - s_miny) + centerx;
+ xy[1] = (s_minx + c_miny) + centery;
+ /* (maxx, miny) */
+ xy[2] = (c_maxx - s_miny) + centerx;
+ xy[3] = (s_maxx + c_miny) + centery;
+ /* (maxx, maxy) */
+ xy[4] = (c_maxx - s_maxy) + centerx;
+ xy[5] = (s_maxx + c_maxy) + centery;
+ /* (minx, maxy) */
+ xy[6] = (c_minx - s_maxy) + centerx;
+ xy[7] = (s_minx + c_maxy) + centery;
+
+ retval = QueueCmdGeometry(renderer, texture,
+ xy, xy_stride, (int *)color, color_stride, uv, uv_stride,
+ num_vertices,
+ indices, num_indices, size_indices,
+ renderer->scale.x, renderer->scale.y);
+ } else {
- retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip);
+ real_dstrect.x *= renderer->scale.x;
+ real_dstrect.y *= renderer->scale.y;
+ real_dstrect.w *= renderer->scale.x;
+ real_dstrect.h *= renderer->scale.y;
+
+ real_center.x *= renderer->scale.x;
+ real_center.y *= renderer->scale.y;
+
+ retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip);
+ }
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
}
@@ -3375,7 +3526,6 @@ SDL_RenderGeometry(SDL_Renderer *renderer,
return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices, indices, num_indices, size_indices);
}
-#if SDL_HAVE_RENDER_GEOMETRY
static int
remap_one_indice(
int prev,
@@ -3718,7 +3868,6 @@ end:
return retval;
}
-#endif
int
SDL_RenderGeometryRaw(SDL_Renderer *renderer,
@@ -3729,7 +3878,6 @@ SDL_RenderGeometryRaw(SDL_Renderer *renderer,
int num_vertices,
const void *indices, int num_indices, int size_indices)
{
-#if SDL_HAVE_RENDER_GEOMETRY
int i;
int retval = 0;
int count = indices ? num_indices : num_vertices;
@@ -3832,9 +3980,6 @@ SDL_RenderGeometryRaw(SDL_Renderer *renderer,
renderer->scale.x, renderer->scale.y);
return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
-#else
- return SDL_SetError("SDL not built with RenderGeometry support");
-#endif
}