From 74ce2b0f37146f063a2c943b0c16755d361f8081 Mon Sep 17 00:00:00 2001 From: Dany LE Date: Sun, 14 Aug 2022 18:13:23 +0200 Subject: [PATCH] Supporting read system settings from file --- Diya/AssetManager.class.st | 8 +-- Diya/DiyaApplicationLauncher.class.st | 8 +-- Diya/DiyaApplicationModel.class.st | 2 +- Diya/DiyaBoot.class.st | 18 +++--- Diya/DiyaExampleApp.class.st | 2 +- Diya/DiyaFontManager.class.st | 11 +--- Diya/DiyaNode.class.st | 4 +- Diya/DiyaRendererContext.class.st | 9 ++- Diya/DiyaRootNode.class.st | 31 +++++---- Diya/DiyaSettings.class.st | 90 ++++++++++++++++++++++++++ Diya/DiyaSingleton.class.st | 4 +- Diya/DiyaTimerNode.class.st | 2 +- Diya/SDL_TouchFingerEvent.extension.st | 2 +- 13 files changed, 140 insertions(+), 51 deletions(-) create mode 100644 Diya/DiyaSettings.class.st diff --git a/Diya/AssetManager.class.st b/Diya/AssetManager.class.st index 414c5c5..73b3ca8 100644 --- a/Diya/AssetManager.class.st +++ b/Diya/AssetManager.class.st @@ -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 } diff --git a/Diya/DiyaApplicationLauncher.class.st b/Diya/DiyaApplicationLauncher.class.st index d94b26d..ddae651 100644 --- a/Diya/DiyaApplicationLauncher.class.st +++ b/Diya/DiyaApplicationLauncher.class.st @@ -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: [ diff --git a/Diya/DiyaApplicationModel.class.st b/Diya/DiyaApplicationModel.class.st index 20cb6b1..ddbe01c 100644 --- a/Diya/DiyaApplicationModel.class.st +++ b/Diya/DiyaApplicationModel.class.st @@ -20,7 +20,7 @@ DiyaApplicationModel >> cleanup [ { #category : #accessing } DiyaApplicationModel >> context [ - ^DiyaRendererContext uniqueInstance + ^DiyaRenderer ] { #category : #initialization } diff --git a/Diya/DiyaBoot.class.st b/Diya/DiyaBoot.class.st index 367620f..67126ab 100644 --- a/Diya/DiyaBoot.class.st +++ b/Diya/DiyaBoot.class.st @@ -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. diff --git a/Diya/DiyaExampleApp.class.st b/Diya/DiyaExampleApp.class.st index bfc9b49..c9daee5 100644 --- a/Diya/DiyaExampleApp.class.st +++ b/Diya/DiyaExampleApp.class.st @@ -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'. diff --git a/Diya/DiyaFontManager.class.st b/Diya/DiyaFontManager.class.st index 9655a22..02b7059 100644 --- a/Diya/DiyaFontManager.class.st +++ b/Diya/DiyaFontManager.class.st @@ -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]. ] diff --git a/Diya/DiyaNode.class.st b/Diya/DiyaNode.class.st index bb10cd3..61f84b6 100644 --- a/Diya/DiyaNode.class.st +++ b/Diya/DiyaNode.class.st @@ -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}. diff --git a/Diya/DiyaRendererContext.class.st b/Diya/DiyaRendererContext.class.st index de2c4e1..00f2de0 100644 --- a/Diya/DiyaRendererContext.class.st +++ b/Diya/DiyaRendererContext.class.st @@ -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] . ] diff --git a/Diya/DiyaRootNode.class.st b/Diya/DiyaRootNode.class.st index 7acebe6..9d1ddb4 100644 --- a/Diya/DiyaRootNode.class.st +++ b/Diya/DiyaRootNode.class.st @@ -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" diff --git a/Diya/DiyaSettings.class.st b/Diya/DiyaSettings.class.st new file mode 100644 index 0000000..ebaf66f --- /dev/null +++ b/Diya/DiyaSettings.class.st @@ -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 +] diff --git a/Diya/DiyaSingleton.class.st b/Diya/DiyaSingleton.class.st index b6836f7..e679326 100644 --- a/Diya/DiyaSingleton.class.st +++ b/Diya/DiyaSingleton.class.st @@ -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 } diff --git a/Diya/DiyaTimerNode.class.st b/Diya/DiyaTimerNode.class.st index 7f42ef8..7fe3bf0 100644 --- a/Diya/DiyaTimerNode.class.st +++ b/Diya/DiyaTimerNode.class.st @@ -26,7 +26,7 @@ DiyaTimerNode class >> timeout: ms doOnce: aBlock [ { #category : #accessing } DiyaTimerNode >> delta [ - ^ DiyaClock uniqueInstance delta asMilliSeconds + ^ DiyaSystemClock delta asMilliSeconds ] { #category : #accessing } diff --git a/Diya/SDL_TouchFingerEvent.extension.st b/Diya/SDL_TouchFingerEvent.extension.st index b3a7e08..1138992 100644 --- a/Diya/SDL_TouchFingerEvent.extension.st +++ b/Diya/SDL_TouchFingerEvent.extension.st @@ -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)). ]