1
0

osx_canvas_layer.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <tataki/canvas/bltcanvas.h>
  2. inline float QuartzBlue(RGB32 color)
  3. {
  4. unsigned char *pixel = (unsigned char *)&color;
  5. return pixel[0] / 255.f;
  6. }
  7. inline float QuartzGreen(RGB32 color)
  8. {
  9. unsigned char *pixel = (unsigned char *)&color;
  10. return pixel[1] / 255.f;
  11. }
  12. inline float QuartzRed(RGB32 color)
  13. {
  14. unsigned char *pixel = (unsigned char *)&color;
  15. return pixel[2] / 255.f;
  16. }
  17. inline float QuartzAlpha(RGB32 color)
  18. {
  19. unsigned char *pixel = (unsigned char *)&color;
  20. return pixel[3] / 255.f;
  21. }
  22. BltCanvas::BltCanvas(int width, int height, OSWINDOWHANDLE wnd)
  23. {
  24. CGrafPtr qdcontext = GetWindowPort(wnd);
  25. CGContextRef temp;
  26. QDBeginCGContext(qdcontext, &temp);
  27. CGSize size = CGSizeMake(width, height);
  28. layer = CGLayerCreateWithContext(temp, size, NULL);
  29. context = CGLayerGetContext(layer);
  30. QDEndCGContext(qdcontext, &temp);
  31. }
  32. void BltCanvas::blit(int srcx, int srcy, Canvas *dest, int dstx, int dsty, int dstw, int dsth)
  33. {
  34. CGPoint point = CGPointMake(dstx-srcx, dsty-srcy);
  35. CGContextDrawLayerAtPoint(dest->getHDC(), point, layer);
  36. }
  37. void BltCanvas::blitToRect(api_canvas *canvas, RECT *src, RECT *dst, int alpha)
  38. {
  39. CGContextRef dest = canvas->getHDC();
  40. CGContextSaveGState(dest);
  41. CGContextSetAlpha(dest, (float)alpha/255.f);
  42. // TODO: deal with width properly
  43. CGRect rect = CGRectMake(dst->left - src->left, dst->top - src->top, dst->right - dst->left, dst->bottom - dst->top);
  44. CGContextDrawLayerInRect(dest, rect, layer);
  45. CGContextRestoreGState(dest);
  46. }
  47. void BltCanvas::stretchblit(int srcx, int srcy, int srcw, int srch, Canvas *dest, int dstx, int dsty, int dstw, int dsth)
  48. {
  49. CGContextSaveGState(context);
  50. CGContextTranslateCTM(context, srcx, srcy);
  51. CGRect rect = CGRectMake(dstx, dsty, dstw, dsth);
  52. CGContextDrawLayerInRect(dest->getHDC(), rect, layer);
  53. CGContextRestoreGState(context);
  54. }
  55. void BltCanvas::stretchToRectAlpha(api_canvas *canvas, RECT *src, RECT *dst, int alpha)
  56. {
  57. CGContextRef dest = canvas->getHDC();
  58. CGContextSaveGState(dest);
  59. CGContextSetAlpha(dest, (float)alpha/255.f);
  60. // TODO: deal with width properly
  61. CGRect rect = CGRectMake(dst->left - src->left, dst->top - src->top, dst->right - dst->left, dst->bottom - dst->top);
  62. CGContextDrawLayerInRect(dest, rect, layer);
  63. CGContextRestoreGState(dest);
  64. }
  65. void BltCanvas::DestructiveResize(int w, int h, int nb_bpp)
  66. {
  67. CGSize size = CGSizeMake(w, h);
  68. CGLayerRef newlayer = CGLayerCreateWithContext(context, size, NULL);
  69. CGContextRelease(context);
  70. CGLayerRelease(layer);
  71. layer = newlayer;
  72. context = CGLayerGetContext(layer);
  73. }
  74. void BltCanvas::fillBits(ARGB32 color)
  75. {
  76. CGContextSetRGBFillColor(context,
  77. QuartzRed(color), // red
  78. QuartzGreen(color), // green
  79. QuartzBlue(color), // blue
  80. QuartzAlpha(color) // alpha
  81. );
  82. CGContextFillRect(context, CGRectInfinite);
  83. }