this repo has no description
0
fork

Configure Feed

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

Added hotspot coordinates to GPU_Image so the drawing, scaling, and rotation pivot can be set (by GPU_SetHotspot() and GPU_SetDefaultHotspot()). This means SDL_gpu can be set to draw just like SDL_BlitSurface() and SDL_RenderCopy() do.

+223 -36
+15
include/SDL_gpu.h
··· 231 231 Uint16 texture_w, texture_h; // Underlying texture dimensions 232 232 Uint8 has_mipmaps; 233 233 234 + float hotspot_x; // Normalized coords for the point at which the image is blitted. Default is (0.5, 0.5), that is, the image is drawn centered. 235 + float hotspot_y; // These are always interpreted as inverted. (0,0) is in the upper left and would draw as SDL_BlitSurface() does. 236 + 234 237 SDL_Color color; 235 238 Uint8 use_blending; 236 239 GPU_BlendMode blend_mode; ··· 590 593 /*! 0 for inverted, 1 for mathematical */ 591 594 Uint8 coordinate_mode; 592 595 596 + /*! Default is (0.5, 0.5) - images draw centered. */ 597 + float default_image_hotspot_x; 598 + float default_image_hotspot_y; 599 + 593 600 struct GPU_RendererImpl* impl; 594 601 }; 595 602 ··· 784 791 785 792 DECLSPEC Uint8 SDLCALL GPU_GetCoordinateMode(void); 786 793 794 + /*! Sets the default image blitting hotspot for newly created images. 795 + * \see GPU_SetHotspot 796 + */ 797 + DECLSPEC void SDLCALL GPU_SetDefaultHotspot(float hotspot_x, float hotspot_y); 798 + 787 799 // End of RendererControls 788 800 /*! @} */ 789 801 ··· 1054 1066 1055 1067 /*! Sets the image filtering mode, if supported by the renderer. */ 1056 1068 DECLSPEC void SDLCALL GPU_SetImageFilter(GPU_Image* image, GPU_FilterEnum filter); 1069 + 1070 + /*! Sets the image hotspot, which is the point about which the image is blitted. The default is to blit the image on-center (0.5, 0.5). The hotspot is in inverted (+y down) normalized coordinates (0.0-1.0). */ 1071 + DECLSPEC void SDLCALL GPU_SetHotspot(GPU_Image* image, float hotspot_x, float hotspot_y); 1057 1072 1058 1073 /*! Gets the current pixel snap setting. The default value is GPU_SNAP_POSITION_AND_DIMENSIONS. */ 1059 1074 DECLSPEC GPU_SnapEnum SDLCALL GPU_GetSnapMode(GPU_Image* image);
+19 -4
src/SDL_gpu.c
··· 815 815 if(y != NULL) 816 816 *y = displayY; 817 817 } 818 - 819 - // Invert coordinates to math coords 820 - if(_gpu_current_renderer->coordinate_mode) 821 - *y = target->h - *y; 822 818 } 823 819 824 820 GPU_Rect GPU_MakeRect(float x, float y, float w, float h) ··· 1682 1678 return; 1683 1679 1684 1680 _gpu_current_renderer->impl->SetImageFilter(_gpu_current_renderer, image, filter); 1681 + } 1682 + 1683 + 1684 + void GPU_SetDefaultHotspot(float hotspot_x, float hotspot_y) 1685 + { 1686 + if(_gpu_current_renderer == NULL) 1687 + return; 1688 + 1689 + _gpu_current_renderer->default_image_hotspot_x = hotspot_x; 1690 + _gpu_current_renderer->default_image_hotspot_y = hotspot_y; 1691 + } 1692 + 1693 + void GPU_SetHotspot(GPU_Image* image, float hotspot_x, float hotspot_y) 1694 + { 1695 + if(image == NULL) 1696 + return; 1697 + 1698 + image->hotspot_x = hotspot_x; 1699 + image->hotspot_y = hotspot_y; 1685 1700 } 1686 1701 1687 1702 GPU_SnapEnum GPU_GetSnapMode(GPU_Image* image)
+3
src/renderer_GLES_1.c
··· 40 40 renderer->min_shader_version = 0; 41 41 renderer->max_shader_version = 0; 42 42 43 + renderer->default_image_hotspot_x = 0.5f; 44 + renderer->default_image_hotspot_y = 0.5f; 45 + 43 46 renderer->current_context_target = NULL; 44 47 45 48 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
src/renderer_GLES_2.c
··· 44 44 renderer->min_shader_version = 100; 45 45 renderer->max_shader_version = SDL_GPU_GLSL_VERSION; 46 46 47 + renderer->default_image_hotspot_x = 0.5f; 48 + renderer->default_image_hotspot_y = 0.5f; 49 + 47 50 renderer->current_context_target = NULL; 48 51 49 52 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
src/renderer_GLES_3.c
··· 44 44 renderer->min_shader_version = 100; 45 45 renderer->max_shader_version = SDL_GPU_GLSL_VERSION; 46 46 47 + renderer->default_image_hotspot_x = 0.5f; 48 + renderer->default_image_hotspot_y = 0.5f; 49 + 47 50 renderer->current_context_target = NULL; 48 51 49 52 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+26 -32
src/renderer_GL_common.inl
··· 2053 2053 result->num_layers = num_layers; 2054 2054 result->bytes_per_pixel = bytes_per_pixel; 2055 2055 result->has_mipmaps = 0; 2056 - 2056 + 2057 + result->hotspot_x = renderer->default_image_hotspot_x; 2058 + result->hotspot_y = renderer->default_image_hotspot_y; 2059 + 2057 2060 result->color = white; 2058 2061 result->use_blending = 1; 2059 2062 result->blend_mode = GPU_GetBlendModeFromPreset(GPU_BLEND_NORMAL); ··· 2300 2303 result->num_layers = num_layers; 2301 2304 result->bytes_per_pixel = bytes_per_pixel; 2302 2305 result->has_mipmaps = 0; 2306 + 2307 + result->hotspot_x = renderer->default_image_hotspot_x; 2308 + result->hotspot_y = renderer->default_image_hotspot_y; 2303 2309 2304 2310 result->color = white; 2305 2311 result->use_blending = 1; ··· 3991 3997 } 3992 3998 3993 3999 // Center the image on the given coords 3994 - dx1 = x - w/2.0f; 3995 - dy1 = y - h/2.0f; 3996 - dx2 = x + w/2.0f; 3997 - dy2 = y + h/2.0f; 4000 + dx1 = x - w * image->hotspot_x; 4001 + dy1 = y - h * image->hotspot_y; 4002 + dx2 = x + w * (1.0f - image->hotspot_x); 4003 + dy2 = y + h * (1.0f - image->hotspot_y); 3998 4004 3999 4005 if(image->snap_mode == GPU_SNAP_DIMENSIONS || image->snap_mode == GPU_SNAP_POSITION_AND_DIMENSIONS) 4000 4006 { ··· 4085 4091 4086 4092 w = (src_rect == NULL? image->w : src_rect->w); 4087 4093 h = (src_rect == NULL? image->h : src_rect->h); 4088 - renderer->impl->BlitTransformX(renderer, image, src_rect, target, x, y, w/2.0f, h/2.0f, degrees, 1.0f, 1.0f); 4094 + renderer->impl->BlitTransformX(renderer, image, src_rect, target, x, y, w*image->hotspot_x, h*image->hotspot_y, degrees, 1.0f, 1.0f); 4089 4095 } 4090 4096 4091 4097 static void BlitScale(GPU_Renderer* renderer, GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y, float scaleX, float scaleY) ··· 4104 4110 4105 4111 w = (src_rect == NULL? image->w : src_rect->w); 4106 4112 h = (src_rect == NULL? image->h : src_rect->h); 4107 - renderer->impl->BlitTransformX(renderer, image, src_rect, target, x, y, w/2.0f, h/2.0f, 0.0f, scaleX, scaleY); 4113 + renderer->impl->BlitTransformX(renderer, image, src_rect, target, x, y, w*image->hotspot_x, h*image->hotspot_y, 0.0f, scaleX, scaleY); 4108 4114 } 4109 4115 4110 4116 static void BlitTransform(GPU_Renderer* renderer, GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y, float degrees, float scaleX, float scaleY) ··· 4123 4129 4124 4130 w = (src_rect == NULL? image->w : src_rect->w); 4125 4131 h = (src_rect == NULL? image->h : src_rect->h); 4126 - renderer->impl->BlitTransformX(renderer, image, src_rect, target, x, y, w/2.0f, h/2.0f, degrees, scaleX, scaleY); 4132 + renderer->impl->BlitTransformX(renderer, image, src_rect, target, x, y, w*image->hotspot_x, h*image->hotspot_y, degrees, scaleX, scaleY); 4127 4133 } 4128 4134 4129 4135 static void BlitTransformX(GPU_Renderer* renderer, GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y, float pivot_x, float pivot_y, float degrees, float scaleX, float scaleY) ··· 4219 4225 y2 *= image->base_h/(float)image->h; 4220 4226 } 4221 4227 4222 - // Center the image on the given coords (offset later) 4223 - dx1 = -w/2.0f; 4224 - dy1 = -h/2.0f; 4225 - dx2 = w/2.0f; 4226 - dy2 = h/2.0f; 4228 + // Create vertices about the hotspot 4229 + dx1 = -pivot_x; 4230 + dy1 = -pivot_y; 4231 + dx2 = w - pivot_x; 4232 + dy2 = h - pivot_y; 4227 4233 4228 4234 if(image->snap_mode == GPU_SNAP_DIMENSIONS || image->snap_mode == GPU_SNAP_POSITION_AND_DIMENSIONS) 4229 4235 { ··· 4246 4252 4247 4253 // Apply transforms 4248 4254 4249 - // Scale 4255 + // Scale about the hotspot 4250 4256 if(scaleX != 1.0f || scaleY != 1.0f) 4251 4257 { 4252 - float w = (dx2 - dx1)*scaleX; 4253 - float h = (dy2 - dy1)*scaleY; 4254 - dx1 = (dx2 + dx1)/2 - w/2; 4255 - dx2 = dx1 + w; 4256 - dy1 = (dy2 + dy1)/2 - h/2; 4257 - dy2 = dy1 + h; 4258 + dx1 *= scaleX; 4259 + dy1 *= scaleY; 4260 + dx2 *= scaleX; 4261 + dy2 *= scaleY; 4258 4262 } 4259 4263 4260 - // Shift away from the center (these are relative to the image corner) 4261 - pivot_x -= w/2.0f; 4262 - pivot_y -= h/2.0f; 4263 - 4264 - // Translate origin to pivot 4265 - dx1 -= pivot_x*scaleX; 4266 - dy1 -= pivot_y*scaleY; 4267 - dx2 -= pivot_x*scaleX; 4268 - dy2 -= pivot_y*scaleY; 4269 - 4270 4264 // Get extra vertices for rotation 4271 4265 dx3 = dx2; 4272 4266 dy3 = dy1; 4273 4267 dx4 = dx1; 4274 4268 dy4 = dy2; 4275 4269 4276 - // Rotate about origin (the pivot) 4270 + // Rotate about the hotspot 4277 4271 if(degrees != 0.0f) 4278 4272 { 4279 4273 float cosA = cos(degrees*M_PI/180); ··· 4292 4286 dy4 = tempX*sinA + dy4*cosA; 4293 4287 } 4294 4288 4295 - // Translate to pos 4289 + // Translate to final position 4296 4290 dx1 += x; 4297 4291 dx2 += x; 4298 4292 dx3 += x;
+3
src/renderer_OpenGL_1.c
··· 39 39 renderer->min_shader_version = 110; 40 40 renderer->max_shader_version = SDL_GPU_GLSL_VERSION; 41 41 42 + renderer->default_image_hotspot_x = 0.5f; 43 + renderer->default_image_hotspot_y = 0.5f; 44 + 42 45 renderer->current_context_target = NULL; 43 46 44 47 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
src/renderer_OpenGL_1_BASE.c
··· 38 38 renderer->min_shader_version = 0; 39 39 renderer->max_shader_version = 0; 40 40 41 + renderer->default_image_hotspot_x = 0.5f; 42 + renderer->default_image_hotspot_y = 0.5f; 43 + 41 44 renderer->current_context_target = NULL; 42 45 43 46 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
src/renderer_OpenGL_2.c
··· 37 37 renderer->min_shader_version = 110; 38 38 renderer->max_shader_version = SDL_GPU_GLSL_VERSION; 39 39 40 + renderer->default_image_hotspot_x = 0.5f; 41 + renderer->default_image_hotspot_y = 0.5f; 42 + 40 43 renderer->current_context_target = NULL; 41 44 42 45 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
src/renderer_OpenGL_3.c
··· 41 41 renderer->min_shader_version = 110; 42 42 renderer->max_shader_version = SDL_GPU_GLSL_VERSION; 43 43 44 + renderer->default_image_hotspot_x = 0.5f; 45 + renderer->default_image_hotspot_y = 0.5f; 46 + 44 47 renderer->current_context_target = NULL; 45 48 46 49 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
src/renderer_OpenGL_4.c
··· 39 39 renderer->min_shader_version = 110; 40 40 renderer->max_shader_version = SDL_GPU_GLSL_VERSION; 41 41 42 + renderer->default_image_hotspot_x = 0.5f; 43 + renderer->default_image_hotspot_y = 0.5f; 44 + 42 45 renderer->current_context_target = NULL; 43 46 44 47 renderer->impl = (GPU_RendererImpl*)SDL_malloc(sizeof(GPU_RendererImpl));
+3
tests/CMakeLists.txt
··· 130 130 add_executable(transform-matrix-test transform-matrix/main.c) 131 131 target_link_libraries (transform-matrix-test ${TEST_LIBS}) 132 132 133 + add_executable(hotspot-test hotspot/main.c) 134 + target_link_libraries (hotspot-test ${TEST_LIBS}) 135 + 133 136 add_executable(video-test video/main.c) 134 137 target_link_libraries (video-test ${TEST_LIBS})
+136
tests/hotspot/main.c
··· 1 + #include "SDL.h" 2 + #include "SDL_gpu.h" 3 + #include "common.h" 4 + #include "math.h" 5 + 6 + 7 + int main(int argc, char* argv[]) 8 + { 9 + GPU_Target* screen; 10 + 11 + screen = initialize_demo(argc, argv, 800, 600); 12 + if(screen == NULL) 13 + return 1; 14 + 15 + { 16 + Uint32 startTime; 17 + long frameCount; 18 + Uint8 done; 19 + SDL_Event event; 20 + float dt = 0.010f; 21 + 22 + const Uint8* keystates = SDL_GetKeyboardState(NULL); 23 + 24 + Uint8 mode = 0; 25 + 26 + float hotspot_x = 0.5f; 27 + float hotspot_y = 0.5f; 28 + 29 + float angle = 0.0f; 30 + float scale = 1.0f; 31 + 32 + GPU_Image* image = GPU_LoadImage("data/test.bmp"); 33 + if(image == NULL) 34 + return 2; 35 + 36 + startTime = SDL_GetTicks(); 37 + frameCount = 0; 38 + 39 + done = 0; 40 + while(!done) 41 + { 42 + while(SDL_PollEvent(&event)) 43 + { 44 + if(event.type == SDL_QUIT) 45 + done = 1; 46 + else if(event.type == SDL_KEYDOWN) 47 + { 48 + if(event.key.keysym.sym == SDLK_ESCAPE) 49 + done = 1; 50 + if(event.key.keysym.sym == SDLK_1) 51 + { 52 + hotspot_x = 0.5f; 53 + hotspot_y = 0.5f; 54 + } 55 + if(event.key.keysym.sym == SDLK_2) 56 + { 57 + hotspot_x = 0.0f; 58 + hotspot_y = 0.0f; 59 + } 60 + if(event.key.keysym.sym == SDLK_3) 61 + { 62 + hotspot_x = 1.0f; 63 + hotspot_y = 1.0f; 64 + } 65 + if(event.key.keysym.sym == SDLK_4) 66 + { 67 + hotspot_x = 1.0f; 68 + hotspot_y = 0.5f; 69 + } 70 + if(event.key.keysym.sym == SDLK_SPACE) 71 + mode = !mode; 72 + } 73 + } 74 + 75 + if(keystates[SDL_SCANCODE_UP]) 76 + hotspot_y -= dt; 77 + if(keystates[SDL_SCANCODE_DOWN]) 78 + hotspot_y += dt; 79 + if(keystates[SDL_SCANCODE_LEFT]) 80 + hotspot_x -= dt; 81 + if(keystates[SDL_SCANCODE_RIGHT]) 82 + hotspot_x += dt; 83 + 84 + GPU_SetHotspot(image, hotspot_x, hotspot_y); 85 + 86 + GPU_Clear(screen); 87 + 88 + if(!mode) 89 + { 90 + GPU_Blit(image, NULL, screen, 0, 0); 91 + GPU_Blit(image, NULL, screen, screen->w, 0); 92 + GPU_Blit(image, NULL, screen, 0, screen->h); 93 + GPU_Blit(image, NULL, screen, screen->w, screen->h); 94 + GPU_Blit(image, NULL, screen, screen->w/2, screen->h/2); 95 + } 96 + else 97 + { 98 + angle = fmodf(SDL_GetTicks()/100.0f, 360.0f); 99 + scale = 1 + 0.5f*sin(SDL_GetTicks()/1000.0f); 100 + 101 + GPU_BlitTransform(image, NULL, screen, 0, 0, angle, scale, scale); 102 + GPU_BlitTransform(image, NULL, screen, screen->w, 0, angle, scale, scale); 103 + GPU_BlitTransform(image, NULL, screen, 0, screen->h, angle, scale, scale); 104 + GPU_BlitTransform(image, NULL, screen, screen->w, screen->h, angle, scale, scale); 105 + GPU_BlitTransform(image, NULL, screen, screen->w/2, screen->h/2, angle, scale, scale); 106 + } 107 + 108 + GPU_Rectangle(screen, screen->w/2 - image->w/2, screen->h/2 - image->h/2, screen->w/2 + image->w/2, screen->h/2 + image->h/2, GPU_MakeColor(0, 255, 255, 255)); 109 + 110 + GPU_CircleFilled(screen, screen->w/2 - image->w/2 + image->hotspot_x * image->w, screen->h/2 - image->h/2 + image->hotspot_y * image->h, 5, GPU_MakeColor(255, 0, 0, 255)); 111 + 112 + GPU_Circle(screen, 0, 0, 5, GPU_MakeColor(255, 128, 0, 255)); 113 + GPU_Circle(screen, screen->w, 0, 5, GPU_MakeColor(255, 128, 0, 255)); 114 + GPU_Circle(screen, 0, screen->h, 5, GPU_MakeColor(255, 128, 0, 255)); 115 + GPU_Circle(screen, screen->w, screen->h, 5, GPU_MakeColor(255, 128, 0, 255)); 116 + GPU_Circle(screen, screen->w/2, screen->h/2, 5, GPU_MakeColor(255, 128, 0, 255)); 117 + 118 + 119 + GPU_Flip(screen); 120 + 121 + frameCount++; 122 + if(frameCount%500 == 0) 123 + printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 124 + } 125 + 126 + printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 127 + 128 + GPU_FreeImage(image); 129 + } 130 + 131 + GPU_Quit(); 132 + 133 + return 0; 134 + } 135 + 136 +