| 1 |
// converted to D by clayasaurus |
|---|
| 2 |
|
|---|
| 3 |
/* |
|---|
| 4 |
|
|---|
| 5 |
SDL_rotozoom - test program |
|---|
| 6 |
|
|---|
| 7 |
Copyright (C) A. Schiffler, July 2001 |
|---|
| 8 |
|
|---|
| 9 |
This library is free software; you can redistribute it and/or |
|---|
| 10 |
modify it under the terms of the GNU Lesser General Public |
|---|
| 11 |
License as published by the Free Software Foundation; either |
|---|
| 12 |
version 2 of the License, or (at your option) any later version. |
|---|
| 13 |
|
|---|
| 14 |
This library is distributed in the hope that it will be useful, |
|---|
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 |
Lesser General Public License for more details. |
|---|
| 18 |
|
|---|
| 19 |
You should have received a copy of the GNU Lesser General Public |
|---|
| 20 |
License along with this library; if not, write to the Free Software |
|---|
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 22 |
|
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
import derelict.sdl.sdl; |
|---|
| 26 |
|
|---|
| 27 |
import std.c.stdlib; |
|---|
| 28 |
import std.stdio; |
|---|
| 29 |
import std.string; |
|---|
| 30 |
|
|---|
| 31 |
import rotozoom; |
|---|
| 32 |
|
|---|
| 33 |
void HandleEvent () |
|---|
| 34 |
{ |
|---|
| 35 |
SDL_Event event; |
|---|
| 36 |
|
|---|
| 37 |
/* Check for events */ |
|---|
| 38 |
while (SDL_PollEvent (&event)) |
|---|
| 39 |
{ |
|---|
| 40 |
switch (event.type) |
|---|
| 41 |
{ |
|---|
| 42 |
case SDL_KEYDOWN: |
|---|
| 43 |
case SDL_QUIT: |
|---|
| 44 |
exit (0); |
|---|
| 45 |
break; |
|---|
| 46 |
default: |
|---|
| 47 |
break; |
|---|
| 48 |
} |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
void |
|---|
| 53 |
ClearScreen (SDL_Surface * screen) |
|---|
| 54 |
{ |
|---|
| 55 |
int i; |
|---|
| 56 |
/* Set the screen to black */ |
|---|
| 57 |
if (SDL_LockSurface (screen) == 0) |
|---|
| 58 |
{ |
|---|
| 59 |
Uint32 black; |
|---|
| 60 |
Uint8 *pixels; |
|---|
| 61 |
black = SDL_MapRGB (screen.format, 0, 0, 0); |
|---|
| 62 |
pixels = cast(Uint8 *) screen.pixels; |
|---|
| 63 |
for (i = 0; i < screen.h; ++i) |
|---|
| 64 |
{ |
|---|
| 65 |
memset (pixels, black, screen.w * screen.format.BytesPerPixel); |
|---|
| 66 |
pixels += screen.pitch; |
|---|
| 67 |
} |
|---|
| 68 |
SDL_UnlockSurface (screen); |
|---|
| 69 |
} |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
void |
|---|
| 73 |
RotatePicture (SDL_Surface * screen, SDL_Surface * picture, int rotate, |
|---|
| 74 |
int smooth) |
|---|
| 75 |
{ |
|---|
| 76 |
SDL_Surface *rotozoom_picture; |
|---|
| 77 |
SDL_Rect dest; |
|---|
| 78 |
int framecount, framemax, frameinc; |
|---|
| 79 |
float zoomf; |
|---|
| 80 |
|
|---|
| 81 |
/* Rotate and display the picture */ |
|---|
| 82 |
framemax = 4 * 360; |
|---|
| 83 |
frameinc = 1; |
|---|
| 84 |
for (framecount = 360; framecount < framemax; framecount += frameinc) |
|---|
| 85 |
{ |
|---|
| 86 |
if ((framecount % 360) == 0) |
|---|
| 87 |
frameinc++; |
|---|
| 88 |
HandleEvent (); |
|---|
| 89 |
ClearScreen (screen); |
|---|
| 90 |
zoomf = cast(float) framecount / cast(float) framemax; |
|---|
| 91 |
zoomf = 1.5 * zoomf * zoomf; |
|---|
| 92 |
if ((rotozoom_picture = |
|---|
| 93 |
rotozoomSurface (picture, framecount * rotate, zoomf, |
|---|
| 94 |
smooth)) != null) |
|---|
| 95 |
{ |
|---|
| 96 |
dest.x = (screen.w - rotozoom_picture.w) / 2;; |
|---|
| 97 |
dest.y = (screen.h - rotozoom_picture.h) / 2; |
|---|
| 98 |
dest.w = rotozoom_picture.w; |
|---|
| 99 |
dest.h = rotozoom_picture.h; |
|---|
| 100 |
if (SDL_BlitSurface (rotozoom_picture, null, screen, &dest) < 0) |
|---|
| 101 |
{ |
|---|
| 102 |
writef ( "Blit failed: %s\n", SDL_GetError ()); |
|---|
| 103 |
break; |
|---|
| 104 |
} |
|---|
| 105 |
SDL_FreeSurface (rotozoom_picture); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
/* Display by flipping screens */ |
|---|
| 109 |
SDL_UpdateRect (screen, 0, 0, 0, 0); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
if (rotate) |
|---|
| 113 |
{ |
|---|
| 114 |
/* Final display with angle=0 */ |
|---|
| 115 |
HandleEvent (); |
|---|
| 116 |
ClearScreen (screen); |
|---|
| 117 |
if ((rotozoom_picture = |
|---|
| 118 |
rotozoomSurface (picture, 0.01, zoomf, smooth)) != null) |
|---|
| 119 |
{ |
|---|
| 120 |
dest.x = (screen.w - rotozoom_picture.w) / 2;; |
|---|
| 121 |
dest.y = (screen.h - rotozoom_picture.h) / 2; |
|---|
| 122 |
dest.w = rotozoom_picture.w; |
|---|
| 123 |
dest.h = rotozoom_picture.h; |
|---|
| 124 |
if (SDL_BlitSurface (rotozoom_picture, null, screen, &dest) < 0) |
|---|
| 125 |
{ |
|---|
| 126 |
writef ( "Blit failed: %s\n", SDL_GetError ()); |
|---|
| 127 |
return; |
|---|
| 128 |
} |
|---|
| 129 |
SDL_FreeSurface (rotozoom_picture); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
/* Display by flipping screens */ |
|---|
| 133 |
SDL_UpdateRect (screen, 0, 0, 0, 0); |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
/* Pause for a sec */ |
|---|
| 137 |
SDL_Delay (1000); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
void |
|---|
| 141 |
ZoomPicture (SDL_Surface * screen, SDL_Surface * picture, int smooth) |
|---|
| 142 |
{ |
|---|
| 143 |
SDL_Surface *rotozoom_picture; |
|---|
| 144 |
SDL_Rect dest; |
|---|
| 145 |
int framecount, framemax, frameinc; |
|---|
| 146 |
float zoomxf, zoomyf; |
|---|
| 147 |
|
|---|
| 148 |
/* Zoom and display the picture */ |
|---|
| 149 |
framemax = 4 * 360; |
|---|
| 150 |
frameinc = 1; |
|---|
| 151 |
for (framecount = 360; framecount < framemax; framecount += frameinc) |
|---|
| 152 |
{ |
|---|
| 153 |
if ((framecount % 360) == 0) |
|---|
| 154 |
frameinc++; |
|---|
| 155 |
HandleEvent (); |
|---|
| 156 |
ClearScreen (screen); |
|---|
| 157 |
zoomxf = cast(float) framecount / cast(float) framemax; |
|---|
| 158 |
zoomxf = 1.5 * zoomxf * zoomxf; |
|---|
| 159 |
zoomyf = 0.5 + fabs (1.0 * sin (cast(double) framecount / 80.0)); |
|---|
| 160 |
if ((rotozoom_picture = |
|---|
| 161 |
zoomSurface (picture, zoomxf, zoomyf, smooth)) != null) |
|---|
| 162 |
{ |
|---|
| 163 |
dest.x = (screen.w - rotozoom_picture.w) / 2;; |
|---|
| 164 |
dest.y = (screen.h - rotozoom_picture.h) / 2; |
|---|
| 165 |
dest.w = rotozoom_picture.w; |
|---|
| 166 |
dest.h = rotozoom_picture.h; |
|---|
| 167 |
if (SDL_BlitSurface (rotozoom_picture, null, screen, &dest) < 0) |
|---|
| 168 |
{ |
|---|
| 169 |
writef ( "Blit failed: %s\n", SDL_GetError ()); |
|---|
| 170 |
break; |
|---|
| 171 |
} |
|---|
| 172 |
SDL_FreeSurface (rotozoom_picture); |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
/* Display by flipping screens */ |
|---|
| 176 |
SDL_UpdateRect (screen, 0, 0, 0, 0); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
/* Pause for a sec */ |
|---|
| 180 |
SDL_Delay (1000); |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
void |
|---|
| 184 |
Draw (SDL_Surface * screen) |
|---|
| 185 |
{ |
|---|
| 186 |
SDL_Surface *picture, picture_again; |
|---|
| 187 |
char *bmpfile; |
|---|
| 188 |
|
|---|
| 189 |
/* --------- 8 bit test -------- */ |
|---|
| 190 |
|
|---|
| 191 |
/* Message */ |
|---|
| 192 |
writef ( "Loading 8bit image\n"); |
|---|
| 193 |
|
|---|
| 194 |
/* Load the image into a surface */ |
|---|
| 195 |
bmpfile = "sample8.bmp"; |
|---|
| 196 |
writef ( "Loading picture: %s\n", bmpfile); |
|---|
| 197 |
picture = SDL_LoadBMP (bmpfile); |
|---|
| 198 |
if (picture is null) |
|---|
| 199 |
{ |
|---|
| 200 |
writef ( "Couldn't load %s: %s\n", bmpfile, SDL_GetError ()); |
|---|
| 201 |
return; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
writef ( "rotozoom: Rotating and zooming\n"); |
|---|
| 205 |
RotatePicture (screen, picture, 1, SMOOTHING_OFF); |
|---|
| 206 |
|
|---|
| 207 |
writef ( "rotozoom: Just zooming (angle=0)\n"); |
|---|
| 208 |
RotatePicture (screen, picture, 0, SMOOTHING_OFF); |
|---|
| 209 |
|
|---|
| 210 |
writef ( "zoom: Just zooming\n"); |
|---|
| 211 |
ZoomPicture (screen, picture, SMOOTHING_OFF); |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
writef ( |
|---|
| 215 |
"rotozoom: Rotating and zooming, interpolation on but unused\n"); |
|---|
| 216 |
RotatePicture (screen, picture, 1, SMOOTHING_ON); |
|---|
| 217 |
|
|---|
| 218 |
writef ( |
|---|
| 219 |
"rotozoom: Just zooming (angle=0), interpolation on but unused\n"); |
|---|
| 220 |
RotatePicture (screen, picture, 0, SMOOTHING_ON); |
|---|
| 221 |
|
|---|
| 222 |
writef ( "zoom: Just zooming, interpolation on but unused\n"); |
|---|
| 223 |
ZoomPicture (screen, picture, SMOOTHING_ON); |
|---|
| 224 |
|
|---|
| 225 |
/* Free the picture */ |
|---|
| 226 |
SDL_FreeSurface (picture); |
|---|
| 227 |
|
|---|
| 228 |
/* -------- 24 bit test --------- */ |
|---|
| 229 |
|
|---|
| 230 |
/* Message */ |
|---|
| 231 |
writef ( "Loading 24bit image\n"); |
|---|
| 232 |
/* Load the image into a surface */ |
|---|
| 233 |
bmpfile = "sample24.bmp"; |
|---|
| 234 |
writef ( "Loading picture: %s\n", bmpfile); |
|---|
| 235 |
picture = SDL_LoadBMP (bmpfile); |
|---|
| 236 |
if (picture is null) |
|---|
| 237 |
{ |
|---|
| 238 |
writef ( "Couldn't load %s: %s\n", bmpfile, SDL_GetError ()); |
|---|
| 239 |
return; |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
writef ( "rotozoom: Rotating and zooming, no interpolation\n"); |
|---|
| 243 |
RotatePicture (screen, picture, 1, SMOOTHING_OFF); |
|---|
| 244 |
|
|---|
| 245 |
writef ( "rotozoom: Just zooming (angle=0), no interpolation\n"); |
|---|
| 246 |
RotatePicture (screen, picture, 0, SMOOTHING_OFF); |
|---|
| 247 |
|
|---|
| 248 |
writef ( "zoom: Just zooming, no interpolation\n"); |
|---|
| 249 |
ZoomPicture (screen, picture, SMOOTHING_OFF); |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
writef ( "rotozoom: Rotating and zooming, with interpolation\n"); |
|---|
| 253 |
RotatePicture (screen, picture, 1, SMOOTHING_ON); |
|---|
| 254 |
|
|---|
| 255 |
writef ( "rotozoom: Just zooming (angle=0), with interpolation\n"); |
|---|
| 256 |
RotatePicture (screen, picture, 0, SMOOTHING_ON); |
|---|
| 257 |
|
|---|
| 258 |
writef ( "zoom: Just zooming, with interpolation\n"); |
|---|
| 259 |
ZoomPicture (screen, picture, SMOOTHING_ON); |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
/* New source surface is 32bit with defined RGBA ordering */ |
|---|
| 263 |
/* Much faster to do this once rather than the routine on the fly */ |
|---|
| 264 |
writef ( "Converting 24bit image into 32bit RGBA surface ...\n"); |
|---|
| 265 |
picture_again = |
|---|
| 266 |
SDL_CreateRGBSurface (SDL_SWSURFACE, picture.w, picture.h, 32, |
|---|
| 267 |
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); |
|---|
| 268 |
SDL_BlitSurface (picture, null, picture_again, null); |
|---|
| 269 |
|
|---|
| 270 |
/* Message */ |
|---|
| 271 |
writef ( "Rotating and zooming, with interpolation\n"); |
|---|
| 272 |
RotatePicture (screen, picture_again, 1, SMOOTHING_ON); |
|---|
| 273 |
|
|---|
| 274 |
/* Message */ |
|---|
| 275 |
writef ( "Just zooming (angle=0), with interpolation\n"); |
|---|
| 276 |
RotatePicture (screen, picture_again, 0, SMOOTHING_ON); |
|---|
| 277 |
|
|---|
| 278 |
SDL_FreeSurface (picture_again); |
|---|
| 279 |
|
|---|
| 280 |
/* New source surface is 32bit with defined ABGR ordering */ |
|---|
| 281 |
/* Much faster to do this once rather than the routine on the fly */ |
|---|
| 282 |
writef ( "Converting 24bit image into 32bit ABGR surface ...\n"); |
|---|
| 283 |
picture_again = |
|---|
| 284 |
SDL_CreateRGBSurface (SDL_SWSURFACE, picture.w, picture.h, 32, |
|---|
| 285 |
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); |
|---|
| 286 |
SDL_BlitSurface (picture, null, picture_again, null); |
|---|
| 287 |
|
|---|
| 288 |
/* Message */ |
|---|
| 289 |
writef ( "Rotating and zooming, with interpolation\n"); |
|---|
| 290 |
RotatePicture (screen, picture_again, 1, SMOOTHING_ON); |
|---|
| 291 |
|
|---|
| 292 |
/* Message */ |
|---|
| 293 |
writef ( "Just zooming (angle=0), with interpolation\n"); |
|---|
| 294 |
RotatePicture (screen, picture_again, 0, SMOOTHING_ON); |
|---|
| 295 |
|
|---|
| 296 |
SDL_FreeSurface (picture_again); |
|---|
| 297 |
|
|---|
| 298 |
/* Free the picture */ |
|---|
| 299 |
SDL_FreeSurface (picture); |
|---|
| 300 |
|
|---|
| 301 |
return; |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
int main () |
|---|
| 306 |
{ |
|---|
| 307 |
|
|---|
| 308 |
try |
|---|
| 309 |
{ |
|---|
| 310 |
DerelictSDL_Load(); |
|---|
| 311 |
} |
|---|
| 312 |
catch(Exception e) |
|---|
| 313 |
{ |
|---|
| 314 |
e.print(); |
|---|
| 315 |
exit(0); |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
SDL_Surface *screen; |
|---|
| 320 |
int w, h; |
|---|
| 321 |
int desired_bpp; |
|---|
| 322 |
Uint32 video_flags; |
|---|
| 323 |
|
|---|
| 324 |
/* Title */ |
|---|
| 325 |
writef ( "SDL_rotozoom test\n"); |
|---|
| 326 |
|
|---|
| 327 |
/* Set default options and check command-line */ |
|---|
| 328 |
w = 640; |
|---|
| 329 |
h = 480; |
|---|
| 330 |
desired_bpp = 0; |
|---|
| 331 |
video_flags = 0; |
|---|
| 332 |
|
|---|
| 333 |
/* Force double buffering */ |
|---|
| 334 |
video_flags |= SDL_DOUBLEBUF; |
|---|
| 335 |
|
|---|
| 336 |
/* Initialize SDL */ |
|---|
| 337 |
if (SDL_Init (SDL_INIT_VIDEO) < 0) |
|---|
| 338 |
{ |
|---|
| 339 |
writef ( "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
|---|
| 340 |
exit (1); |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
/* Initialize the display */ |
|---|
| 346 |
screen = SDL_SetVideoMode (w, h, desired_bpp, video_flags); |
|---|
| 347 |
if (screen is null) |
|---|
| 348 |
{ |
|---|
| 349 |
writef ( "Couldn't set %dx%dx%d video mode: %s\n", |
|---|
| 350 |
w, h, desired_bpp, SDL_GetError ()); |
|---|
| 351 |
exit (1); |
|---|
| 352 |
} |
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
/* Show some info */ |
|---|
| 356 |
printf ("Set %dx%dx%d mode\n", |
|---|
| 357 |
screen.w, screen.h, screen.format.BitsPerPixel); |
|---|
| 358 |
|
|---|
| 359 |
//printf ("Video surface located in %s memory.\n", |
|---|
| 360 |
// (screen.flags & SDL_HWSURFACE) ? "video" : "system"); |
|---|
| 361 |
|
|---|
| 362 |
/* Check for double buffering */ |
|---|
| 363 |
//if (screen.flags & SDL_DOUBLEBUF) |
|---|
| 364 |
// { |
|---|
| 365 |
// printf ("Double-buffering enabled - good!\n"); |
|---|
| 366 |
// } |
|---|
| 367 |
|
|---|
| 368 |
/* Set the window manager title bar */ |
|---|
| 369 |
SDL_WM_SetCaption ("SDL_rotozoom test", "rotozoom"); |
|---|
| 370 |
|
|---|
| 371 |
/* Do all the drawing work */ |
|---|
| 372 |
Draw (screen); |
|---|
| 373 |
|
|---|
| 374 |
SDL_Quit(); |
|---|
| 375 |
|
|---|
| 376 |
return (0); |
|---|
| 377 |
} |
|---|