You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

5 years ago
  1. #include "Raster.h"
  2. namespace CELL
  3. {
  4. Raster::Raster( int w,int h,void* buffer )
  5. {
  6. _width = w;
  7. _height = h;
  8. _buffer = (Rgba*)buffer;
  9. }
  10. Raster::~Raster( void )
  11. {
  12. }
  13. void Raster::clear()
  14. {
  15. memset(_buffer,0,_width * _height * sizeof(Rgba));
  16. }
  17. void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
  18. {
  19. switch(ptSize)
  20. {
  21. case 1:
  22. setPixel(x,y,color);
  23. break;
  24. case 2:
  25. setPixel(x + 0, y + 0,color);
  26. setPixel(x + 1, y + 0,color);
  27. setPixel(x + 0, y + 1,color);
  28. setPixel(x + 1, y + 1,color);
  29. break;
  30. case 3:
  31. setPixel(x - 1, y - 1,color);
  32. setPixel(x + 0, y - 1,color);
  33. setPixel(x + 1, y - 1,color);
  34. setPixel(x - 1, y + 0,color);
  35. setPixel(x + 0, y + 0,color);
  36. setPixel(x + 1, y + 0,color);
  37. setPixel(x - 1, y + 1,color);
  38. setPixel(x + 0, y + 1,color);
  39. setPixel(x + 1, y + 1,color);
  40. break;
  41. }
  42. }
  43. void Raster::drawLine( float2 pt1,float2 pt2,Rgba color )
  44. {
  45. float xOffset = pt1.x - pt2.x;
  46. float yOffset = pt1.y - pt2.y;
  47. if (xOffset == 0)
  48. {
  49. float slope = yOffset / xOffset;
  50. for (float y = pt1.y; y <= pt2.y ; y += 1.0f)
  51. {
  52. setPixel(pt2.x,y,color);
  53. }
  54. }
  55. else if(yOffset == 0)
  56. {
  57. float slope = yOffset / xOffset;
  58. for (float x = pt1.x; x <= pt2.x ; x += 1.0f)
  59. {
  60. setPixel(x,pt2.y,color);
  61. }
  62. }
  63. else
  64. {
  65. float slope = yOffset / xOffset;
  66. for (float x = pt1.x; x <= pt2.x ; x += 1.0f)
  67. {
  68. float y = pt1.y + (x - pt1.x) * slope;
  69. setPixel(x,y,color);
  70. }
  71. }
  72. }
  73. }