this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Added renderer->coordinate_mode to support GPU_SetCoordinateMode() and GPU_GetCoordinateMode(). Added coordinate-mode-test. Added GPU_UnsetViewport().

+250 -7
+13
include/SDL_gpu.h
··· 592 592 /*! Current display target */ 593 593 GPU_Target* current_context_target; 594 594 595 + /*! 0 for inverted, 1 for mathematical */ 596 + Uint8 coordinate_mode; 597 + 595 598 struct GPU_RendererImpl* impl; 596 599 }; 597 600 ··· 779 782 /*! Reapplies the renderer state to the backend API (e.g. OpenGL, Direct3D). Use this if you want SDL_gpu to be able to render after you've used direct backend calls. */ 780 783 DECLSPEC void SDLCALL GPU_ResetRendererState(void); 781 784 785 + /*! Sets the coordinate mode for this renderer. Target and image coordinates will be either "inverted" (0,0 is the upper left corner, y increases downward) or "mathematical" (0,0 is the bottom-left corner, y increases upward). 786 + * The default is inverted (0), as this is traditional for 2D graphics. 787 + * \param inverted 0 is for inverted coordinates, 1 is for mathematical coordinates */ 788 + DECLSPEC void SDLCALL GPU_SetCoordinateMode(Uint8 use_math_coords); 789 + 790 + DECLSPEC Uint8 SDLCALL GPU_GetCoordinateMode(void); 791 + 782 792 // End of RendererControls 783 793 /*! @} */ 784 794 ··· 879 889 880 890 /*! Sets the given target's viewport. */ 881 891 DECLSPEC void SDLCALL GPU_SetViewport(GPU_Target* target, GPU_Rect viewport); 892 + 893 + /*! Resets the given target's viewport to the entire target area. */ 894 + DECLSPEC void SDLCALL GPU_UnsetViewport(GPU_Target* target); 882 895 883 896 /*! \return A GPU_Camera with position (0, 0, -10), angle of 0, and zoom of 1. */ 884 897 DECLSPEC GPU_Camera SDLCALL GPU_GetDefaultCamera(void);
+22
src/SDL_gpu.c
··· 78 78 _gpu_current_renderer->impl->ResetRendererState(_gpu_current_renderer); 79 79 } 80 80 81 + void GPU_SetCoordinateMode(Uint8 use_math_coords) 82 + { 83 + if(_gpu_current_renderer == NULL) 84 + return; 85 + 86 + _gpu_current_renderer->coordinate_mode = use_math_coords; 87 + } 88 + 89 + Uint8 GPU_GetCoordinateMode(void) 90 + { 91 + if(_gpu_current_renderer == NULL) 92 + return 0; 93 + 94 + return _gpu_current_renderer->coordinate_mode; 95 + } 96 + 81 97 GPU_Renderer* GPU_GetCurrentRenderer(void) 82 98 { 83 99 return _gpu_current_renderer; ··· 770 786 { 771 787 if(target != NULL) 772 788 target->viewport = viewport; 789 + } 790 + 791 + void GPU_UnsetViewport(GPU_Target* target) 792 + { 793 + if(target != NULL) 794 + target->viewport = GPU_MakeRect(0, 0, target->w, target->h); 773 795 } 774 796 775 797 GPU_Camera GPU_GetDefaultCamera(void)
+28 -7
src/renderer_GL_common.inl
··· 483 483 glEnable(GL_SCISSOR_TEST); 484 484 if(target->context != NULL) 485 485 { 486 - int y = context_target->h - (target->clip_rect.y + target->clip_rect.h); 486 + int y; 487 + if(renderer->coordinate_mode == 0) 488 + y = context_target->h - (target->clip_rect.y + target->clip_rect.h); 489 + else 490 + y = target->clip_rect.y; 487 491 float xFactor = ((float)context_target->context->window_w)/context_target->w; 488 492 float yFactor = ((float)context_target->context->window_h)/context_target->h; 489 493 glScissor(target->clip_rect.x * xFactor, y * yFactor, target->clip_rect.w * xFactor, target->clip_rect.h * yFactor); ··· 729 733 730 734 cdata->last_viewport = viewport; 731 735 732 - // Need the real height to flip the y-coord (from OpenGL coord system) 733 736 y = viewport.y; 734 - if(target->image != NULL) 735 - y = target->image->h - viewport.h - viewport.y; 736 - else if(target->context != NULL) 737 - y = target->context->window_h - viewport.h - viewport.y; 737 + if(GPU_GetCoordinateMode() == 0) 738 + { 739 + // Need the real height to flip the y-coord (from OpenGL coord system) 740 + if(target->image != NULL) 741 + y = target->image->h - viewport.h - viewport.y; 742 + else if(target->context != NULL) 743 + y = target->context->window_h - viewport.h - viewport.y; 744 + } 738 745 739 746 glViewport(viewport.x, y, viewport.w, viewport.h); 740 747 } ··· 762 769 GPU_MatrixMode( GPU_PROJECTION ); 763 770 GPU_LoadIdentity(); 764 771 765 - if(!invert) 772 + if((!invert ^ GPU_GetCoordinateMode())) 766 773 GPU_Ortho(target->camera.x, target->w + target->camera.x, target->h + target->camera.y, target->camera.y, -1.0f, 1.0f); 767 774 else 768 775 GPU_Ortho(target->camera.x, target->w + target->camera.x, target->camera.y, target->h + target->camera.y, -1.0f, 1.0f); // Special inverted orthographic projection because tex coords are inverted already for render-to-texture ··· 3621 3628 dy1 += fractional; 3622 3629 dy2 += fractional; 3623 3630 } 3631 + 3632 + if(renderer->coordinate_mode == 1) 3633 + { 3634 + float temp = dy1; 3635 + dy1 = dy2; 3636 + dy2 = temp; 3637 + } 3624 3638 3625 3639 cdata = (GPU_CONTEXT_DATA*)renderer->current_context_target->context->data; 3626 3640 ··· 3843 3857 fractional = h/2.0f - floorf(h/2.0f); 3844 3858 dy1 += fractional; 3845 3859 dy2 += fractional; 3860 + } 3861 + 3862 + if(renderer->coordinate_mode == 1) 3863 + { 3864 + float temp = dy1; 3865 + dy1 = dy2; 3866 + dy2 = temp; 3846 3867 } 3847 3868 3848 3869 // Apply transforms
+3
tests/CMakeLists.txt
··· 114 114 add_executable(image-formats-test image-formats/main.c) 115 115 target_link_libraries (image-formats-test ${TEST_LIBS}) 116 116 117 + add_executable(coordinate-mode-test coordinate-mode/main.c) 118 + target_link_libraries (coordinate-mode-test ${TEST_LIBS}) 119 + 117 120 add_executable(sandbox-test sandbox/main.c) 118 121 target_link_libraries (sandbox-test ${TEST_LIBS}) 119 122
+184
tests/coordinate-mode/main.c
··· 1 + #include "SDL.h" 2 + #include "SDL_gpu.h" 3 + #include "compat.h" 4 + #include "common.h" 5 + 6 + 7 + int main(int argc, char* argv[]) 8 + { 9 + GPU_Target* screen; 10 + 11 + printRenderers(); 12 + 13 + screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); 14 + if(screen == NULL) 15 + return -1; 16 + 17 + printCurrentRenderer(); 18 + 19 + GPU_SetCoordinateMode(1); 20 + 21 + { 22 + Uint32 startTime; 23 + long frameCount; 24 + Uint8 done; 25 + SDL_Event event; 26 + 27 + float dt; 28 + const Uint8* keystates; 29 + 30 + #define MAX_SPRITES 50 31 + int numSprites = 1; 32 + 33 + float x[MAX_SPRITES]; 34 + float y[MAX_SPRITES]; 35 + float velx[MAX_SPRITES]; 36 + float vely[MAX_SPRITES]; 37 + int i; 38 + 39 + GPU_Rect small_viewport = GPU_MakeRect(600, 500, 100, 100); 40 + GPU_Rect viewport = GPU_MakeRect(100, 100, 600, 400); 41 + 42 + GPU_Image* image = GPU_LoadImage("data/test.bmp"); 43 + if(image == NULL) 44 + return -1; 45 + 46 + startTime = SDL_GetTicks(); 47 + frameCount = 0; 48 + 49 + for(i = 0; i < MAX_SPRITES; i++) 50 + { 51 + x[i] = rand()%screen->w; 52 + y[i] = rand()%screen->h; 53 + velx[i] = 10 + rand()%screen->w/10; 54 + vely[i] = 10 + rand()%screen->h/10; 55 + } 56 + 57 + 58 + keystates = SDL_GetKeyState(NULL); 59 + 60 + dt = 0.010f; 61 + done = 0; 62 + while(!done) 63 + { 64 + while(SDL_PollEvent(&event)) 65 + { 66 + if(event.type == SDL_QUIT) 67 + done = 1; 68 + else if(event.type == SDL_KEYDOWN) 69 + { 70 + if(event.key.keysym.sym == SDLK_ESCAPE) 71 + done = 1; 72 + else if(event.key.keysym.sym == SDLK_r) 73 + { 74 + viewport = GPU_MakeRect(0.0f, 0.0f, screen->w, screen->h); 75 + } 76 + else if(event.key.keysym.sym == SDLK_EQUALS || event.key.keysym.sym == SDLK_PLUS) 77 + { 78 + if(numSprites < MAX_SPRITES) 79 + numSprites++; 80 + } 81 + else if(event.key.keysym.sym == SDLK_MINUS) 82 + { 83 + if(numSprites > 0) 84 + numSprites--; 85 + } 86 + else if(event.key.keysym.sym == SDLK_SPACE) 87 + { 88 + GPU_SetCoordinateMode(!GPU_GetCoordinateMode()); 89 + } 90 + } 91 + } 92 + 93 + if(keystates[KEY_UP]) 94 + viewport.y -= 100*dt; 95 + else if(keystates[KEY_DOWN]) 96 + viewport.y += 100*dt; 97 + 98 + if(keystates[KEY_LEFT]) 99 + viewport.x -= 100*dt; 100 + else if(keystates[KEY_RIGHT]) 101 + viewport.x += 100*dt; 102 + 103 + if(keystates[KEY_w]) 104 + viewport.h -= 100*dt; 105 + else if(keystates[KEY_s]) 106 + viewport.h += 100*dt; 107 + 108 + if(keystates[KEY_a]) 109 + viewport.w -= 100*dt; 110 + else if(keystates[KEY_d]) 111 + viewport.w += 100*dt; 112 + 113 + for(i = 0; i < numSprites; i++) 114 + { 115 + x[i] += velx[i]*dt; 116 + y[i] += vely[i]*dt; 117 + if(x[i] < 0) 118 + { 119 + x[i] = 0; 120 + velx[i] = -velx[i]; 121 + } 122 + else if(x[i]> screen->w) 123 + { 124 + x[i] = screen->w; 125 + velx[i] = -velx[i]; 126 + } 127 + 128 + if(y[i] < 0) 129 + { 130 + y[i] = 0; 131 + vely[i] = -vely[i]; 132 + } 133 + else if(y[i]> screen->h) 134 + { 135 + y[i] = screen->h; 136 + vely[i] = -vely[i]; 137 + } 138 + } 139 + 140 + 141 + GPU_Clear(screen); 142 + 143 + 144 + GPU_SetClipRect(screen, small_viewport); 145 + GPU_ClearRGBA(screen, 0, 100, 0, 0); 146 + 147 + GPU_SetViewport(screen, small_viewport); 148 + for(i = 0; i < numSprites; i++) 149 + { 150 + GPU_Blit(image, NULL, screen, x[i], y[i]); 151 + } 152 + 153 + 154 + GPU_SetClipRect(screen, viewport); 155 + GPU_ClearRGBA(screen, 0, 0, 100, 0); 156 + 157 + GPU_SetViewport(screen, viewport); 158 + for(i = 0; i < numSprites; i++) 159 + { 160 + GPU_Blit(image, NULL, screen, x[i], y[i]); 161 + } 162 + 163 + GPU_UnsetClip(screen); 164 + GPU_UnsetViewport(screen); 165 + GPU_BlitRotate(image, NULL, screen, 10, 10, SDL_GetTicks()/1000.0f * 100); 166 + 167 + GPU_Flip(screen); 168 + 169 + frameCount++; 170 + if(frameCount%500 == 0) 171 + printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 172 + } 173 + 174 + printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 175 + 176 + GPU_FreeImage(image); 177 + } 178 + 179 + GPU_Quit(); 180 + 181 + return 0; 182 + } 183 + 184 +