1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-27 03:48:21 +01:00

Allow to fallback to VM plugins for image handling if SDL_image lib doesnt exist

This commit is contained in:
Dany LE 2022-08-12 12:02:49 +02:00
parent 133ddb0380
commit a3d00d52ef
3 changed files with 70 additions and 17 deletions

View File

@ -28,7 +28,7 @@ DiyaFFIBase class >> moduleName [
] ]
] on: Error do: [ ] ] on: Error do: [ ]
]. ].
DiyaCoreAPIError signal: 'Unable to find FFI library (', self checkSymbol, ')'. DiyaFFILibNotFound signal: 'Unable to find FFI library (', self checkSymbol, ')'.
] ]
{ #category : #'library path' } { #category : #'library path' }

View File

@ -0,0 +1,5 @@
Class {
#name : #DiyaFFILibNotFound,
#superclass : #DiyaError,
#category : #'Diya-Events'
}

View File

@ -2,8 +2,7 @@ Class {
#name : #DiyaImageTex, #name : #DiyaImageTex,
#superclass : #OpenGLTexImage2D, #superclass : #OpenGLTexImage2D,
#instVars : [ #instVars : [
'name', 'name'
'surface'
], ],
#pools : [ #pools : [
'OpenGLConstants', 'OpenGLConstants',
@ -34,7 +33,7 @@ DiyaImageTex >> drop [
disable: GL_BLEND." disable: GL_BLEND."
] ]
{ #category : #capture } { #category : #conversion }
DiyaImageTex >> flipY [ DiyaImageTex >> flipY [
|buffer size linesize top bottom| |buffer size linesize top bottom|
size := self bytesSize. size := self bytesSize.
@ -51,10 +50,9 @@ DiyaImageTex >> flipY [
buffer free buffer free
] ]
{ #category : #capture } { #category : #'instance creation' }
DiyaImageTex >> fromDisplay: aRect as: assetName [ DiyaImageTex >> fromDisplay: aRect as: assetName [
surface ifNotNil: [ surface free ] ifNil: [data ifNotNil: [data free]]. data ifNotNil: [data free].
surface := nil.
width := aRect extent x asInteger. width := aRect extent x asInteger.
height := aRect extent y asInteger. height := aRect extent y asInteger.
data := FFIExternalArray externalNewType: GLubyte size: self bytesSize. data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
@ -71,25 +69,45 @@ DiyaImageTex >> fromDisplay: aRect as: assetName [
self flipY. self flipY.
] ]
{ #category : #capture } { #category : #'instance creation' }
DiyaImageTex >> fromDisplayAs: assetName [ DiyaImageTex >> fromDisplayAs: assetName [
self fromDisplay: (Rectangle origin: 0@0 extent: DiyaDisplay extent ) as: assetName self fromDisplay: (Rectangle origin: 0@0 extent: DiyaDisplay extent ) as: assetName
] ]
{ #category : #'instance creation' } { #category : #'instance creation' }
DiyaImageTex >> fromFile: aPath [ DiyaImageTex >> fromFile: aPath [
|surface|
Transcript show: 'Loading texture from ', aPath fullName;cr. Transcript show: 'Loading texture from ', aPath fullName;cr.
name := aPath. data ifNotNil: [ data free ].
aPath exists ifFalse: [ ^DiyaCoreAPIError signal:'File not found ', aPath fullName ]. name := aPath fullName.
surface := SDL2Image SDLImgLoad: aPath fullName. surface := self surfaceFromFile: aPath.
surface ifNil: [ ^DiyaCoreAPIError signal:'Unable to load ', aPath fullName ].
width := surface w. width := surface w.
height := surface h. height := surface h.
data := surface pixels. data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
surface autoRelease. LibC memset: data getHandle value: 0 size: data size.
data autoRelease.
LibC memCopy: surface pixels getHandle to: data getHandle size: data size.
SDL2
freeSurface: surface.
Transcript show: 'Loaded ', aPath fullName;cr. Transcript show: 'Loaded ', aPath fullName;cr.
] ]
{ #category : #'instance creation' }
DiyaImageTex >> fromForm: aForm name: aName [
|surface|
name := aName.
data ifNotNil: [ data free ].
surface := self surfaceFromForm: aForm.
width := surface w.
height := surface h.
data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
LibC memset: data getHandle value: 0 size: data size.
data autoRelease.
LibC memCopy: surface pixels getHandle to: data getHandle size: data size.
SDL2
freeSurface: surface.
]
{ #category : #'instance creation' } { #category : #'instance creation' }
DiyaImageTex >> initialize [ DiyaImageTex >> initialize [
super initialize. super initialize.
@ -120,7 +138,37 @@ DiyaImageTex >> setup [
parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MAG_FILTER param: GL_LINEAR. parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MAG_FILTER param: GL_LINEAR.
] ]
{ #category : #accessing } { #category : #conversion }
DiyaImageTex >> surface [ DiyaImageTex >> surfaceFromFile: aPath [
^ surface aPath exists ifFalse: [ ^DiyaCoreAPIError signal:'File not found ', aPath fullName ].
[^ SDL2Image SDLImgLoad: aPath fullName] on: Error do: [
Transcript show: 'FALLLING BACCCCCCCCCCCCCCCCCCCCCCCCK'; cr.
^ self surfaceFromForm: (ImageReadWriter formFromFileNamed: aPath)
].
]
{ #category : #conversion }
DiyaImageTex >> surfaceFromForm: aForm [
|srcSurface destSurface form|
form := aForm asFormOfDepth: 32.
srcSurface :=
SDL2 createRGBSurfaceFromPixels: form bits
width: form width height: form height
depth: 32 pitch: (form width << 2)
rmask: 16r00ff0000
gmask: 16r000ff00
bmask: 16r00000ff
amask: 16rff000000.
destSurface :=
SDL2 createRGBSurfaceFromPixels: srcSurface pixels getHandle
width: form width height: form height
depth: 32 pitch: (form width << 2)
rmask: 16r000000ff
gmask: 16r0000ff00
bmask: 16r00ff0000
amask: 16rff000000.
SDL2 SDLBlitSurface: srcSurface srcRect: nil dest: destSurface dstRect: nil.
SDL2
freeSurface: srcSurface.
^ destSurface
] ]