home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!mimsy!afterlife!mssmith
- From: mssmith@afterlife.ncsc.mil (M. Scott Smith)
- Newsgroups: comp.sys.mac.programmer
- Subject: Direct screen writing; getting to (x,y)
- Message-ID: <1993Jan11.235448.22675@afterlife.ncsc.mil>
- Date: 11 Jan 93 23:54:48 GMT
- Organization: The Great Beyond
- Lines: 68
-
- Howdy..
-
- In my spare time (ha -- "spare time"? Yeah, right..) I've been fiddling
- around with doing direct-screen writing on the Mac. (That is, writing
- directly to the video memory to get fast(er) graphics than the traditional
- (but usually adequate) Quickdraw calls.)
-
- I'm familiar with the concept of writing LONGS out to the video RAM to
- achieve speed, but I want to have a function that simply plots ONE pixel
- point. (Color's not the issue at this point.) You pass an x- and y-, and
- it jumps to the memory location representing that coordinate.
-
- Here's something I threw together: (Think C code)
-
- void PlotPoint(long x, long y, long color)
- {
- if (SCR_X == 640)
- *(char *)(base_addr + x + ((y << 7) + (y << 9))) = color;
- else if (SCR_X == 512)
- *(char *)(base_addr + x + (y << 9)) = color;
- else if (SCR_X == 1024)
- *(char *)(base_addr + x + (y << 10)) = color;
- else
- *(char *)(base_addr + x + (row_bytes * y)) = color;
- }
-
- Actually, x- and y- should be integers, or can be, or whatever. base_addr
- is the base address of the video memory. (Corresponds to (0,0) in all cases,
- I hope!) row_bytes is the number of bytes in each row on the monitor.
- Color is just some hexadecimal value.
-
- And SCR_X is the width of the monitor, in pixels.
-
- The concept behind this code is that to get to (X,Y), I take the base
- address, add x to it, and then add (y * row_bytes) to get down to the right
- place.
-
- For speed's sake, I'm doing some tricks with the left shifts. If the
- width of the monitor is a nice multiple of 2 (say 640, 512, or 1024), then
- I perform left shifts to take care of the (y * row_bytes) part. If all
- else fails, I multiply the long way.
-
- Anyway, this code seems to work. I'd like a few suggestions on better ways
- of doing this. Do I have the right idea, or did I miss the boat completely?
-
- One thing I plan on trying is to create an array of rowbyte values for
- every y- position on the monitor, at the initialization part of the program.
-
- That is, say the monitor is 480 lines vertical; I'd then make an array
- 480 long and manually compute row_bytes*y for each y position, plugging the
- value into the array. Then I could simply do something like:
-
- base_addr + x + line[y]
-
- To get where I want to go. Intuitively it seems like this would be much
- quicker; after all, you're not doing any multiplication, which is the most
- expensive part of this routine. But, on second thought, C is really doing
- multiplication anytime you access a subscript of an array, right? (For
- example, if you want line[50], then it does some multiplication to go from
- the base address -- line -- up 50*sizeof(line) to get the value, right?)
-
- Any thoughts on topics I've raised, or direct-screen graphics in general,
- would be very appreciated.
-
- Thanks!
-
- M. Scott Smith
- (mssmith@afterlife.ncsc.mil)
-