Skip to content

Commit

Permalink
Merge fullscreen moder querying into window-test example
Browse files Browse the repository at this point in the history
  • Loading branch information
crumblingstatue committed Oct 22, 2024
1 parent 206381b commit 187c1e9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ required-features = ["graphics"]
name = "opengl"
required-features = ["graphics"]

[[example]]
name = "video_modes"
required-features = ["window"]

[[example]]
name = "joystick"
required-features = ["window"]
Expand Down
12 changes: 0 additions & 12 deletions examples/video_modes.rs

This file was deleted.

63 changes: 51 additions & 12 deletions examples/window-test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use sfml::{
graphics::{Color, Font, RenderTarget, RenderWindow, Text, Transformable},
window::{ContextSettings, Event, Key, Style},
window::{ContextSettings, Event, Key, Style, VideoMode},
SfResult,
};

Expand All @@ -14,28 +14,29 @@ fn main() -> SfResult<()> {
let configs = [
WindowConfig {
mode: (320, 240),
title: "Retro",
title: "(Windowed) Retro",
style: Style::CLOSE,
},
WindowConfig {
mode: (640, 480),
title: "Classic",
title: "(Windowed) Classic",
style: Style::DEFAULT,
},
WindowConfig {
mode: (800, 600),
title: "Big",
title: "(Windowed) Big",
style: Style::TITLEBAR,
},
];
let mut cfg_idx = 0usize;
let mut cfg_idx = 2usize;
let mut rw = RenderWindow::new(
configs[cfg_idx].mode,
"Window test",
Style::CLOSE,
&ContextSettings::default(),
)?;
let font = Font::from_memory_static(include_bytes!("resources/sansation.ttf"))?;
let fs_modes = VideoMode::fullscreen_modes();

while rw.is_open() {
while let Some(ev) = rw.poll_event() {
Expand All @@ -44,16 +45,26 @@ fn main() -> SfResult<()> {
Event::KeyPressed { code, .. } => match code {
Key::Up => cfg_idx = cfg_idx.saturating_sub(1),
Key::Down => {
if cfg_idx + 1 < configs.len() {
if cfg_idx + 1 < configs.len() + fs_modes.len() {
cfg_idx += 1
}
}
Key::Enter => rw.recreate(
configs[cfg_idx].mode,
configs[cfg_idx].title,
configs[cfg_idx].style,
&ContextSettings::default(),
),
Key::Enter => match configs.get(cfg_idx) {
Some(cfg) => {
rw.recreate(cfg.mode, cfg.title, cfg.style, &ContextSettings::default())
}
None => match fs_modes.get(cfg_idx - configs.len()) {
Some(mode) => rw.recreate(
*mode,
"Fullscreen",
Style::FULLSCREEN,
&ContextSettings::default(),
),
None => {
eprintln!("Invalid index: {cfg_idx}");
}
},
},
_ => {}
},
_ => {}
Expand All @@ -79,6 +90,34 @@ fn main() -> SfResult<()> {
rw.draw(&txt);
y += fontsize as f32;
}
let mut i = configs.len();
y += fontsize as f32;
txt.set_position((0., y));
txt.set_fill_color(Color::WHITE);
txt.set_string("= Fullscreen modes =");
rw.draw(&txt);
for mode in fs_modes.iter() {
let n_rows = 23;
let column = i / n_rows;
let row = i % n_rows;
let fc = if i == cfg_idx {
Color::YELLOW
} else {
Color::WHITE
};
txt.set_fill_color(fc);
let left_pad = 16.0;
let x = left_pad + (column * 128) as f32;
let gap = 16.0;
let y = y + gap + (row * fontsize as usize) as f32;
txt.set_position((x, y));
txt.set_string(&format!(
"{}x{}x{}",
mode.width, mode.height, mode.bits_per_pixel
));
rw.draw(&txt);
i += 1;
}
rw.display();
}
Ok(())
Expand Down

0 comments on commit 187c1e9

Please sign in to comment.