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 Node class
|
||||||
- [ ] Base UI Node class
|
- [ ] Base UI Node class
|
||||||
- [ ] Base Audio Playback Node class
|
- [ ] Base Audio Playback Node class
|
||||||
- [ ] Split other sections (renderers, physics)
|
- [ ] Split other sections into seperate libraries (renderers, physics)
|
||||||
## Audio
|
## Audio
|
||||||
- [ ] OGG and WAV support (do first)
|
- [ ] OGG and WAV support (do first)
|
||||||
- [ ] MP3 (later)
|
- [ ] MP3 (later)
|
||||||
- [ ] Audio playing nodes
|
- [ ] Audio playing nodes
|
||||||
## Hibis UI (NEED A NAME)
|
## Hibis UI (NEED A NAME)
|
||||||
- [ ] GUI creation
|
- [ ] GUI creation
|
||||||
- [ ] Text Object (rendering via FreeType2)
|
- [ ] Text Object (rendering via SDL2_TTF (RSDL) or FreeType2 (EVERYTHING ELSE))
|
||||||
## RSDL (2D ONLY)
|
## RSDL (2D ONLY)
|
||||||
- [X] Window creation
|
- [X] Window creation
|
||||||
- [X] Renderer creation
|
- [X] Renderer creation
|
||||||
|
@ -35,4 +35,4 @@
|
||||||
## Hibis Physics (NEED A NAME)
|
## Hibis Physics (NEED A NAME)
|
||||||
- [ ] Physics Shape creation
|
- [ ] Physics Shape creation
|
||||||
- [ ] Collision via raycasting
|
- [ ] 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 clearScreen(Color col);
|
||||||
void renderCurrent();
|
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
|
// Pre and Post draw
|
||||||
void preDraw();
|
void preDraw();
|
||||||
|
|
|
@ -14,6 +14,7 @@ import std.format;
|
||||||
|
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.string : toStringz;
|
import std.string : toStringz;
|
||||||
|
import std.array : split;
|
||||||
|
|
||||||
/** \class RSDL
|
/** \class RSDL
|
||||||
* Renderer implementation using SDL2 Renderer for the Hibis game engine
|
* Renderer implementation using SDL2 Renderer for the Hibis game engine
|
||||||
|
@ -79,16 +80,21 @@ class RSDL : Renderer {
|
||||||
SDL_RenderPresent(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) {
|
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_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);
|
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;
|
SDL_Rect textRect;
|
||||||
textRect.x = rect.x;
|
textRect.x = pos.x;
|
||||||
textRect.y = rect.y;
|
textRect.y = pos.y;
|
||||||
textRect.w = rect.w;
|
textRect.w = width;
|
||||||
textRect.h = rect.h;
|
textRect.h = height;
|
||||||
|
|
||||||
SDL_RenderCopy(renderer, textTexture, null, &textRect);
|
SDL_RenderCopy(renderer, textTexture, null, &textRect);
|
||||||
|
|
||||||
|
|
|
@ -9,14 +9,43 @@ import std.format : format;
|
||||||
|
|
||||||
class Font : Resource {
|
class Font : Resource {
|
||||||
this(string path, uint size) {
|
this(string path, uint size) {
|
||||||
const char* nPath = cast(const(char)*)toStringz(path);
|
this.size = size;
|
||||||
loadedFont = TTF_OpenFont(nPath, size);
|
this.path = path;
|
||||||
writeln(format("Loaded font at '%s'", path));
|
loadFont();
|
||||||
}
|
}
|
||||||
|
|
||||||
~this() {
|
~this() {
|
||||||
TTF_CloseFont(loadedFont);
|
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);
|
Font font = new Font("/usr/share/fonts/noto/NotoSans-Light.ttf", 16);
|
||||||
}
|
}
|
||||||
ubyte red = 0;
|
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.");
|
logger(LoggingSeverity.Information, "Started Hibis test app! BEHOLD: Colours.");
|
||||||
while (renderer.keepOpen) {
|
while (renderer.keepOpen) {
|
||||||
engine.runNodeProcesses();
|
engine.runNodeProcesses();
|
||||||
|
|
||||||
// Colour changing background!
|
// Colour changing background!
|
||||||
if ((red == 255 && increase) || (red == 0 && !increase)) {
|
if ((red == 255 && increaseRed) || (red == 0 && !increaseRed)) {
|
||||||
increase = !increase;
|
increaseRed = !increaseRed;
|
||||||
}
|
}
|
||||||
if (increase) red += 1;
|
if (increaseRed) red += 1;
|
||||||
else 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
|
// Clear screen then sleep for ~16ms
|
||||||
renderer.clearScreen(Color(red, 0, 0, 255));
|
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();
|
engine.drawNodes();
|
||||||
renderer.renderCurrent();
|
renderer.renderCurrent();
|
||||||
|
|
Loading…
Reference in a new issue