Skip to content

Commit 1c66cb4

Browse files
committed
Random text in the corner
Only took forever... Signed-off-by: Ryan1729 <[email protected]>
1 parent de9fac3 commit 1c66cb4

File tree

1 file changed

+83
-103
lines changed

1 file changed

+83
-103
lines changed

src/main.rs

Lines changed: 83 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,38 @@ impl Application {
113113
}
114114
}
115115

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+
116148
fn find_sdl_gl_driver() -> Option<u32> {
117149
for (index, item) in sdl2::render::drivers().enumerate() {
118150
if item.name == "opengl" {
@@ -242,6 +274,9 @@ impl Resources {
242274

243275
buffer
244276
};
277+
278+
opengl_error_check!();
279+
245280
let text_resources = TextResources::new(&ctx, cache_dim);
246281

247282
let mut result = Resources {
@@ -332,6 +367,11 @@ impl TextResources {
332367
ctx.GetUniformLocation(program, CString::new("tex").unwrap().as_ptr())
333368
};
334369

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+
335375
TextShader {
336376
program,
337377
pos_attr,
@@ -352,19 +392,16 @@ impl TextResources {
352392
ctx.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as _);
353393
ctx.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as _);
354394

355-
let test = vec![255u8; (width * height * 4) as _];
356-
357395
ctx.TexImage2D(
358396
gl::TEXTURE_2D,
359397
0,
360-
gl::RGBA as _,
398+
gl::RED as _,
361399
width as _,
362400
height as _,
363401
0,
364-
gl::RGBA,
402+
gl::RED,
365403
gl::UNSIGNED_BYTE,
366-
// std::ptr::null() as _,
367-
test.as_ptr() as _,
404+
std::ptr::null() as _,
368405
);
369406

370407
texture
@@ -378,6 +415,8 @@ impl TextResources {
378415
buffer
379416
};
380417

418+
opengl_error_check!();
419+
381420
TextResources {
382421
shader,
383422
texture,
@@ -412,8 +451,7 @@ fn main() {
412451
.build()
413452
.unwrap();
414453

415-
let (cache_width, cache_height) = (64, 64);
416-
// let (cache_width, cache_height) = (512, 512);
454+
let (cache_width, cache_height) = (512, 512);
417455

418456
let mut text_cache = rusttype::gpu_cache::Cache::new(cache_width, cache_height, 0.1, 0.1);
419457

@@ -423,21 +461,6 @@ fn main() {
423461

424462
RESOURCES = Resources::new(&app, ctx, canvas.window().drawable_size(), (cache_width, cache_height))
425463
}
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-
}
441464

442465
let mut state = app.new_state();
443466

@@ -461,21 +484,7 @@ fn main() {
461484

462485
app.update_and_render(&platform, &mut state, &mut events);
463486

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!();
479488

480489
if let Some(ref resources) = unsafe { RESOURCES.as_ref() } {
481490
let window = canvas.window();
@@ -485,15 +494,21 @@ fn main() {
485494
let mut event_pump = sdl_context.event_pump().unwrap();
486495

487496
let mut text_counter = 0;
488-
let mut text = "QWERTY".to_owned();
497+
498+
let mut text = "".to_owned();
489499
loop {
490500
let start = std::time::Instant::now();
491501

502+
unsafe {
503+
resources.ctx.Clear(
504+
gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT,
505+
);
506+
}
492507

493-
// text_counter += 1;
508+
text_counter += 1;
494509

495510
if text_counter > 15 {
496-
// text = random_string(&mut state.rng);
511+
text = random_string(&mut state.rng);
497512

498513
text_counter = 0;
499514
}
@@ -503,7 +518,7 @@ fn main() {
503518
{
504519
let ctx = &resources.ctx;
505520

506-
let glyphs = layout_paragraph(&font, Scale::uniform(24.0), width, &text);
521+
let glyphs = layout_paragraph(&font, Scale::uniform(96.0), width, &text);
507522
for glyph in &glyphs {
508523
text_cache.queue_glyph(0, glyph.clone());
509524
}
@@ -512,9 +527,10 @@ fn main() {
512527
unsafe {
513528
ctx.ActiveTexture(gl::TEXTURE2);
514529
ctx.BindTexture(gl::TEXTURE_2D, text_resources.texture);
530+
531+
ctx.PixelStorei(gl::UNPACK_ALIGNMENT, 1);
515532
}
516533
text_cache.cache_queued(|rect, data| {
517-
println!("{:?}", data);
518534
unsafe {
519535
ctx.TexSubImage2D(
520536
gl::TEXTURE_2D,
@@ -523,18 +539,22 @@ fn main() {
523539
rect.min.y as _,
524540
rect.width() as _,
525541
rect.height() as _,
526-
gl::RGB,
542+
gl::RED,
527543
gl::UNSIGNED_BYTE,
528544
data.as_ptr() as _,
529-
);
530-
531-
545+
);
532546
}
533547
}).unwrap();
534548

549+
unsafe {
550+
//back to default
551+
ctx.PixelStorei(gl::UNPACK_ALIGNMENT, 4);
552+
}
553+
554+
opengl_error_check!();
535555
let (screen_width, screen_height) = canvas.window().drawable_size();
536556

537-
let colour = [1.0,0.0,1.0,1.0];
557+
let colour = [0.0,1.0,1.0,1.0];
538558

539559
#[repr(C)]
540560
#[derive(Copy, Clone, Debug)]
@@ -558,31 +578,31 @@ fn main() {
558578
Vertex {
559579
position: [gl_rect.min.x, gl_rect.max.y],
560580
tex_coords: [uv_rect.min.x, uv_rect.max.y],
561-
colour: colour
581+
colour
562582
},
563583
Vertex {
564584
position: [gl_rect.min.x, gl_rect.min.y],
565585
tex_coords: [uv_rect.min.x, uv_rect.min.y],
566-
colour: colour
586+
colour
567587
},
568588
Vertex {
569589
position: [gl_rect.max.x, gl_rect.min.y],
570590
tex_coords: [uv_rect.max.x, uv_rect.min.y],
571-
colour: colour
591+
colour
572592
},
573593
Vertex {
574594
position: [gl_rect.max.x, gl_rect.min.y],
575595
tex_coords: [uv_rect.max.x, uv_rect.min.y],
576-
colour: colour },
596+
colour },
577597
Vertex {
578598
position: [gl_rect.max.x, gl_rect.max.y],
579599
tex_coords: [uv_rect.max.x, uv_rect.max.y],
580-
colour: colour
600+
colour
581601
},
582602
Vertex {
583603
position: [gl_rect.min.x, gl_rect.max.y],
584604
tex_coords: [uv_rect.min.x, uv_rect.max.y],
585-
colour: colour
605+
colour
586606
}]
587607
} else {
588608
Vec::new()
@@ -591,9 +611,6 @@ fn main() {
591611

592612
let vert_count = verts.len() as gl::types::GLint;
593613

594-
// println!("{:?}", verts);
595-
// panic!();
596-
597614
unsafe {
598615
let shader = &text_resources.shader;
599616
ctx.UseProgram(shader.program);
@@ -606,7 +623,6 @@ fn main() {
606623
std::mem::transmute(verts.as_ptr()),
607624
gl::DYNAMIC_DRAW,
608625
);
609-
610626
ctx.EnableVertexAttribArray(shader.pos_attr as _);
611627
ctx.VertexAttribPointer(
612628
shader.pos_attr as _,
@@ -617,6 +633,7 @@ fn main() {
617633
std::ptr::null(),
618634
);
619635

636+
opengl_error_check!();
620637
ctx.EnableVertexAttribArray(shader.tex_attr as _);
621638
ctx.VertexAttribPointer(
622639
shader.tex_attr as _,
@@ -627,6 +644,7 @@ fn main() {
627644
std::ptr::null().offset(std::mem::size_of::<[f32; 2]>() as isize),
628645
);
629646

647+
opengl_error_check!();
630648
ctx.EnableVertexAttribArray(shader.colour_attr as _);
631649
ctx.VertexAttribPointer(
632650
shader.colour_attr as _,
@@ -636,32 +654,21 @@ fn main() {
636654
std::mem::size_of::<Vertex>() as _,
637655
std::ptr::null().offset(2 * std::mem::size_of::<[f32; 2]>() as isize),
638656
);
657+
opengl_error_check!();
639658

640659
ctx.ActiveTexture(gl::TEXTURE2);
641660
ctx.BindTexture(gl::TEXTURE_2D, text_resources.texture);
642-
ctx.Uniform1i(shader.texture_uniform, 0);
661+
ctx.Uniform1i(shader.texture_uniform, 2);
643662

644663
ctx.Clear(gl::STENCIL_BUFFER_BIT);
645664

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);
658666
ctx.Disable(gl::STENCIL_TEST);
667+
ctx.BindTexture(gl::TEXTURE_2D, 0);
668+
}
659669

660-
ctx.BindTexture(gl::TEXTURE_2D, 0);
661670
}
662671

663-
}
664-
665672
events.clear();
666673

667674
for event in event_pump.poll_iter() {
@@ -677,12 +684,6 @@ fn main() {
677684
}
678685
}
679686

680-
unsafe {
681-
resources.ctx.Clear(
682-
gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT | gl::STENCIL_BUFFER_BIT,
683-
);
684-
}
685-
686687
if app.update_and_render(&platform, &mut state, &mut events) {
687688
//quit requested
688689
break;
@@ -698,20 +699,7 @@ fn main() {
698699
}
699700
}
700701

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!();
715703

716704
window.gl_swap_window();
717705

@@ -865,16 +853,8 @@ fn draw_verts_with_texture(
865853
std::ptr::null().offset(start * std::mem::size_of::<f32>() as isize),
866854
);
867855

868-
let a = if let Some(ref resources) = unsafe { RESOURCES.as_ref() } {
869-
870-
resources.text_resources.texture
871-
} else {
872-
500
873-
};
874-
875856
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]);
878858
ctx.Uniform1i(texture_shader.texture_uniforms[0], 0);
879859

880860
ctx.ActiveTexture(gl::TEXTURE1);

0 commit comments

Comments
 (0)