-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
OSSDL2AthensRenderer.class.st
178 lines (152 loc) · 4.26 KB
/
OSSDL2AthensRenderer.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"
I am an Athens renderer which uses SDL2.
"
Class {
#name : #OSSDL2AthensRenderer,
#superclass : #OSWindowAthensRenderer,
#instVars : [
'texture',
'renderer',
'pixels',
'pitch',
'sdlSurface',
'textureExtent',
'surfaceForm'
],
#pools : [
'SDL2Constants'
],
#category : #'OSWindow-SDL2-Rendering'
}
{ #category : #'instance creation' }
OSSDL2AthensRenderer class >> for: aWindowHandle [
^ self basicNew
initializeWindowHandle: aWindowHandle;
yourself
]
{ #category : #'instance creation' }
OSSDL2AthensRenderer class >> new [
self error: 'Use #for:'
]
{ #category : #private }
OSSDL2AthensRenderer >> createSDLSurfaceWithExtent: extent [
surfaceForm ifNotNil: [ surfaceForm destroySurface ].
"createRGBSurfaceForCairoWidth creates the surface with autoRelease."
sdlSurface := SDL2 createRGBSurfaceForCairoWidth: extent x height: extent y.
surfaceForm := OSSDL2ExternalForm extent: extent depth: 32 bits: sdlSurface pixels.
]
{ #category : #deleting }
OSSDL2AthensRenderer >> destroy [
super destroy.
sdlSurface := nil.
texture := nil.
renderer := nil.
]
{ #category : #initialization }
OSSDL2AthensRenderer >> initialize [
super initialize.
sdlSurface := nil.
texture := nil.
renderer := nil.
]
{ #category : #initialization }
OSSDL2AthensRenderer >> initializeWindowHandle: aWindowHandle [
self initialize.
self window: aWindowHandle.
renderer := window handle createDefaultRenderer.
self resetResources.
]
{ #category : #drawing }
OSSDL2AthensRenderer >> prepareForDrawing [
textureExtent ~= window extent ifTrue: [ self resized ].
]
{ #category : #'updating screen' }
OSSDL2AthensRenderer >> present [
self validate ifFalse: [ ^ self ].
renderer
clear;
copy: texture;
present
]
{ #category : #private }
OSSDL2AthensRenderer >> primitiveUpdateRectangle: rectangle externalForm: externalForm [
externalForm copy: rectangle from: surfaceForm to: rectangle origin rule: Form over
]
{ #category : #'updating resources' }
OSSDL2AthensRenderer >> resetResources [
| extent |
self checkSession.
extent := self window extent.
self createSDLSurfaceWithExtent: extent.
athensSurface := AthensCairoSDLSurface fromSDLSurface: sdlSurface.
texture := renderer
createTextureFormat: SDL_PIXELFORMAT_XRGB8888
access: SDL_TEXTUREACCESS_STREAMING
width: extent x
height: extent y.
textureExtent := extent.
]
{ #category : #'updating screen' }
OSSDL2AthensRenderer >> resized [
self resetResources.
super resized
]
{ #category : #'updating screen' }
OSSDL2AthensRenderer >> updateAll [
self validate ifFalse: [ ^ self ].
texture updateTexturePixels: sdlSurface pixels pitch: sdlSurface pitch.
]
{ #category : #'updating screen' }
OSSDL2AthensRenderer >> updateRectangle: aRectangle [
| intersection pitchHolder externalForm |
self validate ifFalse: [ ^ self ].
intersection := aRectangle
intersect: (0 @ 0 corner: textureExtent)
ifNone: [ ^ self ].
pixels := ExternalAddress new.
pitchHolder := ExternalAddress allocate: 4.
[
texture lockPixels: pixels pitch: pitchHolder.
pitch := pitchHolder signedLongAt: 1.
] ensure: [ pitchHolder free ].
externalForm := OSSDL2ExternalForm extent: textureExtent depth: 32 bits: pixels.
[
self primitiveUpdateRectangle: intersection externalForm: externalForm
] ensure: [
texture unlock.
externalForm destroySurface
].
]
{ #category : #'updating screen' }
OSSDL2AthensRenderer >> updateRectangles: allDamage [
| pitchHolder externalForm intersection |
self validate
ifFalse: [ ^ self ].
pixels := ExternalAddress new.
pitchHolder := ExternalAddress allocate: 4.
[
texture lockPixels: pixels pitch: pitchHolder.
pitch := pitchHolder signedLongAt: 1.
] ensure: [ pitchHolder free ].
[
externalForm := OSSDL2ExternalForm extent: textureExtent depth: 32 bits: pixels.
allDamage do: [ :rectangle |
intersection := rectangle
intersect: (0 @ 0 corner: window extent)
ifNone: [ nil ].
intersection ifNotNil: [
self primitiveUpdateRectangle: rectangle externalForm: externalForm.
]
].
] ensure: [
texture unlock.
externalForm destroySurface
].
]
{ #category : #'updating screen' }
OSSDL2AthensRenderer >> validate [
self checkSession.
(texture isNil or: [ texture isNull ]) ifTrue: [ ^ false ].
window extent ~= textureExtent ifTrue: [ ^ false ].
^ true
]