@@ -113,6 +113,38 @@ impl Application {
113
113
}
114
114
}
115
115
116
+ macro_rules! opengl_error_check {
117
+ ( ) => {
118
+
119
+ if cfg!( debug_assertions) {
120
+ #[ allow( unused_unsafe) ] {
121
+ if let Some ( ref resources) = unsafe { RESOURCES . as_ref( ) } {
122
+ let mut err;
123
+ while {
124
+ err = unsafe { resources. ctx. GetError ( ) } ;
125
+ err != gl:: NO_ERROR
126
+ }
127
+ {
128
+ let err_str = match err {
129
+ gl:: INVALID_ENUM => "INVALID_ENUM" ,
130
+ gl:: INVALID_VALUE => "INVALID_VALUE" ,
131
+ gl:: INVALID_OPERATION => "INVALID_OPERATION" ,
132
+ gl:: STACK_OVERFLOW => "STACK_OVERFLOW" ,
133
+ gl:: STACK_UNDERFLOW => "STACK_UNDERFLOW" ,
134
+ gl:: OUT_OF_MEMORY => "OUT_OF_MEMORY" ,
135
+ _ => "Unknown error type" ,
136
+ } ;
137
+ println!( "OpenGL error: {}({}) on line {} of {}" , err_str, err, line!( ) , file!( ) ) ;
138
+ }
139
+ if err != gl:: NO_ERROR {
140
+ panic!( ) ;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ }
147
+
116
148
fn find_sdl_gl_driver ( ) -> Option < u32 > {
117
149
for ( index, item) in sdl2:: render:: drivers ( ) . enumerate ( ) {
118
150
if item. name == "opengl" {
@@ -242,6 +274,9 @@ impl Resources {
242
274
243
275
buffer
244
276
} ;
277
+
278
+ opengl_error_check ! ( ) ;
279
+
245
280
let text_resources = TextResources :: new ( & ctx, cache_dim) ;
246
281
247
282
let mut result = Resources {
@@ -332,6 +367,11 @@ impl TextResources {
332
367
ctx. GetUniformLocation ( program, CString :: new ( "tex" ) . unwrap ( ) . as_ptr ( ) )
333
368
} ;
334
369
370
+ debug_assert ! ( pos_attr != -1 ) ;
371
+ debug_assert ! ( tex_attr != -1 ) ;
372
+ debug_assert ! ( colour_attr != -1 ) ;
373
+ debug_assert ! ( texture_uniform != -1 ) ;
374
+
335
375
TextShader {
336
376
program,
337
377
pos_attr,
@@ -352,19 +392,16 @@ impl TextResources {
352
392
ctx. TexParameteri ( gl:: TEXTURE_2D , gl:: TEXTURE_WRAP_S , gl:: CLAMP_TO_EDGE as _ ) ;
353
393
ctx. TexParameteri ( gl:: TEXTURE_2D , gl:: TEXTURE_WRAP_T , gl:: CLAMP_TO_EDGE as _ ) ;
354
394
355
- let test = vec ! [ 255u8 ; ( width * height * 4 ) as _] ;
356
-
357
395
ctx. TexImage2D (
358
396
gl:: TEXTURE_2D ,
359
397
0 ,
360
- gl:: RGBA as _ ,
398
+ gl:: RED as _ ,
361
399
width as _ ,
362
400
height as _ ,
363
401
0 ,
364
- gl:: RGBA ,
402
+ gl:: RED ,
365
403
gl:: UNSIGNED_BYTE ,
366
- // std::ptr::null() as _,
367
- test. as_ptr ( ) as _ ,
404
+ std:: ptr:: null ( ) as _ ,
368
405
) ;
369
406
370
407
texture
@@ -378,6 +415,8 @@ impl TextResources {
378
415
buffer
379
416
} ;
380
417
418
+ opengl_error_check ! ( ) ;
419
+
381
420
TextResources {
382
421
shader,
383
422
texture,
@@ -412,8 +451,7 @@ fn main() {
412
451
. build ( )
413
452
. unwrap ( ) ;
414
453
415
- let ( cache_width, cache_height) = ( 64 , 64 ) ;
416
- // let (cache_width, cache_height) = (512, 512);
454
+ let ( cache_width, cache_height) = ( 512 , 512 ) ;
417
455
418
456
let mut text_cache = rusttype:: gpu_cache:: Cache :: new ( cache_width, cache_height, 0.1 , 0.1 ) ;
419
457
@@ -423,21 +461,6 @@ fn main() {
423
461
424
462
RESOURCES = Resources :: new ( & app, ctx, canvas. window ( ) . drawable_size ( ) , ( cache_width, cache_height) )
425
463
}
426
- if cfg ! ( debug_assertions) {
427
- if let Some ( ref resources) = unsafe { RESOURCES . as_ref ( ) } {
428
- let mut err;
429
- while {
430
- err = unsafe { resources. ctx . GetError ( ) } ;
431
- err != gl:: NO_ERROR
432
- }
433
- {
434
- println ! ( "OpenGL Setup error: {}" , err) ;
435
- }
436
- if err != gl:: NO_ERROR {
437
- panic ! ( ) ;
438
- }
439
- }
440
- }
441
464
442
465
let mut state = app. new_state ( ) ;
443
466
@@ -461,21 +484,7 @@ fn main() {
461
484
462
485
app. update_and_render ( & platform, & mut state, & mut events) ;
463
486
464
- if cfg ! ( debug_assertions) {
465
- if let Some ( ref resources) = unsafe { RESOURCES . as_ref ( ) } {
466
- let mut err;
467
- while {
468
- err = unsafe { resources. ctx . GetError ( ) } ;
469
- err != gl:: NO_ERROR
470
- }
471
- {
472
- println ! ( "OpenGL First Frame error: {}" , err) ;
473
- }
474
- if err != gl:: NO_ERROR {
475
- panic ! ( ) ;
476
- }
477
- }
478
- }
487
+ opengl_error_check ! ( ) ;
479
488
480
489
if let Some ( ref resources) = unsafe { RESOURCES . as_ref ( ) } {
481
490
let window = canvas. window ( ) ;
@@ -485,15 +494,21 @@ fn main() {
485
494
let mut event_pump = sdl_context. event_pump ( ) . unwrap ( ) ;
486
495
487
496
let mut text_counter = 0 ;
488
- let mut text = "QWERTY" . to_owned ( ) ;
497
+
498
+ let mut text = "" . to_owned ( ) ;
489
499
loop {
490
500
let start = std:: time:: Instant :: now ( ) ;
491
501
502
+ unsafe {
503
+ resources. ctx . Clear (
504
+ gl:: COLOR_BUFFER_BIT | gl:: DEPTH_BUFFER_BIT | gl:: STENCIL_BUFFER_BIT ,
505
+ ) ;
506
+ }
492
507
493
- // text_counter += 1;
508
+ text_counter += 1 ;
494
509
495
510
if text_counter > 15 {
496
- // text = random_string(&mut state.rng);
511
+ text = random_string ( & mut state. rng ) ;
497
512
498
513
text_counter = 0 ;
499
514
}
@@ -503,7 +518,7 @@ fn main() {
503
518
{
504
519
let ctx = & resources. ctx ;
505
520
506
- let glyphs = layout_paragraph ( & font, Scale :: uniform ( 24 .0) , width, & text) ;
521
+ let glyphs = layout_paragraph ( & font, Scale :: uniform ( 96 .0) , width, & text) ;
507
522
for glyph in & glyphs {
508
523
text_cache. queue_glyph ( 0 , glyph. clone ( ) ) ;
509
524
}
@@ -512,9 +527,10 @@ fn main() {
512
527
unsafe {
513
528
ctx. ActiveTexture ( gl:: TEXTURE2 ) ;
514
529
ctx. BindTexture ( gl:: TEXTURE_2D , text_resources. texture ) ;
530
+
531
+ ctx. PixelStorei ( gl:: UNPACK_ALIGNMENT , 1 ) ;
515
532
}
516
533
text_cache. cache_queued ( |rect, data| {
517
- println ! ( "{:?}" , data) ;
518
534
unsafe {
519
535
ctx. TexSubImage2D (
520
536
gl:: TEXTURE_2D ,
@@ -523,18 +539,22 @@ fn main() {
523
539
rect. min . y as _ ,
524
540
rect. width ( ) as _ ,
525
541
rect. height ( ) as _ ,
526
- gl:: RGB ,
542
+ gl:: RED ,
527
543
gl:: UNSIGNED_BYTE ,
528
544
data. as_ptr ( ) as _ ,
529
- ) ;
530
-
531
-
545
+ ) ;
532
546
}
533
547
} ) . unwrap ( ) ;
534
548
549
+ unsafe {
550
+ //back to default
551
+ ctx. PixelStorei ( gl:: UNPACK_ALIGNMENT , 4 ) ;
552
+ }
553
+
554
+ opengl_error_check ! ( ) ;
535
555
let ( screen_width, screen_height) = canvas. window ( ) . drawable_size ( ) ;
536
556
537
- let colour = [ 1 .0, 0 .0, 1.0 , 1.0 ] ;
557
+ let colour = [ 0 .0, 1 .0, 1.0 , 1.0 ] ;
538
558
539
559
#[ repr( C ) ]
540
560
#[ derive( Copy , Clone , Debug ) ]
@@ -558,31 +578,31 @@ fn main() {
558
578
Vertex {
559
579
position: [ gl_rect. min. x, gl_rect. max. y] ,
560
580
tex_coords: [ uv_rect. min. x, uv_rect. max. y] ,
561
- colour: colour
581
+ colour
562
582
} ,
563
583
Vertex {
564
584
position: [ gl_rect. min. x, gl_rect. min. y] ,
565
585
tex_coords: [ uv_rect. min. x, uv_rect. min. y] ,
566
- colour: colour
586
+ colour
567
587
} ,
568
588
Vertex {
569
589
position: [ gl_rect. max. x, gl_rect. min. y] ,
570
590
tex_coords: [ uv_rect. max. x, uv_rect. min. y] ,
571
- colour: colour
591
+ colour
572
592
} ,
573
593
Vertex {
574
594
position: [ gl_rect. max. x, gl_rect. min. y] ,
575
595
tex_coords: [ uv_rect. max. x, uv_rect. min. y] ,
576
- colour: colour } ,
596
+ colour } ,
577
597
Vertex {
578
598
position: [ gl_rect. max. x, gl_rect. max. y] ,
579
599
tex_coords: [ uv_rect. max. x, uv_rect. max. y] ,
580
- colour: colour
600
+ colour
581
601
} ,
582
602
Vertex {
583
603
position: [ gl_rect. min. x, gl_rect. max. y] ,
584
604
tex_coords: [ uv_rect. min. x, uv_rect. max. y] ,
585
- colour: colour
605
+ colour
586
606
} ]
587
607
} else {
588
608
Vec :: new ( )
@@ -591,9 +611,6 @@ fn main() {
591
611
592
612
let vert_count = verts. len ( ) as gl:: types:: GLint ;
593
613
594
- // println!("{:?}", verts);
595
- // panic!();
596
-
597
614
unsafe {
598
615
let shader = & text_resources. shader ;
599
616
ctx. UseProgram ( shader. program ) ;
@@ -606,7 +623,6 @@ fn main() {
606
623
std:: mem:: transmute ( verts. as_ptr ( ) ) ,
607
624
gl:: DYNAMIC_DRAW ,
608
625
) ;
609
-
610
626
ctx. EnableVertexAttribArray ( shader. pos_attr as _ ) ;
611
627
ctx. VertexAttribPointer (
612
628
shader. pos_attr as _ ,
@@ -617,6 +633,7 @@ fn main() {
617
633
std:: ptr:: null ( ) ,
618
634
) ;
619
635
636
+ opengl_error_check ! ( ) ;
620
637
ctx. EnableVertexAttribArray ( shader. tex_attr as _ ) ;
621
638
ctx. VertexAttribPointer (
622
639
shader. tex_attr as _ ,
@@ -627,6 +644,7 @@ fn main() {
627
644
std:: ptr:: null ( ) . offset ( std:: mem:: size_of :: < [ f32 ; 2 ] > ( ) as isize ) ,
628
645
) ;
629
646
647
+ opengl_error_check ! ( ) ;
630
648
ctx. EnableVertexAttribArray ( shader. colour_attr as _ ) ;
631
649
ctx. VertexAttribPointer (
632
650
shader. colour_attr as _ ,
@@ -636,32 +654,21 @@ fn main() {
636
654
std:: mem:: size_of :: < Vertex > ( ) as _ ,
637
655
std:: ptr:: null ( ) . offset ( 2 * std:: mem:: size_of :: < [ f32 ; 2 ] > ( ) as isize ) ,
638
656
) ;
657
+ opengl_error_check ! ( ) ;
639
658
640
659
ctx. ActiveTexture ( gl:: TEXTURE2 ) ;
641
660
ctx. BindTexture ( gl:: TEXTURE_2D , text_resources. texture ) ;
642
- ctx. Uniform1i ( shader. texture_uniform , 0 ) ;
661
+ ctx. Uniform1i ( shader. texture_uniform , 2 ) ;
643
662
644
663
ctx. Clear ( gl:: STENCIL_BUFFER_BIT ) ;
645
664
646
- ctx. Enable ( gl:: STENCIL_TEST ) ;
647
- ctx. ColorMask ( gl:: FALSE , gl:: FALSE , gl:: FALSE , gl:: FALSE ) ;
648
- ctx. StencilOp ( gl:: INVERT , gl:: INVERT , gl:: INVERT ) ;
649
- ctx. StencilFunc ( gl:: ALWAYS , 0x1 , 0x1 ) ;
650
-
651
- ctx. DrawArrays ( gl:: TRIANGLE_FAN , 0 , vert_count) ;
652
-
653
- ctx. ColorMask ( gl:: TRUE , gl:: TRUE , gl:: TRUE , gl:: TRUE ) ;
654
-
655
- ctx. StencilOp ( gl:: ZERO , gl:: ZERO , gl:: ZERO ) ;
656
- ctx. StencilFunc ( gl:: EQUAL , 1 , 1 ) ;
657
- ctx. DrawArrays ( gl:: TRIANGLE_FAN , 0 , vert_count) ;
665
+ ctx. DrawArrays ( gl:: TRIANGLES , 0 , vert_count) ;
658
666
ctx. Disable ( gl:: STENCIL_TEST ) ;
667
+ ctx. BindTexture ( gl:: TEXTURE_2D , 0 ) ;
668
+ }
659
669
660
- ctx. BindTexture ( gl:: TEXTURE_2D , 0 ) ;
661
670
}
662
671
663
- }
664
-
665
672
events. clear ( ) ;
666
673
667
674
for event in event_pump. poll_iter ( ) {
@@ -677,12 +684,6 @@ fn main() {
677
684
}
678
685
}
679
686
680
- unsafe {
681
- resources. ctx . Clear (
682
- gl:: COLOR_BUFFER_BIT | gl:: DEPTH_BUFFER_BIT | gl:: STENCIL_BUFFER_BIT ,
683
- ) ;
684
- }
685
-
686
687
if app. update_and_render ( & platform, & mut state, & mut events) {
687
688
//quit requested
688
689
break ;
@@ -698,20 +699,7 @@ fn main() {
698
699
}
699
700
}
700
701
701
- if cfg ! ( debug_assertions) {
702
- let mut err;
703
- while {
704
- err = unsafe { resources. ctx . GetError ( ) } ;
705
- err != gl:: NO_ERROR
706
- }
707
- {
708
- println ! ( "OpenGL error: {}" , err) ;
709
- }
710
- if err != gl:: NO_ERROR {
711
- panic ! ( ) ;
712
- }
713
-
714
- }
702
+ opengl_error_check ! ( ) ;
715
703
716
704
window. gl_swap_window ( ) ;
717
705
@@ -865,16 +853,8 @@ fn draw_verts_with_texture(
865
853
std:: ptr:: null ( ) . offset ( start * std:: mem:: size_of :: < f32 > ( ) as isize ) ,
866
854
) ;
867
855
868
- let a = if let Some ( ref resources) = unsafe { RESOURCES . as_ref ( ) } {
869
-
870
- resources. text_resources . texture
871
- } else {
872
- 500
873
- } ;
874
-
875
856
ctx. ActiveTexture ( gl:: TEXTURE0 ) ;
876
- // ctx.BindTexture(gl::TEXTURE_2D, textures[0]);
877
- ctx. BindTexture ( gl:: TEXTURE_2D , a) ;
857
+ ctx. BindTexture ( gl:: TEXTURE_2D , textures[ 0 ] ) ;
878
858
ctx. Uniform1i ( texture_shader. texture_uniforms [ 0 ] , 0 ) ;
879
859
880
860
ctx. ActiveTexture ( gl:: TEXTURE1 ) ;
0 commit comments