1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-26 11:28:22 +01:00

use SDL2_image for texture loading

This commit is contained in:
Dany LE 2022-08-09 00:22:18 +02:00
parent 676113a697
commit 141bf2e226
8 changed files with 94 additions and 34 deletions

View File

@ -19,7 +19,17 @@ AssetManager class >> fromLocation: loc [
^self new from: loc; yourself ^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 [ AssetManager >> from: location [
base := location base := location
] ]
@ -31,12 +41,18 @@ AssetManager >> initialize [
base := self class defaultAssetLocation base := self class defaultAssetLocation
] ]
{ #category : #'as yet unclassified' } { #category : #accessing }
AssetManager >> name: fileName of: type [ AssetManager >> name: name [
^assets at: fileName ifAbsentPut: [(type fromFile: (base / fileName))]. ^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 [ AssetManager >> texture: name [
^self name: name of: DiyaImageTex ^self name: name of: DiyaImageTex
] ]

View File

@ -90,6 +90,8 @@ DiyaBoot >> init [
SDL2 showCursor: 0. SDL2 showCursor: 0.
DiyaSingleton resetAll. DiyaSingleton resetAll.
DiyaFontManager uniqueInstance loadFonts. DiyaFontManager uniqueInstance loadFonts.
Form class removeSelector: #serviceImageAsBackground
] ]
{ #category : #events } { #category : #events }

View File

@ -68,13 +68,16 @@ DiyaExampleApp >> main [
{ #category : #accessing } { #category : #accessing }
DiyaExampleApp >> setup [ DiyaExampleApp >> setup [
|node node1 ell label icon button| |node node1 ell label icon button|
"DiyaRendererContext uniqueInstance assets
addAsset:
((Form fromDisplay: ( Rectangle origin: 0@0 corner: 300@300 )) asDiyaTexture: 'display')."
self defineStyleSheet. self defineStyleSheet.
label := root addNode: (DiyaLabel new) at: 10@40. label := root addNode: (DiyaLabel new) at: 10@40.
label extent: 250@24. label extent: 250@24.
label styleName:#text_icon_1. label styleName:#text_icon_1.
label icon: 16rF254. label icon: 16rF254.
node1 := root addNode: (DiyaRectangle size:100@150 shader: DiyaExampleShader uniqueInstance) at: 300 @ 40. node1 := root addNode: (DiyaRectangle size:100@150 shader: DiyaExampleShader uniqueInstance) at: 300 @ 40.
"node1 rotation: (Float pi / 8.0)." "node1 rotation: (Float pi / 8.0)."
node1 scale: 1.2@1.2. node1 scale: 1.2@1.2.

View File

@ -22,9 +22,11 @@ DiyaFFIBase class >> libNames [
{ #category : #accessing } { #category : #accessing }
DiyaFFIBase class >> moduleName [ DiyaFFIBase class >> moduleName [
self libNames do:[:aName| 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, ')'. DiyaCoreAPIError signal: 'Unable to find FFI library (', self checkSymbol, ')'.
] ]

View File

@ -2,7 +2,8 @@ Class {
#name : #DiyaImageTex, #name : #DiyaImageTex,
#superclass : #OpenGLTexImage2D, #superclass : #OpenGLTexImage2D,
#instVars : [ #instVars : [
'path' 'name',
'surface'
], ],
#pools : [ #pools : [
'OpenGLConstants', 'OpenGLConstants',
@ -25,28 +26,15 @@ DiyaImageTex >> drop [
{ #category : #'instance creation' } { #category : #'instance creation' }
DiyaImageTex >> fromFile: aPath [ DiyaImageTex >> fromFile: aPath [
|form color index| Transcript show: 'Loading texture from ', aPath fullName;cr.
path := aPath. aPath exists ifFalse: [ ^DiyaCoreAPIError signal:'File not found ', aPath fullName ].
Transcript show: 'Loading texture from ', path fullName;cr. surface := SDL2Image SDLImgLoad: aPath fullName.
form := ImageReadWriter formFromFileNamed: path. surface ifNil: [ ^DiyaCoreAPIError signal:'Unable to load ', aPath fullName ].
data := FFIExternalArray externalNewType: GLubyte size:(form width) * (form height) * 4. width := surface w.
LibC memset: data getHandle value: 0 size: data size. height := surface h.
data autoRelease. data := surface pixels.
width := form width. surface autoRelease.
height := form height. Transcript show: 'Loaded ', aPath fullName;cr.
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.
]
].
] ]
{ #category : #'instance creation' } { #category : #'instance creation' }
@ -66,8 +54,8 @@ DiyaImageTex >> mipmap [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaImageTex >> path [ DiyaImageTex >> name [
^ path ^ name
] ]
{ #category : #accessing } { #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_MIN_FILTER param: GL_LINEAR;
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 }
DiyaImageTex >> surface [
^ surface
]

View File

@ -11,6 +11,8 @@ Class {
'GL_ARRAY_BUFFER', 'GL_ARRAY_BUFFER',
'GL_ARRAY_BUFFER_BINDING', 'GL_ARRAY_BUFFER_BINDING',
'GL_ATTACHED_SHADERS', 'GL_ATTACHED_SHADERS',
'GL_BGR',
'GL_BGRA',
'GL_BLEND', 'GL_BLEND',
'GL_BLUE', 'GL_BLUE',
'GL_BYTE', 'GL_BYTE',
@ -115,6 +117,8 @@ OpenGLConstants class >> initCommonConstants [
GL_ALPHA := 16r1906. GL_ALPHA := 16r1906.
GL_RGB := 16r1907. GL_RGB := 16r1907.
GL_RGBA := 16r1908. GL_RGBA := 16r1908.
GL_BGR := 16r80E0.
GL_BGRA := 16r80E1.
GL_LINE_SMOOTH := 16r0B20. GL_LINE_SMOOTH := 16r0B20.
GL_LINE_SMOOTH_HINT := 16r0C52 GL_LINE_SMOOTH_HINT := 16r0C52
] ]

30
Diya/SDL2Image.class.st Normal file
View File

@ -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'
)
]

View File

@ -30,6 +30,11 @@ SDL_DisplayMode >> asString [
^ 'SDL display: ', self width asString, 'x', self height asString, ' - ', self depth asString, ' bits depth'. ^ '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' } { #category : #'accessing structure variables' }
SDL_DisplayMode >> depth [ SDL_DisplayMode >> depth [
|format depth| |format depth|
@ -51,6 +56,11 @@ SDL_DisplayMode >> driverdata: anObject [
handle pointerAt: OFFSET_DRIVERDATA put: anObject getHandle. handle pointerAt: OFFSET_DRIVERDATA put: anObject getHandle.
] ]
{ #category : #'accessing structure variables' }
SDL_DisplayMode >> extent [
^ self w @ self height
]
{ #category : #other } { #category : #other }
SDL_DisplayMode >> forceDisplayUpdate [ SDL_DisplayMode >> forceDisplayUpdate [