diff --git a/Diya/DiyaFFIBase.class.st b/Diya/DiyaFFIBase.class.st index 6c52755..46898d0 100644 --- a/Diya/DiyaFFIBase.class.st +++ b/Diya/DiyaFFIBase.class.st @@ -28,7 +28,7 @@ DiyaFFIBase class >> moduleName [ ] ] on: Error do: [ ] ]. - DiyaCoreAPIError signal: 'Unable to find FFI library (', self checkSymbol, ')'. + DiyaFFILibNotFound signal: 'Unable to find FFI library (', self checkSymbol, ')'. ] { #category : #'library path' } diff --git a/Diya/DiyaFFILibNotFound.class.st b/Diya/DiyaFFILibNotFound.class.st new file mode 100644 index 0000000..534797c --- /dev/null +++ b/Diya/DiyaFFILibNotFound.class.st @@ -0,0 +1,5 @@ +Class { + #name : #DiyaFFILibNotFound, + #superclass : #DiyaError, + #category : #'Diya-Events' +} diff --git a/Diya/DiyaImageTex.class.st b/Diya/DiyaImageTex.class.st index 8575755..84dce7d 100644 --- a/Diya/DiyaImageTex.class.st +++ b/Diya/DiyaImageTex.class.st @@ -2,8 +2,7 @@ Class { #name : #DiyaImageTex, #superclass : #OpenGLTexImage2D, #instVars : [ - 'name', - 'surface' + 'name' ], #pools : [ 'OpenGLConstants', @@ -34,7 +33,7 @@ DiyaImageTex >> drop [ disable: GL_BLEND." ] -{ #category : #capture } +{ #category : #conversion } DiyaImageTex >> flipY [ |buffer size linesize top bottom| size := self bytesSize. @@ -51,10 +50,9 @@ DiyaImageTex >> flipY [ buffer free ] -{ #category : #capture } +{ #category : #'instance creation' } DiyaImageTex >> fromDisplay: aRect as: assetName [ - surface ifNotNil: [ surface free ] ifNil: [data ifNotNil: [data free]]. - surface := nil. + data ifNotNil: [data free]. width := aRect extent x asInteger. height := aRect extent y asInteger. data := FFIExternalArray externalNewType: GLubyte size: self bytesSize. @@ -71,25 +69,45 @@ DiyaImageTex >> fromDisplay: aRect as: assetName [ self flipY. ] -{ #category : #capture } +{ #category : #'instance creation' } DiyaImageTex >> fromDisplayAs: assetName [ self fromDisplay: (Rectangle origin: 0@0 extent: DiyaDisplay extent ) as: assetName ] { #category : #'instance creation' } DiyaImageTex >> fromFile: aPath [ + |surface| Transcript show: 'Loading texture from ', aPath fullName;cr. - name := aPath. - aPath exists ifFalse: [ ^DiyaCoreAPIError signal:'File not found ', aPath fullName ]. - surface := SDL2Image SDLImgLoad: aPath fullName. - surface ifNil: [ ^DiyaCoreAPIError signal:'Unable to load ', aPath fullName ]. + data ifNotNil: [ data free ]. + name := aPath fullName. + surface := self surfaceFromFile: aPath. width := surface w. height := surface h. - data := surface pixels. - surface autoRelease. + 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. 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' } DiyaImageTex >> initialize [ super initialize. @@ -120,7 +138,37 @@ DiyaImageTex >> setup [ parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MAG_FILTER param: GL_LINEAR. ] -{ #category : #accessing } -DiyaImageTex >> surface [ - ^ surface +{ #category : #conversion } +DiyaImageTex >> surfaceFromFile: aPath [ + 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 ]