Giter Site home page Giter Site logo

Comments (1)

jwortmann avatar jwortmann commented on June 25, 2024

I've gotten it to work!

There were a few pitfalls that I had to figure out:

  • the SDL function expects the pixel data as an array of UInt32 numbers, so the 8-bit color values for each channel first need to be combined into a single UInt32 for each pixel
  • (I think that) the pixel array which is passed as an argument needs to be stored in a permanent variable, so that the garbage collector won't free the corresponding memory which is referenced from the SDL function
  • depth is the combined bit-depth of all channels, i.e. here it should be set to 32
  • excerpt from the SDL3 docs at https://wiki.libsdl.org/SDL3/SDL_CreateSurfaceFrom#remarks

    Pitch is the offset in bytes from one row of pixels to the next, e.g. width*4 for SDL_PIXELFORMAT_RGBA8888.

So with this my code example looks as follows:

using SimpleDirectMediaLayer

images = Vector{UInt32}[]

function MyImageActor(image; kv...)
    height, width = size(image)
    depth = 32  # 8 bit per channel
    pitch = 4 * width
    format = SimpleDirectMediaLayer.LibSDL2.SDL_PIXELFORMAT_RGBA8888

    pixels = UInt32[]
    sizehint!(pixels, height * width)

    for pixel in 255 * image'
        push!(pixels, UInt32(red(pixel)) << 24 | UInt32(green(pixel)) << 16 | UInt32(blue(pixel)) << 8 | UInt32(alpha(pixel)))
    end

    push!(images, pixels)  # prevent memory of the pixel array being freed by garbage collector
    sf = SimpleDirectMediaLayer.LibSDL2.SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, depth, pitch, format)
    sf == C_NULL && error("surface is NULL")
    a = GameZero.Actor("", sf, Rect(0, 0, width, height), [1.0, 1.0], 0, 255, Dict{Symbol,Any}())
    for (k, v) in kv
        setproperty!(a, k, v)
    end
    return a
end

Probably there is potential for improvements in the code, but it seems to work so far. I still think it would be useful to have something like this built-in and managed directly by GameZero, so perhaps this issue can stay open as a feature request.

from gamezero.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.