DISTRHO Plugin Framework
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.

209 lines
5.3KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // ------------------------------------------------------
  17. // DGL Stuff
  18. #define GL_GLEXT_PROTOTYPES
  19. #include "App.hpp"
  20. #include "Window.hpp"
  21. #include "Widget.hpp"
  22. #include "src/freetype-gl/text-buffer.h"
  23. #include "src/freetype-gl/mat4.h"
  24. #include <GL/glext.h>
  25. #include <cstdio>
  26. #include <cwchar>
  27. // ------------------------------------------------------
  28. extern "C" {
  29. int z_verbose = 0;
  30. void z_error (char* message)
  31. {
  32. d_stderr2(message);
  33. }
  34. }
  35. // ------------------------------------------------------
  36. // use namespace
  37. using namespace DGL;
  38. // ------------------------------------------------------
  39. // Single color widget
  40. class TextWidget : public Widget
  41. {
  42. public:
  43. TextWidget(Window& parent)
  44. : Widget(parent),
  45. #if 1
  46. atlas(nullptr),
  47. fontmgr(nullptr),
  48. font(nullptr),
  49. #endif
  50. textbuf(nullptr)
  51. {
  52. vec2 pen = {{20, 200}};
  53. vec4 black = {{0.0, 0.0, 0.0, 1.0}};
  54. vec4 white = {{1.0, 1.0, 1.0, 1.0}};
  55. vec4 none = {{1.0, 1.0, 1.0, 0.0}};
  56. markup_t markup = {
  57. "normal",
  58. 24.0f, 0, 0,
  59. 0.0, 0.0, 2.0f,
  60. white, none,
  61. 0, white,
  62. 0, white,
  63. 0, white,
  64. 0, white,
  65. 0
  66. };
  67. wchar_t* text = L"A Quick Brown Fox Jumps Over The Lazy Dog 0123456789";
  68. #if 1
  69. atlas = texture_atlas_new(600, 300, 1);
  70. DISTRHO_SAFE_ASSERT_RETURN(atlas != nullptr,);
  71. //fontmgr = font_manager_new(600, 200, 2);
  72. //DISTRHO_SAFE_ASSERT_RETURN(fontmgr != nullptr,);
  73. //font = font_manager_get_from_filename(fontmgr, "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", 12.0f);
  74. //DISTRHO_SAFE_ASSERT_RETURN(font != nullptr,);
  75. font = texture_font_new_from_file(atlas, 12.0f, "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf");
  76. DISTRHO_SAFE_ASSERT_RETURN(font != nullptr,);
  77. #endif
  78. textbuf = text_buffer_new(LCD_FILTERING_OFF);
  79. DISTRHO_SAFE_ASSERT_RETURN(textbuf != nullptr,);
  80. textbuf->base_color = black;
  81. //markup.family = "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf";
  82. markup.font = font;
  83. text_buffer_printf(textbuf, &pen,
  84. &markup, text, nullptr);
  85. text_buffer_add_text(textbuf, &pen, &markup, text, std::wcslen(text));
  86. mat4_set_identity(&projection);
  87. mat4_set_identity(&model);
  88. mat4_set_identity(&view);
  89. }
  90. ~TextWidget()
  91. {
  92. if (textbuf != nullptr)
  93. {
  94. text_buffer_delete(textbuf);
  95. textbuf = nullptr;
  96. }
  97. #if 1
  98. if (font != nullptr)
  99. {
  100. texture_font_delete(font);
  101. font = nullptr;
  102. }
  103. if (fontmgr != nullptr)
  104. {
  105. font_manager_delete(fontmgr);
  106. fontmgr = nullptr;
  107. }
  108. if (atlas != nullptr)
  109. {
  110. texture_atlas_delete(atlas);
  111. atlas = nullptr;
  112. }
  113. #endif
  114. }
  115. private:
  116. void onDisplay() override
  117. {
  118. DISTRHO_SAFE_ASSERT_RETURN(textbuf != nullptr,);
  119. glClearColor(0.4f, 0.4f, 0.45f, 1.0f);
  120. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  121. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  122. glUseProgram(textbuf->shader);
  123. {
  124. glUniformMatrix4fv(glGetUniformLocation(textbuf->shader, "model"), 1, 0, model.data);
  125. glUniformMatrix4fv(glGetUniformLocation(textbuf->shader, "view"), 1, 0, view.data);
  126. glUniformMatrix4fv(glGetUniformLocation(textbuf->shader, "projection"), 1, 0, projection.data);
  127. text_buffer_render(textbuf);
  128. }
  129. }
  130. void onReshape(int width, int height) override
  131. {
  132. // make widget same size as window
  133. setSize(width, height);
  134. //Widget::onReshape(width, height);
  135. //mat4_set_identity(&projection);
  136. //mat4_set_identity(&model);
  137. //mat4_set_identity(&view);
  138. glViewport(0, 0, width, height);
  139. //mat4_set_orthographic(&projection, 0, width, 0, height, width, height);
  140. mat4_set_orthographic(&projection, 0, width, 0, height, -1, 1);
  141. }
  142. texture_atlas_t* atlas;
  143. font_manager_t* fontmgr;
  144. texture_font_t* font;
  145. text_buffer_t* textbuf;
  146. mat4 model, view, projection;
  147. };
  148. // ------------------------------------------------------
  149. // main entry point
  150. int main()
  151. {
  152. App app;
  153. Window win(app);
  154. TextWidget color(win);
  155. win.setSize(600, 300);
  156. win.setTitle("Text");
  157. win.show();
  158. app.exec();
  159. return 0;
  160. }
  161. // ------------------------------------------------------