| | 3 | #include "SDL/SDL_rotozoom.h" |
| | 4 | |
| | 5 | void HandleEvent() |
| | 6 | { |
| | 7 | SDL_Event event; |
| | 8 | |
| | 9 | /* Check for events */ |
| | 10 | while ( SDL_PollEvent(&event) ) { |
| | 11 | switch (event.type) { |
| | 12 | case SDL_KEYDOWN: |
| | 13 | case SDL_QUIT: |
| | 14 | exit(0); |
| | 15 | break; |
| | 16 | } |
| | 17 | } |
| | 18 | } |
| | 19 | |
| | 20 | void ClearScreen(SDL_Surface *screen) |
| | 21 | { |
| | 22 | int i; |
| | 23 | /* Set the screen to black */ |
| | 24 | if ( SDL_LockSurface(screen) == 0 ) { |
| | 25 | Uint8 *pixels; |
| | 26 | pixels = (Uint8 *)screen->pixels; |
| | 27 | for ( i=0; i<screen->h; ++i ) { |
| | 28 | memset(pixels, 0, |
| | 29 | screen->w*screen->format->BytesPerPixel); |
| | 30 | pixels += screen->pitch; |
| | 31 | } |
| | 32 | SDL_UnlockSurface(screen); |
| | 33 | } |
| | 34 | } |
| | 35 | |
| | 36 | void ZoomPicture (SDL_Surface *screen, SDL_Surface *image, int smooth) |
| | 37 | { |
| | 38 | SDL_Surface *rotozoom_image; |
| | 39 | SDL_Rect dest; |
| | 40 | int framecount, framemax, frameinc; |
| | 41 | int new_w, new_h, new_x, new_y; |
| | 42 | float zoomxf,zoomyf; |
| | 43 | double factorx, factory, factor; |
| | 44 | printf ("image w=%d, h=%d\n", image->w, image->h); |
| | 45 | printf ("screen w=%d, h=%d\n", screen->w, screen->h); |
| | 46 | // Need to take into account the pixel aspect, this is 1:1? |
| | 47 | factorx = (double)screen->w / (double)image->w; |
| | 48 | factory = (double)screen->h / (double)image->h; |
| | 49 | printf ("screen x=%.3f, y=%.3f\n", factorx, factory); |
| | 50 | if (factorx < factory) { |
| | 51 | printf ("scale full width\n"); |
| | 52 | factor = factorx; |
| | 53 | } else { |
| | 54 | printf ("scale full height\n"); |
| | 55 | factor = factory; |
| | 56 | } |
| | 57 | new_w = (int)(image->w * factor + 0.5); |
| | 58 | new_h = (int)(image->h * factor + 0.5); |
| | 59 | new_x = (screen->w - new_w) / 2; |
| | 60 | new_y = (screen->h - new_h) / 2; |
| | 61 | printf ("new x=%d, y=%d\n", new_x, new_y); |
| | 62 | new_x = (int)(((double)screen->w - (double)new_w) / 2.0 + 0.5); |
| | 63 | new_y = (int)(((double)screen->h - (double)new_h) / 2.0 + 0.5); |
| | 64 | printf ("new w=%d, h=%d\n", new_w, new_h); |
| | 65 | printf ("new x=%d, y=%d\n", new_x, new_y); |
| | 66 | if ((rotozoom_image=zoomSurface (image, factor, factor, smooth))!=NULL) { |
| | 67 | dest.x = new_x; |
| | 68 | dest.y = new_y; |
| | 69 | dest.w = rotozoom_image->w; |
| | 70 | dest.h = rotozoom_image->h; |
| | 71 | printf ("dest w=%d, h=%d\n", dest.w, dest.h); |
| | 72 | if (SDL_BlitSurface(rotozoom_image, NULL, screen, &dest) < 0) { |
| | 73 | fprintf(stderr, "Blit failed: %s\n", SDL_GetError()); |
| | 74 | } |
| | 75 | SDL_FreeSurface(rotozoom_image); |
| | 76 | SDL_Flip(screen); |
| | 77 | } |
| | 78 | /* Pause for 5 secs */ |
| | 79 | SDL_Delay(5000); |
| | 80 | |
| | 81 | /* Zoom and display the image */ |
| | 82 | framemax=4*360; frameinc=1; |
| | 83 | for (framecount=360; framecount<framemax; framecount += frameinc) { |
| | 84 | if ((framecount % 360)==0) frameinc++; |
| | 85 | HandleEvent(); |
| | 86 | ClearScreen(screen); |
| | 87 | zoomxf=(float)framecount/(float)framemax; |
| | 88 | zoomxf=1.5*zoomxf*zoomxf; |
| | 89 | zoomyf=0.5+fabs(1.0*sin((double)framecount/80.0)); |
| | 90 | if ((framecount % 120)==0) { |
| | 91 | printf (" Frame: %i Zoom: x=%.2f y=%.2f\n",framecount,zoomxf,zoomyf); |
| | 92 | } |
| | 93 | if ((rotozoom_image=zoomSurface (image, zoomxf, zoomyf, smooth))!=NULL) { |
| | 94 | dest.x = (screen->w - rotozoom_image->w)/2;; |
| | 95 | dest.y = (screen->h - rotozoom_image->h)/2; |
| | 96 | dest.w = rotozoom_image->w; |
| | 97 | dest.h = rotozoom_image->h; |
| | 98 | if ( SDL_BlitSurface(rotozoom_image, NULL, screen, &dest) < 0 ) { |
| | 99 | fprintf(stderr, "Blit failed: %s\n", SDL_GetError()); |
| | 100 | break; |
| | 101 | } |
| | 102 | SDL_FreeSurface(rotozoom_image); |
| | 103 | } |
| | 104 | |
| | 105 | /* Display by flipping screens */ |
| | 106 | SDL_Flip(screen); |
| | 107 | } |
| | 108 | |
| | 109 | /* Pause for a sec */ |
| | 110 | SDL_Delay(1000); |
| | 111 | } |
| | 112 | |