Comments (1)
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)
- `Circle` does not have same properties as `Actor` e.g. actor.pos, actor.position HOT 2
- Impossible to include other files in the game HOT 5
- Too many open files error HOT 5
- Make GameZero types better and more extensible HOT 2
- does not launch game window HOT 3
- UndefVarError: libsdl2_{ttf,image} not defined (Apple Silicon / M1 Mac) HOT 6
- Is GameZero.jl going to get any more updates? HOT 1
- Draw basic forms (Rect, Triangle) with angle
- How to prevent clearing of the screen? HOT 2
- Graphics not properly scaled on high dpi screen on Mac HOT 3
- Gamepad Controller Support HOT 1
- Unicode Chinese Characters (ttf) are not being shown HOT 5
- Unicode Chinese Characters (ttf) are not being shown HOT 2
- Control Render HOT 1
- New Bug HOT 1
- not working on julia 1.9 HOT 1
- This isn't an issue but I though it would be cool to share.
- Will GZ.jl eventually be able to do multiple files in a project? HOT 2
- Attempting to load resource files crashes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gamezero.jl.