this repo has no description
0
fork

Configure Feed

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

Added simple font code for use in the demos.

+230 -31
+31 -31
demos/CMakeLists.txt
··· 2 2 include_directories(${SDL_gpu_SOURCE_DIR}/SDL_gpu/OpenGL_common) 3 3 include_directories(${SDL_gpu_SOURCE_DIR}/demos/common) 4 4 5 - add_executable(init-demo init/main.c common/common.c) 5 + add_executable(init-demo init/main.c common/common.c common/demo-font.c) 6 6 target_link_libraries (init-demo SDL_gpu) 7 7 8 - add_executable(create-demo create/main.c common/common.c) 8 + add_executable(create-demo create/main.c common/common.c common/demo-font.c) 9 9 target_link_libraries (create-demo SDL_gpu) 10 10 11 - add_executable(get-pixel-demo get-pixel/main.c common/common.c) 11 + add_executable(get-pixel-demo get-pixel/main.c common/common.c common/demo-font.c) 12 12 target_link_libraries (get-pixel-demo SDL_gpu) 13 13 14 - add_executable(scale-demo scale/main.c common/common.c) 14 + add_executable(scale-demo scale/main.c common/common.c common/demo-font.c) 15 15 target_link_libraries (scale-demo SDL_gpu) 16 16 17 - add_executable(transform-demo transform/main.c common/common.c) 17 + add_executable(transform-demo transform/main.c common/common.c common/demo-font.c) 18 18 target_link_libraries (transform-demo SDL_gpu) 19 19 20 - add_executable(sprite-demo sprite/main.c common/common.c) 20 + add_executable(sprite-demo sprite/main.c common/common.c common/demo-font.c) 21 21 target_link_libraries (sprite-demo SDL_gpu) 22 22 23 - add_executable(sprite-stress-demo sprite-stress/main.c common/common.c) 23 + add_executable(sprite-stress-demo sprite-stress/main.c common/common.c common/demo-font.c) 24 24 target_link_libraries (sprite-stress-demo SDL_gpu) 25 25 26 - add_executable(rotate-demo rotate/main.c common/common.c) 26 + add_executable(rotate-demo rotate/main.c common/common.c common/demo-font.c) 27 27 target_link_libraries (rotate-demo SDL_gpu) 28 28 29 - add_executable(render-target-demo render-target/main.c common/common.c) 29 + add_executable(render-target-demo render-target/main.c common/common.c common/demo-font.c) 30 30 target_link_libraries (render-target-demo SDL_gpu) 31 31 32 - add_executable(blend-demo blend/main.c common/common.c) 32 + add_executable(blend-demo blend/main.c common/common.c common/demo-font.c) 33 33 target_link_libraries (blend-demo SDL_gpu) 34 34 35 - add_executable(blend-mode-demo blend-mode/main.c common/common.c) 35 + add_executable(blend-mode-demo blend-mode/main.c common/common.c common/demo-font.c) 36 36 target_link_libraries (blend-mode-demo SDL_gpu) 37 37 38 - add_executable(shapes-demo shapes/main.c common/common.c) 38 + add_executable(shapes-demo shapes/main.c common/common.c common/demo-font.c) 39 39 target_link_libraries (shapes-demo SDL_gpu) 40 40 41 - add_executable(clip-demo clip/main.c common/common.c) 41 + add_executable(clip-demo clip/main.c common/common.c common/demo-font.c) 42 42 target_link_libraries (clip-demo SDL_gpu) 43 43 44 - add_executable(color-demo color/main.c common/common.c) 44 + add_executable(color-demo color/main.c common/common.c common/demo-font.c) 45 45 target_link_libraries (color-demo SDL_gpu) 46 46 47 - add_executable(mipmap-demo mipmap/main.c common/common.c) 47 + add_executable(mipmap-demo mipmap/main.c common/common.c common/demo-font.c) 48 48 target_link_libraries (mipmap-demo SDL_gpu) 49 49 50 - add_executable(polygon-blit-demo polygon-blit/main.c common/common.c) 50 + add_executable(polygon-blit-demo polygon-blit/main.c common/common.c common/demo-font.c) 51 51 target_link_libraries (polygon-blit-demo SDL_gpu) 52 52 53 - add_executable(camera-demo camera/main.c common/common.c) 53 + add_executable(camera-demo camera/main.c common/common.c common/demo-font.c) 54 54 target_link_libraries (camera-demo SDL_gpu) 55 55 56 - add_executable(virtual-resolution-demo virtual-resolution/main.c common/common.c) 56 + add_executable(virtual-resolution-demo virtual-resolution/main.c common/common.c common/demo-font.c) 57 57 target_link_libraries (virtual-resolution-demo SDL_gpu) 58 58 59 - add_executable(transformx-demo transformx/main.c common/common.c) 59 + add_executable(transformx-demo transformx/main.c common/common.c common/demo-font.c) 60 60 target_link_libraries (transformx-demo SDL_gpu) 61 61 62 - add_executable(transform-matrix-demo transform-matrix/main.c common/common.c) 62 + add_executable(transform-matrix-demo transform-matrix/main.c common/common.c common/demo-font.c) 63 63 target_link_libraries (transform-matrix-demo SDL_gpu) 64 64 65 - add_executable(copy-demo copy/main.c common/common.c) 65 + add_executable(copy-demo copy/main.c common/common.c common/demo-font.c) 66 66 target_link_libraries (copy-demo SDL_gpu) 67 67 68 - add_executable(npot-demo npot/main.c common/common.c) 68 + add_executable(npot-demo npot/main.c common/common.c common/demo-font.c) 69 69 target_link_libraries (npot-demo SDL_gpu) 70 70 71 - add_executable(subsurface-demo subsurface/main.c common/common.c) 71 + add_executable(subsurface-demo subsurface/main.c common/common.c common/demo-font.c) 72 72 target_link_libraries (subsurface-demo SDL_gpu) 73 73 74 - add_executable(features-demo features/main.c common/common.c) 74 + add_executable(features-demo features/main.c common/common.c common/demo-font.c) 75 75 target_link_libraries (features-demo SDL_gpu) 76 76 77 - add_executable(save-demo save/main.c common/common.c) 77 + add_executable(save-demo save/main.c common/common.c common/demo-font.c) 78 78 target_link_libraries (save-demo SDL_gpu) 79 79 80 - add_executable(shader-demo shader/main.c common/common.c) 80 + add_executable(shader-demo shader/main.c common/common.c common/demo-font.c) 81 81 target_link_libraries (shader-demo SDL_gpu) 82 82 83 - add_executable(3d-demo 3d/main.c common/common.c) 83 + add_executable(3d-demo 3d/main.c common/common.c common/demo-font.c) 84 84 target_link_libraries (3d-demo SDL_gpu) 85 85 86 - add_executable(windows-demo windows/main.c common/common.c) 86 + add_executable(windows-demo windows/main.c common/common.c common/demo-font.c) 87 87 target_link_libraries (windows-demo SDL_gpu) 88 88 89 - add_executable(windows2-demo windows2/main.c common/common.c) 89 + add_executable(windows2-demo windows2/main.c common/common.c common/demo-font.c) 90 90 target_link_libraries (windows2-demo SDL_gpu) 91 91 92 - add_executable(shader-attributes-demo shader-attributes/main.c common/common.c) 92 + add_executable(shader-attributes-demo shader-attributes/main.c common/common.c common/demo-font.c) 93 93 target_link_libraries (shader-attributes-demo SDL_gpu) 94 94 95 - add_executable(blit-batch-demo blit-batch/main.c common/common.c) 95 + add_executable(blit-batch-demo blit-batch/main.c common/common.c common/demo-font.c) 96 96 target_link_libraries (blit-batch-demo SDL_gpu)
+163
demos/common/demo-font.c
··· 1 + #include "demo-font.h" 2 + 3 + static Uint32 _GetPixel(SDL_Surface *Surface, int x, int y) 4 + { 5 + Uint8* bits; 6 + Uint32 bpp; 7 + 8 + if(x < 0 || x >= Surface->w) 9 + return 0; // Best I could do for errors 10 + 11 + bpp = Surface->format->BytesPerPixel; 12 + bits = ((Uint8*)Surface->pixels) + y*Surface->pitch + x*bpp; 13 + 14 + Uint8 r, g, b; 15 + switch (bpp) 16 + { 17 + case 1: 18 + return *((Uint8*)Surface->pixels + y * Surface->pitch + x); 19 + break; 20 + case 2: 21 + return *((Uint16*)Surface->pixels + y * Surface->pitch/2 + x); 22 + break; 23 + case 3: 24 + // Endian-correct, but slower 25 + r = *((bits)+Surface->format->Rshift/8); 26 + g = *((bits)+Surface->format->Gshift/8); 27 + b = *((bits)+Surface->format->Bshift/8); 28 + return SDL_MapRGB(Surface->format, r, g, b); 29 + break; 30 + case 4: 31 + return *((Uint32*)Surface->pixels + y * Surface->pitch/4 + x); 32 + break; 33 + } 34 + 35 + return 0; // Best I could do for errors 36 + } 37 + 38 + DemoFont* FONT_Alloc(SDL_Surface* source_surface) 39 + { 40 + if(source_surface == NULL) 41 + { 42 + printf("ERROR: DemoFont given a NULL surface\n"); 43 + return NULL; 44 + } 45 + 46 + DemoFont* font = (DemoFont*)malloc(sizeof(DemoFont)); 47 + if(font == NULL) 48 + return NULL; 49 + 50 + font->buffer = (char*)malloc(sizeof(char)*1024); 51 + 52 + int x = 1, i = 0; 53 + 54 + memset(font->charPos, 0, sizeof(int)*256); 55 + memset(font->charWidth, 0, sizeof(Uint16)*256); 56 + 57 + SDL_LockSurface(source_surface); 58 + 59 + Uint32 pixel = SDL_MapRGB(source_surface->format, 255, 0, 255); // pink pixel 60 + 61 + // Get the character positions and widths 62 + while(x < source_surface->w) 63 + { 64 + if(_GetPixel(source_surface, x, 0) != pixel) 65 + { 66 + font->charPos[i] = x; 67 + font->charWidth[i] = x; 68 + while(x < source_surface->w && _GetPixel(source_surface, x, 0) != pixel) 69 + x++; 70 + font->charWidth[i] = x - font->charWidth[i]; 71 + i++; 72 + } 73 + 74 + x++; 75 + } 76 + 77 + 78 + pixel = _GetPixel(source_surface, 0, source_surface->h - 1); 79 + 80 + Uint8 uses_alpha; 81 + #ifdef SDL_GPU_USE_SDL2 82 + SDL_BlendMode blendMode; 83 + SDL_GetSurfaceBlendMode(source_surface, &blendMode); 84 + uses_alpha = (blendMode != SDL_BLENDMODE_NONE); 85 + #else 86 + uses_alpha = ((source_surface->flags & SDL_SRCALPHA) == SDL_SRCALPHA); 87 + #endif 88 + 89 + if(!uses_alpha) 90 + { 91 + pixel = _GetPixel(source_surface, 0, source_surface->h - 1); 92 + SDL_UnlockSurface(source_surface); 93 + #ifdef SDL_GPU_USE_SDL2 94 + SDL_SetColorKey(source_surface, SDL_TRUE, pixel); 95 + #else 96 + SDL_SetColorKey(source_surface, SDL_SRCCOLORKEY, pixel); 97 + #endif 98 + } 99 + else 100 + SDL_UnlockSurface(source_surface); 101 + 102 + font->image = GPU_CopyImageFromSurface(source_surface); 103 + if(font->image == NULL) 104 + { 105 + FONT_Free(font); 106 + return NULL; 107 + } 108 + 109 + return font; 110 + } 111 + 112 + void FONT_Free(DemoFont* font) 113 + { 114 + GPU_FreeImage(font->image); 115 + free(font->buffer); 116 + free(font); 117 + } 118 + 119 + 120 + void FONT_Draw(DemoFont* font, GPU_Target* target, float x, float y, const char* formatted_text, ...) 121 + { 122 + if(font == NULL || formatted_text == NULL || font->image == NULL) 123 + return; 124 + 125 + va_list lst; 126 + va_start(lst, formatted_text); 127 + vsnprintf(font->buffer, 1024, formatted_text, lst); 128 + va_end(lst); 129 + 130 + 131 + char* text = font->buffer; 132 + const char* c = text; 133 + unsigned char num; 134 + GPU_Rect srcRect; 135 + 136 + srcRect.y = 0; 137 + srcRect.h = font->image->h; 138 + 139 + int letterSpacing = 1; 140 + 141 + for(; *c != '\0'; c++) 142 + { 143 + if (*c == ' ') 144 + { 145 + x += font->charWidth[0] + letterSpacing; 146 + continue; 147 + } 148 + unsigned char ctest = (unsigned char)(*c); 149 + // Skip bad characters 150 + if(ctest < 33 || (ctest > 126 && ctest < 161)) 151 + continue; 152 + 153 + num = ctest - 33; // Get array index 154 + if(num > 126) // shift the extended characters down to the correct index 155 + num -= 34; 156 + 157 + srcRect.x = font->charPos[num]; 158 + srcRect.w = font->charWidth[num]; 159 + GPU_Blit(font->image, &srcRect, target, x, y); 160 + 161 + x += srcRect.w + letterSpacing; 162 + } 163 + }
+28
demos/common/demo-font.h
··· 1 + #ifndef _DEMO_FONT_H__ 2 + #define _DEMO_FONT_H__ 3 + 4 + #ifdef __cplusplus 5 + extern "C" { 6 + #endif 7 + 8 + #include "SDL_gpu.h" 9 + 10 + typedef struct DemoFont 11 + { 12 + GPU_Image* image; 13 + 14 + char* buffer; 15 + int charPos[256]; 16 + Uint16 charWidth[256]; 17 + } DemoFont; 18 + 19 + DemoFont* FONT_Alloc(SDL_Surface* source_surface); 20 + void FONT_Free(DemoFont* font); 21 + 22 + void FONT_Draw(DemoFont* font, GPU_Target* target, float x, float y, const char* formatted_text, ...); 23 + 24 + #ifdef __cplusplus 25 + } 26 + #endif 27 + 28 + #endif
demos/data/comic14.png

This is a binary file and will not be displayed.

+8
demos/render-target/main.c
··· 3 3 #include <math.h> 4 4 #include "compat.h" 5 5 #include "common.h" 6 + #include "demo-font.h" 6 7 7 8 8 9 int main(int argc, char* argv[]) ··· 25 26 if(image3 == NULL) 26 27 return -1; 27 28 29 + SDL_Surface* font_surface = GPU_LoadSurface("data/comic14.png"); 30 + DemoFont* font = FONT_Alloc(font_surface); 31 + GPU_SetRGB(font->image, 255, 0, 0); 32 + SDL_FreeSurface(font_surface); 28 33 29 34 GPU_Image* mode1image = GPU_CreateImage(300, 300, 4); 30 35 if(mode1image == NULL) ··· 130 135 GPU_Blit(mode2image, NULL, screen, mode2image->w/2, mode2image->h/2); 131 136 } 132 137 138 + FONT_Draw(font, screen, 20, 20, "Testing"); 139 + 133 140 GPU_Flip(screen); 134 141 135 142 frameCount++; ··· 142 149 143 150 printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); 144 151 152 + FONT_Free(font); 145 153 GPU_FreeImage(mode2image); 146 154 GPU_FreeImage(mode1image); 147 155 GPU_Quit();