UIRenderer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_core.h>
  23. #include <TurboBadger/tb_system.h>
  24. #include <TurboBadger/tb_debug.h>
  25. #include <TurboBadger/tb_node_tree.h>
  26. #include <TurboBadger/tb_widgets_reader.h>
  27. #include "../Graphics/Graphics.h"
  28. #include "../Graphics/Texture2D.h"
  29. #include "UIRenderer.h"
  30. namespace Atomic
  31. {
  32. class TBUIBitmap : public tb::TBBitmap
  33. {
  34. public:
  35. TBUIBitmap(UIRenderer *renderer);
  36. virtual ~TBUIBitmap();
  37. bool Init(int width, int height, tb::uint32 *data);
  38. virtual int Width() { return width_; }
  39. virtual int Height() { return height_; }
  40. virtual void SetData(tb::uint32 *data);
  41. public:
  42. UIRenderer *renderer_;
  43. int width_;
  44. int height_;
  45. SharedPtr<Texture2D> texture_;
  46. };
  47. TBUIBitmap::TBUIBitmap(UIRenderer *renderer)
  48. : renderer_(renderer), width_(0), height_(0)
  49. {
  50. }
  51. UIRenderer::UIRenderer(Context* context)
  52. {
  53. context_ = context;
  54. }
  55. UIRenderer::~UIRenderer()
  56. {
  57. }
  58. void UIRenderer::BeginPaint(int render_target_w, int render_target_h)
  59. {
  60. TBRendererBatcher::BeginPaint(render_target_w, render_target_h);
  61. }
  62. void UIRenderer::EndPaint()
  63. {
  64. TBRendererBatcher::EndPaint();
  65. }
  66. tb::TBBitmap *UIRenderer::CreateBitmap(int width, int height, tb::uint32 *data)
  67. {
  68. TBUIBitmap *bitmap = new TBUIBitmap(this);
  69. if (!bitmap->Init(width, height, data))
  70. {
  71. delete bitmap;
  72. return nullptr;
  73. }
  74. return bitmap;
  75. }
  76. void UIRenderer::RenderBatch(Batch *batch)
  77. {
  78. if (!batch->vertex_count)
  79. return;
  80. Texture2D* texture = NULL;
  81. if (batch->bitmap)
  82. {
  83. TBUIBitmap* tbuibitmap = (TBUIBitmap*)batch->bitmap;
  84. texture = tbuibitmap->texture_;
  85. }
  86. UIBatch b(BLEND_ALPHA , currentScissor_, texture, vertexData_);
  87. unsigned begin = b.vertexData_->Size();
  88. b.vertexData_->Resize(begin + batch->vertex_count * UI_VERTEX_SIZE);
  89. float* dest = &(b.vertexData_->At(begin));
  90. b.vertexEnd_ = b.vertexData_->Size();
  91. for (int i = 0; i < batch->vertex_count; i++)
  92. {
  93. Vertex* v = &batch->vertex[i];
  94. dest[0] = v->x; dest[1] = v->y; dest[2] = 0.0f;
  95. ((unsigned&)dest[3]) = v->col;
  96. dest[4] = v->u; dest[5] = v->v;
  97. dest += UI_VERTEX_SIZE;
  98. }
  99. UIBatch::AddOrMerge(b, *batches_);
  100. }
  101. void UIRenderer::SetClipRect(const tb::TBRect &rect)
  102. {
  103. currentScissor_.top_ = rect.y;
  104. currentScissor_.bottom_ = rect.y + rect.h;
  105. currentScissor_.left_ = rect.x;
  106. currentScissor_.right_ = rect.x + rect.w;
  107. }
  108. TBUIBitmap::~TBUIBitmap()
  109. {
  110. // Must flush and unbind before we delete the texture
  111. renderer_->FlushBitmap(this);
  112. }
  113. bool TBUIBitmap::Init(int width, int height, tb::uint32 *data)
  114. {
  115. assert(width == tb::TBGetNearestPowerOfTwo(width));
  116. assert(height == tb::TBGetNearestPowerOfTwo(height));
  117. width_ = width;
  118. height_ = height;
  119. texture_ = new Texture2D(renderer_->GetContext());
  120. texture_->SetMipsToSkip(QUALITY_LOW, 0); // No quality reduction
  121. texture_->SetNumLevels(1);
  122. texture_->SetAddressMode(COORD_U, ADDRESS_BORDER);
  123. texture_->SetAddressMode(COORD_V, ADDRESS_BORDER),
  124. texture_->SetBorderColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
  125. texture_->SetSize(width_, height_, Graphics::GetRGBAFormat(), TEXTURE_STATIC);
  126. SetData(data);
  127. return true;
  128. }
  129. void TBUIBitmap::SetData(tb::uint32 *data)
  130. {
  131. renderer_->FlushBitmap(this);
  132. texture_->SetData(0, 0, 0, width_, height_, data);
  133. }
  134. }