11 files changed, 175 insertions(+), 149 deletions(-)
@@ -70,17 +70,17 @@
},
_ => {}
},
GameState::Paused => match keycode {
Keycode::Q => break 'running,
_ => {}
},
GameState::GameOver => match keycode {
Keycode::R => {
GameState::Paused => {
if keycode == Keycode::Q {
break 'running;
}
}
GameState::GameOver => {
if keycode == Keycode::R {
self.change_state(GameState::Playing);
self.game_context.restart();
}
_ => {}
},
}
},
_ => {}
}
@@ -1,7 +1,7 @@
use rand::Rng;
use std::ops::Add;
use crate::{renderer::Renderer};
use crate::renderer::Renderer;
#[derive(Copy, Clone, PartialEq)]
pub enum GameState {
@@ -3,7 +3,7 @@
mod app;
mod game_context;
mod renderer;
mod ui_components;
mod ui;
fn main() -> Result<(), String> {
let mut app: App = App::new()?;
@@ -1,8 +1,8 @@
use sdl2::{pixels::Color, rect::Rect, ttf};
use crate::{
app::App,
game_context::{GameContext, GameState, Point}, ui_components::Ui,
game_context::{GameContext, GameState, Point},
ui::Ui,
};
pub struct Renderer {
@@ -47,7 +47,10 @@
}
pub fn get_center(&mut self) -> (i32, i32) {
(((Renderer::GRID_X_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32, ((Renderer::GRID_Y_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32)
(
((Renderer::GRID_X_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32,
((Renderer::GRID_Y_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32,
)
}
pub fn draw(&mut self, game_context: &GameContext) -> Result<(), String> {
@@ -1,0 +1,60 @@
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, TextureQuery};
use sdl2::ttf;
use sdl2::video::Window;
use crate::game_context::GameEvent;
pub struct Button {
pub rect: Rect,
pub color: Color,
pub text: String,
pub event: GameEvent,
}
impl Button {
pub fn new(x: i32, y: i32, width: u32, height: u32, text: &str, event: GameEvent) -> Self {
Self {
rect: Rect::new(x, y, width, height),
color: Color::RGB(0, 120, 215),
text: text.to_string(),
event,
}
}
pub fn draw(
&mut self,
canvas: &mut Canvas<Window>,
font: &ttf::Font<'static, 'static>,
) -> Result<(), String> {
canvas.set_draw_color(self.color);
canvas.fill_rect(self.rect)?;
let surface = font
.render(&self.text)
.blended(Color::RGB(255, 255, 255))
.map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();
let texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string())?;
let TextureQuery { width, height, .. } = texture.query();
let text_rect = Rect::new(
self.rect.x() + (self.rect.width() as i32 - width as i32) / 2,
self.rect.y() + (self.rect.height() as i32 - height as i32) / 2,
width,
height,
);
canvas.copy(&texture, None, Some(text_rect))?;
Ok(())
}
pub fn is_clicked(&self, mouse_x: i32, mouse_y: i32) -> bool {
self.rect.contains_point((mouse_x, mouse_y))
}
}
@@ -1,0 +1,55 @@
use sdl2::{pixels::Color, rect::Rect};
use crate::{
game_context::{GameEvent, Point},
ui::{button::Button, text::Text},
};
pub mod button;
pub mod text;
pub struct Ui {
pub buttons: Vec<Button>,
pub texts: Vec<Text>,
}
impl Ui {
pub fn new() -> Self {
Self {
buttons: vec![],
texts: vec![],
}
}
pub fn button(
&mut self,
x: i32,
y: i32,
width: u32,
height: u32,
text: &str,
event: GameEvent,
) -> &mut Button {
self.buttons.push(Button {
rect: Rect::new(x, y, width, height),
color: Color::RGB(0, 120, 215),
text: text.to_string(),
event,
});
self.buttons.last_mut().unwrap()
}
pub fn text(&mut self, text: &str, color: Color, point: Point) -> &mut Text {
self.texts.push(Text {
text: text.to_string(),
color,
point,
});
self.texts.last_mut().unwrap()
}
pub fn clear(&mut self) {
self.buttons.clear();
self.texts.clear();
}
}
@@ -1,0 +1,44 @@
use sdl2::{pixels::Color, rect::Rect, render::Canvas, ttf, video::Window};
use crate::game_context::Point;
pub struct Text {
pub text: String,
pub color: Color,
pub point: Point,
}
impl Text {
pub fn new(text: &str, color: Color, point: Point) -> Self {
Self {
text: text.to_string(),
color,
point,
}
}
pub fn draw(
&mut self,
canvas: &mut Canvas<Window>,
font: &ttf::Font<'static, 'static>,
) -> Result<(), String> {
let surface = font
.render(&self.text)
.blended(self.color)
.map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();
let texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string())?;
let target = Rect::new(
self.point.0,
self.point.1,
surface.width(),
surface.height(),
);
canvas.copy(&texture, None, Some(target))?;
Ok(())
}
}
@@ -1,56 +1,0 @@
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, TextureQuery};
use sdl2::ttf;
use sdl2::video::Window;
use crate::game_context::GameEvent;
pub struct Button {
pub rect: Rect,
pub color: Color,
pub text: String,
pub event: GameEvent
}
impl Button {
pub fn new(x: i32, y: i32, width: u32, height: u32, text: &str, event: GameEvent) -> Self {
Self {
rect: Rect::new(x, y, width, height),
color: Color::RGB(0, 120, 215),
text: text.to_string(),
event,
}
}
pub fn draw(&mut self, canvas: &mut Canvas<Window>, font: &ttf::Font<'static, 'static>) -> Result<(), String> {
canvas.set_draw_color(self.color);
canvas.fill_rect(self.rect)?;
let surface = font
.render(&self.text)
.blended(Color::RGB(255, 255, 255))
.map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();
let texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string())?;
let TextureQuery { width, height, .. } = texture.query();
let text_rect = Rect::new(
self.rect.x() + (self.rect.width() as i32 - width as i32) / 2,
self.rect.y() + (self.rect.height() as i32 - height as i32) / 2,
width,
height,
);
canvas.copy(&texture, None, Some(text_rect))?;
Ok(())
}
pub fn is_clicked(&self, mouse_x: i32, mouse_y: i32) -> bool {
self.rect.contains_point((mouse_x, mouse_y))
}
}
@@ -1,45 +1,0 @@
use sdl2::{pixels::Color, rect::Rect};
use crate::{game_context::{GameEvent, Point}, ui_components::{button::Button, text::Text}};
pub mod button;
pub mod text;
pub struct Ui {
pub buttons: Vec<Button>,
pub texts: Vec<Text>,
}
impl Ui {
pub fn new() -> Self {
Self {
buttons: vec![],
texts: vec![],
}
}
pub fn button(&mut self, x: i32, y: i32, width: u32, height: u32, text: &str, event: GameEvent) -> &mut Button {
self.buttons.push(Button {
rect: Rect::new(x, y, width, height),
color: Color::RGB(0, 120, 215),
text: text.to_string(),
event
});
self.buttons.last_mut().unwrap()
}
pub fn text(&mut self, text: &str, color: Color, point: Point) -> &mut Text {
self.texts.push(Text {
text: text.to_string(),
color,
point
});
self.texts.last_mut().unwrap()
}
pub fn clear(&mut self) {
self.buttons.clear();
self.texts.clear();
}
}
@@ -1,35 +1,0 @@
use sdl2::{pixels::Color, rect::Rect, ttf, video::Window, render::Canvas};
use crate::{game_context::Point};
pub struct Text {
pub text: String,
pub color: Color,
pub point: Point,
}
impl Text {
pub fn new(text: &str, color: Color, point: Point) -> Self {
Self {
text: text.to_string(),
color,
point,
}
}
pub fn draw(&mut self, canvas: &mut Canvas<Window>, font: &ttf::Font<'static, 'static>) -> Result<(), String> {
let surface = font
.render(&self.text)
.blended(self.color)
.map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();
let texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string())?;
let target = Rect::new(self.point.0, self.point.1, surface.width(), surface.height());
canvas.copy(&texture, None, Some(target))?;
Ok(())
}
}