当前位置: 动力学知识库 > 问答 > 编程问答 >

d - dmd and gdc compiling code differently?

问题描述:

I am currently trying out DerelictSDL2 (a binding to the SDL2 library for D) and I've written a code that successfully loads a JPG image and displays it in a window. That is, when it's compiled with dmd. When I try with gdc (and no code modification), it does compile but it won't load the image at runtime.

I believe I did things right :

SDL_Init(SDL_INIT_VIDEO)

then

IMG_Init(IMG_INIT_JPG)

and somewhere after that

this.data = IMG_LoadTexture(Window.renderer, name.ptr)

where Window.renderer is (obviously) the SDL_Renderer* and name.ptr is a char* pointing to the name of the image to load. But when compiling with gdc, IMG_Load and IMG_LoadTexture both return null, while with dmd they return a pointer to the newly created texture...

Did I forget something else (after all, with dmd it worked even without IMG_Init) ? Does Derelict only works with dmd (even if it's only interfacing to C functions) ?

dmd : 2.065

gdc : 4.9.1

EDIT :

Turns out the problem is completely different. IMG_LoadTexture takes a pointer to data for its second argument but name.ptr seems to only work with dmd. However If I try with a hard-coded argument like this :

IMG_LoadTexture(renderer, "../test/res/image.jpg")

it works with both dmd and gdc.

网友答案:

there is no guarantee that D string will be 0-terminated. it just happens by chance with dmd. the correct way is to use toStringz() function from std.string module.

p.s. please note that string literals are 0-terminated, that's why hardcoded arguments works.

分享给朋友:
您可能感兴趣的文章:
随机阅读: