UIRenderer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <TurboBadger/tb_core.h>
  2. #include <TurboBadger/tb_system.h>
  3. #include <TurboBadger/tb_debug.h>
  4. #include <TurboBadger/tb_node_tree.h>
  5. #include <TurboBadger/tb_widgets_reader.h>
  6. #include "../Graphics/Graphics.h"
  7. #include "../Graphics/Texture2D.h"
  8. #include "UIRenderer.h"
  9. namespace Atomic
  10. {
  11. class TBUIBitmap : public tb::TBBitmap
  12. {
  13. public:
  14. TBUIBitmap(UIRenderer *renderer);
  15. virtual ~TBUIBitmap();
  16. bool Init(int width, int height, tb::uint32 *data);
  17. virtual int Width() { return width_; }
  18. virtual int Height() { return height_; }
  19. virtual void SetData(tb::uint32 *data);
  20. public:
  21. UIRenderer *renderer_;
  22. int width_;
  23. int height_;
  24. SharedPtr<Texture2D> texture_;
  25. };
  26. TBUIBitmap::TBUIBitmap(UIRenderer *renderer)
  27. : renderer_(renderer), width_(0), height_(0)
  28. {
  29. }
  30. UIRenderer::UIRenderer(Context* context)
  31. {
  32. context_ = context;
  33. }
  34. UIRenderer::~UIRenderer()
  35. {
  36. }
  37. void UIRenderer::BeginPaint(int render_target_w, int render_target_h)
  38. {
  39. TBRendererBatcher::BeginPaint(render_target_w, render_target_h);
  40. }
  41. void UIRenderer::EndPaint()
  42. {
  43. TBRendererBatcher::EndPaint();
  44. }
  45. tb::TBBitmap *UIRenderer::CreateBitmap(int width, int height, tb::uint32 *data)
  46. {
  47. TBUIBitmap *bitmap = new TBUIBitmap(this);
  48. if (!bitmap->Init(width, height, data))
  49. {
  50. delete bitmap;
  51. return nullptr;
  52. }
  53. return bitmap;
  54. }
  55. void UIRenderer::RenderBatch(Batch *batch)
  56. {
  57. if (!batch->vertex_count)
  58. return;
  59. Texture2D* texture = NULL;
  60. if (batch->bitmap)
  61. {
  62. TBUIBitmap* tbuibitmap = (TBUIBitmap*)batch->bitmap;
  63. texture = tbuibitmap->texture_;
  64. }
  65. UIBatch b(BLEND_ALPHA , currentScissor_, texture, vertexData_);
  66. unsigned begin = b.vertexData_->Size();
  67. b.vertexData_->Resize(begin + batch->vertex_count * UI_VERTEX_SIZE);
  68. float* dest = &(b.vertexData_->At(begin));
  69. b.vertexEnd_ = b.vertexData_->Size();
  70. for (int i = 0; i < batch->vertex_count; i++)
  71. {
  72. Vertex* v = &batch->vertex[i];
  73. dest[0] = v->x; dest[1] = v->y; dest[2] = 0.0f;
  74. ((unsigned&)dest[3]) = v->col;
  75. dest[4] = v->u; dest[5] = v->v;
  76. dest += UI_VERTEX_SIZE;
  77. }
  78. UIBatch::AddOrMerge(b, *batches_);
  79. }
  80. void UIRenderer::SetClipRect(const tb::TBRect &rect)
  81. {
  82. currentScissor_.top_ = rect.y;
  83. currentScissor_.bottom_ = rect.y + rect.h;
  84. currentScissor_.left_ = rect.x;
  85. currentScissor_.right_ = rect.x + rect.w;
  86. }
  87. TBUIBitmap::~TBUIBitmap()
  88. {
  89. // Must flush and unbind before we delete the texture
  90. renderer_->FlushBitmap(this);
  91. }
  92. bool TBUIBitmap::Init(int width, int height, tb::uint32 *data)
  93. {
  94. assert(width == tb::TBGetNearestPowerOfTwo(width));
  95. assert(height == tb::TBGetNearestPowerOfTwo(height));
  96. width_ = width;
  97. height_ = height;
  98. texture_ = new Texture2D(renderer_->GetContext());
  99. texture_->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  100. texture_->SetNumLevels(1);
  101. texture_->SetAddressMode(COORD_U, ADDRESS_BORDER);
  102. texture_->SetAddressMode(COORD_V, ADDRESS_BORDER),
  103. texture_->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  104. texture_->SetSize(width_, height_, Graphics::GetRGBAFormat(), TEXTURE_STATIC);
  105. SetData(data);
  106. return true;
  107. }
  108. void TBUIBitmap::SetData(tb::uint32 *data)
  109. {
  110. renderer_->FlushBitmap(this);
  111. texture_->SetData(0, 0, 0, width_, height_, data);
  112. }
  113. }