1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
mod cheats;
mod config;
mod d_main;
mod shaders;
mod test_funcs;
mod timestep;
mod wipe;

use dirs::{cache_dir, data_dir};
use std::{env::set_var, error::Error, fs::File, io::Write, path::PathBuf, str::FromStr};

use d_main::d_doom_loop;
use env_logger::fmt::Color;
use gamestate::{DoomOptions, Game};
use golem::*;
use gumdrop::Options;

use crate::config::UserConfig;
use gameplay::{log, Skill};
use input::Input;
use shaders::Shaders;
use sound_sdl2::timidity::{make_timidity_cfg, GusMemSize};

use crate::log::{info, warn};
use wad::WadData;

const SOUND_DIR: &str = "room4doom/sound/";
const TIMIDITY_CFG: &str = "timidity.cfg";
const BASE_DIR: &str = "room4doom/";

#[derive(Debug, Clone, Copy)]
pub enum ShaderType {
    Basic,
    Lottes,
    Cgwg,
}

impl FromStr for ShaderType {
    type Err = std::io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "basic" => Ok(ShaderType::Basic),
            "lottes" => Ok(ShaderType::Lottes),
            "cgwg" => Ok(ShaderType::Cgwg),
            _ => Err(std::io::Error::new(
                std::io::ErrorKind::Unsupported,
                "Doh! ShaderType invalid",
            )),
        }
    }
}

/// CLI options for the game-exe
#[derive(Debug, Clone, Options)]
pub struct CLIOptions {
    #[options(
        help = "verbose level: off, error, warn, info, debug",
        default = "info"
    )]
    pub verbose: log::LevelFilter,
    #[options(no_short, meta = "", help = "path to game-exe WAD")]
    pub iwad: String,
    #[options(free, help = "path to patch WAD")]
    pub pwad: Vec<String>,
    #[options(meta = "", help = "resolution width in pixels", default = "0")]
    pub width: u32,
    #[options(meta = "", help = "resolution height in pixels", default = "0")]
    pub height: u32,
    #[options(meta = "", help = "fullscreen?")]
    pub fullscreen: Option<bool>,
    #[options(meta = "", help = "double-resolution?")]
    pub double: Option<bool>,

    #[options(help = "Disable monsters")]
    pub no_monsters: bool,
    // #[options(help = "Monsters respawn after being killed")]
    // pub respawn_parm: bool,
    // #[options(help = "Monsters move faster")]
    // pub fast_parm: bool,
    // #[options(
    //     no_short,
    //     help = "Developer mode. F1 saves a screenshot in the current working directory"
    // )]
    // pub dev_parm: bool,
    // #[options(
    //     meta = "",
    //     help = "Start a deathmatch game-exe: 1 = classic, 2 = Start a deathmatch 2.0 game-exe.  Weapons do not stay in place and all items respawn after 30 seconds"
    // )]
    // pub deathmatch: u8,
    // pub autostart: bool,
    #[options(
        meta = "",
        help = "Set the game-exe skill, 0-4 (0: easiest, 4: hardest)"
    )]
    pub skill: Skill,
    #[options(meta = "", help = "Select episode", default = "0")]
    pub episode: i32,
    #[options(meta = "", help = "Select level in episode", default = "0")]
    pub map: i32,
    #[options(help = "game-exe options help")]
    pub help: bool,

    #[options(help = "palette test, cycles through palette display")]
    pub palette_test: bool,
    #[options(meta = "", help = "image test, pass the sprite name to render")]
    pub image_test: Option<String>,
    #[options(help = "image test, cycle through the patches for texture compose")]
    pub image_cycle_test: bool,
    #[options(help = "texture compose test, cycle through the composable textures")]
    pub texture_test: bool,
    #[options(help = "flat texture test, cycle through the floor/ceiling flats")]
    pub flats_test: bool,
    #[options(help = "sprite test, cycle through the sprites")]
    pub sprites_test: bool,

    #[options(meta = "", help = "Screen shader <basic, cgwg, lottes>")]
    pub shader: Option<Shaders>,
}

impl From<CLIOptions> for DoomOptions {
    fn from(g: CLIOptions) -> Self {
        DoomOptions {
            iwad: g.iwad,
            pwad: g.pwad,
            no_monsters: g.no_monsters,
            // respawn_parm: g.respawn_parm,
            // fast_parm: g.fast_parm,
            // dev_parm: g.dev_parm,
            // deathmatch: g.deathmatch,
            // autostart: g.autostart,
            skill: g.skill,
            episode: g.episode,
            map: g.map,
            warp: g.map != 0 || g.episode != 0,
            hi_res: g.double.unwrap_or(true),
            verbose: g.verbose,
            ..DoomOptions::default()
        }
    }
}

fn setup_timidity(wad: &WadData) {
    if let Some(mut path) = data_dir() {
        path.push(SOUND_DIR);
        if path.exists() {
            let mut cache_dir = cache_dir().unwrap_or_else(|| PathBuf::from("/tmp"));
            cache_dir.push(TIMIDITY_CFG);
            if let Some(cfg) = make_timidity_cfg(wad, path, GusMemSize::Perfect) {
                let mut file = File::create(cache_dir.as_path()).unwrap();
                file.write_all(&cfg).unwrap();
                set_var("SDL_MIXER_DISABLE_FLUIDSYNTH", "1");
                set_var("TIMIDITY_CFG", cache_dir.as_path());
                info!("Using timidity for sound");
            } else {
                warn!("Sound fonts were missing, using fluidsynth instead");
            }
        } else {
            info!("No sound fonts installed to {:?}", path);
            info!("Using fluidsynth for sound");
        }
    }
}

/// The main `game-exe` crate should take care of initialising a few things
fn main() -> Result<(), Box<dyn Error>> {
    let mut options = CLIOptions::parse_args_default_or_exit();

    let mut logger = env_logger::Builder::new();
    logger
        .target(env_logger::Target::Stdout)
        .format(move |buf, record| {
            let mut style = buf.style();
            let colour = match record.level() {
                log::Level::Error => Color::Red,
                log::Level::Warn => Color::Yellow,
                log::Level::Info => Color::Green,
                log::Level::Debug => Color::Magenta,
                log::Level::Trace => Color::Magenta,
            };
            style.set_color(colour);

            if options.verbose == log::Level::Debug {
                writeln!(
                    buf,
                    "{}: {}: {}",
                    style.value(record.level()),
                    record.target(),
                    record.args()
                )
            } else {
                //record.target().split("::").last().unwrap_or("")
                writeln!(buf, "{}: {}", style.value(record.level()), record.args())
            }
        })
        .filter(None, options.verbose)
        .init();

    let mut user_config = UserConfig::load();

    let sdl_ctx = sdl2::init()?;
    let snd_ctx = sdl_ctx.audio()?;
    let video_ctx = sdl_ctx.video()?;

    let events = sdl_ctx.event_pump()?;
    let input = Input::new(events);

    user_config.sync_cli(&mut options);
    user_config.write();

    let mut window = video_ctx
        .window("ROOM for DOOM", options.width, options.height)
        .allow_highdpi()
        .position_centered()
        .opengl()
        .build()?;
    let _gl_ctx = window.gl_create_context()?;

    let context = unsafe {
        Context::from_glow(glow::Context::from_loader_function(|s| {
            video_ctx.gl_get_proc_address(s) as *const _
        }))
        .unwrap()
    };

    let wad = WadData::new(options.iwad.clone().into());
    setup_timidity(&wad);

    let game = Game::new(
        options.clone().into(),
        wad,
        snd_ctx,
        user_config.sfx_vol,
        user_config.mus_vol,
    );

    if let Some(fullscreen) = options.fullscreen {
        if fullscreen {
            let mode = if matches!(options.width, 320 | 640) {
                info!("Fullscreen mode is 320x200 or 640x400");
                sdl2::video::FullscreenType::Desktop
            } else {
                warn!("Fullscreen resolution isn't 320x200 or 640x400. Image will be stretched.");
                sdl2::video::FullscreenType::True
            };
            window.set_fullscreen(mode)?;
        }
    }
    window.show();

    sdl_ctx.mouse().show_cursor(false);
    sdl_ctx.mouse().set_relative_mouse_mode(true);
    sdl_ctx.mouse().capture(true);

    d_doom_loop(game, input, window, context, options)?;
    Ok(())
}