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
]
{ #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
]

View File

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

View File

@ -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.

View File

@ -22,9 +22,11 @@ DiyaFFIBase class >> libNames [
{ #category : #accessing }
DiyaFFIBase class >> moduleName [
self libNames do:[:aName|
[
(ExternalAddress loadSymbol: self checkSymbol from: aName) ifNotNil: [
^ aName
].
]
] on: Error do: [ ]
].
DiyaCoreAPIError signal: 'Unable to find FFI library (', self checkSymbol, ')'.
]

View File

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

View File

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

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'.
]
{ #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 [