From 141bf2e226ca8f4033f7a9cc4e8e299a1f6273b7 Mon Sep 17 00:00:00 2001 From: Dany LE Date: Tue, 9 Aug 2022 00:22:18 +0200 Subject: [PATCH] use SDL2_image for texture loading --- Diya/AssetManager.class.st | 26 +++++++++++++++++---- Diya/DiyaBoot.class.st | 2 ++ Diya/DiyaExampleApp.class.st | 5 +++- Diya/DiyaFFIBase.class.st | 8 ++++--- Diya/DiyaImageTex.class.st | 43 +++++++++++++++-------------------- Diya/OpenGLConstants.class.st | 4 ++++ Diya/SDL2Image.class.st | 30 ++++++++++++++++++++++++ Diya/SDL_DisplayMode.class.st | 10 ++++++++ 8 files changed, 94 insertions(+), 34 deletions(-) create mode 100644 Diya/SDL2Image.class.st diff --git a/Diya/AssetManager.class.st b/Diya/AssetManager.class.st index 1e203a1..2188207 100644 --- a/Diya/AssetManager.class.st +++ b/Diya/AssetManager.class.st @@ -19,7 +19,17 @@ AssetManager class >> fromLocation: loc [ ^self new from: loc; yourself ] -{ #category : #'as yet unclassified' } +{ #category : #adding } +AssetManager >> addAsset: value [ + assets at: value name put: value +] + +{ #category : #adding } +AssetManager >> addAssetName: name value: value [ + assets at: name put: value +] + +{ #category : #'instance creation' } AssetManager >> from: location [ base := location ] @@ -31,12 +41,18 @@ AssetManager >> initialize [ base := self class defaultAssetLocation ] -{ #category : #'as yet unclassified' } -AssetManager >> name: fileName of: type [ - ^assets at: fileName ifAbsentPut: [(type fromFile: (base / fileName))]. +{ #category : #accessing } +AssetManager >> name: name [ + ^assets at: name. ] -{ #category : #'as yet unclassified' } +{ #category : #accessing } +AssetManager >> name: name of: type [ + ^assets at: name ifAbsentPut: [ + (type fromFile: (base / name))]. +] + +{ #category : #accessing } AssetManager >> texture: name [ ^self name: name of: DiyaImageTex ] diff --git a/Diya/DiyaBoot.class.st b/Diya/DiyaBoot.class.st index f0941c1..245cf43 100644 --- a/Diya/DiyaBoot.class.st +++ b/Diya/DiyaBoot.class.st @@ -90,6 +90,8 @@ DiyaBoot >> init [ SDL2 showCursor: 0. DiyaSingleton resetAll. DiyaFontManager uniqueInstance loadFonts. + +Form class removeSelector: #serviceImageAsBackground ] { #category : #events } diff --git a/Diya/DiyaExampleApp.class.st b/Diya/DiyaExampleApp.class.st index 225d776..fcf8c45 100644 --- a/Diya/DiyaExampleApp.class.st +++ b/Diya/DiyaExampleApp.class.st @@ -68,13 +68,16 @@ DiyaExampleApp >> main [ { #category : #accessing } DiyaExampleApp >> setup [ |node node1 ell label icon button| + + "DiyaRendererContext uniqueInstance assets + addAsset: + ((Form fromDisplay: ( Rectangle origin: 0@0 corner: 300@300 )) asDiyaTexture: 'display')." self defineStyleSheet. label := root addNode: (DiyaLabel new) at: 10@40. label extent: 250@24. label styleName:#text_icon_1. label icon: 16rF254. - node1 := root addNode: (DiyaRectangle size:100@150 shader: DiyaExampleShader uniqueInstance) at: 300 @ 40. "node1 rotation: (Float pi / 8.0)." node1 scale: 1.2@1.2. diff --git a/Diya/DiyaFFIBase.class.st b/Diya/DiyaFFIBase.class.st index 16042dc..6c52755 100644 --- a/Diya/DiyaFFIBase.class.st +++ b/Diya/DiyaFFIBase.class.st @@ -22,9 +22,11 @@ DiyaFFIBase class >> libNames [ { #category : #accessing } DiyaFFIBase class >> moduleName [ self libNames do:[:aName| - (ExternalAddress loadSymbol: self checkSymbol from: aName) ifNotNil: [ - ^ aName - ]. + [ + (ExternalAddress loadSymbol: self checkSymbol from: aName) ifNotNil: [ + ^ aName + ] + ] on: Error do: [ ] ]. DiyaCoreAPIError signal: 'Unable to find FFI library (', self checkSymbol, ')'. ] diff --git a/Diya/DiyaImageTex.class.st b/Diya/DiyaImageTex.class.st index c7f2713..972be3b 100644 --- a/Diya/DiyaImageTex.class.st +++ b/Diya/DiyaImageTex.class.st @@ -2,7 +2,8 @@ Class { #name : #DiyaImageTex, #superclass : #OpenGLTexImage2D, #instVars : [ - 'path' + 'name', + 'surface' ], #pools : [ 'OpenGLConstants', @@ -25,28 +26,15 @@ DiyaImageTex >> drop [ { #category : #'instance creation' } DiyaImageTex >> fromFile: aPath [ - |form color index| - path := aPath. - Transcript show: 'Loading texture from ', path fullName;cr. - form := ImageReadWriter formFromFileNamed: path. - data := FFIExternalArray externalNewType: GLubyte size:(form width) * (form height) * 4. - LibC memset: data getHandle value: 0 size: data size. - data autoRelease. - width := form width. - height := form height. - index := 1. - 0 to: height -1 do:[:y| - 0 to: width - 1 do:[:x| - color := (form colorAt: x@y) as4bytesRGB. - data - at: index put: color first; - at: index + 1 put: (color at: 2); - at: index +2 put: (color at: 3); - at: index + 3 put: color last. - index := index + 4. - ] - ]. - + Transcript show: 'Loading texture from ', aPath fullName;cr. + aPath exists ifFalse: [ ^DiyaCoreAPIError signal:'File not found ', aPath fullName ]. + surface := SDL2Image SDLImgLoad: aPath fullName. + surface ifNil: [ ^DiyaCoreAPIError signal:'Unable to load ', aPath fullName ]. + width := surface w. + height := surface h. + data := surface pixels. + surface autoRelease. + Transcript show: 'Loaded ', aPath fullName;cr. ] { #category : #'instance creation' } @@ -66,8 +54,8 @@ DiyaImageTex >> mipmap [ ] { #category : #accessing } -DiyaImageTex >> path [ - ^ path +DiyaImageTex >> name [ + ^ name ] { #category : #accessing } @@ -78,3 +66,8 @@ DiyaImageTex >> setup [ parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MIN_FILTER param: GL_LINEAR; parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MAG_FILTER param: GL_LINEAR. ] + +{ #category : #accessing } +DiyaImageTex >> surface [ + ^ surface +] diff --git a/Diya/OpenGLConstants.class.st b/Diya/OpenGLConstants.class.st index 81e7f0d..b78c9b8 100644 --- a/Diya/OpenGLConstants.class.st +++ b/Diya/OpenGLConstants.class.st @@ -11,6 +11,8 @@ Class { 'GL_ARRAY_BUFFER', 'GL_ARRAY_BUFFER_BINDING', 'GL_ATTACHED_SHADERS', + 'GL_BGR', + 'GL_BGRA', 'GL_BLEND', 'GL_BLUE', 'GL_BYTE', @@ -115,6 +117,8 @@ OpenGLConstants class >> initCommonConstants [ GL_ALPHA := 16r1906. GL_RGB := 16r1907. GL_RGBA := 16r1908. + GL_BGR := 16r80E0. + GL_BGRA := 16r80E1. GL_LINE_SMOOTH := 16r0B20. GL_LINE_SMOOTH_HINT := 16r0C52 ] diff --git a/Diya/SDL2Image.class.st b/Diya/SDL2Image.class.st new file mode 100644 index 0000000..4fafc91 --- /dev/null +++ b/Diya/SDL2Image.class.st @@ -0,0 +1,30 @@ +Class { + #name : #SDL2Image, + #superclass : #DiyaFFIBase, + #pools : [ + 'SDL2Constants', + 'SDL2ConstantsHint', + 'SDL2Types' + ], + #category : #'Diya-SDL2' +} + +{ #category : #'as yet unclassified' } +SDL2Image class >> SDLImgLoad: file [ + ^ self ffiCall: #(SDL_Surface * IMG_Load(const char *file)) +] + +{ #category : #accessing } +SDL2Image class >> checkSymbol [ + ^'IMG_Load' +] + +{ #category : #accessing } +SDL2Image class >> libNames [ + ^#( + 'libSDL2_image-2.0.so.0' + 'libSDL2_image.so' + 'libSDL2_image-2.0.so' + 'libSDL2_image-2.0.so.0.2.3' + ) +] diff --git a/Diya/SDL_DisplayMode.class.st b/Diya/SDL_DisplayMode.class.st index dc6349f..43d5d9b 100644 --- a/Diya/SDL_DisplayMode.class.st +++ b/Diya/SDL_DisplayMode.class.st @@ -30,6 +30,11 @@ SDL_DisplayMode >> asString [ ^ 'SDL display: ', self width asString, 'x', self height asString, ' - ', self depth asString, ' bits depth'. ] +{ #category : #'color mapping' } +SDL_DisplayMode >> colormapIfNeededFor: dest [ + ^ Color colorMapIfNeededFrom: self depth to: dest depth +] + { #category : #'accessing structure variables' } SDL_DisplayMode >> depth [ |format depth| @@ -51,6 +56,11 @@ SDL_DisplayMode >> driverdata: anObject [ handle pointerAt: OFFSET_DRIVERDATA put: anObject getHandle. ] +{ #category : #'accessing structure variables' } +SDL_DisplayMode >> extent [ + ^ self w @ self height +] + { #category : #other } SDL_DisplayMode >> forceDisplayUpdate [