Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Introduce d2c module to handle correct conversion from d strings to c…
Browse files Browse the repository at this point in the history
… strings.

Fix for #40
  • Loading branch information
Dgame committed May 1, 2015
1 parent 9a3651f commit a6d32e7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 41 deletions.
11 changes: 6 additions & 5 deletions source/Dgame/Graphic/Surface.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Dgame.Math.Vector2;
import Dgame.Graphic.Color;

import Dgame.Internal.Error;
import Dgame.Internal.d2c;

@nogc
void verifyDepth(ubyte depth) pure nothrow {
Expand Down Expand Up @@ -215,15 +216,15 @@ public:

immutable bool ex = exists(filename);
if (!ex) {
print_fmt("File %s does not exists.\n", filename.ptr);
print_fmt("File %s does not exists.\n", toStringz(filename));
return false;
}

SDL_FreeSurface(_surface); // free old surface

_surface = IMG_Load(filename.ptr);
_surface = IMG_Load(toStringz(filename));
if (!_surface) {
print_fmt("Could not load image %s. Error: %s.\n", filename.ptr, SDL_GetError());
print_fmt("Could not load image %s. Error: %s.\n", toStringz(filename), SDL_GetError());
return false;
}

Expand Down Expand Up @@ -259,9 +260,9 @@ public:
*/
@nogc
bool saveToFile(string filename) nothrow {
immutable int result = IMG_SavePNG(_surface, filename.ptr);
immutable int result = IMG_SavePNG(_surface, toStringz(filename));
if (result != 0) {
print_fmt("Could not save image %s. Error: %s.\n", filename.ptr, SDL_GetError());
print_fmt("Could not save image %s. Error: %s.\n", toStringz(filename), SDL_GetError());
return false;
}

Expand Down
46 changes: 46 additions & 0 deletions source/Dgame/Internal/d2c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Dgame.Internal.d2c;

private:

//import Dgame.Internal.Error;
import Dgame.Internal.m3;

char[] heapBuffer;
char[256] stackBuffer = void;

@nogc
char* make_buffer(size_t len) nothrow {
if (stackBuffer.length > len)
return stackBuffer.ptr;

if (heapBuffer.length > len)
return heapBuffer.ptr;

//print_fmt("(Re)Order heap buffer: %d\n", len + 1);
heapBuffer = remake(heapBuffer, len + 1);

return heapBuffer.ptr;
}

public:

@nogc
static ~this() nothrow {
//print_fmt("Free heap buffer: %d\n", heapBuffer.length);
unmake(heapBuffer);
}

@nogc
const(char)* toStringz(string text) nothrow {
if (text.length == 0)
return null;

if (text[$ - 1] == '\0')
return text.ptr;

char* ptr = make_buffer(text.length);
ptr[0 .. text.length] = text[];
ptr[text.length] = '\0';

return ptr;
}
39 changes: 6 additions & 33 deletions source/Dgame/System/Font.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,7 @@ import Dgame.Graphic.Color;
import Dgame.Graphic.Surface;

import Dgame.Internal.Error;
import Dgame.Internal.m3;

char[] buffer;

@nogc
static ~this() nothrow {
//print_fmt("Free font buffer: %d\n", buffer.length);
unmake(buffer);
}

@nogc
char* make(size_t len, char[] buf) nothrow {
if (buf.length > len)
return buf.ptr;

if (buffer.length > len)
return buffer.ptr;

//print_fmt("(Re)Order font buffer: %d\n", len + 1);
buffer = remake(buffer, len + 1);

return buffer.ptr;
}
import Dgame.Internal.d2c;

public:

Expand Down Expand Up @@ -129,9 +107,9 @@ public:
@nogc
bool loadFromFile(string filename, ubyte fontSize) nothrow {
_fontSize = fontSize == 0 ? DefaultSize : fontSize;
_ttf = TTF_OpenFont(filename.ptr, _fontSize);
_ttf = TTF_OpenFont(toStringz(filename), _fontSize);
if (!_ttf) {
print_fmt("Error by loading TTF_Font %s: %s\n", filename.ptr, TTF_GetError());
print_fmt("Error by loading TTF_Font %s: %s\n", toStringz(filename), TTF_GetError());
return false;
}

Expand Down Expand Up @@ -179,21 +157,16 @@ public:
_transfer(fg, a);
_transfer(bg, b);

char[256] buf = void;
char* ptr = make(text.length, buf[]);
ptr[0 .. text.length] = text[];
ptr[text.length] = '\0';

SDL_Surface* srfc;
final switch (mode) {
case Mode.Solid:
srfc = TTF_RenderUTF8_Solid(_ttf, ptr, a);
srfc = TTF_RenderUTF8_Solid(_ttf, toStringz(text), a);
break;
case Mode.Shaded:
srfc = TTF_RenderUTF8_Shaded(_ttf, ptr, a, b);
srfc = TTF_RenderUTF8_Shaded(_ttf, toStringz(text), a, b);
break;
case Mode.Blended:
srfc = TTF_RenderUTF8_Blended(_ttf, ptr, a);
srfc = TTF_RenderUTF8_Blended(_ttf, toStringz(text), a);
break;
}

Expand Down
5 changes: 3 additions & 2 deletions source/Dgame/Window/Window.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import Dgame.Window.Internal.Init;

import Dgame.Internal.Error;
import Dgame.Internal.m3;
import Dgame.Internal.d2c;

static if (!SDL_VERSION_ATLEAST(2, 0, 4))
enum int SDL_WINDOW_MOUSE_CAPTURE = 0;
Expand Down Expand Up @@ -150,7 +151,7 @@ public:
if (style & Style.OpenGL)
_initGLAttr(gl_settings);

_window = SDL_CreateWindow(title.ptr, rect.x, rect.y, rect.width, rect.height, style);
_window = SDL_CreateWindow(toStringz(title), rect.x, rect.y, rect.width, rect.height, style);
assert(_window, "SDL_Window could not be created.");

if (style & Style.OpenGL) {
Expand Down Expand Up @@ -586,7 +587,7 @@ public:
@nogc
string setTitle(string title) nothrow {
string old_title = this.getTitle();
SDL_SetWindowTitle(_window, title.ptr);
SDL_SetWindowTitle(_window, toStringz(title));

return old_title;
}
Expand Down
10 changes: 10 additions & 0 deletions source/Dgame/docs/d2c.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html><head>
<META http-equiv="content-type" content="text/html; charset=utf-8">
<title>Dgame.Internal.d2c</title>
</head><body>
<h1>Dgame.Internal.d2c</h1>
<!-- Generated by Ddoc from ..\Internal\d2c.d -->
<br><br>

<hr><small>Page generated by <a href="http://dlang.org/ddoc.html">Ddoc</a>. </small>
</body></html>
2 changes: 1 addition & 1 deletion source/Dgame/test/.dub/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"derelict-sdl2": "1.9.5",
"derelict-gl3": "1.0.12"
},
"lastUpgrade": "2015-04-29T23:04:38.3196326"
"lastUpgrade": "2015-05-01T11:05:13.0177006"
}
}
6 changes: 6 additions & 0 deletions source/Dgame/test/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ void main() {
blit_test.blit(sdl_logo, null, &dest);
blit_test.saveToFile("samples/images/blit_test.png");

string filenameWithTail = "samples/images/XS.png;trailing chars goes here";
string cleanFilename = filenameWithTail[0 .. 21];
writeln("Clean filename is ", cleanFilename);
Surface xs2 = Surface(cleanFilename);
xs2.saveToFile("samples/images/xs2.png");

Surface wiki_srfc = Surface("samples/images/wiki.png");
wiki_srfc.saveToFile("samples/images/wiki_copy.png");

Expand Down
Binary file added source/Dgame/test/samples/images/xs2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a6d32e7

Please sign in to comment.