Skip to content
New issue

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

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

Already on GitHub? # to your account

SDL_Image support #15

Open
DCubix opened this issue Jun 21, 2016 · 7 comments
Open

SDL_Image support #15

DCubix opened this issue Jun 21, 2016 · 7 comments

Comments

@DCubix
Copy link

DCubix commented Jun 21, 2016

So I was using this (really nice) library to port my code from C++ to Haxe but I see that there's no SDL_image support.
I need to use PNG and JPG images, but the SDL library only accepts BMPs.

I wanted to implement it myself, but I the library isn't on native-toolkit's GitHub.

Any ideas?

@ruby0x1
Copy link
Member

ruby0x1 commented Jun 21, 2016

You could in the meantime try linc_stb which supports more formats.

The key thing to note here is that linc_sdl will not and should not include external dependency (like SDL_image).

The ideal path would be:

  • make a native-toolkit version of the sdl image lib
  • make a linc library using it
  • use it alongside linc_sdl

I've never needed nor used SDL_image myself, so I don't know how dependent or independent it is from SDL and it's headers, but since they're both libraries, linc SDL image would just import linc SDL types on the haxe level, which would rope in the headers as needed I think.

@DCubix
Copy link
Author

DCubix commented Jun 21, 2016

Thanks! I'll try to use linc_stb.

@DCubix
Copy link
Author

DCubix commented Jun 21, 2016

No, it doesn't work. linc_sdl doesn't implement some crucial methods like createRGBSurfaceFrom and even if I implement this myself, something else crashes the program (createTextureFromSurface fails).
So I can't use StbImage...
Have you done this before?

Here's my code:

package com.sgtek.sengine;

import haxe.io.Bytes;
import sdl.SDL;
import sdl.Texture;
import stb.Image in IMG;

class Image {

    public var width(default, null) : Int;
    public var height(default, null) : Int;
    public var data(default, null) : Texture;

    public function new(game: Game, data: Bytes) {
        var sdata = IMG.load_from_memory(data.getData(), data.length, 4);
        var pitch = sdata.w * sdata.comp;
        pitch = (pitch + 3) & ~3;

                // Missing byte order check...
        var rm, gm, bm, am = 0;
        rm = 0x000000FF;
        gm = 0x0000FF00;
        bm = 0x00FF0000;
        am = (sdata.comp == 4) ? 0xFF000000 : 0;

        width = sdata.w;
        height = sdata.h;

        var tmp = SDL.createRGBSurfaceFrom(sdata.bytes, sdata.w, sdata.h,
                        sdata.comp * 8, pitch, rm, gm, bm, am); // I implemented this in the linc_sdl sources.
        if (tmp != null) {
            this.data = SDL.createTextureFromSurface(game.renderer, tmp); // Crash!!!
        }
    }
}

@ruby0x1
Copy link
Member

ruby0x1 commented Jun 21, 2016

I haven't directly, as I don't use the SDL renderer. Did you build with -debug flag? You can have a look at the callstack when it crashes - it's just c++, so you can use gdb/xcode/visual studio and attach to the process and then see where it's crashing and probably why. It's probably something simple enough to resolve at least.

@DCubix
Copy link
Author

DCubix commented Jun 21, 2016

This is my implementation in SDL.hx; I'm almost sure it's wrong.
I just need to pass the pixel data, which is of type BytesData, to the function, but the functions only accepts void*

static inline function createRGBSurfaceFrom(data:BytesData, w:Int, h:Int, depth:Int, pitch:Int, rm:UInt32, gm:UInt32, bm:UInt32, am:UInt32):Surface {
    return untyped __cpp__('SDL_CreateRGBSurfaceFrom(&{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})', data[0], w, h, depth, pitch, rm, gm, bm, am);
}

@ruby0x1
Copy link
Member

ruby0x1 commented Jun 21, 2016

It should be along the lines of (void*)&data[0] to the function. I don't see anything else obviously wrong with your function though. Don't forget SDL error handling. You can check if it disliked what you gave it.

btw, it's less ideal to implement it in untyped, rather put it in the linc cpp file.

@DCubix
Copy link
Author

DCubix commented Jun 21, 2016

Cool! thanks!
I implemented it in linc_sdl.cpp and it worked just fine 👍

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants