Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module rgp.dialog;
- import rengine.tehnic.draw;
- import rengine.text;
- import derelict.sdl2.ttf;
- import derelict.sdl2.sdl;
- import rengine.type;
- import std.string;
- import std.stdio : writeln;
- import std.math;
- import std.utf;
- static this()
- {
- version(Windows)
- DerelictSDL2ttf.load("SDL2_ttf.dll");
- version(Posix)
- DerelictSDL2ttf.load();
- if(TTF_Init() == -1)
- {
- writeln("ttf not init");
- }
- }
- import rengine.list;
- class Dialog : Draw
- {
- private
- {
- float speed = 0.3f;
- float frame = 1f;
- ubyte line = 0;
- uint oldline_frame = 1;
- //uint max_frame = 0;
- wstring text;
- wstring ctex;
- bool end = false;
- }
- public bool isEnd() //Is the dialogue over
- {
- return end;
- }
- public this() // constructor
- {
- c = cast(SDL_Color)Color(0,0,0,0); /// Color text
- cback = cast(SDL_Color)Color(255,255,255,0); /// Color background
- type = 2; /// type of drawing, do not pay attention
- larr.add(null);
- }
- void setMax(uint max) /// Maximum dialog width
- {
- max_width_dialog = cast(int)max;
- }
- public void setText(wstring t) /// set text
- {
- text = t;
- }
- public void setSpeed(float s) /// speed dialog
- {
- speed = s;
- }
- public void setFont(string font_filename,int size) /// font
- {
- tfont = TTF_OpenFont(font_filename.toStringz, size);
- }
- /*
- At each step, it should add a letter to the line,
- if the line exceeds the width of the dialog,
- it should go to a new line until the text ends.
- */
- public ListHandle!SDL_Texture step(SDL_Renderer* e)
- {
- /*
- If the current character number exceeds the number
- of characters in the text, he must end it,
- otherwise, continues, moving on to the next character.
- */
- if(frame < text.length)
- frame += speed;
- else
- end = true;
- if(!end) // if not end
- {
- SDL_Texture* ef = larr.get(line); //Get the texture of the current line
- if(ef != null) // If not empty, delete, for the sake of memory
- SDL_DestroyTexture(ef);
- /// Get the text of the current line from the text
- wstring curr = text[oldline_frame .. cast(uint)frame];
- //@DEBUG: writeln("Text:",curr);
- /// Make from text to surface(Unicode)
- surfaceMessage = TTF_RenderUNICODE_Solid(tfont,cast(ushort*)curr,c);
- assert(surfaceMessage);
- /// Make from surface to TEXTure
- ef = SDL_CreateTextureFromSurface(e,surfaceMessage);
- assert(ef);
- // yep
- SDL_FreeSurface(surfaceMessage);
- int w = 0,
- h = 0;
- SDL_QueryTexture(ef,null,null,&w,&h); /// get size texture
- larr.set(line,ef); /// Sets the texture to the current line
- ef = null;
- //@DEBUG: writeln(w);
- if(w > max_width_dialog)
- {
- larr.add(null);
- oldline_frame = cast(uint)frame;
- line++;
- }
- }
- return larr;
- }
- // Further it does not matter
- void setColorBack(Color cc)
- {
- cback = cast(SDL_Color)cc;
- }
- void setColor(Color cc)
- {
- c = cast(SDL_Color)cc;
- }
- override ListHandle!SDL_Texture DrawDLG(SDL_Renderer* e)
- {
- return step(e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement