Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raylib + LoadTexture #221

Closed
RobLoach opened this issue Oct 10, 2022 · 4 comments
Closed

raylib + LoadTexture #221

RobLoach opened this issue Oct 10, 2022 · 4 comments
Labels
question How to use the language

Comments

@RobLoach
Copy link

RobLoach commented Oct 10, 2022

Hey @vtereshkov... Saw you put together some small raylib examples, and I kind of built off of your idea here with raylib-umka, with a goal of supporting the entire raylib API through Umka 🚀

Ran into an issue though with Texture LoadTexture(const char* fileName)...

void umkaLoadTexture(UmkaStackSlot *params, UmkaStackSlot *result) {
    // TODO: Why does this need to be 1 instead of 0? Definition is...
    //     LoadTexture(fileName: str) : Texture
    const char * fileName = (const char *)params[1].ptrVal;
    TraceLog(LOG_INFO, "Loading: %s", fileName);
    result->ptrVal = umkaAllocData(result->ptrVal, sizeof(Texture2D), NULL);
    Texture2D out = LoadTexture(fileName);
    RAYLIB_UMKA_MEMCPY(result->ptrVal, &out, sizeof(Texture2D));
}

One would expect it to be params[0].ptrVal since fileName is the only parameter for the function. Do you know if I'm doing something wrong? I've created a Pull Request to demonstrate the change over at RobLoach/raylib-umka#10 .

    // LoadTexture()
    if (!umkaAddFunc(umka, "LoadTexture", &umkaLoadTexture)) {
        TraceLog(LOG_ERROR, "UMKA: Failed to add function LoadTexture()");
        return false;
    }
fn LoadTexture*(fileName: str): Texture

Texture* = struct {
  id: uint32
  width: int32
  height: int32
  mipmaps: int32
  format: int32
}
@marekmaskarinec
Copy link
Contributor

If you return a structure larger than one stack slot, it will overlap into the parameters. I recommend passing a return pointer for larger structures.

@vtereshkov
Copy link
Owner

vtereshkov commented Oct 10, 2022

@RobLoach A function that returns a structured result always has a hidden last parameter (a pointer to where the result should be stored in the caller). So this hidden parameter will be params[0] and the file name will be params[1]. You should also save the pointer from this hidden parameter into result.

However, your code seems to be assuming that you allocate the memory for the returned value in the callee, not the caller. Then you should return ^Texture instead of Texture and forget about the hidden parameter, because a pointer is not a structured result.

@vtereshkov vtereshkov added the question How to use the language label Oct 10, 2022
@RobLoach
Copy link
Author

Thanks so much for the help! That fixed it. I've created a follow up issue to switch the API to return pointers like ^Texture instead RobLoach/raylib-umka#12 . Raylib expects full objects, so I'll need to do some thinking on that one.

@RobLoach
Copy link
Author

Thanks for the inspiration with the Playground! Got something up and running over at https://robloach.github.io/raylib-umka/ 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question How to use the language
Projects
None yet
Development

No branches or pull requests

3 participants