Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. module rgp.dialog;
  2.  
  3. import rengine.tehnic.draw;
  4. import rengine.text;
  5. import derelict.sdl2.ttf;
  6. import derelict.sdl2.sdl;
  7. import rengine.type;
  8. import std.string;
  9. import std.stdio : writeln;
  10. import std.math;
  11. import std.utf;
  12. static this()
  13. {
  14. version(Windows)
  15. DerelictSDL2ttf.load("SDL2_ttf.dll");
  16. version(Posix)
  17. DerelictSDL2ttf.load();
  18. if(TTF_Init() == -1)
  19. {
  20. writeln("ttf not init");
  21. }
  22. }
  23.  
  24. import rengine.list;
  25.  
  26. class Dialog : Draw
  27. {
  28. private
  29. {
  30. float speed = 0.3f;
  31. float frame = 1f;
  32. ubyte line = 0;
  33. uint oldline_frame = 1;
  34. //uint max_frame = 0;
  35. wstring text;
  36. wstring ctex;
  37. bool end = false;
  38. }
  39.  
  40. public bool isEnd() //Is the dialogue over
  41. {
  42. return end;
  43. }
  44.  
  45. public this() // constructor
  46. {
  47. c = cast(SDL_Color)Color(0,0,0,0); /// Color text
  48. cback = cast(SDL_Color)Color(255,255,255,0); /// Color background
  49. type = 2; /// type of drawing, do not pay attention
  50.  
  51. larr.add(null);
  52. }
  53.  
  54. void setMax(uint max) /// Maximum dialog width
  55. {
  56. max_width_dialog = cast(int)max;
  57. }
  58.  
  59. public void setText(wstring t) /// set text
  60. {
  61. text = t;
  62. }
  63.  
  64. public void setSpeed(float s) /// speed dialog
  65. {
  66. speed = s;
  67. }
  68.  
  69. public void setFont(string font_filename,int size) /// font
  70. {
  71. tfont = TTF_OpenFont(font_filename.toStringz, size);
  72. }
  73.  
  74.  
  75. /*
  76. At each step, it should add a letter to the line,
  77. if the line exceeds the width of the dialog,
  78. it should go to a new line until the text ends.
  79. */
  80. public ListHandle!SDL_Texture step(SDL_Renderer* e)
  81. {
  82. /*
  83. If the current character number exceeds the number
  84. of characters in the text, he must end it,
  85. otherwise, continues, moving on to the next character.
  86. */
  87. if(frame < text.length)
  88. frame += speed;
  89. else
  90. end = true;
  91.  
  92. if(!end) // if not end
  93. {
  94. SDL_Texture* ef = larr.get(line); //Get the texture of the current line
  95.  
  96. if(ef != null) // If not empty, delete, for the sake of memory
  97. SDL_DestroyTexture(ef);
  98.  
  99. /// Get the text of the current line from the text
  100. wstring curr = text[oldline_frame .. cast(uint)frame];
  101. //@DEBUG: writeln("Text:",curr);
  102. /// Make from text to surface(Unicode)
  103. surfaceMessage = TTF_RenderUNICODE_Solid(tfont,cast(ushort*)curr,c);
  104. assert(surfaceMessage);
  105.  
  106. /// Make from surface to TEXTure
  107. ef = SDL_CreateTextureFromSurface(e,surfaceMessage);
  108. assert(ef);
  109.  
  110. // yep
  111. SDL_FreeSurface(surfaceMessage);
  112.  
  113. int w = 0,
  114. h = 0;
  115.  
  116. SDL_QueryTexture(ef,null,null,&w,&h); /// get size texture
  117.  
  118. larr.set(line,ef); /// Sets the texture to the current line
  119.  
  120. ef = null;
  121. //@DEBUG: writeln(w);
  122. if(w > max_width_dialog)
  123. {
  124. larr.add(null);
  125. oldline_frame = cast(uint)frame;
  126. line++;
  127. }
  128. }
  129.  
  130. return larr;
  131. }
  132.  
  133. // Further it does not matter
  134.  
  135. void setColorBack(Color cc)
  136. {
  137. cback = cast(SDL_Color)cc;
  138. }
  139.  
  140. void setColor(Color cc)
  141. {
  142. c = cast(SDL_Color)cc;
  143. }
  144.  
  145. override ListHandle!SDL_Texture DrawDLG(SDL_Renderer* e)
  146. {
  147. return step(e);
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement