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

API improvement

- Refactor code
- Texture performance improvement
This commit is contained in:
Dany LE 2022-08-10 19:55:49 +02:00
parent 6f5c6b8551
commit 9442050825
19 changed files with 153 additions and 112 deletions

View File

@ -0,0 +1,6 @@
Extension { #name : #DiskStore }
{ #category : #'*Diya' }
DiskStore class >> inform: string [
Transcript show: string; cr.
]

View File

@ -34,7 +34,7 @@ DiyaApplicationLauncher >> defaultApplication [
^DiyaExampleApp ^DiyaExampleApp
] ]
{ #category : #'as yet unclassified' } { #category : #accessing }
DiyaApplicationLauncher >> delta: delta [ DiyaApplicationLauncher >> delta: delta [
|fps| |fps|
delta = 0 ifTrue:[^self]. delta = 0 ifTrue:[^self].

View File

@ -84,14 +84,12 @@ DiyaBoot >> init [
status = 0 status = 0
ifFalse: [ ^ DiyaCoreAPIError signal: SDL2 getErrorMessage ]. ifFalse: [ ^ DiyaCoreAPIError signal: SDL2 getErrorMessage ].
display := SDL_DisplayMode externalNew autoRelease. display := SDL_DisplayMode externalNew autoRelease.
Smalltalk globals at: #Display ifAbsentPut:display.
Smalltalk globals at: #DiyaDisplay put:display.
SDL2 SDLGetCurrentDisplayMode: display from:0. SDL2 SDLGetCurrentDisplayMode: display from:0.
SDL2 showCursor: 0. SDL2 showCursor: 0.
Smalltalk globals at: #Display ifAbsentPut:display.
Smalltalk globals at: #DiyaDisplay put:display.
DiyaSingleton resetAll. DiyaSingleton resetAll.
DiyaFontManager uniqueInstance loadFonts. DiyaFontManager uniqueInstance loadFonts.
Form class removeSelector: #serviceImageAsBackground
] ]
{ #category : #events } { #category : #events }
@ -191,6 +189,7 @@ DiyaBoot >> showSystemInfo [
]. ].
stream cr. stream cr.
stream nextPutAll: DiyaDisplay asString; cr. stream nextPutAll: DiyaDisplay asString; cr.
stream cr.
self stdout nextPutAll: stream contents self stdout nextPutAll: stream contents
] ]
@ -203,8 +202,10 @@ DiyaBoot >> startx [
"SDL2 glMakeCurrent: window context: context." "SDL2 glMakeCurrent: window context: context."
self showSystemInfo. self showSystemInfo.
DiyaRendererContext DiyaRendererContext
uniqueInstance display: display; uniqueInstance
useProjection: OrthoProjectionMatrix. display: display;
window: window;
useProjection: OrthoProjectionMatrix.
self render. self render.
context delete. context delete.
window destroy. window destroy.

View File

@ -39,14 +39,14 @@ DiyaButton >> label [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaButton >> text: string [ DiyaButton >> process [
label txt: string.
]
{ #category : #accessing }
DiyaButton >> update [
rec extent: self extent. rec extent: self extent.
label position: 0@0. label position: 0@0.
label extent: self extent. label extent: self extent.
^true ^true
] ]
{ #category : #accessing }
DiyaButton >> text: string [
label txt: string.
]

View File

@ -49,6 +49,21 @@ DiyaEllipse >> inner: aPoint [
^ ((((dxy x) ** 2)/(rx**2)) + (((dxy y) ** 2) / (ry**2))) < 1. ^ ((((dxy x) ** 2)/(rx**2)) + (((dxy y) ** 2) / (ry**2))) < 1.
] ]
{ #category : #accessing }
DiyaEllipse >> process [
bbox := Rectangle origin: ((rx negated) @ (ry negated)) corner: (rx @ ry).
{
bbox origin x. bbox origin y. 0.0. 0.0.
bbox origin x. bbox corner y. 0.0. 1.0.
bbox corner x. bbox corner y. 1.0. 1.0.
bbox corner x. bbox corner y. 1.0. 1.0.
bbox corner x. bbox origin y. 1.0. 0.0.
bbox origin x. bbox origin y. 0.0. 0.0.
} doWithIndex: [:e :i| vbuffer at: i put: e].
^true
]
{ #category : #accessing } { #category : #accessing }
DiyaEllipse >> rx [ DiyaEllipse >> rx [
^ rx ^ rx
@ -80,18 +95,3 @@ DiyaEllipse >> setUpShader [
setUniform: #u_rx value: rx; setUniform: #u_rx value: rx;
setUniform: #u_ry value: ry. setUniform: #u_ry value: ry.
] ]
{ #category : #accessing }
DiyaEllipse >> update [
bbox := Rectangle origin: ((rx negated) @ (ry negated)) corner: (rx @ ry).
{
bbox origin x. bbox origin y. 0.0. 0.0.
bbox origin x. bbox corner y. 0.0. 1.0.
bbox corner x. bbox corner y. 1.0. 1.0.
bbox corner x. bbox corner y. 1.0. 1.0.
bbox corner x. bbox origin y. 1.0. 0.0.
bbox origin x. bbox origin y. 0.0. 0.0.
} doWithIndex: [:e :i| vbuffer at: i put: e].
^true
]

View File

@ -130,5 +130,6 @@ DiyaExampleApp >> setup [
button icon:16rF185"'mrsang.png'". button icon:16rF185"'mrsang.png'".
"button rotation: Float pi / 2.0." "button rotation: Float pi / 2.0."
button styleName: #button_view. button styleName: #button_view.
Transcript show: 'Application setup';cr.
^ root ^ root
] ]

View File

@ -59,8 +59,8 @@ DiyaFontIcon >> lastSeparatorFrom: i [
] ]
{ #category : #initialization } { #category : #initialization }
DiyaFontIcon >> update [ DiyaFontIcon >> process [
data ifNil: [ ^self ]. data ifNil: [ ^self ].
bbox := Rectangle origin: 0@0 corner: ((data size) * (self fontSize) ) @ self fontSize. bbox := Rectangle origin: 0@0 corner: ((data size) * (self fontSize) ) @ self fontSize.
^ super update. ^ super process.
] ]

View File

@ -20,9 +20,9 @@ DiyaImageIcon >> initialize [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaImageIcon >> update [ DiyaImageIcon >> process [
|v| |v|
v := self iconSize. v := self iconSize.
self extent: (v@v). self extent: (v@v).
^super update ^super process
] ]

View File

@ -36,32 +36,28 @@ DiyaImageTex >> drop [
{ #category : #capture } { #category : #capture }
DiyaImageTex >> flipY [ DiyaImageTex >> flipY [
|buffer idx idxf| |buffer size linesize top bottom|
buffer := FFIExternalArray externalNewType: GLubyte size: width * height * 4. size := self bytesSize.
linesize := width << 2.
buffer := FFIExternalArray externalNewType: GLubyte size: linesize.
LibC memset: buffer getHandle value: 0 size: buffer size. LibC memset: buffer getHandle value: 0 size: buffer size.
buffer autoRelease. 0 to: (height >> 1) -1 do: [ :line|
0 to: height - 1 do:[:y| top := line * linesize.
0 to: width - 1 do:[:x| bottom := (size - (linesize * (line + 1))).
idx := ((y * width) + x)*4 + 1. LibC memCopy: (data getHandle) + top to: buffer getHandle size: linesize.
idxf := ((height - y)*width + x)*4 + 1. LibC memCopy: (data getHandle) + bottom to: (data getHandle) + top size: linesize.
buffer at: idx put: (data at: idxf). LibC memCopy: buffer getHandle to: (data getHandle) + bottom size: linesize.
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. buffer free
data := buffer.
] ]
{ #category : #capture } { #category : #capture }
DiyaImageTex >> fromDisplay: aRect as: assetName [ DiyaImageTex >> fromDisplay: aRect as: assetName [
surface ifNotNil: [ surface free ] ifNil: [data ifNotNil: [data free]]. surface ifNotNil: [ surface free ] ifNil: [data ifNotNil: [data free]].
surface := nil. surface := nil.
Transcript show:'allocate ', ((aRect extent x) * (aRect extent y) * 4) asInteger asString,' bytes';cr. width := aRect extent x asInteger.
data := FFIExternalArray externalNewType: GLubyte size:((aRect extent x) * (aRect extent y) * 4) asInteger. height := aRect extent y asInteger.
data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
LibC memset: data getHandle value: 0 size: data size. LibC memset: data getHandle value: 0 size: data size.
data autoRelease. data autoRelease.
OpenGL readPixelsOn: data getHandle OpenGL readPixelsOn: data getHandle
@ -71,11 +67,8 @@ DiyaImageTex >> fromDisplay: aRect as: assetName [
h: aRect extent y h: aRect extent y
format:GL_RGBA format:GL_RGBA
type: GL_UNSIGNED_BYTE. type: GL_UNSIGNED_BYTE.
width := aRect extent x.
height := aRect extent y.
name := assetName. name := assetName.
self flipY. self flipY.
] ]
{ #category : #capture } { #category : #capture }

View File

@ -52,18 +52,7 @@ DiyaLabel >> initialize [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaLabel >> txt [ DiyaLabel >> process [
^ txt
]
{ #category : #accessing }
DiyaLabel >> txt: anObject [
txt data: anObject.
self setDirty
]
{ #category : #accessing }
DiyaLabel >> update [
|offset isize align| |offset isize align|
offset := 0. offset := 0.
icon ifNotNil: [ icon ifNotNil: [
@ -73,7 +62,7 @@ DiyaLabel >> update [
]. ].
txt extent: (extent x - offset) @ (extent y). txt extent: (extent x - offset) @ (extent y).
"lookahead update" "lookahead update"
txt update. txt process.
align := self getHAlign: offset. align := self getHAlign: offset.
txt position: offset @ ( (self extent y - txt extent y ) >> 1). txt position: offset @ ( (self extent y - txt extent y ) >> 1).
@ -83,3 +72,14 @@ DiyaLabel >> update [
^ true ^ true
] ]
{ #category : #accessing }
DiyaLabel >> txt [
^ txt
]
{ #category : #accessing }
DiyaLabel >> txt: anObject [
txt data: anObject.
self setDirty
]

View File

@ -82,18 +82,7 @@ DiyaLine >> inner: aPoint [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaLine >> to [ DiyaLine >> process [
^ to
]
{ #category : #accessing }
DiyaLine >> to: anObject [
to := anObject.
self setDirty
]
{ #category : #accessing }
DiyaLine >> update [
|extent| |extent|
bbox := (Rectangle origin: from corner: to ). bbox := (Rectangle origin: from corner: to ).
bbox origin = translation ifFalse:[self position: bbox origin]. bbox origin = translation ifFalse:[self position: bbox origin].
@ -108,3 +97,14 @@ DiyaLine >> update [
} doWithIndex: [:e :i| vbuffer at: i put: e]. } doWithIndex: [:e :i| vbuffer at: i put: e].
^true ^true
] ]
{ #category : #accessing }
DiyaLine >> to [
^ to
]
{ #category : #accessing }
DiyaLine >> to: anObject [
to := anObject.
self setDirty
]

View File

@ -57,7 +57,21 @@ DiyaNode >> addNode: node at: pos [
node position: pos. node position: pos.
children add: node. children add: node.
node root: self root. node root: self root.
^ node Transcript show:'Added node #', self id asString; cr.
^ node
]
{ #category : #accessing }
DiyaNode >> allChildren [
|nodes|
nodes := OrderedCollection new.
children ifNil: [ ^ nodes ].
children do:[:c|
nodes add: c.
c allChildren do:[:e| nodes add:e].
].
^nodes
] ]
{ #category : #accessing } { #category : #accessing }
@ -148,6 +162,11 @@ DiyaNode >> position: anObject [
self updateTF. self updateTF.
] ]
{ #category : #accessing }
DiyaNode >> process [
^self subclassResponsibility
]
{ #category : #convenience } { #category : #convenience }
DiyaNode >> register: aBlock to: eventName [ DiyaNode >> register: aBlock to: eventName [
|evtCode| |evtCode|
@ -159,12 +178,13 @@ DiyaNode >> register: aBlock to: eventName [
{ #category : #accessing } { #category : #accessing }
DiyaNode >> render [ DiyaNode >> render [
self step.
dirty ifTrue:[ dirty ifTrue:[
dirty := self update not]. dirty := self process not].
shader ifNotNil: [self setUpShader]. shader ifNotNil: [self setUpShader].
self draw. self draw.
children ifNil: [ ^self ]. children ifNil: [ ^self ].
children do: [:c | c render ]. children do: [:c | c render ].
] ]
{ #category : #accessing } { #category : #accessing }
@ -241,6 +261,10 @@ DiyaNode >> shader: anObject [
shader := anObject shader := anObject
] ]
{ #category : #stepping }
DiyaNode >> step [
]
{ #category : #accessing } { #category : #accessing }
DiyaNode >> styleName [ DiyaNode >> styleName [
^ styleName ^ styleName
@ -276,11 +300,6 @@ DiyaNode >> trigger: evt [
]. ].
] ]
{ #category : #accessing }
DiyaNode >> update [
^self subclassResponsibility
]
{ #category : #accessing } { #category : #accessing }
DiyaNode >> updateTF [ DiyaNode >> updateTF [
self subclassResponsibility self subclassResponsibility

View File

@ -40,6 +40,16 @@ DiyaPolygon >> points: anObject [
self setDirty self setDirty
] ]
{ #category : #accessing }
DiyaPolygon >> process [
bbox := self recFromPoints.
translation = bbox origin ifFalse:[ self position: bbox origin].
points := points collect:[:e | e - bbox origin].
bbox := self recFromPoints.
self calculateVertices.
^true
]
{ #category : #accessing } { #category : #accessing }
DiyaPolygon >> recFromPoints [ DiyaPolygon >> recFromPoints [
|maxX maxY minX minY x y| |maxX maxY minX minY x y|
@ -55,13 +65,3 @@ DiyaPolygon >> recFromPoints [
]. ].
^ Rectangle origin: minX@minY corner: maxX @ maxY ^ Rectangle origin: minX@minY corner: maxX @ maxY
] ]
{ #category : #accessing }
DiyaPolygon >> update [
bbox := self recFromPoints.
translation = bbox origin ifFalse:[ self position: bbox origin].
points := points collect:[:e | e - bbox origin].
bbox := self recFromPoints.
self calculateVertices.
^true
]

View File

@ -37,7 +37,7 @@ DiyaRectangle >> initialize [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaRectangle >> update [ DiyaRectangle >> process [
|extent| |extent|
extent := self extent. extent := self extent.
{ {

View File

@ -8,7 +8,8 @@ Class {
'vao', 'vao',
'texture0', 'texture0',
'projection', 'projection',
'assets' 'assets',
'window'
], ],
#pools : [ #pools : [
'OpenGLConstants', 'OpenGLConstants',
@ -106,3 +107,13 @@ DiyaRendererContext >> vao [
DiyaRendererContext >> vbo [ DiyaRendererContext >> vbo [
^ vbo ^ vbo
] ]
{ #category : #accessing }
DiyaRendererContext >> window [
^ window
]
{ #category : #accessing }
DiyaRendererContext >> window: anObject [
window := anObject
]

View File

@ -43,7 +43,7 @@ DiyaRootNode >> isRoot [
] ]
{ #category : #initialization } { #category : #initialization }
DiyaRootNode >> update [ DiyaRootNode >> process [
^true ^true
] ]

View File

@ -200,6 +200,18 @@ DiyaText >> maxLineWidth [
^ maxLineWidth ^ maxLineWidth
] ]
{ #category : #initialization }
DiyaText >> process [
bbox ifNil: [ ^true ].
data ifNil:[^true].
data ifEmpty:[^true].
vbuffer ifNotNil: [vbuffer free].
vbuffer := FFIExternalArray externalNewType: GLfloat size: data size * 16.
vbuffer autoRelease.
self drawText.
^true
]
{ #category : #accessing } { #category : #accessing }
DiyaText >> splitLines [ DiyaText >> splitLines [
|line ret tex2D| |line ret tex2D|
@ -219,24 +231,12 @@ DiyaText >> texture [
texture ifNil: [ self initTexture ]. texture ifNil: [ self initTexture ].
texheight = texture height ifFalse: [ texheight = texture height ifFalse: [
texheight := texture height. texheight := texture height.
self update. self process.
self setClean. self setClean.
]. ].
^texture ^texture
] ]
{ #category : #initialization }
DiyaText >> update [
bbox ifNil: [ ^true ].
data ifNil:[^true].
data ifEmpty:[^true].
vbuffer ifNotNil: [vbuffer free].
vbuffer := FFIExternalArray externalNewType: GLfloat size: data size * 16.
vbuffer autoRelease.
self drawText.
^true
]
{ #category : #'menu messages' } { #category : #'menu messages' }
DiyaText >> valign [ DiyaText >> valign [
^ self ? #yAlign ^ self ? #yAlign

View File

@ -29,6 +29,11 @@ OpenGLTexImage2D >> border: anObject [
border := anObject border := anObject
] ]
{ #category : #accessing }
OpenGLTexImage2D >> bytesSize [
^(width * height) << 2
]
{ #category : #accessing } { #category : #accessing }
OpenGLTexImage2D >> data [ OpenGLTexImage2D >> data [
^ data ^ data

View File

@ -6,6 +6,11 @@ SDL2 class >> SDLAllocFormat: pixel_format [
] ]
{ #category : #'*Diya' }
SDL2 class >> SDLBlitSurface: src srcRect: srcrect dest: dst dstRect: dstrect [
^ self ffiCall: #(int SDL_UpperBlit(SDL_Surface* src,SDL_Rect* srcrect,SDL_Surface* dst,SDL_Rect* dstrect))
]
{ #category : #'*Diya' } { #category : #'*Diya' }
SDL2 class >> SDLClearError [ SDL2 class >> SDLClearError [
^ self ffiCall: #(void SDL_ClearError(void)) ^ self ffiCall: #(void SDL_ClearError(void))