Skip to content

Commit

Permalink
Console toggling now works correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cormac-obrien committed May 9, 2018
1 parent d03aaf2 commit 4efa19e
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 171 deletions.
200 changes: 105 additions & 95 deletions Cargo.lock

Large diffs are not rendered by default.

80 changes: 69 additions & 11 deletions src/bin/quake-client/game.rs
Expand Up @@ -18,15 +18,15 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::rc::Rc;

use richter::client::Client;
use richter::client::input::{Input, InputFocus};
use richter::client::render::{self, GraphicsPackage, PipelineState2d, pipe, SceneRenderer};
use richter::client::render::hud::HudRenderer;
use richter::common::math;
use richter::common::console::CvarRegistry;
use richter::common::console::{CmdRegistry, CvarRegistry};
use richter::common::net::SignOnStage;
use richter::common::pak::Pak;

Expand All @@ -37,6 +37,7 @@ use gfx::{CommandBuffer, Encoder};
use gfx_device_gl::Resources;
use glutin::WindowEvent;

#[derive(Clone, Copy)]
enum InGameFocus {
// active in game
Game,
Expand All @@ -49,9 +50,51 @@ enum InGameFocus {
}

struct InGameState {
cmds: Rc<RefCell<CmdRegistry>>,
renderer: SceneRenderer,
hud_renderer: HudRenderer,
focus: InGameFocus,
focus: Rc<Cell<InGameFocus>>,
}

impl InGameState {
pub fn new(
cmds: Rc<RefCell<CmdRegistry>>,
scene_renderer: SceneRenderer,
hud_renderer: HudRenderer,
focus: InGameFocus
) -> InGameState {
let focus_rc = Rc::new(Cell::new(focus));
let toggleconsole_focus = focus_rc.clone();

cmds.borrow_mut().insert("toggleconsole", Box::new(move |_| {
match toggleconsole_focus.get() {
InGameFocus::Game => {
println!("toggleconsole: ON");
toggleconsole_focus.set(InGameFocus::Console);
}

InGameFocus::Console => {
println!("toggleconsole: OFF");
toggleconsole_focus.set(InGameFocus::Game);
}

InGameFocus::Menu => (),
}
})).unwrap();

InGameState {
cmds,
renderer: scene_renderer,
hud_renderer,
focus: focus_rc,
}
}
}

impl ::std::ops::Drop for InGameState {
fn drop(&mut self) {
// TODO: delete toggleconsole from cmds
}
}

enum GameState {
Expand All @@ -65,6 +108,7 @@ enum GameState {
pub struct Game {
pak: Rc<Pak>,
cvars: Rc<RefCell<CvarRegistry>>,
cmds: Rc<RefCell<CmdRegistry>>,
gfx_pkg: Rc<RefCell<GraphicsPackage>>,
state: GameState,
input: Rc<RefCell<Input>>,
Expand All @@ -75,13 +119,15 @@ impl Game {
pub fn new(
pak: Rc<Pak>,
cvars: Rc<RefCell<CvarRegistry>>,
cmds: Rc<RefCell<CmdRegistry>>,
gfx_pkg: Rc<RefCell<GraphicsPackage>>,
input: Rc<RefCell<Input>>,
client: Client
) -> Result<Game, Error> {
Ok(Game {
pak,
cvars,
cmds,
gfx_pkg,
state: GameState::Loading,
input,
Expand Down Expand Up @@ -109,14 +155,14 @@ impl Game {
&mut self.gfx_pkg.borrow_mut(),
).unwrap();

// TODO: HUD renderer
let hud_renderer = HudRenderer::new(self.gfx_pkg.clone()).unwrap();

self.state = GameState::InGame(InGameState {
self.state = GameState::InGame(InGameState::new(
self.cmds.clone(),
renderer,
hud_renderer,
focus: InGameFocus::Game,
});
InGameFocus::Game,
));
}
}
}
Expand All @@ -128,15 +174,15 @@ impl Game {

GameState::InGame(ref state) => {
// set the proper focus
match state.focus {
match state.focus.get() {
InGameFocus::Game => self.input.borrow_mut().set_focus(InputFocus::Game).unwrap(),
InGameFocus::Menu => self.input.borrow_mut().set_focus(InputFocus::Menu).unwrap(),
InGameFocus::Console => self.input.borrow_mut().set_focus(InputFocus::Console).unwrap(),
}
}
}

self.input.borrow_mut().handle_event(event);
self.input.borrow_mut().handle_event(event).unwrap();
}

pub fn render<C>(
Expand Down Expand Up @@ -188,12 +234,24 @@ impl Game {
display_height,
).unwrap();

match state.focus {
match state.focus.get() {
// don't need to render anything else
InGameFocus::Game => (),

// render the console
InGameFocus::Console => unimplemented!(),
InGameFocus::Console => {
let mut data = self.gfx_pkg.borrow().gen_user_data_2d();

self.gfx_pkg.borrow().console_renderer().render(
encoder,
self.gfx_pkg.borrow().pipeline_2d(),
&mut data,
display_width,
display_height,
1.0,
1.0,
).unwrap();
},

// render the menu
InGameFocus::Menu => unimplemented!(),
Expand Down
37 changes: 6 additions & 31 deletions src/bin/quake-client/main.rs
Expand Up @@ -88,13 +88,11 @@ struct ClientProgram {
device: RefCell<Device>,
encoder: RefCell<Encoder<Resources, CommandBuffer>>,
data: RefCell<render::pipe::Data<Resources>>,
data_2d: RefCell<render::pipeline2d::Data<Resources>>,

endpoint: Rc<Endpoint>,

state: RefCell<ProgramState>,
input: Rc<RefCell<Input>>,
ui_renderer: Rc<RefCell<UiRenderer>>,
}

impl ClientProgram {
Expand Down Expand Up @@ -153,15 +151,7 @@ impl ClientProgram {
gfx::texture::WrapMode::Tile,
));

let mut data = render::pipe::Data {
vertex_buffer: factory.create_vertex_buffer(&[]),
transform: Matrix4::identity().into(),
sampler: (dummy_texture.clone(), sampler.clone()),
out_color: color.clone(),
out_depth: depth.clone(),
};

let mut data_2d = render::pipeline2d::Data {
let data = render::pipe::Data {
vertex_buffer: factory.create_vertex_buffer(&[]),
transform: Matrix4::identity().into(),
sampler: (dummy_texture.clone(), sampler.clone()),
Expand All @@ -173,25 +163,12 @@ impl ClientProgram {

let endpoint = Rc::new(rodio::get_endpoints_list().next().unwrap());

let palette = render::Palette::load(&pak, "gfx/palette.lmp");

let gfx_wad = Wad::load(pak.open("gfx.wad").unwrap()).unwrap();
let ui_renderer = Rc::new(RefCell::new(UiRenderer::new(
&gfx_wad,
&palette,
&mut factory,
console.clone(),
).unwrap()));

let glyph_renderer = GlyphRenderer::new(&mut factory, &gfx_wad.open_conchars().unwrap(), &palette).unwrap();

let gfx_pkg = Rc::new(RefCell::new(GraphicsPackage::new(
palette,
gfx_wad,
glyph_renderer,
&pak,
factory,
color,
depth,
console.clone(),
)));

ClientProgram {
Expand All @@ -205,11 +182,9 @@ impl ClientProgram {
device: RefCell::new(device),
encoder: RefCell::new(encoder),
data: RefCell::new(data),
data_2d: RefCell::new(data_2d),
endpoint,
state: RefCell::new(ProgramState::Title),
input,
ui_renderer,
}
}

Expand All @@ -231,17 +206,17 @@ impl ClientProgram {
self.state.replace(ProgramState::Game(Game::new(
self.pak.clone(),
self.cvars.clone(),
self.cmds.clone(),
self.gfx_pkg.clone(),
self.input.clone(),
cl,
).unwrap()));
}

fn render(&mut self) {
self.encoder.borrow_mut().clear(&self.data.borrow().out_color, [0.0, 0.0, 0.0, 1.0]);
self.encoder.borrow_mut().clear_depth(&self.data.borrow().out_depth, 1.0);
self.encoder.borrow_mut().clear(&self.gfx_pkg.borrow().color_target(), [0.0, 0.0, 0.0, 1.0]);
self.encoder.borrow_mut().clear_depth(&self.gfx_pkg.borrow().depth_stencil(), 1.0);
let (win_w, win_h) = self.window.borrow().get_inner_size().unwrap();
let aspect = win_w as f32 / win_h as f32;

match *self.state.borrow_mut() {
ProgramState::Title => unimplemented!(),
Expand Down
5 changes: 3 additions & 2 deletions src/client/input/console.rs
Expand Up @@ -21,7 +21,7 @@ use std::rc::Rc;
use common::console::Console;

use failure::Error;
use winit::{VirtualKeyCode as Key, KeyboardInput, WindowEvent};
use winit::{ElementState, VirtualKeyCode as Key, KeyboardInput, WindowEvent};

pub struct ConsoleInput {
console: Rc<RefCell<Console>>,
Expand All @@ -41,14 +41,15 @@ impl ConsoleInput {
WindowEvent::ReceivedCharacter(c) => self.console.borrow_mut().send_char(c)?,

WindowEvent::KeyboardInput {
input: KeyboardInput { virtual_keycode: Some(key), .. },
input: KeyboardInput { virtual_keycode: Some(key), state: ElementState::Pressed, .. },
..
} => {
match key {
Key::Up => self.console.borrow_mut().history_up(),
Key::Down => self.console.borrow_mut().history_down(),
Key::Left => self.console.borrow_mut().cursor_left(),
Key::Right => self.console.borrow_mut().cursor_right(),
Key::Grave => self.console.borrow_mut().stuff_text("toggleconsole\n"),
_ => (),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/input/game.rs
Expand Up @@ -349,7 +349,7 @@ impl GameInput {
debug!("{}{}", if state == trigger { '+' } else { '-' }, action.to_string());
}

BindTarget::ConsoleInput { ref text } => {
BindTarget::ConsoleInput { ref text } => if state == ElementState::Pressed {
self.console.borrow_mut().stuff_text(text);
}
}
Expand Down
18 changes: 3 additions & 15 deletions src/client/render/console.rs
Expand Up @@ -62,7 +62,7 @@ impl ConsoleRenderer {
ensure!(alpha >= 0.0 && alpha <= 1.0, "alpha must be between 0 and 1");

// determine how far down the screen we should start drawing
let y_min = (proportion * display_height as f32) as i32;
let y_min = ((1.0 - proportion) * display_height as f32) as i32;

// draw version string
// TODO: get this dynamically
Expand All @@ -78,21 +78,9 @@ impl ConsoleRenderer {
y_min,
)?;

for (chr_id, chr) in version_string.chars().enumerate() {
self.glyph_renderer.render_glyph(
encoder,
pso,
user_data,
chr as u8,
display_width,
display_height,
display_width as i32 - GLYPH_WIDTH as i32 * (version_string.len() - chr_id) as i32,
y_min,
)?;
}

// draw input line
// TODO: draw input line

// draw output
for (line_id, line) in self.console.borrow().output_lines().enumerate() {
for (chr_id, chr) in line.iter().enumerate() {
let mut c = *chr;
Expand Down
12 changes: 1 addition & 11 deletions src/client/render/hud.rs
Expand Up @@ -278,16 +278,6 @@ impl HudRenderer {
}
}

fn gen_user_data(&self) -> PipelineData2d {
PipelineData2d {
vertex_buffer: self.gfx_pkg.borrow().quad_vertex_buffer(),
transform: Matrix4::identity().into(),
sampler: (self.gfx_pkg.borrow().dummy_diffuse_texture(), self.gfx_pkg.borrow().sampler()),
out_color: self.gfx_pkg.borrow().color_target(),
out_depth: self.gfx_pkg.borrow().depth_stencil(),
}
}

pub fn render<C>(
&mut self,
encoder: &mut Encoder<Resources, C>,
Expand All @@ -298,7 +288,7 @@ impl HudRenderer {
where
C: CommandBuffer<Resources>,
{
let mut user_data = self.gen_user_data();
let mut user_data = self.gfx_pkg.borrow().gen_user_data_2d();

// TODO: scale using a cvar (`r_hudscale` or something)
let display_width = display_width / 2;
Expand Down

0 comments on commit 4efa19e

Please sign in to comment.