1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-27 03:48:21 +01:00

Supporting read system settings from file

This commit is contained in:
Dany LE 2022-08-14 18:13:23 +02:00
parent 2d09a8501a
commit 74ce2b0f37
13 changed files with 140 additions and 51 deletions

View File

@ -8,12 +8,6 @@ Class {
#category : #'Diya-Core' #category : #'Diya-Core'
} }
{ #category : #'instance creation' }
AssetManager class >> defaultAssetLocation [
^Smalltalk imageDirectory / 'assets'
]
{ #category : #'instance creation' } { #category : #'instance creation' }
AssetManager class >> fromLocation: loc [ AssetManager class >> fromLocation: loc [
^self new from: loc; yourself ^self new from: loc; yourself
@ -40,7 +34,7 @@ AssetManager >> from: location [
AssetManager >> initialize [ AssetManager >> initialize [
super initialize. super initialize.
assets := Dictionary new. assets := Dictionary new.
base := self class defaultAssetLocation base := DiyaSystemSettings assetPath
] ]
{ #category : #accessing } { #category : #accessing }

View File

@ -24,7 +24,7 @@ DiyaApplicationLauncher >> bindGlobalEvent [
root on: #quit do: [:e| running := false]. root on: #quit do: [:e| running := false].
root on: #(fingerdown fingermotion mousemotion) do:[:e| root on: #(fingerdown fingermotion mousemotion) do:[:e|
pointer position: e mapped worldPosition. pointer position: e mapped worldPosition.
DiyaRendererContext uniqueInstance mouse: (e mapped x) @ (e mapped y). DiyaRenderer mouse: (e mapped x) @ (e mapped y).
]. ].
] ]
@ -36,7 +36,7 @@ DiyaApplicationLauncher >> defaultApplication [
{ #category : #initialization } { #category : #initialization }
DiyaApplicationLauncher >> initialize [ DiyaApplicationLauncher >> initialize [
super initialize. super initialize.
root := DiyaRendererContext uniqueInstance root. root := DiyaRenderer root.
currapp := nil. currapp := nil.
] ]
@ -62,8 +62,8 @@ DiyaApplicationLauncher >> launch: app [
{ #category : #initialization } { #category : #initialization }
DiyaApplicationLauncher >> main [ DiyaApplicationLauncher >> main [
| fps delta| | fps delta|
delta := DiyaClock uniqueInstance delta asMilliSeconds. delta := DiyaSystemClock delta asMilliSeconds.
fps := DiyaBoot maxFPS. fps := DiyaSystemSettings maxFPS.
delta = 0 ifFalse:[ fps := (1000/ delta) asInteger]. delta = 0 ifFalse:[ fps := (1000/ delta) asInteger].
txtFPS data: ('FPS:', fps asString). txtFPS data: ('FPS:', fps asString).
[(SDL2 pollEvent: event) > 0] whileTrue: [ [(SDL2 pollEvent: event) > 0] whileTrue: [

View File

@ -20,7 +20,7 @@ DiyaApplicationModel >> cleanup [
{ #category : #accessing } { #category : #accessing }
DiyaApplicationModel >> context [ DiyaApplicationModel >> context [
^DiyaRendererContext uniqueInstance ^DiyaRenderer
] ]
{ #category : #initialization } { #category : #initialization }

View File

@ -21,11 +21,6 @@ DiyaBoot class >> initialize [
Smalltalk globals at: #DiyaDisplay put: nil Smalltalk globals at: #DiyaDisplay put: nil
] ]
{ #category : #'instance creation' }
DiyaBoot class >> maxFPS [
^60
]
{ #category : #'instance creation' } { #category : #'instance creation' }
DiyaBoot class >> startUp: status [ DiyaBoot class >> startUp: status [
self startx. self startx.
@ -80,9 +75,11 @@ DiyaBoot >> init [
display := SDL_DisplayMode externalNew autoRelease. display := SDL_DisplayMode externalNew autoRelease.
SDL2 SDLGetCurrentDisplayMode: display from:0. SDL2 SDLGetCurrentDisplayMode: display from:0.
SDL2 showCursor: 0. SDL2 showCursor: 0.
DiyaSingleton resetAll.
Smalltalk globals at: #Display ifAbsentPut:display. Smalltalk globals at: #Display ifAbsentPut:display.
Smalltalk globals at: #DiyaDisplay put:display. Smalltalk globals at: #DiyaDisplay put:display.
DiyaSingleton resetAll. Smalltalk globals at: #DiyaSystemSettings put: DiyaSettings uniqueInstance.
Smalltalk globals at: #DiyaSystemClock put: DiyaClock uniqueInstance.
DiyaFontManager uniqueInstance loadFonts. DiyaFontManager uniqueInstance loadFonts.
] ]
@ -91,7 +88,7 @@ DiyaBoot >> initialize [
display := nil. display := nil.
window := nil. window := nil.
context := nil. context := nil.
clock := DiyaClock uniqueInstance. clock := DiyaSystemClock.
] ]
{ #category : #events } { #category : #events }
@ -103,12 +100,12 @@ DiyaBoot >> render [
OpenGL viewportX: 0 Y:0 W: display w H: display h. OpenGL viewportX: 0 Y:0 W: display w H: display h.
OpenGL enable: GL_TEXTURE_2D. OpenGL enable: GL_TEXTURE_2D.
[ launcher running ] whileTrue: [ [ launcher running ] whileTrue: [
DiyaClock uniqueInstance tick. DiyaSystemClock tick.
launcher main. launcher main.
DiyaRendererContext uniqueInstance render. DiyaRenderer render.
SDL2 delay: SDL2 delay:
(0 max: (0 max:
(1000/ self class maxFPS) asInteger - (DiyaClock uniqueInstance lapDelta asMilliSeconds)). (1000/ DiyaSystemSettings maxFPS) asInteger - (DiyaSystemClock lapDelta asMilliSeconds)).
]. ].
] ]
@ -200,6 +197,7 @@ DiyaBoot >> startx [
display: display; display: display;
window: window; window: window;
useProjection: OrthoProjectionMatrix. useProjection: OrthoProjectionMatrix.
Smalltalk globals at: #DiyaRenderer put: DiyaRendererContext uniqueInstance.
self render. self render.
context delete. context delete.
window destroy. window destroy.

View File

@ -99,7 +99,7 @@ DiyaExampleApp >> setup [
|p| |p|
p := e mapped worldPosition. p := e mapped worldPosition.
label txt: 'Mouse ', p asIntegerPoint asString. label txt: 'Mouse ', p asIntegerPoint asString.
DiyaRendererContext uniqueInstance assets DiyaRenderer assets
addAsset:(texture fromDisplay: (Rectangle origin: ((p x - 100) @ (p y - 100)) extent: 200@200 ) as: 'capture'). addAsset:(texture fromDisplay: (Rectangle origin: ((p x - 100) @ (p y - 100)) extent: 200@200 ) as: 'capture').
img textureNamed: 'capture'. img textureNamed: 'capture'.

View File

@ -13,13 +13,6 @@ DiyaFontManager class >> cleanUpInstance: singleton [
singleton reset. singleton reset.
] ]
{ #category : #'as yet unclassified' }
DiyaFontManager class >> searchPaths [
^ {
Smalltalk imageDirectory / 'fonts'
}
]
{ #category : #initialization } { #category : #initialization }
DiyaFontManager >> defaultFamily [ DiyaFontManager >> defaultFamily [
^'Ubuntu' ^'Ubuntu'
@ -90,7 +83,9 @@ DiyaFontManager >> loadFonts [
{ #category : #initialization } { #category : #initialization }
DiyaFontManager >> loadFonts: force [ DiyaFontManager >> loadFonts: force [
force ifTrue:[self reset]. force ifTrue:[self reset].
self class searchPaths do:[:path| self loadPath: path]. DiyaSystemSettings fontPaths
select:[:p| p asFileReference exists]
thenDo:[:path| self loadPath: path].
] ]

View File

@ -107,7 +107,7 @@ DiyaNode >> initialize [
super initialize. super initialize.
parent := nil. parent := nil.
shader := nil. shader := nil.
context := DiyaRendererContext uniqueInstance. context := DiyaRenderer.
children := OrderedCollection new. children := OrderedCollection new.
ehandlers := Dictionary new. ehandlers := Dictionary new.
styleName := nil. styleName := nil.
@ -264,7 +264,7 @@ DiyaNode >> setUpShader [
|mem| |mem|
mem := self tf asGLBuffer. mem := self tf asGLBuffer.
shader use; shader use;
setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat; setUniform: #u_time value: DiyaSystemClock elapsedTime asFloat;
setUniform: #u_projection value: {GL_FALSE. context projection buffer}; setUniform: #u_projection value: {GL_FALSE. context projection buffer};
setUniform: #u_resolution value: { context resolution x. context resolution y }; setUniform: #u_resolution value: { context resolution x. context resolution y };
setUniform: #u_transform value: {GL_TRUE. mem}. setUniform: #u_transform value: {GL_TRUE. mem}.

View File

@ -30,6 +30,11 @@ DiyaRendererContext class >> maxFloatBufferSize [
^4096 ^4096
] ]
{ #category : #accessing }
DiyaRendererContext class >> maxTextureNumber [
^ 32
]
{ #category : #accessing } { #category : #accessing }
DiyaRendererContext >> assets [ DiyaRendererContext >> assets [
^ assets ^ assets
@ -65,7 +70,7 @@ DiyaRendererContext >> findTextureUnit [
e value ifNil: [ ^ i - 1] e value ifNil: [ ^ i - 1]
]. ].
"random unit value" "random unit value"
^ (Random new nextInt: 32) - 1 ^ (Random new nextInt: self class maxTextureNumber) - 1
] ]
{ #category : #accessing } { #category : #accessing }
@ -87,7 +92,7 @@ DiyaRendererContext >> initialize [
stride: 16 stride: 16
pointer: nil. pointer: nil.
textures := textures :=
(1 to: 32) collect:[:i| (1 to: self class maxTextureNumber ) collect:[:i|
(OpenGLTexture fromUnit: i - 1) -> nil] . (OpenGLTexture fromUnit: i - 1) -> nil] .
] ]

View File

@ -69,24 +69,15 @@ DiyaRootNode >> readyForSwap [
{ #category : #rendering } { #category : #rendering }
DiyaRootNode >> render [ DiyaRootNode >> render [
|node maxProcessingTime| | maxProcessingTime|
self stepDown. self stepDown.
R ifEmpty: [ R ifEmpty: [
self draw. self draw.
self renderNext: children self renderNext: children
]. ].
maxProcessingTime := (1000 / DiyaBoot maxFPS) asInteger. maxProcessingTime := (1000 / DiyaSystemSettings maxFPS) asInteger.
[ [
Q ifNotEmpty: [ self spinOnce: maxProcessingTime
node := Q removeFirst.
node process.
].
R ifNotEmpty: [
node := R removeFirst.
node render.
].
(R isNotEmpty or: Q isNotEmpty) and:
(DiyaClock uniqueInstance lapDelta asMilliSeconds < maxProcessingTime)
] whileTrue ] whileTrue
] ]
@ -108,6 +99,22 @@ DiyaRootNode >> setDirty [
] ]
{ #category : #rendering }
DiyaRootNode >> spinOnce: maxProcessingTime [
|node|
Q ifNotEmpty: [
node := Q removeFirst.
node process.
].
R ifNotEmpty: [
node := R removeFirst.
node render.
].
(Q isEmpty and: R isEmpty) ifTrue: [ ^false ].
DiyaSystemSettings renderAtOnce ifTrue: [ ^ true ].
^(DiyaSystemClock lapDelta asMilliSeconds < maxProcessingTime)
]
{ #category : #accessing } { #category : #accessing }
DiyaRootNode >> updateTF [ DiyaRootNode >> updateTF [
"donothing" "donothing"

View File

@ -0,0 +1,90 @@
Class {
#name : #DiyaSettings,
#superclass : #DiyaSingleton,
#instVars : [
'maxFPS',
'fontPaths',
'assetPath',
'renderAtOnce'
],
#category : #'Diya-Core'
}
{ #category : #'as yet unclassified' }
DiyaSettings class >> defaultSettingFile [
^ Smalltalk imageDirectory / 'settings.json'
]
{ #category : #accessing }
DiyaSettings >> assetPath [
^ assetPath
]
{ #category : #accessing }
DiyaSettings >> assetPath: anObject [
assetPath := anObject
]
{ #category : #accessing }
DiyaSettings >> fontPaths [
^ fontPaths
]
{ #category : #accessing }
DiyaSettings >> fontPaths: anObject [
fontPaths := anObject
]
{ #category : #initialization }
DiyaSettings >> initialize [
super initialize.
maxFPS := 60.
renderAtOnce := false.
fontPaths := { Smalltalk imageDirectory / 'fonts' }.
assetPath := Smalltalk imageDirectory / 'assets'.
self loadFromFile.
self logSettings
]
{ #category : #initialization }
DiyaSettings >> loadFromFile [
|d v|
self class defaultSettingFile exists ifFalse: [
self stderror: 'Setting file not found. Using default settings'.
^self
].
d := STON fromStream: self class defaultSettingFile readStream.
maxFPS := d at:'max_fps' ifAbsent:[maxFPS].
v := d at:'asset_dir' ifAbsent:[nil].
v ifNotNil: [ assetPath := v ].
renderAtOnce := d at: 'render_all_at_once' ifAbsent:[renderAtOnce ].
fontPaths := fontPaths , (d at:'font_dirs' ifAbsent:[#()]).
]
{ #category : #initialization }
DiyaSettings >> logSettings [
self stdlog: 'max_fps = ', maxFPS asString.
self stdlog: 'font_dirs = ', fontPaths asString.
self stdlog: 'asset_dir = ', assetPath asString.
self stdlog: 'render_all_at_once = ', renderAtOnce asString.
]
{ #category : #accessing }
DiyaSettings >> maxFPS [
^ maxFPS
]
{ #category : #accessing }
DiyaSettings >> maxFPS: anObject [
maxFPS := anObject
]
{ #category : #accessing }
DiyaSettings >> renderAtOnce [
^ renderAtOnce
]
{ #category : #accessing }
DiyaSettings >> renderAtOnce: anObject [
renderAtOnce := anObject
]

View File

@ -25,7 +25,7 @@ DiyaSingleton class >> new [
{ #category : #'instance creation' } { #category : #'instance creation' }
DiyaSingleton class >> reset [ DiyaSingleton class >> reset [
|singleton key| |singleton key|
key := self class asString asSymbol. key := self class.
singleton := singletons at: key ifAbsent: [ ^ self ]. singleton := singletons at: key ifAbsent: [ ^ self ].
self cleanUpInstance: singleton. self cleanUpInstance: singleton.
singletons removeKey: key singletons removeKey: key
@ -38,7 +38,7 @@ DiyaSingleton class >> resetAll [
{ #category : #'instance creation' } { #category : #'instance creation' }
DiyaSingleton class >> uniqueInstance [ DiyaSingleton class >> uniqueInstance [
^singletons at: self class asString asSymbol ifAbsentPut: [ super new ]. ^singletons at: self class ifAbsentPut: [ super new ].
] ]
{ #category : #initialization } { #category : #initialization }

View File

@ -26,7 +26,7 @@ DiyaTimerNode class >> timeout: ms doOnce: aBlock [
{ #category : #accessing } { #category : #accessing }
DiyaTimerNode >> delta [ DiyaTimerNode >> delta [
^ DiyaClock uniqueInstance delta asMilliSeconds ^ DiyaSystemClock delta asMilliSeconds
] ]
{ #category : #accessing } { #category : #accessing }

View File

@ -8,6 +8,6 @@ SDL_TouchFingerEvent >> triggableOn: aNode [
{ #category : #'*Diya' } { #category : #'*Diya' }
SDL_TouchFingerEvent >> worldPosition [ SDL_TouchFingerEvent >> worldPosition [
|resolution| |resolution|
resolution := DiyaRendererContext uniqueInstance resolution. resolution := DiyaRenderer resolution.
^((self x)* (resolution x) ) @ ((self y)* (resolution y)). ^((self x)* (resolution x) ) @ ((self y)* (resolution y)).
] ]