This commit is contained in:
2025-10-29 17:49:14 -06:00
parent 05198c716d
commit 970d021a9a
13 changed files with 318 additions and 0 deletions

12
user-apps/doom/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "doom"
version = "0.1.0"
edition = "2024"
[dependencies]
abi = { path = "../../abi" }
embedded-graphics = "0.8.1"
[build-dependencies]
bindgen = "0.71.0"
cc = "1.0"

70
user-apps/doom/build.rs Normal file
View File

@@ -0,0 +1,70 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("../memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rustc-link-arg-bins=-Tmemory.x");
let ref dg_src_dir = std::path::PathBuf::from("doomgeneric/doomgeneric");
let mut dg_c_paths = vec![];
let mut dg_h_paths = vec![];
// Find most c and h files
for entry in std::fs::read_dir(dg_src_dir).unwrap() {
let entry = entry.unwrap();
if let Some(filename) = entry.file_name().to_str() {
if filename.starts_with("doomgeneric")
|| filename.contains("_allegro")
|| filename.contains("_sdl")
|| filename == "i_main.c"
{
continue;
}
if filename.ends_with(".h") {
dg_h_paths.push(dg_src_dir.join(filename));
} else if filename.ends_with(".c") {
dg_c_paths.push(dg_src_dir.join(filename));
}
}
}
dg_c_paths
.iter()
.chain(dg_h_paths.iter())
.for_each(|path| println!("cargo:rerun-if-changed={}", path.to_str().unwrap()));
cc::Build::new()
.flag("-w") // silence warnings
.flag("-Os") // optimize for size
.define("CMAP256", None)
.define("DOOMGENERIC_RESX", Some("320"))
.define("DOOMGENERIC_RESY", Some("200"))
.define("MAXPLAYERS", Some("1"))
.flag_if_supported("-std=gnu89") // old-style C, allows implicit int
.flag("-Wno-implicit-function-declaration") // ignore missing prototypes
.define("_POSIX_C_SOURCE", Some("200809L"))
.files(dg_c_paths)
.include(".")
.compile("doomgeneric");
}

View File

@@ -0,0 +1,4 @@
#ifndef INTTYPES_H
#define INTTYPES_H
#endif

4
user-apps/doom/limits.h Normal file
View File

@@ -0,0 +1,4 @@
#ifndef STRINGS_H
#define STRINGS_H
#endif

View File

@@ -0,0 +1,55 @@
const X: usize = 320;
const Y: usize = 200;
const SIZE: usize = X * Y;
pub static mut DISPLAY: Option<SimulatorDisplay<Rgb565>> = None;
pub static mut SCREEN_BUFFER: ScreenBuffer<X, Y, SIZE> = ScreenBuffer::new();
pub static mut START: Option<Instant> = None;
#[unsafe(no_mangle)]
extern "C" fn DG_DrawFrame() {
let palette565: [Rgb565; 256] = unsafe {
colors.map(|c| {
Rgb565::new(
((c.r as u16 * 31) / 255) as u8, // red 5 bits
((c.g as u16 * 63) / 255) as u8, // green 6 bits
((c.b as u16 * 31) / 255) as u8, // blue 5 bits
)
})
};
let buf = unsafe { &SCREEN_BUFFER.0 };
let display = unsafe { DISPLAY.as_mut().unwrap() };
display.clear(Rgb565::BLACK).unwrap();
for y in 0..Y {
for x in 0..X {
let idx = y * X + x;
let color = palette565[buf[idx] as usize];
Pixel(Point::new(x as i32, y as i32), color)
.draw(display)
.unwrap();
}
}
}
#[unsafe(no_mangle)]
extern "C" fn DG_GetTicksMs() -> u32 {
let start = unsafe { START.unwrap() };
Instant::now()
.duration_since(start)
.as_millis()
.try_into()
.expect("Cannot Fit Start time into u32")
}
#[unsafe(no_mangle)]
extern "C" fn DG_GetKey(pressed: *mut raw::c_int, key: *mut raw::c_uchar) -> raw::c_int {
0
}
#[unsafe(no_mangle)]
extern "C" fn DG_SleepMs(ms: u32) {
sleep(Duration::from_millis(ms as u64));
}

View File

@@ -0,0 +1,30 @@
#![no_std]
#![no_main]
extern crate alloc;
use abi::{
display::Display,
fs::{file_len, read_file},
get_key, get_ms,
keyboard::{KeyCode, KeyState},
print, sleep,
};
use alloc::vec;
use core::panic::PanicInfo;
use embedded_graphics::{pixelcolor::Rgb565, prelude::Point};
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
print!("user panic: {} @ {:?}", info.message(), info.location(),);
loop {}
}
#[unsafe(no_mangle)]
pub extern "Rust" fn _start() {
main()
}
pub fn main() {
print!("Starting Doom app");
let mut display = Display;
}

4
user-apps/doom/stdio.h Normal file
View File

@@ -0,0 +1,4 @@
#ifndef STDIO_H
#define STDIO_H
#endif

4
user-apps/doom/string.h Normal file
View File

@@ -0,0 +1,4 @@
#ifndef STRING_H
#define STRING_H
#endif

4
user-apps/doom/strings.h Normal file
View File

@@ -0,0 +1,4 @@
#ifndef STRINGS_H
#define STRINGS_H
#endif