Render the board in the background

This commit is contained in:
Savanni D'Gerinel 2024-03-31 19:45:22 -04:00
parent 0a62c96b0f
commit 32391a46e7
1 changed files with 47 additions and 31 deletions

View File

@ -37,14 +37,16 @@ You should have received a copy of the GNU General Public License along with On
use crate::perftrace; use crate::perftrace;
use gio::resources_lookup_data;
use glib::Object; use glib::Object;
use gtk::{ use gtk::{
gdk_pixbuf::{InterpType, Pixbuf},
prelude::*, prelude::*,
subclass::prelude::*, subclass::prelude::*,
}; };
use image::{io::Reader as ImageReader, ImageError};
use otg_core::{Color, Coordinate}; use otg_core::{Color, Coordinate};
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, io::Cursor, rc::Rc};
const WIDTH: i32 = 800; const WIDTH: i32 = 800;
const HEIGHT: i32 = 800; const HEIGHT: i32 = 800;
@ -105,29 +107,7 @@ impl Goban {
fn redraw(&self, ctx: &cairo::Context, width: i32, height: i32) { fn redraw(&self, ctx: &cairo::Context, width: i32, height: i32) {
println!("{} x {}", width, height); println!("{} x {}", width, height);
/* let background = load_pixbuf("/com/luminescent-dreams/otg-gtk/wood_texture.jpg", WIDTH+40, HEIGHT+40);
let wood_texture = resources_lookup_data(
"/com/luminescent-dreams/otg-gtk/wood_texture.jpg",
gio::ResourceLookupFlags::NONE,
)
.unwrap();
let background = ImageReader::new(Cursor::new(wood_texture))
.with_guessed_format()
.unwrap()
.decode();
let background = background.map(|background| {
Pixbuf::from_bytes(
&glib::Bytes::from(background.as_bytes()),
gtk::gdk_pixbuf::Colorspace::Rgb,
false,
8,
background.width() as i32,
background.height() as i32,
background.to_rgb8().sample_layout().height_stride as i32,
)
.scale_simple(WIDTH, HEIGHT, InterpType::Nearest)
});
match background { match background {
Ok(Some(ref background)) => { Ok(Some(ref background)) => {
@ -136,10 +116,6 @@ impl Goban {
} }
Ok(None) | Err(_) => ctx.set_source_rgb(0.7, 0.7, 0.7), Ok(None) | Err(_) => ctx.set_source_rgb(0.7, 0.7, 0.7),
} }
*/
ctx.set_source_rgb(0.7, 0.7, 0.7);
let _ = ctx.paint();
let board = self.imp().board_state.borrow(); let board = self.imp().board_state.borrow();
@ -189,7 +165,7 @@ impl Goban {
(0..board.size.height).for_each(|row| { (0..board.size.height).for_each(|row| {
(0..board.size.width).for_each(|column| { (0..board.size.width).for_each(|column| {
match board.stone(&Coordinate { row, column }) { match board.stone(&Coordinate { row, column }) {
None => {}, None => {}
Some(Color::White) => pen.stone(ctx, row, column, Color::White, None), Some(Color::White) => pen.stone(ctx, row, column, Color::White, None),
Some(Color::Black) => pen.stone(ctx, row, column, Color::Black, None), Some(Color::Black) => pen.stone(ctx, row, column, Color::Black, None),
} }
@ -206,6 +182,25 @@ struct Pen {
} }
impl Pen { impl Pen {
/*
fn new(x_offset: f64, y_offset: f64, hspace_between: f64, vspace_between: f64) -> Self {
/*
let black_stone = resources_lookup_data(
"/com/luminescent-dreams/otg-gtk/black_stone.png",
gio::ResourceLookupFlags::NONE,
)
.unwrap();
*/
Pen {
x_offset,
y_offset,
hspace_between,
vspace_between,
}
}
*/
fn star_point(&self, context: &cairo::Context, row: u8, col: u8) { fn star_point(&self, context: &cairo::Context, row: u8, col: u8) {
context.arc( context.arc(
self.x_offset + (col as f64) * self.hspace_between, self.x_offset + (col as f64) * self.hspace_between,
@ -263,3 +258,24 @@ impl Pen {
) )
} }
} }
fn load_pixbuf(path: &str, width: i32, height: i32) -> Result<Option<Pixbuf>, ImageError> {
let wood_texture = resources_lookup_data(path, gio::ResourceLookupFlags::NONE).unwrap();
let background = ImageReader::new(Cursor::new(wood_texture))
.with_guessed_format()
.unwrap()
.decode();
background.map(|background| {
Pixbuf::from_bytes(
&glib::Bytes::from(background.as_bytes()),
gtk::gdk_pixbuf::Colorspace::Rgb,
false,
8,
background.width() as i32,
background.height() as i32,
background.to_rgb8().sample_layout().height_stride as i32,
)
.scale_simple(width, height, InterpType::Nearest)
})
}