static Uint8 SaveImage(GPU_Renderer* renderer, GPU_Image* image, const char* filename, GPU_FileFormatEnum format) { SDL_Surface* surface; GPU_Log(" %s (dummy)\n", __func__); surface = GPU_CopySurfaceFromImage(image); GPU_SaveSurface(surface, filename, format); SDL_FreeSurface(surface); return 1; }
void shiftHSVExcept(GPU_Image* image, int hue, int saturation, int value, int notHue, int notSat, int notVal, int range) { SDL_Surface* surface = GPU_CopySurfaceFromImage(image); Uint8* pixels = surface->pixels; int x,y,i; int r, g, b, h, s, v; for(y = 0; y < surface->h; y++) { for(x = 0; x < surface->w; x++) { i = y*surface->pitch + x*surface->format->BytesPerPixel; if(surface->format->BytesPerPixel == 4 && pixels[i+3] == 0) continue; r = pixels[i]; g = pixels[i+1]; b = pixels[i+2]; rgb_to_hsv(r, g, b, &h, &s, &v); h += hue; s += saturation; v += value; // Wrap hue while(h < 0) h += 256; while(h > 255) h -= 256; // Clamp s = clamp(s, 0, 255); v = clamp(v, 0, 255); hsv_to_rgb(h, s, v, &r, &g, &b); if(notHue - range <= h && notHue + range >= h && notSat - range <= s && notSat + range >= s && notVal - range <= v && notVal + range >= v) continue; pixels[i] = r; pixels[i+1] = g; pixels[i+2] = b; } } GPU_UpdateImage(image, NULL, surface, NULL); SDL_FreeSurface(surface); }
void makeColorTransparent(GPU_Image* image, SDL_Color color) { SDL_Surface* surface = GPU_CopySurfaceFromImage(image); Uint8* pixels = surface->pixels; int x,y,i; for(y = 0; y < surface->h; y++) { for(x = 0; x < surface->w; x++) { i = y*surface->pitch + x*surface->format->BytesPerPixel; if(pixels[i] == color.r && pixels[i+1] == color.g && pixels[i+2] == color.b) pixels[i+3] = 0; } } GPU_UpdateImage(image, NULL, surface, NULL); SDL_FreeSurface(surface); }
void replaceColor(GPU_Image* image, SDL_Color from, SDL_Color to) { SDL_Surface* surface = GPU_CopySurfaceFromImage(image); Uint8* pixels = surface->pixels; int x,y,i; for(y = 0; y < surface->h; y++) { for(x = 0; x < surface->w; x++) { i = y*surface->pitch + x*surface->format->BytesPerPixel; if(pixels[i] == from.r && pixels[i+1] == from.g && pixels[i+2] == from.b) { pixels[i] = to.r; pixels[i+1] = to.g; pixels[i+2] = to.b; } } } GPU_UpdateImage(image, NULL, surface, NULL); SDL_FreeSurface(surface); }
int main(int argc, char* argv[]) { GPU_Target* screen; printRenderers(); screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); if(screen == NULL) return -1; printCurrentRenderer(); { Uint32 startTime; long frameCount; Uint8 done; SDL_Event event; const Uint8* keystates; GPU_Camera camera; float dt; SDL_Surface* surface; GPU_Image* image; GPU_Image* image1; GPU_Image* image2; GPU_Image* image3; GPU_Image* image4; image = GPU_LoadImage("data/test.bmp"); //image = GPU_LoadImage("data/big_test.png"); if(image == NULL) return -1; // Copying the annoying way image1 = GPU_CreateImage(image->w, image->h, GPU_FORMAT_RGBA); GPU_LoadTarget(image1); GPU_Blit(image, NULL, image1->target, image1->target->w/2, image1->target->h/2); GPU_FreeTarget(image1->target); // Copying the normal way image2 = GPU_CopyImage(image); // Copying from a surface dump surface = GPU_CopySurfaceFromImage(image); //GPU_SaveSurface(surface, "save_surf1.bmp", GPU_FILE_AUTO); image3 = GPU_CopyImageFromSurface(surface); SDL_FreeSurface(surface); // A buffer for window capture image4 = NULL; keystates = SDL_GetKeyState(NULL); camera = GPU_GetDefaultCamera(); startTime = SDL_GetTicks(); frameCount = 0; dt = 0.010f; done = 0; while(!done) { while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) done = 1; else if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE) done = 1; else if(event.key.keysym.sym == SDLK_SPACE) { // Take a window capture GPU_FreeImage(image4); image4 = GPU_CopyImageFromTarget(screen); } } } if(keystates[KEY_UP]) { camera.y -= 200*dt; } else if(keystates[KEY_DOWN]) { camera.y += 200*dt; } if(keystates[KEY_LEFT]) { camera.x -= 200*dt; } else if(keystates[KEY_RIGHT]) { camera.x += 200*dt; } if(keystates[KEY_MINUS]) { camera.zoom -= 1.0f*dt; } else if(keystates[KEY_EQUALS]) { camera.zoom += 1.0f*dt; } GPU_ClearRGBA(screen, 100, 100, 100, 255); GPU_SetCamera(screen, &camera); GPU_Blit(image, NULL, screen, 128, 128); GPU_Blit(image1, NULL, screen, 128 + 256, 128); GPU_Blit(image2, NULL, screen, 128 + 512, 128); GPU_Blit(image3, NULL, screen, 128, 128 + 256); if(image4 != NULL) GPU_BlitScale(image4, NULL, screen, 3*screen->w/4, 3*screen->h/4, 0.25f, 0.25f); GPU_Flip(screen); frameCount++; if(frameCount%500 == 0) GPU_LogError("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); } GPU_LogError("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime)); GPU_FreeImage(image); GPU_FreeImage(image1); GPU_FreeImage(image2); GPU_FreeImage(image3); GPU_FreeImage(image4); } GPU_Quit(); return 0; }