this repo has no description
0
fork

Configure Feed

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

Added GPU_FileFormatEnum. Added extra GPU_FileFormatEnum parameter to GPU_SaveSurface() and GPU_SaveImage() so they can save to a particular format regardless of the type. Use GPU_FILE_AUTO for the previous behavior of auto-detecting from file extension.

Added an internal implementation of strcasecmp() because SDL_strcasecmp() was hanging.

+185 -72
+18 -11
demos/camera/main.c
··· 134 134 else 135 135 target = screen; 136 136 } 137 - else if(event.key.keysym.sym == SDLK_SPACE) 138 - { 139 - int mx, my; 140 - float x, y; 141 - SDL_GetMouseState(&mx, &my); 142 - GPU_GetVirtualCoords(screen, &x, &y, mx, my); 143 - 144 - printf("Angle: %.1f\n", camera.angle); 145 - printScreenToWorld(x, y); 146 - printWorldToScreen(50, 50); 147 - } 137 + else if (event.key.keysym.sym == SDLK_SPACE) 138 + { 139 + if(screen->using_virtual_resolution) 140 + GPU_UnsetVirtualResolution(screen); 141 + else 142 + GPU_SetVirtualResolution(screen, 400, 400); 143 + } 148 144 else if(event.key.keysym.sym == SDLK_w) 149 145 { 150 146 camera.y -= 100; ··· 161 157 { 162 158 camera.x += 100; 163 159 } 160 + } 161 + else if(event.type == SDL_MOUSEBUTTONDOWN) 162 + { 163 + int mx, my; 164 + float x, y; 165 + SDL_GetMouseState(&mx, &my); 166 + GPU_GetVirtualCoords(screen, &x, &y, mx, my); 167 + 168 + printf("Angle: %.1f\n", camera.angle); 169 + printScreenToWorld(x, y); 170 + printWorldToScreen(50, 50); 164 171 } 165 172 } 166 173
+1 -1
demos/copy/main.c
··· 47 47 48 48 // Copying from a surface dump 49 49 surface = GPU_CopySurfaceFromImage(image); 50 - //GPU_SaveSurface(surface, "save_surf1.bmp"); 50 + //GPU_SaveSurface(surface, "save_surf1.bmp", GPU_FILE_AUTO); 51 51 image3 = GPU_CopyImageFromSurface(surface); 52 52 SDL_FreeSurface(surface); 53 53
+1 -1
demos/pixel-perfect/main.c
··· 45 45 SDL_Color color = (i%2 == 0? GPU_MakeColor(255, 255, 255, 255) : GPU_MakeColor(0, 0, 255, 255)); 46 46 GPU_Rectangle(gen->target, i, i, gen->w-i-1, gen->h-1-i, color); 47 47 } 48 - GPU_SaveImage(gen, "data/pixel_perfect_odd.png");*/ 48 + GPU_SaveImage(gen, "data/pixel_perfect_odd.png", GPU_FILE_AUTO);*/ 49 49 50 50 image2 = GPU_LoadImage("data/pixel_perfect_odd.png"); 51 51 if(image2 == NULL)
+1 -25
demos/sandbox/main.c
··· 29 29 int y; 30 30 Uint8 done; 31 31 SDL_Event event; 32 - 33 - GPU_Image* image[5]; 34 - int i; 35 - 36 - for(i = 0; i < 5; i++) 37 - { 38 - image[i] = GPU_CreateImage(200, 200, GPU_FORMAT_RGBA); 39 - GPU_LoadTarget(image[i]); 40 - } 41 32 42 33 font_surface = GPU_LoadSurface("data/comic14.png"); 43 34 font = FONT_Alloc(font_surface); ··· 92 83 GPU_RectangleRound(screen, 200, 20, 250, 50, 5, white); 93 84 GPU_SetLineThickness(1); 94 85 95 - for(i = 0; i < 5; i++) 96 - { 97 - GPU_Clear(image[i]->target); 98 - GPU_CircleFilled(image[i]->target, 40 + i*10, 40 + i*10, 30, GPU_MakeColor(255/5 * (i+1), 255, 255/5 * (i+1), 255)); 99 - } 100 - 101 - GPU_Blit(image[0], NULL, image[1]->target, 100, 100); 102 - GPU_Blit(image[2], NULL, image[3]->target, 100, 100); 103 - GPU_Blit(image[1], NULL, image[4]->target, 100, 100); 104 - GPU_Blit(image[3], NULL, image[4]->target, 100, 100); 105 - 106 - for(i = 0; i < 5; i++) 107 - { 108 - GPU_Blit(image[i], NULL, screen, 100 + image[i]->w * i, 100); 109 - } 110 - 86 + GPU_RectangleRound(screen, 20 + x/7.0f, 100 + y/13.0f, 90 + x*1.101f, 300 + y*1.005f, 5, white); 111 87 112 88 GPU_Flip(screen); 113 89
+1 -1
demos/save/main.c
··· 40 40 } 41 41 42 42 GPU_LogError("Saving image\n"); 43 - GPU_SaveImage(image, SAVE_FILE); 43 + GPU_SaveImage(image, SAVE_FILE, GPU_FILE_AUTO); 44 44 45 45 GPU_LogError("Reloading image\n"); 46 46 image1 = GPU_LoadImage(SAVE_FILE);
+20 -4
include/SDL_gpu.h
··· 207 207 GPU_FORMAT_YCbCr420P = 8 208 208 } GPU_FormatEnum; 209 209 210 + /*! \ingroup ImageControls 211 + * File format enum 212 + * \see GPU_SaveSurface() 213 + * \see GPU_SaveImage() 214 + */ 215 + typedef enum { 216 + GPU_FILE_AUTO = 0, 217 + GPU_FILE_PNG, 218 + GPU_FILE_BMP, 219 + GPU_FILE_TGA 220 + } GPU_FileFormatEnum; 221 + 210 222 211 223 212 224 /*! \ingroup ImageControls ··· 910 922 /*! Load surface from an image file that is supported by this renderer. Don't forget to SDL_FreeSurface() it. */ 911 923 DECLSPEC SDL_Surface* SDLCALL GPU_LoadSurface(const char* filename); 912 924 913 - /*! Save surface to a file. The file type is deduced from the extension. Supported formats are: png, bmp, tga. Returns 0 on failure. */ 914 - DECLSPEC Uint8 SDLCALL GPU_SaveSurface(SDL_Surface* surface, const char* filename); 925 + /*! Save surface to a file. 926 + * With a format of GPU_FILE_AUTO, the file type is deduced from the extension. Supported formats are: png, bmp, tga. 927 + * Returns 0 on failure. */ 928 + DECLSPEC Uint8 SDLCALL GPU_SaveSurface(SDL_Surface* surface, const char* filename, GPU_FileFormatEnum format); 915 929 916 930 // End of SurfaceControls 917 931 /*! @} */ ··· 954 968 /*! Update an image from an array of pixel data. */ 955 969 DECLSPEC void SDLCALL GPU_UpdateImageBytes(GPU_Image* image, const GPU_Rect* image_rect, const unsigned char* bytes, int bytes_per_row); 956 970 957 - /*! Save image to a file. The file type is deduced from the extension. Supported formats are: png, bmp, tga. Returns 0 on failure. */ 958 - DECLSPEC Uint8 SDLCALL GPU_SaveImage(GPU_Image* image, const char* filename); 971 + /*! Save image to a file. 972 + * With a format of GPU_FILE_AUTO, the file type is deduced from the extension. Supported formats are: png, bmp, tga. 973 + * Returns 0 on failure. */ 974 + DECLSPEC Uint8 SDLCALL GPU_SaveImage(GPU_Image* image, const char* filename, GPU_FileFormatEnum format); 959 975 960 976 /*! Loads mipmaps for the given image, if supported by the renderer. */ 961 977 DECLSPEC void SDLCALL GPU_GenerateMipmaps(GPU_Image* image);
+1 -1
include/SDL_gpu_RendererImpl.h
··· 68 68 GPU_Image* (SDLCALL *CreateAliasImage)(GPU_Renderer* renderer, GPU_Image* image); 69 69 70 70 /*! \see GPU_SaveImage() */ 71 - Uint8 (SDLCALL *SaveImage)(GPU_Renderer* renderer, GPU_Image* image, const char* filename); 71 + Uint8 (SDLCALL *SaveImage)(GPU_Renderer* renderer, GPU_Image* image, const char* filename, GPU_FileFormatEnum format); 72 72 73 73 /*! \see GPU_CopyImage() */ 74 74 GPU_Image* (SDLCALL *CopyImage)(GPU_Renderer* renderer, GPU_Image* image);
+109 -15
src/SDL_gpu.c
··· 25 25 #define CHECK_CONTEXT (current_renderer->current_context_target != NULL) 26 26 #define RETURN_ERROR(code, details) do{ GPU_PushErrorCode(__func__, code, "%s", details); return; } while(0) 27 27 28 + int GPU_strcasecmp(const char* s1, const char* s2); 29 + 28 30 void GPU_InitRendererRegister(void); 29 31 GPU_Renderer* GPU_AddRenderer(GPU_RendererID id); 30 32 void GPU_RemoveRenderer(GPU_RendererID id); ··· 739 741 return current_renderer->impl->CreateAliasImage(current_renderer, image); 740 742 } 741 743 742 - Uint8 GPU_SaveImage(GPU_Image* image, const char* filename) 744 + Uint8 GPU_SaveImage(GPU_Image* image, const char* filename, GPU_FileFormatEnum format) 743 745 { 744 746 if(current_renderer == NULL || current_renderer->current_context_target == NULL) 745 747 return 0; 746 748 747 - return current_renderer->impl->SaveImage(current_renderer, image, filename); 749 + return current_renderer->impl->SaveImage(current_renderer, image, filename, format); 748 750 } 749 751 750 752 GPU_Image* GPU_CopyImage(GPU_Image* image) ··· 896 898 return dot + 1; 897 899 } 898 900 899 - Uint8 GPU_SaveSurface(SDL_Surface* surface, const char* filename) 901 + Uint8 GPU_SaveSurface(SDL_Surface* surface, const char* filename, GPU_FileFormatEnum format) 900 902 { 901 - const char* extension; 902 903 Uint8 result; 903 904 unsigned char* data; 904 905 ··· 908 909 return 0; 909 910 } 910 911 911 - extension = get_filename_ext(filename); 912 912 913 913 data = surface->pixels; 914 - 915 - if(SDL_strcasecmp(extension, "png") == 0) 916 - result = (stbi_write_png(filename, surface->w, surface->h, surface->format->BytesPerPixel, (const unsigned char *const)data, 0) > 0); 917 - else if(SDL_strcasecmp(extension, "bmp") == 0) 918 - result = (stbi_write_bmp(filename, surface->w, surface->h, surface->format->BytesPerPixel, (void*)data) > 0); 919 - else if(SDL_strcasecmp(extension, "tga") == 0) 920 - result = (stbi_write_tga(filename, surface->w, surface->h, surface->format->BytesPerPixel, (void*)data) > 0); 921 - else 914 + 915 + if(format == GPU_FILE_AUTO) 916 + { 917 + const char* extension = get_filename_ext(filename); 918 + if(GPU_strcasecmp(extension, "png") == 0) 919 + format = GPU_FILE_PNG; 920 + else if(GPU_strcasecmp(extension, "bmp") == 0) 921 + format = GPU_FILE_BMP; 922 + else if(GPU_strcasecmp(extension, "tga") == 0) 923 + format = GPU_FILE_TGA; 924 + else 925 + { 926 + GPU_PushErrorCode(__func__, GPU_ERROR_DATA_ERROR, "Could not detect output file format from file name"); 927 + return 0; 928 + } 929 + } 930 + 931 + switch(format) 922 932 { 923 - GPU_PushErrorCode(__func__, GPU_ERROR_DATA_ERROR, "Unsupported output file format"); 924 - result = 0; 933 + case GPU_FILE_PNG: 934 + result = (stbi_write_png(filename, surface->w, surface->h, surface->format->BytesPerPixel, (const unsigned char *const)data, 0) > 0); 935 + break; 936 + case GPU_FILE_BMP: 937 + result = (stbi_write_bmp(filename, surface->w, surface->h, surface->format->BytesPerPixel, (void*)data) > 0); 938 + break; 939 + case GPU_FILE_TGA: 940 + result = (stbi_write_tga(filename, surface->w, surface->h, surface->format->BytesPerPixel, (void*)data) > 0); 941 + break; 942 + default: 943 + GPU_PushErrorCode(__func__, GPU_ERROR_DATA_ERROR, "Unsupported output file format"); 944 + result = 0; 945 + break; 925 946 } 926 947 927 948 return result; ··· 2646 2667 return; 2647 2668 2648 2669 current_renderer->impl->SetAttributeSource(current_renderer, num_values, source); 2670 + } 2671 + 2672 + 2673 + 2674 + 2675 + // GPU_strcasecmp() 2676 + // A portable strcasecmp() from UC Berkeley 2677 + /* 2678 + * Copyright (c) 1987 Regents of the University of California. 2679 + * All rights reserved. 2680 + * 2681 + * Redistribution and use in source and binary forms are permitted 2682 + * provided that this notice is preserved and that due credit is given 2683 + * to the University of California at Berkeley. The name of the University 2684 + * may not be used to endorse or promote products derived from this 2685 + * software without specific written prior permission. This software 2686 + * is provided ``as is'' without express or implied warranty. 2687 + */ 2688 + 2689 + /* 2690 + * This array is designed for mapping upper and lower case letter 2691 + * together for a case independent comparison. The mappings are 2692 + * based upon ascii character sequences. 2693 + */ 2694 + static const unsigned char caseless_charmap[] = { 2695 + '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007', 2696 + '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017', 2697 + '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027', 2698 + '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037', 2699 + '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047', 2700 + '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057', 2701 + '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067', 2702 + '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077', 2703 + '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147', 2704 + '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', 2705 + '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', 2706 + '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137', 2707 + '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147', 2708 + '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', 2709 + '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', 2710 + '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177', 2711 + '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207', 2712 + '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217', 2713 + '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227', 2714 + '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237', 2715 + '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247', 2716 + '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257', 2717 + '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267', 2718 + '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277', 2719 + '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347', 2720 + '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357', 2721 + '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367', 2722 + '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337', 2723 + '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347', 2724 + '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357', 2725 + '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367', 2726 + '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377', 2727 + }; 2728 + 2729 + int GPU_strcasecmp(const char* s1, const char* s2) 2730 + { 2731 + unsigned char u1, u2; 2732 + 2733 + for (;;) 2734 + { 2735 + u1 = (unsigned char) *s1++; 2736 + u2 = (unsigned char) *s2++; 2737 + if (caseless_charmap[u1] != caseless_charmap[u2]) 2738 + return caseless_charmap[u1] - caseless_charmap[u2]; 2739 + if (u1 == '\0') 2740 + return 0; 2741 + } 2742 + return 0; 2649 2743 } 2650 2744 2651 2745
+33 -13
src/renderer_GL_common.inl
··· 40 40 } 41 41 #endif 42 42 43 + int GPU_strcasecmp(const char* s1, const char* s2); 43 44 44 45 #include "SDL_platform.h" 45 46 ··· 2203 2204 return dot + 1; 2204 2205 } 2205 2206 2206 - static Uint8 SaveImage(GPU_Renderer* renderer, GPU_Image* image, const char* filename) 2207 + static Uint8 SaveImage(GPU_Renderer* renderer, GPU_Image* image, const char* filename, GPU_FileFormatEnum format) 2207 2208 { 2208 - const char* extension; 2209 2209 Uint8 result; 2210 2210 unsigned char* data; 2211 2211 ··· 2215 2215 return 0; 2216 2216 } 2217 2217 2218 - extension = get_filename_ext(filename); 2219 - 2220 2218 data = getRawImageData(renderer, image); 2221 2219 2222 2220 if(data == NULL) ··· 2225 2223 return 0; 2226 2224 } 2227 2225 2228 - if(SDL_strcasecmp(extension, "png") == 0) 2229 - result = stbi_write_png(filename, image->base_w, image->base_h, image->bytes_per_pixel, (const unsigned char *const)data, 0); 2230 - else if(SDL_strcasecmp(extension, "bmp") == 0) 2231 - result = stbi_write_bmp(filename, image->base_w, image->base_h, image->bytes_per_pixel, (void*)data); 2232 - else if(SDL_strcasecmp(extension, "tga") == 0) 2233 - result = stbi_write_tga(filename, image->base_w, image->base_h, image->bytes_per_pixel, (void*)data); 2234 - else 2226 + if(format == GPU_FILE_AUTO) 2227 + { 2228 + const char* extension = get_filename_ext(filename); 2229 + if(GPU_strcasecmp(extension, "png") == 0) 2230 + format = GPU_FILE_PNG; 2231 + else if(GPU_strcasecmp(extension, "bmp") == 0) 2232 + format = GPU_FILE_BMP; 2233 + else if(GPU_strcasecmp(extension, "tga") == 0) 2234 + format = GPU_FILE_TGA; 2235 + else 2236 + { 2237 + GPU_PushErrorCode("GPU_SaveImage", GPU_ERROR_DATA_ERROR, "Could not detect output file format from file name"); 2238 + free(data); 2239 + return 0; 2240 + } 2241 + } 2242 + 2243 + switch(format) 2235 2244 { 2236 - GPU_PushErrorCode("GPU_SaveImage", GPU_ERROR_DATA_ERROR, "Unsupported output file format (%s)", extension); 2237 - result = 0; 2245 + case GPU_FILE_PNG: 2246 + result = (stbi_write_png(filename, image->base_w, image->base_h, image->bytes_per_pixel, (const unsigned char *const)data, 0) > 0); 2247 + break; 2248 + case GPU_FILE_BMP: 2249 + result = (stbi_write_bmp(filename, image->base_w, image->base_h, image->bytes_per_pixel, (void*)data) > 0); 2250 + break; 2251 + case GPU_FILE_TGA: 2252 + result = (stbi_write_tga(filename, image->base_w, image->base_h, image->bytes_per_pixel, (void*)data) > 0); 2253 + break; 2254 + default: 2255 + GPU_PushErrorCode("GPU_SaveImage", GPU_ERROR_DATA_ERROR, "Unsupported output file format"); 2256 + result = 0; 2257 + break; 2238 2258 } 2239 2259 2240 2260 free(data);