Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module rgp.dialog;
- private import rengine.list; /// container
- private import rengine.type.image; /// image (analog texture)
- private import rengine.type.coord; /// coordinates
- private import rengine.type.color; /// color
- private import derelict.sdl2.ttf;
- private import derelict.sdl2.sdl;
- private import std.string;
- private import std.stdio : writeln;
- class Dialog
- {
- public string text; /// input text
- public uint presy_x = 0; /// Counter position when moving to a new line
- public bool isEnd = false; /// isEnd?
- public uint depth = 0;
- public float current = 1f; /// current sumbol
- public float speed = 0.1f; /// symbol/second
- private ListHandle!Image tex; /// container with textures
- public uint sy = 0; /// line position in the dialog
- public Color color = Color(0,0,0,255);
- public Color background = Color(255,255,255,255);
- public Coord position = Coord(0,0);
- private TTF_Font* font;
- private int oldTime = 0;
- private int FrameRate = 100;
- public uint size_x = 256; /// sie dialog
- public this()
- {
- auto tt = new Image;
- tex.add(tt);
- }
- public ListHandle!Image step(SDL_Renderer* e) /// called when drawing
- {
- if(oldTime + FrameRate > SDL_GetTicks()) {
- return tex;
- }
- oldTime = SDL_GetTicks();
- if(current < text.length)
- current += speed;
- else
- {
- isEnd = true;
- return tex;
- }
- Image tt;
- string temp = text[presy_x .. cast(int)current]; /// Copy line from text
- //writeln(temp);
- if(!tex.get(sy).isNull)
- tex.get(sy).free();
- // @DELETEME: temp = temp[0 .. $ - 1] ~ '\0';
- const(char*) t = (temp.toStringz); /// to 'c' string
- tex.get(sy).makeFromSurface(e,TTF_RenderUTF8_Blended(font,t,cast(SDL_Color)color)); /// the process of translating a string into a texture
- //writeln("W",tex.get(sy).getWidth);
- debug if(tex.get(sy).isNull) /// ERROR HANDLING
- assert(null,"ERROR");
- if(tex.get(sy).getWidth > size_x) /// If the texture exceeds the size, go to the next line
- {
- sy += 1;
- auto tx = new Image;
- tex.add(tx);
- presy_x = cast(int)current;
- current += 1f;
- }
- return tex;
- }
- public void loadFont(string path,int size = 14)
- {
- import std.file : exists;
- if(exists(path))
- font = TTF_OpenFont(path.toStringz,size);
- else
- writeln("Not find font");
- if(font == null)
- {
- writeln("SDL_ERROR!",TTF_GetError);
- }
- }
- public void setFont(TTF_Font* t)
- {
- font = t;
- }
- public int getX()
- {
- return position.x;
- }
- public int getY()
- {
- return position.y;
- }
- public Coord getCoord()
- {
- return position;
- }
- public void setCoord(Coord c)
- {
- position = c;
- }
- public int getHeight()
- {
- return TTF_FontHeight(font);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement