Reduce skill issue in text drawing
This commit is contained in:
parent
cb6e2efead
commit
5541f9657f
6
TODO.md
6
TODO.md
|
@ -8,14 +8,14 @@
|
|||
- [ ] Base Node class
|
||||
- [ ] Base UI Node class
|
||||
- [ ] Base Audio Playback Node class
|
||||
- [ ] Split other sections (renderers, physics)
|
||||
- [ ] Split other sections into seperate libraries (renderers, physics)
|
||||
## Audio
|
||||
- [ ] OGG and WAV support (do first)
|
||||
- [ ] MP3 (later)
|
||||
- [ ] Audio playing nodes
|
||||
## Hibis UI (NEED A NAME)
|
||||
- [ ] GUI creation
|
||||
- [ ] Text Object (rendering via FreeType2)
|
||||
- [ ] Text Object (rendering via SDL2_TTF (RSDL) or FreeType2 (EVERYTHING ELSE))
|
||||
## RSDL (2D ONLY)
|
||||
- [X] Window creation
|
||||
- [X] Renderer creation
|
||||
|
@ -35,4 +35,4 @@
|
|||
## Hibis Physics (NEED A NAME)
|
||||
- [ ] Physics Shape creation
|
||||
- [ ] Collision via raycasting
|
||||
- [ ] Option to *allow* for Physics Shapes to be able to be drawn (on by default, both compile time and run time, turn off at compile time via version(`-version=NoPhysShapeDraw`?))
|
||||
- [ ] Option to *allow* for Physics Shapes to be able to be drawn (on by default, both compile time and run time, turn off at compile time via version(`-version=NoPhysShapeDraw`?))
|
||||
|
|
|
@ -8,7 +8,7 @@ abstract class Renderer {
|
|||
void clearScreen(Color col);
|
||||
void renderCurrent();
|
||||
|
||||
void drawText(Resource resource, string text, IntRect rect, Color color);
|
||||
void drawText(Resource resource, string text, IntVec2 pos, Color color);
|
||||
|
||||
// Pre and Post draw
|
||||
void preDraw();
|
||||
|
|
|
@ -14,6 +14,7 @@ import std.format;
|
|||
|
||||
import std.stdio;
|
||||
import std.string : toStringz;
|
||||
import std.array : split;
|
||||
|
||||
/** \class RSDL
|
||||
* Renderer implementation using SDL2 Renderer for the Hibis game engine
|
||||
|
@ -79,16 +80,21 @@ class RSDL : Renderer {
|
|||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
override void drawText(Resource resource, string text, IntRect rect, Color color) {
|
||||
override void drawText(Resource resource, string text, IntVec2 pos, Color color) {
|
||||
if (Font font = cast(Font)resource) {
|
||||
SDL_Surface* textSurface = TTF_RenderText_Solid(font.loadedFont, cast(const(char)*)toStringz(text), SDL_Color(color.r, color.g, color.b, color.a));
|
||||
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
TTF_SizeText(font.loadedFont, cast(const(char)*)toStringz(text), &width, &height);
|
||||
|
||||
SDL_Rect textRect;
|
||||
textRect.x = rect.x;
|
||||
textRect.y = rect.y;
|
||||
textRect.w = rect.w;
|
||||
textRect.h = rect.h;
|
||||
textRect.x = pos.x;
|
||||
textRect.y = pos.y;
|
||||
textRect.w = width;
|
||||
textRect.h = height;
|
||||
|
||||
SDL_RenderCopy(renderer, textTexture, null, &textRect);
|
||||
|
||||
|
|
|
@ -9,14 +9,43 @@ import std.format : format;
|
|||
|
||||
class Font : Resource {
|
||||
this(string path, uint size) {
|
||||
const char* nPath = cast(const(char)*)toStringz(path);
|
||||
loadedFont = TTF_OpenFont(nPath, size);
|
||||
writeln(format("Loaded font at '%s'", path));
|
||||
this.size = size;
|
||||
this.path = path;
|
||||
loadFont();
|
||||
}
|
||||
|
||||
~this() {
|
||||
TTF_CloseFont(loadedFont);
|
||||
}
|
||||
|
||||
public TTF_Font* loadedFont;
|
||||
uint getFontSize() { return size; }
|
||||
|
||||
void setFontSize(uint newSize) {
|
||||
size = newSize;
|
||||
loadFont(true);
|
||||
}
|
||||
|
||||
private void loadFont(bool reload = false) {
|
||||
const char* nPath = cast(const(char)*)toStringz(path);
|
||||
|
||||
// If already loaded, close font
|
||||
if (!(loadedFont is null)) {
|
||||
TTF_CloseFont(loadedFont);
|
||||
loadedFont = null;
|
||||
}
|
||||
|
||||
loadedFont = TTF_OpenFont(nPath, size);
|
||||
|
||||
// Do the message
|
||||
if (!didReload) {
|
||||
writeln(format(reload ? "Reloaded font from '%s'" : "Loaded font at '%s'", path));
|
||||
didReload = reload;
|
||||
}
|
||||
}
|
||||
|
||||
public TTF_Font* loadedFont = null;
|
||||
private uint size;
|
||||
private string path;
|
||||
|
||||
private bool didReload = false;
|
||||
}
|
|
@ -25,22 +25,36 @@ void main() {
|
|||
Font font = new Font("/usr/share/fonts/noto/NotoSans-Light.ttf", 16);
|
||||
}
|
||||
ubyte red = 0;
|
||||
bool increase = true;
|
||||
bool increaseRed = true;
|
||||
bool increaseSize = true;
|
||||
uint size = 16;
|
||||
uint f = 0;
|
||||
|
||||
logger(LoggingSeverity.Information, "Started Hibis test app! BEHOLD: Colours.");
|
||||
while (renderer.keepOpen) {
|
||||
engine.runNodeProcesses();
|
||||
|
||||
// Colour changing background!
|
||||
if ((red == 255 && increase) || (red == 0 && !increase)) {
|
||||
increase = !increase;
|
||||
if ((red == 255 && increaseRed) || (red == 0 && !increaseRed)) {
|
||||
increaseRed = !increaseRed;
|
||||
}
|
||||
if (increase) red += 1;
|
||||
if (increaseRed) red += 1;
|
||||
else red -= 1;
|
||||
|
||||
f++;
|
||||
if (f == 16) {
|
||||
f = 0;
|
||||
if ((size == 64 && increaseSize) || (size == 16 && !increaseSize)) {
|
||||
increaseSize = !increaseSize;
|
||||
}
|
||||
if (increaseSize) size += 2;
|
||||
else size -= 2;
|
||||
font.setFontSize(size);
|
||||
}
|
||||
|
||||
// Clear screen then sleep for ~16ms
|
||||
renderer.clearScreen(Color(red, 0, 0, 255));
|
||||
renderer.drawText(font, "TEST", IntRect(0, 0, 64, 64), Color(255, 255, 255, 255));
|
||||
renderer.drawText(font, "Testing Text", IntVec2(0, 0), Color(255, 255, 255, 255));
|
||||
|
||||
engine.drawNodes();
|
||||
renderer.renderCurrent();
|
||||
|
|
Loading…
Reference in a new issue