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

317 lines
8.7 KiB
Smalltalk
Raw Normal View History

2021-12-18 02:02:16 +01:00
Class {
#name : #DiyaBoot,
2022-02-15 18:04:54 +01:00
#superclass : #DiyaSingleton,
2021-12-19 12:32:23 +01:00
#instVars : [
'running',
'window',
2021-12-20 01:14:35 +01:00
'context',
2022-02-15 18:04:54 +01:00
'display',
'clock'
2021-12-19 12:32:23 +01:00
],
#pools : [
2021-12-20 01:14:35 +01:00
'OpenGLConstants',
'OpenGLTypes',
2021-12-19 12:32:23 +01:00
'SDL2Constants',
'SDL2Types'
],
2021-12-19 16:15:10 +01:00
#category : #'Diya-Runtime'
2021-12-18 02:02:16 +01:00
}
2022-03-06 18:33:10 +01:00
{ #category : #'instance creation' }
DiyaBoot class >> maxFPS [
^60
]
2021-12-19 12:32:23 +01:00
{ #category : #'instance creation' }
2021-12-18 02:02:16 +01:00
DiyaBoot class >> startUp: status [
2021-12-18 22:49:03 +01:00
self startx.
2021-12-18 02:02:16 +01:00
]
2021-12-19 12:32:23 +01:00
{ #category : #'instance creation' }
2021-12-18 22:49:03 +01:00
DiyaBoot class >> startx [
2021-12-19 16:15:10 +01:00
self uniqueInstance run
2021-12-19 12:32:23 +01:00
]
{ #category : #events }
DiyaBoot >> GLinit. [
OpenGL viewportX: 0 Y:0 W: display w H: display h.
OpenGL enable: GL_TEXTURE_2D.
]
2022-03-19 02:18:29 +01:00
{ #category : #events }
DiyaBoot >> bindGlobalEventTo: aNode [
|pointer|
pointer := aNode addNode: (DiyaCircle r: 10) at: 200@200.
pointer color: Color orange.
"pointer borderColor: Color red.
pointer borderWidth: 3."
aNode on: #keydown do:[:e| Transcript show: 'keydown...';cr. running := false.].
aNode on: #quit do: [:e| running := false].
2022-03-21 01:45:21 +01:00
aNode on: #(fingerdown fingermotion mousemotion) do:[:e|
2022-03-19 02:18:29 +01:00
pointer position: e mapped worldPosition.
DiyaRendererContext uniqueInstance mouse: (e mapped x) @ (e mapped y).
].
]
2021-12-20 01:14:35 +01:00
{ #category : #events }
DiyaBoot >> createGLContext [
context := SDL2 glCreateContext: window.
2022-03-19 02:18:29 +01:00
context ifNil: [ ^DiyaCoreAPIError signal: SDL2 getErrorMessage ].
2021-12-20 01:14:35 +01:00
^context
]
2021-12-19 12:32:23 +01:00
{ #category : #events }
DiyaBoot >> createWindow [
2022-03-16 01:32:01 +01:00
SDL2
glSetAttribute: SDL_GL_MULTISAMPLEBUFFERS value: 1;
glSetAttribute: SDL_GL_MULTISAMPLESAMPLES value: 2.
OpenGL enable: GL_MULTISAMPLE.
2021-12-20 01:14:35 +01:00
window := SDL2 createWindow: 'Diya'
2021-12-19 12:32:23 +01:00
x: 0
y: 0
2021-12-19 16:15:10 +01:00
width: display w
height: display h
2021-12-20 01:14:35 +01:00
flags: SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL.
2022-03-19 02:18:29 +01:00
window ifNil: [ ^DiyaCoreAPIError signal: SDL2 getErrorMessage ].
2021-12-19 12:32:23 +01:00
"handle fullscreen: SDL_WINDOW_FULLSCREEN."
2022-03-04 20:28:38 +01:00
"SDL2 glSetAttribute: SDL_GL_CONTEXT_PROFILE_MASK value: SDL_GL_CONTEXT_PROFILE_ES.
SDL2 glSetAttribute: SDL_GL_CONTEXT_MAJOR_VERSION value: 2.
SDL2 glSetAttribute: SDL_GL_CONTEXT_MINOR_VERSION value: 0.
SDL2 glSetAttribute: SDL_GL_ACCELERATED_VISUAL value: 1."
"SDL2 glSetAttribute: SDL_GL_DOUBLEBUFFER value: 1.
SDL2 glSetAttribute: SDL_GL_DEPTH_SIZE value: 24."
2021-12-20 01:14:35 +01:00
^window
2021-12-19 12:32:23 +01:00
]
2022-03-06 18:33:10 +01:00
{ #category : #events }
2022-03-19 02:18:29 +01:00
DiyaBoot >> exampleNodes: tree [
2022-03-21 01:45:21 +01:00
|root node node1 ell tex txtNode icon|
2022-03-19 02:18:29 +01:00
root := tree addNode: (Diya2DNode new) at: 0@10.
2022-03-16 21:33:35 +01:00
tex := (DiyaImageTex fromFile:Smalltalk imageDirectory / 'assets'/'mrsang.png').
2022-03-21 01:45:21 +01:00
txtNode := root addNode: (DiyaText data: 'Event') at: 10@55.
2022-03-19 02:18:29 +01:00
txtNode color: Color orange.
txtNode extent: 200@40.
node1 := root addNode: (DiyaRectangle size:100@150 shader: DiyaExampleShader uniqueInstance) at: 100 @ 400.
node1 rotation: (Float pi / 8.0).
node1 scale: 1.2@1.2.
node1 on: #mousebuttondown do:[:e| txtNode data: 'Mouse ', (node1 local: e mapped worldPosition) asIntegerPoint asString].
2022-03-21 01:45:21 +01:00
node1 on: #fingerdown do:[:e| txtNode data: 'Mouse ', (node1 local: e mapped worldPosition) asIntegerPoint asString].
2022-03-19 02:18:29 +01:00
2022-03-16 01:32:01 +01:00
node := root addNode: (DiyaRectangle size: 200@200) at: 250 @ 430.
2022-03-16 21:33:35 +01:00
node texture: tex.
2022-03-16 01:32:01 +01:00
node color: (Color r: 1.0 g:1.0 b:1.0 alpha:1.0 ).
node borderColor: Color red.
node borderWidth: 3.0.
2022-03-08 23:30:01 +01:00
"style := DiyaFontManager uniqueInstance style: 'Regular' from:'Ubuntu'.
tex1 := (style textureOf: 18).
style := DiyaFontManager uniqueInstance style: 'Regular' from: 'bootstrap-icons'.
node := root addNode: (DiyaRectangle size: tex1 extent) at: 250 @ 300.
2022-03-16 01:32:01 +01:00
node color: (Color orange).
node texture: tex1.
2022-03-16 01:32:01 +01:00
node borderColor: Color red.
node borderWidth: 3.0."
2022-03-16 01:32:01 +01:00
2022-03-19 02:18:29 +01:00
node := root addNode: (DiyaText data: String loremIpsum) at: 10@400.
2022-03-21 01:45:21 +01:00
node extent: 240@320.
2022-03-16 01:32:01 +01:00
node wordWrap: true.
node := root addNode: (DiyaLine from: 10@620 to: 200@635).
node color: (Color red).
node borderWidth: 2.0.
2022-03-08 23:30:01 +01:00
2022-03-19 02:18:29 +01:00
ell := root addNode: (DiyaEllipse rx:150 ry: 100) at: 320@300.
ell borderColor: Color red.
ell color: Color white.
ell rotation: Float pi / 6.0.
ell borderWidth: 3.0.
"node rotation: Float pi / 2.0."
ell texture: tex.
ell on: #mousebuttondown do:[:e| txtNode data: 'Ellipse clicked', (ell local:e mapped worldPosition) asIntegerPoint asString].
2022-03-21 01:45:21 +01:00
ell on: #fingerdown do:[:e| txtNode data: 'Ellipse clicked', (ell local:e mapped worldPosition) asIntegerPoint asString].
2022-03-16 21:36:18 +01:00
2022-03-16 21:33:35 +01:00
node := root addNode: (DiyaConvexPolygon points:{250@100. 400@250. 450@80. 350@60}).
node color: Color green.
node borderColor: Color red.
node texture: tex.
node borderWidth: 3.0.
2022-03-19 02:18:29 +01:00
2022-03-21 01:45:21 +01:00
icon := root addNode: (DiyaFontIcon data: #(16rF101 16rF155 16rF185 16rF21B 16rF298 16rF254)) at: 10@10.
icon fontSize: 24.
icon color: (Color r: 209/255 g: 66/255 b:245/255 ).
2022-03-06 18:33:10 +01:00
^ root
]
2021-12-19 12:32:23 +01:00
{ #category : #events }
DiyaBoot >> init [
2021-12-19 22:17:29 +01:00
| status |
2022-03-04 20:28:38 +01:00
SDL2 setHint: 'SDL_RENDER_DRIVER' value: 'opengles2'.
2021-12-19 16:15:10 +01:00
status := SDL2 init: SDL_INIT_EVERYTHING.
2022-03-03 23:23:43 +01:00
status = 0
2022-03-19 02:18:29 +01:00
ifFalse: [ ^ DiyaCoreAPIError signal: SDL2 getErrorMessage ].
2021-12-19 22:17:29 +01:00
display := SDL_DisplayMode externalNew autoRelease.
SDL2 SDLGetCurrentDisplayMode: display from:0.
DiyaRendererContext reset.
2022-03-06 12:07:20 +01:00
DiyaFontManager reset.
OpenGLSL resetShaders.
2022-03-03 19:19:40 +01:00
DiyaFontManager uniqueInstance loadFonts.
2021-12-19 12:32:23 +01:00
]
{ #category : #events }
DiyaBoot >> initialize [
running := true.
display := nil.
window := nil.
2021-12-20 01:14:35 +01:00
context := nil.
2022-02-15 18:04:54 +01:00
clock := DiyaClock uniqueInstance.
2021-12-19 12:32:23 +01:00
]
{ #category : #events }
DiyaBoot >> randomColorChannel [
| rand |
rand := Random new.
rand := (rand next) * 255.
rand := rand asInteger.
^ rand
]
{ #category : #events }
2021-12-19 16:15:10 +01:00
DiyaBoot >> render [
2022-03-16 21:33:35 +01:00
|event root text delta fps|
2021-12-19 12:32:23 +01:00
event := SDL_Event new.
2022-03-19 02:18:29 +01:00
root := DiyaRootNode new.
self exampleNodes: root.
DiyaRenderer uniqueInstance root: root.
2022-03-16 21:33:35 +01:00
text := root addNode:(DiyaText data: 'tick') at: (display w - 80)@40.
text extent: 80@40.
text fontSize: 18.
2022-03-06 18:33:10 +01:00
text color: Color red.
2022-03-19 02:18:29 +01:00
self bindGlobalEventTo: root.
2022-03-15 19:11:19 +01:00
DiyaRendererContext uniqueInstance.
self GLinit.
2022-03-03 23:23:43 +01:00
[ running ] whileTrue: [
2022-03-06 18:33:10 +01:00
delta := DiyaClock uniqueInstance delta asMilliSeconds.
2022-03-16 01:32:01 +01:00
fps := ((1000/delta) asInteger).
2022-03-16 21:33:35 +01:00
text data: ('FPS:', fps asString).
2022-03-06 18:33:10 +01:00
DiyaClock uniqueInstance tick.
[(SDL2 pollEvent: event) > 0] whileTrue: [
2022-03-19 02:18:29 +01:00
root trigger: (DiyaEvent from: event mapped).
2021-12-19 12:32:23 +01:00
].
DiyaRenderer uniqueInstance render.
2022-03-15 19:11:19 +01:00
2021-12-20 01:14:35 +01:00
SDL2 glSwapWindow: window.
2022-03-06 18:33:10 +01:00
delta := DiyaClock uniqueInstance delta asMilliSeconds.
2022-03-16 21:33:35 +01:00
SDL2 delay: (0 max: (1000/ self class maxFPS) asInteger - delta).
2022-02-13 17:15:23 +01:00
].
]
2021-12-19 16:15:10 +01:00
{ #category : #events }
DiyaBoot >> run [
self init.
2021-12-19 22:17:29 +01:00
self startx.
2021-12-19 20:03:54 +01:00
]
2021-12-19 22:17:29 +01:00
{ #category : #running }
2021-12-19 20:03:54 +01:00
DiyaBoot >> run: screenSize [
2021-12-19 22:17:29 +01:00
self run: screenSize app: nil
]
{ #category : #running }
DiyaBoot >> run: screenSize app: application [
"
this function should be used only in
SDK environment, in real embeded system
it is always the #run command that is executed
automatically.
"
2021-12-20 01:14:35 +01:00
OpenGLTypes initialize.
OpenGLConstants initialize.
2021-12-19 20:03:54 +01:00
self init.
display w: screenSize x.
display h: screenSize y.
self startx.
self class reset.
2022-02-15 18:04:54 +01:00
DiyaClock reset.
DiyaRendererContext reset.
Smalltalk garbageCollect.
2021-12-19 20:03:54 +01:00
]
{ #category : #events }
DiyaBoot >> setCursorPosition: mappedEvt [
window warpMouseX:((mappedEvt x)* (display w) )
Y: ((mappedEvt y) * (display h))
]
2021-12-19 22:17:29 +01:00
{ #category : #logging }
DiyaBoot >> showSystemInfo [
|stream numdriver rinfo|
stream := (String new: 255) writeStream.
2021-12-19 22:17:29 +01:00
stream nextPutAll:'System: ';
nextPutAll:(Smalltalk globals at: #CODENAME ifAbsent:['']);
nextPutAll: '-v';
nextPutAll:(Smalltalk globals at: #VERSION ifAbsent: ['']);cr.
numdriver := SDL2 SDLGetNumVideoDrivers.
stream nextPutAll: 'Supported video dirvers:'.
0 to: numdriver -1 do: [ :i |
stream nextPutAll: (SDL2 SDLGetVideoDriver: i); nextPutAll: ' '.
].
stream cr.
stream nextPutAll: 'Current selected video driver: ';
nextPutAll:(SDL2 SDLGetCurrentVideoDriver);cr.
numdriver := SDL2 SDLGetNumRenderDrivers.
stream nextPutAll: 'SDL_RENDER_DRIVER available:'.
rinfo := SDL_RendererInfo externalNew autoRelease.
0 to: numdriver - 1 do:[:i|
SDL2 SDLGetRendererDriverInfo: rinfo from: i.
stream nextPutAll: rinfo name readString; nextPutAll:' '.
].
stream cr.
stream nextPutAll:'Display resolution: ';
nextPutAll:display w asString;
nextPutAll: 'x';
nextPutAll: display h asString; cr.
self stdout nextPutAll: stream contents
2021-12-19 22:17:29 +01:00
]
2021-12-19 20:03:54 +01:00
{ #category : #events }
DiyaBoot >> startx [
2022-03-19 02:18:29 +01:00
display ifNil: [ ^DiyaCoreAPIError signal: 'Please run #init before this method' ].
2021-12-20 01:14:35 +01:00
self createWindow.
self createGLContext.
2022-03-15 19:11:19 +01:00
"SDL2 glMakeCurrent: window context: context."
2021-12-19 22:17:29 +01:00
self showSystemInfo.
2022-03-15 19:11:19 +01:00
DiyaRendererContext
uniqueInstance display: display;
useProjection: OrthoProjectionMatrix.
self render.
2021-12-20 01:14:35 +01:00
context delete.
2021-12-19 16:15:10 +01:00
window destroy.
2022-03-01 19:22:26 +01:00
DiyaFontManager reset.
DiyaRendererContext reset.
2021-12-19 16:15:10 +01:00
SDL2 quit.
]
2021-12-20 01:14:35 +01:00
{ #category : #events }
DiyaBoot >> step [
"renderer drawColorR: 0
g: 0
b: 0
a: 255."
2021-12-20 01:14:35 +01:00
OpenGL begin: GL_TRIANGLES.
"draw a simple triangle here"
OpenGL color3fR: 0.1 G:0.2 B: 0.3.
OpenGL vertex3fX: 0 Y: 0 Z: 0.
OpenGL vertex3fX: 1 Y: 0 Z: 0.
OpenGL vertex3fX: 0 Y: 1 Z: 0.
OpenGL end.
]