Re: A86: Rotating sprites


[Prev][Next][Index][Thread]

Re: A86: Rotating sprites



With a 6 Mhz z80 and 128k ram, it's possible to program just about anything.
But the qeustion is whether or not you want do it.  Bitmap rotation has
always been hot topic in graphics programming.  Matt Johnson (cool guy who
created 86 Central) and I were talking about this a while ago, and I have
attached the html document that he found on it.  If you look at the last
part of the C source, you'll see a pretty simple, yet intensive loop for
drawing a rotated bitmap (sprite).

The first thing you'll notice is that there is trig and multiplications
being done in the loop.  You'd have to store preculated trig tables for both
sine and cosine (you'd end up storing the first quadrant and creating the
rest at runtime...good example is Matthew Shepcar's Peaworm), which would
take up a good amount of space.

The next thing you'll notice is that they're doing a lot of floating point
calculations in the inner loop.  That's almost always a no in any
time-critical graphics function (unless you have a math coprocessor and the
float and integer operations are interleaved in the loop, in which case you
save time because the integer cpu doesn't have to waste time with fixed
point calculations).  You would have to use fixed point numbers, which will
be a little tricky on an 8-bit processor.

There are also two multiplies for each pixel on the inner loop, and two per
line in the outer loop.  Not fun with fixed point.  Now, it could be done,
though it would be very slow for a sprite routine.  And the problem would be
that using small sprites (8x8 or even 16x16) would introduce so many errors
that the rotated sprites would look horrible (I wrote a fairly large space
shooter in c++ using a simple routine like the one here, and the sprites
look pretty bad).

Pre-rotating a few sprites doesn't sound so bad now, does it?  :)

>
> is there any way to display a sprite then rotate it without having to draw
all
> the sprites in the prog
> a routine could take the x,y cords a pointer to the sprite and a angle of
> rotation
>  is this possible at all???????

Title: DN - Re: 2D Bitmap rotation.
Deja News Toolbar
 
Interest Finder
Browse Groups
 New On Our Site!

 
Search
 

Find messages in the archive
Home About Deja News My Deja News Post Communities Home Power Search Help
 
Happy Holidays From Deja News
uBid Online Auction
uBid Online Auction

Message 24 of 51
return to current results

previous · post reply · next

help
 
Re: 2D Bitmap rotation.  more options
 
Author:   Wayne O. Cochran
Email: wcochran@eecs.wsu.edu
Date: 1997/09/16
Forums: comp.graphics.algorithms
more headers
  author profile
view thread


Steven wrote:
> 
> Does anyone know if there any free source code in 'C' for rotating  a
> bitmap? Very Urgently, any help would be appreciated.

The trick is to think backwards. Instead or rotating the source bitmap, 
scan the destination bitmap and find where the corresponding bit(s)
would
be in a rotated version of the source bitmap. Here is some C code for
rotating a PGM image -- should be cake to make it work with bitmaps.

good luck
--wayne

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "pixel.h"
#include "pgm.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define D2R(D) ((D)*(M_PI/180.0))

#define BACKGROUND 0

int main(int argc, char *argv[]) {
  PIXEL *image, *dstimage;
  PGM_ERROR error;
  int width, height;
  double w2, h2;
  int i,j;
  double angle, sine, cosine;
  
  if (argc != 4) {
    printf("syntax: %s <degrees> <input pgm file name> <out pgm file
name>\n", 
           argv[0]);
    exit (-1);
  }

  angle = D2R(atof(argv[1]));

  if ((image = pgmGetImage(argv[2], &width, &height, &error)) == NULL) {
    fprintf(stderr, "error reading image `%s'!\n", argv[2]);
    exit(-1);
  }
    
  if ((dstimage = (PIXEL *) malloc(width*height*sizeof(PIXEL))) == NULL)
{
    perror("malloc");
    exit(-1);
  }
  sine = sin(angle);
  cosine = cos(angle);

  w2 = width/2.0;
  h2 = height/2.0;

  for (i = 0; i < height; i++) {
    double x, y = i - h2;
    double ysine = y*sine, ycosine = y*cosine;
    for (j = 0; j < width; j++) {
      double srcx, srcy;
      int srci, srcj;
      x = j - w2;
      srcx = x*cosine + ysine;
      srcy = -x*sine + ycosine;
      srci = (int) (srcy + h2 + 0.5);
      srcj = (int) (srcx + w2 + 0.5);
      if (srci < 0 || srci >= height || srcj < 0 || srcj >= width)
        PUTPIXEL(dstimage,j,i,width,BACKGROUND);
      else {
        int pixel = GETPIXEL(image,srcj,srci,width);
        PUTPIXEL(dstimage,j,i,width,pixel);
      }
    }
  }

  if ((error = pgmPutImage (argv[3], dstimage, width, height)) 
      != PGM_NO_ERROR) {
    fprintf(stderr, "error writing image `%s'!\n", argv[3]);
    exit(-1);
  }

  return 0;
}

-- 
Wayne O. Cochran
wcochran@eecs.wsu.edu  
http://www.eecs.wsu.edu/~wcochran  
Ecclesiastes 3:11

Email a Friend
Sponsored by:
Audio Book Club

previous · post reply · next
help

 
uBid Online Auction
uBid Online Auction
 
  Home  ·  Search  ·  My Deja News  ·  Post  ·  Power Search  ·  Help  ·  How are we doing?

FREE STUFF @FreeShop! |  Developer Resources |  Express by Infoseek
NextCard VISA 2.9% intro APR |  Directories |  Shopping |  Auctions & Classifieds
Yellow Pages |  Four audio books for a penny! |  Get Great Deals at uBid!
Trade Now! with Datek Online |  Win CASH on Uproar game shows!

New Users · About Deja News · Ad Info · Our Advertisers · Deja News Store
Free Web Email · Link to Deja News · Create Your Own Community


Copyright © 1995-98 Deja News, Inc. All rights reserved. Conditions of use.
Site privacy statement reviewed by TRUSTe.


Follow-Ups: References: