snowkit / linc_sdl

http://snowkit.github.io/linc/ Haxe/hxcpp @:native bindings for SDL
Other
30 stars 16 forks source link

SDL_Image support #15

Open DCubix opened 8 years ago

DCubix commented 8 years ago

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 commented 8 years ago

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:

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 commented 8 years ago

Thanks! I'll try to use linc_stb.

DCubix commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

Cool! thanks! I implemented it in linc_sdl.cpp and it worked just fine :+1: