Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1744 lines (1497 loc) · 47.4 KB

SDL_render.c

File metadata and controls

1744 lines (1497 loc) · 47.4 KB
 
Jun 22, 2011
Jun 22, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Jun 22, 2011
Jun 22, 2011
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
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
/* The SDL 2D rendering system */
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_render.h"
#include "SDL_sysrender.h"
#include "software/SDL_render_sw_c.h"
#define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
#define CHECK_RENDERER_MAGIC(renderer, retval) \
if (!renderer || renderer->magic != &renderer_magic) { \
SDL_SetError("Invalid renderer"); \
return retval; \
}
#define CHECK_TEXTURE_MAGIC(texture, retval) \
if (!texture || texture->magic != &texture_magic) { \
SDL_SetError("Invalid texture"); \
return retval; \
}
static const SDL_RenderDriver *render_drivers[] = {
#if !SDL_RENDER_DISABLED
#if SDL_VIDEO_RENDER_D3D
&D3D_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_OGL
&GL_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_OGL_ES2
&GLES2_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_OGL_ES
&GLES_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_DIRECTFB
&DirectFB_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_NDS
&NDS_RenderDriver,
#endif
&SW_RenderDriver
#endif /* !SDL_RENDER_DISABLED */
};
static char renderer_magic;
static char texture_magic;
Oct 2, 2012
Oct 2, 2012
73
74
static int UpdateLogicalSize(SDL_Renderer *renderer);
Jun 22, 2011
Jun 22, 2011
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
int
SDL_GetNumRenderDrivers(void)
{
return SDL_arraysize(render_drivers);
}
int
SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
{
if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
SDL_SetError("index must be in the range of 0 - %d",
SDL_GetNumRenderDrivers() - 1);
return -1;
}
*info = render_drivers[index]->info;
return 0;
}
static int
SDL_RendererEventWatch(void *userdata, SDL_Event *event)
{
SDL_Renderer *renderer = (SDL_Renderer *)userdata;
if (event->type == SDL_WINDOWEVENT) {
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
if (window == renderer->window) {
if (renderer->WindowEvent) {
renderer->WindowEvent(renderer, &event->window);
}
Jan 23, 2012
Jan 23, 2012
105
if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
Oct 2, 2012
Oct 2, 2012
106
107
if (renderer->logical_w) {
/* We'll update the renderer in the SIZE_CHANGED event */
Jan 22, 2012
Jan 22, 2012
108
} else {
Oct 2, 2012
Oct 2, 2012
109
110
111
112
113
114
115
116
117
118
119
120
/* Try to keep the previous viewport centered */
int w, h;
SDL_GetWindowSize(window, &w, &h);
if (renderer->target) {
renderer->viewport_backup.x = (w - renderer->viewport_backup.w) / 2;
renderer->viewport_backup.y = (h - renderer->viewport_backup.h) / 2;
} else {
renderer->viewport.x = (w - renderer->viewport.w) / 2;
renderer->viewport.y = (h - renderer->viewport.h) / 2;
renderer->UpdateViewport(renderer);
}
Jan 22, 2012
Jan 22, 2012
121
}
Jan 23, 2012
Jan 23, 2012
122
123
renderer->resized = SDL_TRUE;
} else if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
Oct 2, 2012
Oct 2, 2012
124
125
126
if (renderer->logical_w) {
UpdateLogicalSize(renderer);
} else if (!renderer->resized) {
Jan 23, 2012
Jan 23, 2012
127
/* Window was programmatically resized, reset viewport */
Oct 2, 2012
Oct 2, 2012
128
129
int w, h;
Jan 23, 2012
Jan 23, 2012
130
131
132
133
134
135
136
SDL_GetWindowSize(window, &w, &h);
if (renderer->target) {
renderer->viewport_backup.x = 0;
renderer->viewport_backup.y = 0;
renderer->viewport_backup.w = w;
renderer->viewport_backup.h = h;
} else {
Oct 2, 2012
Oct 2, 2012
137
138
139
140
141
renderer->viewport.x = 0;
renderer->viewport.y = 0;
renderer->viewport.w = w;
renderer->viewport.h = h;
renderer->UpdateViewport(renderer);
Jan 23, 2012
Jan 23, 2012
142
143
}
}
Oct 2, 2012
Oct 2, 2012
144
renderer->resized = SDL_FALSE;
Jan 23, 2012
Jan 23, 2012
145
146
147
148
149
150
} else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
renderer->hidden = SDL_TRUE;
} else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
renderer->hidden = SDL_FALSE;
}
Nov 8, 2011
Nov 8, 2011
151
} else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
Jan 23, 2012
Jan 23, 2012
152
renderer->hidden = SDL_TRUE;
Nov 8, 2011
Nov 8, 2011
153
} else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
Jan 23, 2012
Jan 23, 2012
154
155
156
if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
renderer->hidden = SDL_FALSE;
}
Jun 22, 2011
Jun 22, 2011
157
158
}
}
Oct 2, 2012
Oct 2, 2012
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
} else if (event->type == SDL_MOUSEMOTION) {
if (renderer->logical_w) {
event->motion.x -= renderer->viewport.x;
event->motion.y -= renderer->viewport.y;
event->motion.x = (int)(event->motion.x / renderer->scale.x);
event->motion.y = (int)(event->motion.y / renderer->scale.y);
}
} else if (event->type == SDL_MOUSEBUTTONDOWN ||
event->type == SDL_MOUSEBUTTONUP) {
if (renderer->logical_w) {
event->button.x -= renderer->viewport.x;
event->button.y -= renderer->viewport.y;
event->button.x = (int)(event->button.x / renderer->scale.x);
event->button.y = (int)(event->button.y / renderer->scale.y);
}
Jun 22, 2011
Jun 22, 2011
174
175
176
177
}
return 0;
}
Jan 23, 2012
Jan 23, 2012
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
int
SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
SDL_Window **window, SDL_Renderer **renderer)
{
*window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height, window_flags);
if (!*window) {
*renderer = NULL;
return -1;
}
*renderer = SDL_CreateRenderer(*window, -1, 0);
if (!*renderer) {
return -1;
}
return 0;
}
Jun 22, 2011
Jun 22, 2011
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
SDL_Renderer *
SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
{
SDL_Renderer *renderer = NULL;
int n = SDL_GetNumRenderDrivers();
const char *hint;
if (!window) {
SDL_SetError("Invalid window");
return NULL;
}
if (SDL_GetRenderer(window)) {
SDL_SetError("Renderer already associated with window");
return NULL;
}
hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
if (hint) {
if (*hint == '0') {
flags &= ~SDL_RENDERER_PRESENTVSYNC;
} else {
flags |= SDL_RENDERER_PRESENTVSYNC;
}
}
if (index < 0) {
hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
if (hint) {
for (index = 0; index < n; ++index) {
const SDL_RenderDriver *driver = render_drivers[index];
if (SDL_strcasecmp(hint, driver->info.name) == 0) {
/* Create a new renderer instance */
renderer = driver->CreateRenderer(window, flags);
break;
}
}
}
if (!renderer) {
for (index = 0; index < n; ++index) {
const SDL_RenderDriver *driver = render_drivers[index];
if ((driver->info.flags & flags) == flags) {
/* Create a new renderer instance */
renderer = driver->CreateRenderer(window, flags);
if (renderer) {
/* Yay, we got one! */
break;
}
}
}
}
if (index == n) {
SDL_SetError("Couldn't find matching render driver");
return NULL;
}
} else {
if (index >= SDL_GetNumRenderDrivers()) {
SDL_SetError("index must be -1 or in the range of 0 - %d",
SDL_GetNumRenderDrivers() - 1);
return NULL;
}
/* Create a new renderer instance */
renderer = render_drivers[index]->CreateRenderer(window, flags);
}
if (renderer) {
renderer->magic = &renderer_magic;
renderer->window = window;
Oct 2, 2012
Oct 2, 2012
269
270
renderer->scale.x = 1.0f;
renderer->scale.y = 1.0f;
Jun 22, 2011
Jun 22, 2011
271
Jan 23, 2012
Jan 23, 2012
272
273
if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
renderer->hidden = SDL_TRUE;
Nov 8, 2011
Nov 8, 2011
274
} else {
Jan 23, 2012
Jan 23, 2012
275
renderer->hidden = SDL_FALSE;
Nov 8, 2011
Nov 8, 2011
276
277
}
Jun 22, 2011
Jun 22, 2011
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
SDL_RenderSetViewport(renderer, NULL);
SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
"Created renderer: %s", renderer->info.name);
}
return renderer;
}
SDL_Renderer *
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
{
#if !SDL_RENDER_DISABLED
SDL_Renderer *renderer;
renderer = SW_CreateRendererForSurface(surface);
if (renderer) {
renderer->magic = &renderer_magic;
Oct 2, 2012
Oct 2, 2012
300
301
renderer->scale.x = 1.0f;
renderer->scale.y = 1.0f;
Jun 22, 2011
Jun 22, 2011
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
SDL_RenderSetViewport(renderer, NULL);
}
return renderer;
#else
SDL_SetError("SDL not built with rendering support");
return NULL;
#endif /* !SDL_RENDER_DISABLED */
}
SDL_Renderer *
SDL_GetRenderer(SDL_Window * window)
{
return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
}
int
SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
{
CHECK_RENDERER_MAGIC(renderer, -1);
*info = renderer->info;
return 0;
}
static SDL_bool
IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
{
Uint32 i;
for (i = 0; i < renderer->info.num_texture_formats; ++i) {
if (renderer->info.texture_formats[i] == format) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static Uint32
GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
{
Uint32 i;
if (SDL_ISPIXELFORMAT_FOURCC(format)) {
/* Look for an exact match */
for (i = 0; i < renderer->info.num_texture_formats; ++i) {
if (renderer->info.texture_formats[i] == format) {
return renderer->info.texture_formats[i];
}
}
} else {
SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
/* We just want to match the first format that has the same channels */
for (i = 0; i < renderer->info.num_texture_formats; ++i) {
if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
return renderer->info.texture_formats[i];
}
}
}
return renderer->info.texture_formats[0];
}
SDL_Texture *
SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
{
SDL_Texture *texture;
CHECK_RENDERER_MAGIC(renderer, NULL);
if (!format) {
format = renderer->info.texture_formats[0];
}
if (SDL_ISPIXELFORMAT_INDEXED(format)) {
SDL_SetError("Palettized textures are not supported");
return NULL;
}
if (w <= 0 || h <= 0) {
SDL_SetError("Texture dimensions can't be 0");
return NULL;
}
texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
if (!texture) {
SDL_OutOfMemory();
return NULL;
}
texture->magic = &texture_magic;
texture->format = format;
texture->access = access;
texture->w = w;
texture->h = h;
texture->r = 255;
texture->g = 255;
texture->b = 255;
texture->a = 255;
texture->renderer = renderer;
texture->next = renderer->textures;
if (renderer->textures) {
renderer->textures->prev = texture;
}
renderer->textures = texture;
if (IsSupportedFormat(renderer, format)) {
if (renderer->CreateTexture(renderer, texture) < 0) {
SDL_DestroyTexture(texture);
return 0;
}
} else {
texture->native = SDL_CreateTexture(renderer,
GetClosestSupportedFormat(renderer, format),
access, w, h);
if (!texture->native) {
SDL_DestroyTexture(texture);
return NULL;
}
Sep 28, 2012
Sep 28, 2012
419
420
/* Swap textures to have texture before texture->native in the list */
texture->native->next = texture->next;
Oct 2, 2012
Oct 2, 2012
421
422
423
if (texture->native->next) {
texture->native->next->prev = texture->native;
}
Sep 28, 2012
Sep 28, 2012
424
texture->prev = texture->native->prev;
Oct 2, 2012
Oct 2, 2012
425
426
427
if (texture->prev) {
texture->prev->next = texture;
}
Sep 28, 2012
Sep 28, 2012
428
429
430
431
texture->native->prev = texture;
texture->next = texture->native;
renderer->textures = texture;
Jun 22, 2011
Jun 22, 2011
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
if (!texture->yuv) {
SDL_DestroyTexture(texture);
return NULL;
}
} else if (access == SDL_TEXTUREACCESS_STREAMING) {
/* The pitch is 4 byte aligned */
texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
texture->pixels = SDL_calloc(1, texture->pitch * h);
if (!texture->pixels) {
SDL_DestroyTexture(texture);
return NULL;
}
}
}
return texture;
}
SDL_Texture *
SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
{
const SDL_PixelFormat *fmt;
SDL_bool needAlpha;
Uint32 i;
Uint32 format;
SDL_Texture *texture;
CHECK_RENDERER_MAGIC(renderer, NULL);
if (!surface) {
SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
return NULL;
}
/* See what the best texture format is */
fmt = surface->format;
if (fmt->Amask || SDL_GetColorKey(surface, NULL) == 0) {
needAlpha = SDL_TRUE;
} else {
needAlpha = SDL_FALSE;
}
format = renderer->info.texture_formats[0];
for (i = 0; i < renderer->info.num_texture_formats; ++i) {
if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
format = renderer->info.texture_formats[i];
break;
}
}
texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
surface->w, surface->h);
if (!texture) {
return NULL;
}
if (format == surface->format->format) {
if (SDL_MUSTLOCK(surface)) {
SDL_LockSurface(surface);
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
SDL_UnlockSurface(surface);
} else {
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
}
} else {
SDL_PixelFormat *dst_fmt;
SDL_Surface *temp = NULL;
/* Set up a destination surface for the texture update */
dst_fmt = SDL_AllocFormat(format);
temp = SDL_ConvertSurface(surface, dst_fmt, 0);
SDL_FreeFormat(dst_fmt);
if (temp) {
SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
SDL_FreeSurface(temp);
} else {
SDL_DestroyTexture(texture);
return NULL;
}
}
{
Uint8 r, g, b, a;
SDL_BlendMode blendMode;
SDL_GetSurfaceColorMod(surface, &r, &g, &b);
SDL_SetTextureColorMod(texture, r, g, b);
SDL_GetSurfaceAlphaMod(surface, &a);
SDL_SetTextureAlphaMod(texture, a);
if (SDL_GetColorKey(surface, NULL) == 0) {
/* We converted to a texture with alpha format */
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
} else {
SDL_GetSurfaceBlendMode(surface, &blendMode);
SDL_SetTextureBlendMode(texture, blendMode);
}
}
return texture;
}
int
SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
int *w, int *h)
{
CHECK_TEXTURE_MAGIC(texture, -1);
if (format) {
*format = texture->format;
}
if (access) {
*access = texture->access;
}
if (w) {
*w = texture->w;
}
if (h) {
*h = texture->h;
}
return 0;
}
int
SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
{
SDL_Renderer *renderer;
CHECK_TEXTURE_MAGIC(texture, -1);
renderer = texture->renderer;
if (r < 255 || g < 255 || b < 255) {
texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
} else {
texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
}
texture->r = r;
texture->g = g;
texture->b = b;
if (texture->native) {
return SDL_SetTextureColorMod(texture->native, r, g, b);
} else if (renderer->SetTextureColorMod) {
return renderer->SetTextureColorMod(renderer, texture);
} else {
return 0;
}
}
int
SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
Uint8 * b)
{
CHECK_TEXTURE_MAGIC(texture, -1);
if (r) {
*r = texture->r;
}
if (g) {
*g = texture->g;
}
if (b) {
*b = texture->b;
}
return 0;
}
int
SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
{
SDL_Renderer *renderer;
CHECK_TEXTURE_MAGIC(texture, -1);
renderer = texture->renderer;
if (alpha < 255) {
texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
} else {
texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
}
texture->a = alpha;
if (texture->native) {
return SDL_SetTextureAlphaMod(texture->native, alpha);
} else if (renderer->SetTextureAlphaMod) {
return renderer->SetTextureAlphaMod(renderer, texture);
} else {
return 0;
}
}
int
SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
{
CHECK_TEXTURE_MAGIC(texture, -1);
if (alpha) {
*alpha = texture->a;
}
return 0;
}
int
SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
{
SDL_Renderer *renderer;
CHECK_TEXTURE_MAGIC(texture, -1);
renderer = texture->renderer;
texture->blendMode = blendMode;
if (texture->native) {
return SDL_SetTextureBlendMode(texture->native, blendMode);
} else if (renderer->SetTextureBlendMode) {
return renderer->SetTextureBlendMode(renderer, texture);
} else {
return 0;
}
}
int
SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
{
CHECK_TEXTURE_MAGIC(texture, -1);
if (blendMode) {
*blendMode = texture->blendMode;
}
return 0;
}
static int
SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
const void *pixels, int pitch)
{
SDL_Texture *native = texture->native;
SDL_Rect full_rect;
if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
return -1;
}
full_rect.x = 0;
full_rect.y = 0;
full_rect.w = texture->w;
full_rect.h = texture->h;
rect = &full_rect;
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
/* We can lock the texture and copy to it */
void *native_pixels;
int native_pitch;
if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
return -1;
}
SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
rect->w, rect->h, native_pixels, native_pitch);
SDL_UnlockTexture(native);
} else {
/* Use a temporary buffer for updating */
void *temp_pixels;
int temp_pitch;
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) {
SDL_OutOfMemory();
return -1;
}
SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
rect->w, rect->h, temp_pixels, temp_pitch);
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
SDL_free(temp_pixels);
}
return 0;
}
static int
SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
const void *pixels, int pitch)
{
SDL_Texture *native = texture->native;
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
/* We can lock the texture and copy to it */
void *native_pixels;
int native_pitch;
if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
return -1;
}
SDL_ConvertPixels(rect->w, rect->h,
texture->format, pixels, pitch,
native->format, native_pixels, native_pitch);
SDL_UnlockTexture(native);
} else {
/* Use a temporary buffer for updating */
void *temp_pixels;
int temp_pitch;
temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
temp_pixels = SDL_malloc(rect->h * temp_pitch);
if (!temp_pixels) {
SDL_OutOfMemory();
return -1;
}
SDL_ConvertPixels(rect->w, rect->h,
texture->format, pixels, pitch,
native->format, temp_pixels, temp_pitch);
SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
SDL_free(temp_pixels);
}
return 0;
}
int
SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
const void *pixels, int pitch)
{
SDL_Renderer *renderer;
SDL_Rect full_rect;
CHECK_TEXTURE_MAGIC(texture, -1);
if (!rect) {
full_rect.x = 0;
full_rect.y = 0;
full_rect.w = texture->w;
full_rect.h = texture->h;
rect = &full_rect;
}
if (texture->yuv) {
return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
} else if (texture->native) {
return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
} else {
renderer = texture->renderer;
return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
}
}
static int
SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
void **pixels, int *pitch)
{
return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
}
static int
SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
void **pixels, int *pitch)
{
texture->locked_rect = *rect;
*pixels = (void *) ((Uint8 *) texture->pixels +
rect->y * texture->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = texture->pitch;
return 0;
}
int
SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
void **pixels, int *pitch)
{
SDL_Renderer *renderer;
SDL_Rect full_rect;
CHECK_TEXTURE_MAGIC(texture, -1);
if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
SDL_SetError("SDL_LockTexture(): texture must be streaming");
return -1;
}
if (!rect) {
full_rect.x = 0;
full_rect.y = 0;
full_rect.w = texture->w;
full_rect.h = texture->h;
rect = &full_rect;
}
if (texture->yuv) {
return SDL_LockTextureYUV(texture, rect, pixels, pitch);
} else if (texture->native) {
return SDL_LockTextureNative(texture, rect, pixels, pitch);
} else {
renderer = texture->renderer;
return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
}
}
static void
SDL_UnlockTextureYUV(SDL_Texture * texture)
{
SDL_Texture *native = texture->native;
void *native_pixels;
int native_pitch;
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = texture->w;
rect.h = texture->h;
if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
return;
}
SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
rect.w, rect.h, native_pixels, native_pitch);
SDL_UnlockTexture(native);
}
Oct 31, 2011
Oct 31, 2011
846
static void
Jun 22, 2011
Jun 22, 2011
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
SDL_UnlockTextureNative(SDL_Texture * texture)
{
SDL_Texture *native = texture->native;
void *native_pixels;
int native_pitch;
const SDL_Rect *rect = &texture->locked_rect;
const void* pixels = (void *) ((Uint8 *) texture->pixels +
rect->y * texture->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
int pitch = texture->pitch;
if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
return;
}
SDL_ConvertPixels(rect->w, rect->h,
texture->format, pixels, pitch,
native->format, native_pixels, native_pitch);
SDL_UnlockTexture(native);
}
void
SDL_UnlockTexture(SDL_Texture * texture)
{
SDL_Renderer *renderer;
CHECK_TEXTURE_MAGIC(texture, );
if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
return;
}
if (texture->yuv) {
SDL_UnlockTextureYUV(texture);
} else if (texture->native) {
SDL_UnlockTextureNative(texture);
} else {
renderer = texture->renderer;
renderer->UnlockTexture(renderer, texture);
}
}
Jan 22, 2012
Jan 22, 2012
887
888
889
SDL_bool
SDL_RenderTargetSupported(SDL_Renderer *renderer)
{
Jan 22, 2012
Jan 22, 2012
890
if (!renderer || !renderer->SetRenderTarget) {
Jan 22, 2012
Jan 22, 2012
891
892
893
894
895
896
return SDL_FALSE;
}
return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
}
int
Jan 22, 2012
Jan 22, 2012
897
SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
Jan 22, 2012
Jan 22, 2012
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
{
if (!SDL_RenderTargetSupported(renderer)) {
SDL_Unsupported();
return -1;
}
if (texture == renderer->target) {
/* Nothing to do! */
return 0;
}
/* texture == NULL is valid and means reset the target to the window */
if (texture) {
CHECK_TEXTURE_MAGIC(texture, -1);
if (renderer != texture->renderer) {
SDL_SetError("Texture was not created with this renderer");
return -1;
}
Jun 21, 2012
Jun 21, 2012
915
if (texture->access != SDL_TEXTUREACCESS_TARGET) {
Jan 22, 2012
Jan 22, 2012
916
917
918
919
920
921
922
923
924
925
926
927
SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
return -1;
}
if (texture->native) {
/* Always render to the native texture */
texture = texture->native;
}
}
if (texture && !renderer->target) {
/* Make a backup of the viewport */
renderer->viewport_backup = renderer->viewport;
Oct 2, 2012
Oct 2, 2012
928
renderer->scale_backup = renderer->scale;
Jan 22, 2012
Jan 22, 2012
929
930
931
}
renderer->target = texture;
Jan 22, 2012
Jan 22, 2012
932
if (renderer->SetRenderTarget(renderer, texture) < 0) {
Jan 22, 2012
Jan 22, 2012
933
934
935
936
return -1;
}
if (texture) {
Oct 2, 2012
Oct 2, 2012
937
938
939
940
941
942
renderer->viewport.x = 0;
renderer->viewport.y = 0;
renderer->viewport.w = texture->w;
renderer->viewport.h = texture->h;
renderer->scale.x = 1.0f;
renderer->scale.y = 1.0f;
Jan 22, 2012
Jan 22, 2012
943
} else {
Oct 2, 2012
Oct 2, 2012
944
945
renderer->viewport = renderer->viewport_backup;
renderer->scale = renderer->scale_backup;
Jan 22, 2012
Jan 22, 2012
946
}
Oct 2, 2012
Oct 2, 2012
947
if (renderer->UpdateViewport(renderer) < 0) {
Jan 22, 2012
Jan 22, 2012
948
949
950
951
952
953
954
return -1;
}
/* All set! */
return 0;
}
Oct 12, 2012
Oct 12, 2012
955
956
957
958
959
960
SDL_Texture *
SDL_GetRenderTarget(SDL_Renderer *renderer)
{
return renderer->target;
}
Oct 2, 2012
Oct 2, 2012
961
962
963
964
965
966
967
968
969
static int
UpdateLogicalSize(SDL_Renderer *renderer)
{
int w, h;
float want_aspect;
float real_aspect;
float scale;
SDL_Rect viewport;
Oct 12, 2012
Oct 12, 2012
970
971
972
if (renderer->target) {
SDL_QueryTexture(renderer->target, NULL, NULL, &w, &h);
} else if (renderer->window) {
Oct 2, 2012
Oct 2, 2012
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SDL_GetWindowSize(renderer->window, &w, &h);
} else {
/* FIXME */
SDL_SetError("Internal error: No way to get output resolution");
return -1;
}
want_aspect = (float)renderer->logical_w / renderer->logical_h;
real_aspect = (float)w / h;
/* Clear the scale because we're setting viewport in output coordinates */
SDL_RenderSetScale(renderer, 1.0f, 1.0f);
if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
/* The aspect ratios are the same, just scale appropriately */
scale = (float)w / renderer->logical_w;
SDL_RenderSetViewport(renderer, NULL);
} else if (want_aspect > real_aspect) {
/* We want a wider aspect ratio than is available - letterbox it */
scale = (float)w / renderer->logical_w;
viewport.x = 0;
viewport.w = w;
viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
viewport.y = (h - viewport.h) / 2;
SDL_RenderSetViewport(renderer, &viewport);
} else {
/* We want a narrower aspect ratio than is available - use side-bars */
scale = (float)h / renderer->logical_h;