linedraw.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <windows.h>
  2. static void __inline DrawPixel(int color, unsigned char *fb)
  3. {
  4. int a=*fb;
  5. a+=color;
  6. if (a>255)a=255;
  7. *fb=(unsigned char)a;
  8. }
  9. // based on abrash's wu antialiasing routine
  10. void line(unsigned char *fb, int X0, int Y0, int X1, int Y1, int w, int h)
  11. {
  12. unsigned int ErrorAdj=0, ErrorAcc;
  13. int DeltaX, DeltaY, XDir=1;
  14. if (Y0 > Y1)
  15. {
  16. int t = Y0; Y0 = Y1; Y1 = t;
  17. t = X0; X0 = X1; X1 = t;
  18. }
  19. DeltaY = Y1 - Y0;
  20. DeltaX = X1 - X0;
  21. if (DeltaX < 0)
  22. {
  23. XDir = -1;
  24. DeltaX = -DeltaX;
  25. }
  26. if (DeltaY > DeltaX) {
  27. ErrorAcc = (X0&0xffff);
  28. Y0>>=16;
  29. X0>>=16;
  30. DeltaY += 65535;
  31. DeltaY >>= 16;
  32. if (DeltaY) ErrorAdj = DeltaX/DeltaY;
  33. fb+=Y0*w+X0;
  34. while (DeltaY-- >= 0) {
  35. if (X0 > 0 && Y0 >= 0 && X0 < w-1 && Y0 < h)
  36. {
  37. int Weighting = ErrorAcc >> 10;
  38. DrawPixel(Weighting^63,fb);
  39. DrawPixel(Weighting,fb+XDir);
  40. }
  41. ErrorAcc += ErrorAdj;
  42. if (ErrorAcc >= 65536) {
  43. X0 += XDir;
  44. fb += XDir;
  45. ErrorAcc -= 65536;
  46. }
  47. fb+=w;
  48. Y0++;
  49. }
  50. }
  51. else
  52. {
  53. ErrorAcc = (Y0&0xffff);
  54. X0>>=16;
  55. Y0>>=16;
  56. DeltaX += 65535;
  57. DeltaX >>= 16;
  58. if (DeltaX) ErrorAdj = DeltaY/DeltaX;
  59. fb+=Y0*w+X0;
  60. while (DeltaX-- >= 0) {
  61. if (Y0 >= 0 && Y0 < h-1 && X0 >= 0 && X0 < w)
  62. {
  63. int Weighting = ErrorAcc >> 10;
  64. DrawPixel(Weighting^63,fb);
  65. DrawPixel(Weighting,fb+w);
  66. }
  67. ErrorAcc += ErrorAdj;
  68. if (ErrorAcc >= 65536) {
  69. Y0++;
  70. fb+=w;
  71. ErrorAcc-=65536;
  72. }
  73. fb+=XDir;
  74. X0+=XDir;
  75. }
  76. }
  77. }