comparison src/video/SDL_renderer_gl.c @ 1985:8055185ae4ed

Added source color and alpha modulation support. Added perl script to generate optimized render copy functions.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 28 Aug 2006 03:17:39 +0000
parents ccef0d0c40c6
children f4c65e3bfaed
comparison
equal deleted inserted replaced
1984:b910bcabec26 1985:8055185ae4ed
29 #include "SDL_pixels_c.h" 29 #include "SDL_pixels_c.h"
30 #include "SDL_rect_c.h" 30 #include "SDL_rect_c.h"
31 #include "SDL_yuv_sw_c.h" 31 #include "SDL_yuv_sw_c.h"
32 32
33 /* OpenGL renderer implementation */ 33 /* OpenGL renderer implementation */
34
35 static const float inv255f = 1.0f / 255.0f;
34 36
35 static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags); 37 static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
36 static int GL_ActivateRenderer(SDL_Renderer * renderer); 38 static int GL_ActivateRenderer(SDL_Renderer * renderer);
37 static int GL_DisplayModeChanged(SDL_Renderer * renderer); 39 static int GL_DisplayModeChanged(SDL_Renderer * renderer);
38 static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); 40 static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
41 const SDL_Color * colors, int firstcolor, 43 const SDL_Color * colors, int firstcolor,
42 int ncolors); 44 int ncolors);
43 static int GL_GetTexturePalette(SDL_Renderer * renderer, 45 static int GL_GetTexturePalette(SDL_Renderer * renderer,
44 SDL_Texture * texture, SDL_Color * colors, 46 SDL_Texture * texture, SDL_Color * colors,
45 int firstcolor, int ncolors); 47 int firstcolor, int ncolors);
48 static int GL_SetTextureColorMod(SDL_Renderer * renderer,
49 SDL_Texture * texture);
50 static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
51 SDL_Texture * texture);
52 static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
53 SDL_Texture * texture);
54 static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
55 SDL_Texture * texture);
46 static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, 56 static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
47 const SDL_Rect * rect, const void *pixels, 57 const SDL_Rect * rect, const void *pixels,
48 int pitch); 58 int pitch);
49 static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, 59 static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
50 const SDL_Rect * rect, int markDirty, 60 const SDL_Rect * rect, int markDirty, void **pixels,
51 void **pixels, int *pitch); 61 int *pitch);
52 static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture); 62 static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
53 static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, 63 static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
54 int numrects, const SDL_Rect * rects); 64 int numrects, const SDL_Rect * rects);
55 static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, 65 static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
56 Uint32 color); 66 Uint8 a, const SDL_Rect * rect);
57 static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 67 static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
58 const SDL_Rect * srcrect, const SDL_Rect * dstrect, 68 const SDL_Rect * srcrect, const SDL_Rect * dstrect);
59 int blendMode, int scaleMode);
60 static void GL_RenderPresent(SDL_Renderer * renderer); 69 static void GL_RenderPresent(SDL_Renderer * renderer);
61 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture); 70 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
62 static void GL_DestroyRenderer(SDL_Renderer * renderer); 71 static void GL_DestroyRenderer(SDL_Renderer * renderer);
63 72
64 73
66 GL_CreateRenderer, 75 GL_CreateRenderer,
67 { 76 {
68 "opengl", 77 "opengl",
69 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD | 78 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
70 SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED), 79 SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
80 (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
81 SDL_TEXTUREMODULATE_ALPHA),
71 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK | 82 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
72 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD | 83 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
73 SDL_TEXTUREBLENDMODE_MOD), 84 SDL_TEXTUREBLENDMODE_MOD),
74 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST | 85 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
75 SDL_TEXTURESCALEMODE_SLOW), 86 SDL_TEXTURESCALEMODE_SLOW),
234 renderer->ActivateRenderer = GL_ActivateRenderer; 245 renderer->ActivateRenderer = GL_ActivateRenderer;
235 renderer->DisplayModeChanged = GL_DisplayModeChanged; 246 renderer->DisplayModeChanged = GL_DisplayModeChanged;
236 renderer->CreateTexture = GL_CreateTexture; 247 renderer->CreateTexture = GL_CreateTexture;
237 renderer->SetTexturePalette = GL_SetTexturePalette; 248 renderer->SetTexturePalette = GL_SetTexturePalette;
238 renderer->GetTexturePalette = GL_GetTexturePalette; 249 renderer->GetTexturePalette = GL_GetTexturePalette;
250 renderer->SetTextureColorMod = GL_SetTextureColorMod;
251 renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
252 renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
253 renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
239 renderer->UpdateTexture = GL_UpdateTexture; 254 renderer->UpdateTexture = GL_UpdateTexture;
240 renderer->LockTexture = GL_LockTexture; 255 renderer->LockTexture = GL_LockTexture;
241 renderer->UnlockTexture = GL_UnlockTexture; 256 renderer->UnlockTexture = GL_UnlockTexture;
242 renderer->DirtyTexture = GL_DirtyTexture; 257 renderer->DirtyTexture = GL_DirtyTexture;
243 renderer->RenderFill = GL_RenderFill; 258 renderer->RenderFill = GL_RenderFill;
568 renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, 583 renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
569 pitch / SDL_BYTESPERPIXEL(texture->format)); 584 pitch / SDL_BYTESPERPIXEL(texture->format));
570 } 585 }
571 586
572 static int 587 static int
588 GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
589 {
590 return -1;
591 }
592
593 static int
594 GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
595 {
596 return -1;
597 }
598
599 static int
600 GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
601 {
602 switch (texture->blendMode) {
603 case SDL_TEXTUREBLENDMODE_NONE:
604 case SDL_TEXTUREBLENDMODE_MASK:
605 case SDL_TEXTUREBLENDMODE_BLEND:
606 case SDL_TEXTUREBLENDMODE_ADD:
607 case SDL_TEXTUREBLENDMODE_MOD:
608 return 0;
609 default:
610 SDL_Unsupported();
611 texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
612 return -1;
613 }
614 }
615
616 static int
617 GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
618 {
619 switch (texture->scaleMode) {
620 case SDL_TEXTURESCALEMODE_NONE:
621 case SDL_TEXTURESCALEMODE_FAST:
622 case SDL_TEXTURESCALEMODE_SLOW:
623 return 0;
624 case SDL_TEXTURESCALEMODE_BEST:
625 SDL_Unsupported();
626 texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
627 return -1;
628 default:
629 SDL_Unsupported();
630 texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
631 return -1;
632 }
633 }
634
635 static int
573 GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, 636 GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
574 const SDL_Rect * rect, const void *pixels, int pitch) 637 const SDL_Rect * rect, const void *pixels, int pitch)
575 { 638 {
576 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; 639 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
577 GL_TextureData *data = (GL_TextureData *) texture->driverdata; 640 GL_TextureData *data = (GL_TextureData *) texture->driverdata;
634 SDL_AddDirtyRect(&data->dirty, &rects[i]); 697 SDL_AddDirtyRect(&data->dirty, &rects[i]);
635 } 698 }
636 } 699 }
637 700
638 static int 701 static int
639 GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color) 702 GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
703 const SDL_Rect * rect)
640 { 704 {
641 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; 705 GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
642 SDL_Window *window = SDL_GetWindowFromID(renderer->window); 706 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
643 GLclampf r, g, b, a; 707
644 708 data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
645 a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f; 709 (GLclampf) b * inv255f, (GLclampf) a * inv255f);
646 r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
647 g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
648 b = ((GLclampf) (color & 0xFF)) / 255.0f;
649
650 data->glClearColor(r, g, b, a);
651 data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h); 710 data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
652 data->glClear(GL_COLOR_BUFFER_BIT); 711 data->glClear(GL_COLOR_BUFFER_BIT);
653 data->glViewport(0, 0, window->w, window->h); 712 data->glViewport(0, 0, window->w, window->h);
654 return 0; 713 return 0;
655 } 714 }
656 715
657 static int 716 static int
658 GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 717 GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
659 const SDL_Rect * srcrect, const SDL_Rect * dstrect, 718 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
660 int blendMode, int scaleMode)
661 { 719 {
662 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; 720 GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
663 GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata; 721 GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
664 int minx, miny, maxx, maxy; 722 int minx, miny, maxx, maxy;
665 GLfloat minu, maxu, minv, maxv; 723 GLfloat minu, maxu, minv, maxv;
698 maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; 756 maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
699 maxv *= texturedata->texh; 757 maxv *= texturedata->texh;
700 758
701 data->glBindTexture(texturedata->type, texturedata->texture); 759 data->glBindTexture(texturedata->type, texturedata->texture);
702 760
703 if (blendMode != data->blendMode) { 761 if (texture->modMode) {
704 switch (blendMode) { 762 data->glColor4f((GLfloat) texture->r * inv255f,
763 (GLfloat) texture->g * inv255f,
764 (GLfloat) texture->b * inv255f,
765 (GLfloat) texture->a * inv255f);
766 } else {
767 data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
768 }
769
770 if (texture->blendMode != data->blendMode) {
771 switch (texture->blendMode) {
705 case SDL_TEXTUREBLENDMODE_NONE: 772 case SDL_TEXTUREBLENDMODE_NONE:
706 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 773 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
707 data->glDisable(GL_BLEND); 774 data->glDisable(GL_BLEND);
708 break; 775 break;
709 case SDL_TEXTUREBLENDMODE_MASK: 776 case SDL_TEXTUREBLENDMODE_MASK:
721 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 788 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
722 data->glEnable(GL_BLEND); 789 data->glEnable(GL_BLEND);
723 data->glBlendFunc(GL_ZERO, GL_SRC_COLOR); 790 data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
724 break; 791 break;
725 } 792 }
726 data->blendMode = blendMode; 793 data->blendMode = texture->blendMode;
727 } 794 }
728 795
729 if (scaleMode != data->scaleMode) { 796 if (texture->scaleMode != data->scaleMode) {
730 switch (scaleMode) { 797 switch (texture->scaleMode) {
731 case SDL_TEXTURESCALEMODE_NONE: 798 case SDL_TEXTURESCALEMODE_NONE:
732 case SDL_TEXTURESCALEMODE_FAST: 799 case SDL_TEXTURESCALEMODE_FAST:
733 data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, 800 data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
734 GL_NEAREST); 801 GL_NEAREST);
735 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, 802 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
741 GL_LINEAR); 808 GL_LINEAR);
742 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, 809 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
743 GL_LINEAR); 810 GL_LINEAR);
744 break; 811 break;
745 } 812 }
746 data->scaleMode = scaleMode; 813 data->scaleMode = texture->scaleMode;
747 } 814 }
748 815
749 data->glBegin(GL_TRIANGLE_STRIP); 816 data->glBegin(GL_TRIANGLE_STRIP);
750 data->glTexCoord2f(minu, minv); 817 data->glTexCoord2f(minu, minv);
751 data->glVertex2i(minx, miny); 818 data->glVertex2i(minx, miny);