mirror of
https://github.com/lxsang/Diya-API.git
synced 2024-12-28 12:28:21 +01:00
example of working SDL window
This commit is contained in:
parent
48fe04d362
commit
03d3a59560
@ -1,34 +1,148 @@
|
|||||||
Class {
|
Class {
|
||||||
#name : #DiyaBoot,
|
#name : #DiyaBoot,
|
||||||
#superclass : #DiyaBaseObject,
|
#superclass : #DiyaBaseObject,
|
||||||
#category : #'Diya-Runtime'
|
#instVars : [
|
||||||
|
'running',
|
||||||
|
'window',
|
||||||
|
'renderer'
|
||||||
|
],
|
||||||
|
#classVars : [
|
||||||
|
'singleton'
|
||||||
|
],
|
||||||
|
#pools : [
|
||||||
|
'SDL2Constants',
|
||||||
|
'SDL2Types'
|
||||||
|
],
|
||||||
|
#category : #'Diya-Core'
|
||||||
}
|
}
|
||||||
|
|
||||||
{ #category : #'system startup' }
|
{ #category : #'instance creation' }
|
||||||
DiyaBoot class >> getLoadedClasses [
|
|
||||||
^ SessionManager default startupList
|
|
||||||
collect: [ :c |
|
|
||||||
(c respondsTo: #registeredClass)
|
|
||||||
ifTrue: [ c registeredClass ]
|
|
||||||
ifFalse: [ c ] ]
|
|
||||||
]
|
|
||||||
|
|
||||||
{ #category : #'system startup' }
|
|
||||||
DiyaBoot class >> startUp: status [
|
DiyaBoot class >> startUp: status [
|
||||||
"
|
|
||||||
As is the entry point for Diya, it need to be disabled
|
|
||||||
when inside the SDK image.
|
|
||||||
"
|
|
||||||
((Smalltalk globals at:#codeName ifAbsent:[nil]) = 'diya') ifFalse: [^self].
|
|
||||||
self getLoadedClasses
|
|
||||||
do: [ :c |
|
|
||||||
Transcript
|
|
||||||
show: c className;
|
|
||||||
cr ].
|
|
||||||
self startx.
|
self startx.
|
||||||
]
|
]
|
||||||
|
|
||||||
{ #category : #'system startup' }
|
{ #category : #'instance creation' }
|
||||||
DiyaBoot class >> startx [
|
DiyaBoot class >> startx [
|
||||||
DiyaInit uniqueInstance eventLoop
|
self uniqueInstance eventLoop
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #'instance creation' }
|
||||||
|
DiyaBoot class >> uniqueInstance [
|
||||||
|
singleton ifNil: [ singleton := self new ].
|
||||||
|
^ singleton
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> checkDrivers [
|
||||||
|
|ndriver usableDrivers|
|
||||||
|
ndriver := SDL2 SDLGetNumVideoDrivers.
|
||||||
|
usableDrivers := OrderedCollection new.
|
||||||
|
Transcript show: 'Available drivers: '.
|
||||||
|
0 to: ndriver - 1 do: [ :i | |dname|
|
||||||
|
dname := SDL2 SDLGetVideoDriver: i.
|
||||||
|
Transcript show: dname, ' '.
|
||||||
|
(SDL2 SDLVideoInit: dname) = 0 ifTrue:[
|
||||||
|
usableDrivers add: dname.
|
||||||
|
SDL2 SDLVideoQuit
|
||||||
|
]
|
||||||
|
].
|
||||||
|
Transcript cr.
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> createRenderer [
|
||||||
|
|numdriver rinfo|
|
||||||
|
numdriver := SDL2 SDLGetNumRenderDrivers.
|
||||||
|
Transcript show: 'SDL_RENDER_DRIVER available:'.
|
||||||
|
rinfo := SDL_RendererInfo externalNew autoRelease.
|
||||||
|
0 to: numdriver - 1 do:[:i|
|
||||||
|
SDL2 SDLGetRendererDriverInfo: rinfo from: i.
|
||||||
|
Transcript show: rinfo name readString, ' '.
|
||||||
|
].
|
||||||
|
Transcript cr.
|
||||||
|
renderer := window primCreateRenderer: -1 flags: SDL_RENDERER_ACCELERATED.
|
||||||
|
renderer ifNil: [ ^self error: SDL2 getErrorMessage ].
|
||||||
|
renderer primGetRendererInfo: rinfo.
|
||||||
|
Transcript show: 'SDL_RENDER_DRIVER selected: ', rinfo name readString;cr.
|
||||||
|
^renderer
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> createWindow [
|
||||||
|
|handle|
|
||||||
|
handle := SDL2 createWindow: 'Diya test'
|
||||||
|
x: 0
|
||||||
|
y: 0
|
||||||
|
width: 480
|
||||||
|
height: 640
|
||||||
|
flags: SDL_WINDOW_SHOWN.
|
||||||
|
handle ifNil: [ ^self error: SDL2 getErrorMessage ].
|
||||||
|
"handle fullscreen: SDL_WINDOW_FULLSCREEN."
|
||||||
|
^handle
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> eventLoop [
|
||||||
|
| dm|
|
||||||
|
self init.
|
||||||
|
Transcript show: 'Current selected video driver ', (SDL2 SDLGetCurrentVideoDriver); cr.
|
||||||
|
dm := SDL_DisplayMode externalNew autoRelease.
|
||||||
|
SDL2 SDLGetCurrentDisplayMode: dm from:0.
|
||||||
|
Transcript show: dm w asString, 'x',dm h asString;cr.
|
||||||
|
window := self createWindow.
|
||||||
|
renderer := self createRenderer.
|
||||||
|
self render: renderer.
|
||||||
|
renderer destroy.
|
||||||
|
window destroy.
|
||||||
|
SDL2 quit.
|
||||||
|
Transcript show: 'System quit'
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> init [
|
||||||
|
| tmp1 |
|
||||||
|
tmp1 := SDL2 init: SDL_INIT_EVERYTHING.
|
||||||
|
tmp1 = 0
|
||||||
|
ifFalse: [ ^ self error: SDL2 getErrorMessage ]
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> initialize [
|
||||||
|
running := true
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> processEvent: event [
|
||||||
|
|mappedEvt|
|
||||||
|
mappedEvt := event mapped.
|
||||||
|
mappedEvt type = SDL_KEYDOWN ifTrue: [ Transcript show: 'keydown...'. running := false. ].
|
||||||
|
mappedEvt type = SDL_QUIT ifTrue:[ running:= false ].
|
||||||
|
mappedEvt type = SDL_FINGERDOWN ifTrue:[window warpMouseX:((mappedEvt x)* 480 )Y: ((mappedEvt y) * 640)].
|
||||||
|
mappedEvt type = SDL_FINGERMOTION ifTrue:[window warpMouseX:((mappedEvt x)* 480 )Y: ((mappedEvt y) * 640)].
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> randomColorChannel [
|
||||||
|
| rand |
|
||||||
|
rand := Random new.
|
||||||
|
rand := (rand next) * 255.
|
||||||
|
rand := rand asInteger.
|
||||||
|
^ rand
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #events }
|
||||||
|
DiyaBoot >> render: renderer [
|
||||||
|
|event|
|
||||||
|
event := SDL_Event new.
|
||||||
|
[ running ] whileTrue: [
|
||||||
|
[ (SDL2 pollEvent: event) > 0 ] whileTrue: [
|
||||||
|
self processEvent: event
|
||||||
|
].
|
||||||
|
renderer drawColorR: self randomColorChannel g: self randomColorChannel b: self randomColorChannel a: 255.
|
||||||
|
renderer clear.
|
||||||
|
renderer present.
|
||||||
|
SDL2 delay: 50.
|
||||||
|
].
|
||||||
]
|
]
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
Class {
|
|
||||||
#name : #DiyaInit,
|
|
||||||
#superclass : #Object,
|
|
||||||
#classVars : [
|
|
||||||
'singleton'
|
|
||||||
],
|
|
||||||
#pools : [
|
|
||||||
'SDL2Constants',
|
|
||||||
'SDL2Types'
|
|
||||||
],
|
|
||||||
#category : #'Diya-Core'
|
|
||||||
}
|
|
||||||
|
|
||||||
{ #category : #'instance creation' }
|
|
||||||
DiyaInit class >> uniqueInstance [
|
|
||||||
singleton ifNil: [ singleton := self new ].
|
|
||||||
^ singleton
|
|
||||||
]
|
|
||||||
|
|
||||||
{ #category : #events }
|
|
||||||
DiyaInit >> checkDrivers [
|
|
||||||
|ndriver usableDrivers|
|
|
||||||
ndriver := SDL2 SDLGetNumVideoDrivers.
|
|
||||||
usableDrivers := OrderedCollection new.
|
|
||||||
Transcript show: 'Available drivers: '.
|
|
||||||
0 to: ndriver - 1 do: [ :i | |dname|
|
|
||||||
dname := SDL2 SDLGetVideoDriver: i.
|
|
||||||
Transcript show: dname, ' '.
|
|
||||||
(SDL2 SDLVideoInit: dname) = 0 ifTrue:[
|
|
||||||
usableDrivers add: dname.
|
|
||||||
SDL2 SDLVideoQuit
|
|
||||||
]
|
|
||||||
].
|
|
||||||
Transcript cr.
|
|
||||||
|
|
||||||
]
|
|
||||||
|
|
||||||
{ #category : #events }
|
|
||||||
DiyaInit >> eventLoop [
|
|
||||||
|status dm|
|
|
||||||
self checkDrivers.
|
|
||||||
status := SDL2 init: SDL_INIT_EVERYTHING.
|
|
||||||
status = 0 ifFalse:[
|
|
||||||
^ self error: SDL2 getErrorMessage.
|
|
||||||
].
|
|
||||||
Transcript show: 'Current selected video driver ', (SDL2 SDLGetCurrentVideoDriver); cr.
|
|
||||||
dm := SDL_DisplayMode new.
|
|
||||||
SDL2 SDLGetCurrentDisplayMode: dm from:0.
|
|
||||||
Transcript show: dm w asString, 'x',dm h asString;cr.
|
|
||||||
SDL2 quit
|
|
||||||
]
|
|
@ -89,7 +89,7 @@ ImageInitializer >> preloadSystem: loaded [
|
|||||||
ImageInitializer >> snap [
|
ImageInitializer >> snap [
|
||||||
Smalltalk globals at: #codeName put: 'diya'.
|
Smalltalk globals at: #codeName put: 'diya'.
|
||||||
self class removeFromSystem.
|
self class removeFromSystem.
|
||||||
Smalltalk addToShutDownList: DiyaBoot.
|
Smalltalk addToStartUpList: DiyaBoot.
|
||||||
Smalltalk garbageCollect.
|
Smalltalk garbageCollect.
|
||||||
Smalltalk snapshot: true andQuit: true.
|
Smalltalk snapshot: true andQuit: true.
|
||||||
|
|
||||||
|
@ -30,6 +30,11 @@ SDL2 class >> SDLGetNumVideoDrivers [
|
|||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
{ #category : #'*Diya' }
|
||||||
|
SDL2 class >> SDLGetRendererDriverInfo: info from: index [
|
||||||
|
^self ffiCall: #(int SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info))
|
||||||
|
]
|
||||||
|
|
||||||
{ #category : #'*Diya' }
|
{ #category : #'*Diya' }
|
||||||
SDL2 class >> SDLGetVideoDriver: index [
|
SDL2 class >> SDLGetVideoDriver: index [
|
||||||
^self ffiCall: #(const char* SDL_GetVideoDriver(int index))
|
^self ffiCall: #(const char* SDL_GetVideoDriver(int index))
|
||||||
|
13
Diya/SDL_Window.extension.st
Normal file
13
Diya/SDL_Window.extension.st
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Extension { #name : #'SDL_Window' }
|
||||||
|
|
||||||
|
{ #category : #'*Diya' }
|
||||||
|
SDL_Window >> destroy [
|
||||||
|
self primDestroy.
|
||||||
|
handle beNull
|
||||||
|
]
|
||||||
|
|
||||||
|
{ #category : #'*Diya' }
|
||||||
|
SDL_Window >> warpMouseX:x Y:y [
|
||||||
|
^self ffiCall: #(void SDL_WarpMouseInWindow(self,int x, int y))
|
||||||
|
|
||||||
|
]
|
Loading…
Reference in New Issue
Block a user