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

allow get texture from screen, VM bug on device

This commit is contained in:
Dany LE 2022-08-09 03:14:23 +02:00
parent 141bf2e226
commit 6f5c6b8551
8 changed files with 107 additions and 7 deletions

View File

@ -3,7 +3,8 @@ Class {
#superclass : #DiyaBaseObject,
#instVars : [
'enable',
'mapped'
'mapped',
'target'
],
#category : #'Diya-Events'
}
@ -38,3 +39,13 @@ DiyaEvent >> mapped: anObject [
DiyaEvent >> preventDefault [
enable := false
]
{ #category : #accessing }
DiyaEvent >> target [
^ target
]
{ #category : #accessing }
DiyaEvent >> target: anObject [
target := anObject
]

View File

@ -67,7 +67,7 @@ DiyaExampleApp >> main [
{ #category : #accessing }
DiyaExampleApp >> setup [
|node node1 ell label icon button|
|node node1 img ell label icon button|
"DiyaRendererContext uniqueInstance assets
addAsset:
@ -84,9 +84,19 @@ DiyaExampleApp >> setup [
node1 on: #(mousebuttondown fingerdown) do:[:e|
label txt: 'Mouse ', (node1 local: e mapped worldPosition) asIntegerPoint asString].
node := root addNode: (DiyaImageView from:'mrsang.png') at: 10 @ 400.
node styleName: #image_view.
node extent:200@200.
img := root addNode: (DiyaImageView from:'mrsang.png') at: 10 @ 400.
img styleName: #image_view.
img extent:200@200.
root on: #(mousebuttondown fingerdown) do:[:e|
"change texture"
|p|
p := e mapped worldPosition.
label txt: 'Mouse ', p asIntegerPoint asString.
DiyaRendererContext uniqueInstance assets
addAsset:(DiyaImageTex fromDisplay: (Rectangle origin: ((p x - 100) @ (p y - 100)) extent: 200@200 ) as: 'capture').
img textureNamed: 'capture'.
].
node := root addNode: (DiyaRectangle new) at: 10@80.
node styleName: #rect_view.

View File

@ -12,6 +12,16 @@ Class {
#category : #'Diya-Graphics'
}
{ #category : #'instance creation' }
DiyaImageTex class >> fromDisplay: aRect as: assetName [
^ self new fromDisplay: aRect as: assetName; yourself
]
{ #category : #'instance creation' }
DiyaImageTex class >> fromDisplayAs: assetName [
^ self new fromDisplayAs: assetName; yourself
]
{ #category : #'instance creation' }
DiyaImageTex class >> fromFile: path [
^ self new fromFile: path; yourself
@ -24,9 +34,59 @@ DiyaImageTex >> drop [
disable: GL_BLEND."
]
{ #category : #capture }
DiyaImageTex >> flipY [
|buffer idx idxf|
buffer := FFIExternalArray externalNewType: GLubyte size: width * height * 4.
LibC memset: buffer getHandle value: 0 size: buffer size.
buffer autoRelease.
0 to: height - 1 do:[:y|
0 to: width - 1 do:[:x|
idx := ((y * width) + x)*4 + 1.
idxf := ((height - y)*width + x)*4 + 1.
buffer at: idx put: (data at: idxf).
buffer at: idx+1 put: (data at: idxf+1).
buffer at: idx+2 put: (data at: idxf+2).
buffer at: idx+3 put: (data at: idxf+3).
].
].
data free.
data := buffer.
]
{ #category : #capture }
DiyaImageTex >> fromDisplay: aRect as: assetName [
surface ifNotNil: [ surface free ] ifNil: [data ifNotNil: [data free]].
surface := nil.
Transcript show:'allocate ', ((aRect extent x) * (aRect extent y) * 4) asInteger asString,' bytes';cr.
data := FFIExternalArray externalNewType: GLubyte size:((aRect extent x) * (aRect extent y) * 4) asInteger.
LibC memset: data getHandle value: 0 size: data size.
data autoRelease.
OpenGL readPixelsOn: data getHandle
x: aRect origin x
y: (DiyaDisplay height) - (aRect origin y) - (aRect extent y)
w: aRect extent x
h: aRect extent y
format:GL_RGBA
type: GL_UNSIGNED_BYTE.
width := aRect extent x.
height := aRect extent y.
name := assetName.
self flipY.
]
{ #category : #capture }
DiyaImageTex >> fromDisplayAs: assetName [
self fromDisplay: (Rectangle origin: 0@0 extent: DiyaDisplay extent ) as: assetName
]
{ #category : #'instance creation' }
DiyaImageTex >> fromFile: aPath [
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 ].

View File

@ -266,7 +266,9 @@ DiyaNode >> tf [
{ #category : #'event handling' }
DiyaNode >> trigger: evt [
evt enable ifFalse:[^self].
ehandlers at: evt mapped type ifPresent:[:handler| handler value: evt].
ehandlers at: evt mapped type ifPresent:[:handler|
evt target: self.
handler value: evt].
children ifNil: [^self].
evt enable ifTrue: [
"evt mapped triggableOn: children first."

View File

@ -62,6 +62,7 @@ ImageInitializer >> initializeImage [
FFIMethodRegistry resetAll.
Smalltalk garbageCollect.
SourceFiles := SourceFileArray new.
"Smalltalk vm parameterAt: 45 put: (Smalltalk vm parameterAt: 44) * 2."
Transcript show: 'Image initialized'; cr.
]

View File

@ -1,5 +1,16 @@
Extension { #name : #LibC }
{ #category : #'*Diya' }
LibC class >> alloc: size [
^self uniqueInstance alloc: size
]
{ #category : #'*Diya' }
LibC >> alloc:size [
^self ffiCall: #(void *malloc(size_t size))
]
{ #category : #'*Diya' }
LibC >> memset:pointer value: value size: size [
^self ffiCall: #(void *memset(void *pointer, int value, size_t size))

View File

@ -93,6 +93,11 @@ OpenGL class >> pixelstorei: pname param: param [
^self ffiCall: #(void glPixelStorei( GLenum pname,GLint param))
]
{ #category : #geometry }
OpenGL class >> readPixelsOn: buffer x:x y: y w:w h:h format: fmt type: type [
^ self ffiCall: #(void glReadPixels( GLint x,GLint y,GLsizei w,GLsizei h,GLenum fmt,GLenum type,void * buffer))
]
{ #category : #accessing }
OpenGL class >> vertex3fX:x Y:y Z:z [
^self ffiCall: #(void glVertex3f( GLfloat x,GLfloat y,GLfloat z))

View File

@ -59,7 +59,7 @@ OpenGLTexImage2D >> drop [
{ #category : #accessing }
OpenGLTexImage2D >> extent [
^ width @ height
^ self width @ self height
]
{ #category : #accessing }