@@ -7,7 +7,6 @@ use std::thread;
7
7
use glutin:: config:: ConfigTemplateBuilder ;
8
8
use glutin:: context:: { ContextAttributesBuilder , PossiblyCurrentContext } ;
9
9
use glutin:: display:: GetGlDisplay ;
10
- use glutin:: error:: { Error as GlutinError , ErrorKind } ;
11
10
use glutin:: prelude:: * ;
12
11
use glutin:: surface:: { Surface , WindowSurface } ;
13
12
use glutin_examples:: gl:: types:: GLfloat ;
@@ -148,7 +147,7 @@ impl AppState {
148
147
149
148
/// A rendering context that can be shared between tasks.
150
149
struct RenderContext {
151
- context : Option < PossiblyCurrentContext > ,
150
+ context : PossiblyCurrentContext ,
152
151
surface : Surface < WindowSurface > ,
153
152
renderer : Renderer ,
154
153
}
@@ -161,44 +160,27 @@ impl RenderContext {
161
160
surface : Surface < WindowSurface > ,
162
161
renderer : Renderer ,
163
162
) -> Self {
164
- Self { context : Some ( context ) , surface, renderer }
163
+ Self { context, surface, renderer }
165
164
}
166
165
167
166
fn make_current ( & mut self ) -> Result < ( ) , impl Error > {
168
- let ctx =
169
- self . context . take ( ) . ok_or_else ( || GlutinError :: from ( ErrorKind :: BadContextState ) ) ?;
170
- let result = ctx. make_current ( & self . surface ) ;
171
- self . context = Some ( ctx) ;
172
- result
167
+ self . context . make_current ( & self . surface )
173
168
}
174
169
175
170
fn make_not_current ( & mut self ) -> Result < ( ) , impl Error > {
176
- let ctx =
177
- self . context . take ( ) . ok_or_else ( || GlutinError :: from ( ErrorKind :: BadContextState ) ) ?;
178
- let not_current_ctx = ctx. make_not_current ( ) ?;
179
- self . context = Some ( not_current_ctx. treat_as_possibly_current ( ) ) ;
180
- Ok :: < ( ) , GlutinError > ( ( ) )
171
+ self . context . make_not_current_in_place ( )
181
172
}
182
173
183
174
fn swap_buffers ( & mut self ) -> Result < ( ) , impl Error > {
184
- let ctx =
185
- self . context . take ( ) . ok_or_else ( || GlutinError :: from ( ErrorKind :: BadContextState ) ) ?;
186
- let result = self . surface . swap_buffers ( & ctx) ;
187
- self . context = Some ( ctx) ;
188
- result
175
+ self . surface . swap_buffers ( & self . context )
189
176
}
190
177
191
178
fn draw_with_clear_color ( & self , red : GLfloat , green : GLfloat , blue : GLfloat , alpha : GLfloat ) {
192
179
self . renderer . draw_with_clear_color ( red, green, blue, alpha)
193
180
}
194
181
195
182
fn resize ( & mut self , size : PhysicalSize < NonZeroU32 > ) {
196
- let Some ( ctx) = self . context . take ( ) else {
197
- return ;
198
- } ;
199
- self . surface . resize ( & ctx, size. width , size. height ) ;
200
- self . context = Some ( ctx) ;
201
-
183
+ self . surface . resize ( & self . context , size. width , size. height ) ;
202
184
self . renderer . resize ( size. width . get ( ) as i32 , size. height . get ( ) as i32 ) ;
203
185
}
204
186
}
0 commit comments