1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-26 03:18:22 +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 : #'instance creation' }
AssetManager class >> defaultAssetLocation [
^Smalltalk imageDirectory / 'assets'
]
{ #category : #'instance creation' }
AssetManager class >> fromLocation: loc [
^self new from: loc; yourself
@ -40,7 +34,7 @@ AssetManager >> from: location [
AssetManager >> initialize [
super initialize.
assets := Dictionary new.
base := self class defaultAssetLocation
base := DiyaSystemSettings assetPath
]
{ #category : #accessing }

View File

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

View File

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

View File

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

View File

@ -99,7 +99,7 @@ DiyaExampleApp >> setup [
|p|
p := e mapped worldPosition.
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').
img textureNamed: 'capture'.

View File

@ -13,13 +13,6 @@ DiyaFontManager class >> cleanUpInstance: singleton [
singleton reset.
]
{ #category : #'as yet unclassified' }
DiyaFontManager class >> searchPaths [
^ {
Smalltalk imageDirectory / 'fonts'
}
]
{ #category : #initialization }
DiyaFontManager >> defaultFamily [
^'Ubuntu'
@ -90,7 +83,9 @@ DiyaFontManager >> loadFonts [
{ #category : #initialization }
DiyaFontManager >> loadFonts: force [
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.
parent := nil.
shader := nil.
context := DiyaRendererContext uniqueInstance.
context := DiyaRenderer.
children := OrderedCollection new.
ehandlers := Dictionary new.
styleName := nil.
@ -264,7 +264,7 @@ DiyaNode >> setUpShader [
|mem|
mem := self tf asGLBuffer.
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_resolution value: { context resolution x. context resolution y };
setUniform: #u_transform value: {GL_TRUE. mem}.

View File

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

View File

@ -69,24 +69,15 @@ DiyaRootNode >> readyForSwap [
{ #category : #rendering }
DiyaRootNode >> render [
|node maxProcessingTime|
| maxProcessingTime|
self stepDown.
R ifEmpty: [
self draw.
self renderNext: children
].
maxProcessingTime := (1000 / DiyaBoot maxFPS) asInteger.
maxProcessingTime := (1000 / DiyaSystemSettings maxFPS) asInteger.
[
Q ifNotEmpty: [
node := Q removeFirst.
node process.
].
R ifNotEmpty: [
node := R removeFirst.
node render.
].
(R isNotEmpty or: Q isNotEmpty) and:
(DiyaClock uniqueInstance lapDelta asMilliSeconds < maxProcessingTime)
self spinOnce: maxProcessingTime
] 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 }
DiyaRootNode >> updateTF [
"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' }
DiyaSingleton class >> reset [
|singleton key|
key := self class asString asSymbol.
key := self class.
singleton := singletons at: key ifAbsent: [ ^ self ].
self cleanUpInstance: singleton.
singletons removeKey: key
@ -38,7 +38,7 @@ DiyaSingleton class >> resetAll [
{ #category : #'instance creation' }
DiyaSingleton class >> uniqueInstance [
^singletons at: self class asString asSymbol ifAbsentPut: [ super new ].
^singletons at: self class ifAbsentPut: [ super new ].
]
{ #category : #initialization }

View File

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

View File

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