Draw SDL2 window

main
mat ess 2022-08-15 23:28:39 -04:00
parent a9024dde9c
commit 6b6dd344bf
5 changed files with 126 additions and 110 deletions

56
Cargo.lock generated
View File

@ -2,6 +2,62 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.131"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04c3b4822ccebfa39c02fc03d1534441b22ead323fa0f48bb7ddd8e6ba076a40"
[[package]]
name = "medit"
version = "0.1.0"
dependencies = [
"sdl2",
]
[[package]]
name = "sdl2"
version = "0.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a"
dependencies = [
"bitflags",
"lazy_static",
"libc",
"sdl2-sys",
]
[[package]]
name = "sdl2-sys"
version = "0.35.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0"
dependencies = [
"cfg-if",
"libc",
"version-compare",
]
[[package]]
name = "version-compare"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73"

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = "0.35.2"

View File

@ -1,87 +1,5 @@
{
"nodes": {
"crane": {
"flake": false,
"locked": {
"lastModified": 1654444508,
"narHash": "sha256-4OBvQ4V7jyt7afs6iKUvRzJ1u/9eYnKzVQbeQdiamuY=",
"owner": "ipetkov",
"repo": "crane",
"rev": "db5482bf225acc3160899124a1df5a617cfa27b5",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"devshell": {
"flake": false,
"locked": {
"lastModified": 1658746384,
"narHash": "sha256-CCJcoMOcXyZFrV1ag4XMTpAPjLWb4Anbv+ktXFI1ry0=",
"owner": "numtide",
"repo": "devshell",
"rev": "0ffc7937bb5e8141af03d462b468bd071eb18e1b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "devshell",
"type": "github"
}
},
"dream2nix": {
"inputs": {
"alejandra": [
"nci",
"nixpkgs"
],
"crane": "crane",
"devshell": [
"nci",
"devshell"
],
"flake-utils-pre-commit": [
"nci",
"nixpkgs"
],
"gomod2nix": [
"nci",
"nixpkgs"
],
"mach-nix": [
"nci",
"nixpkgs"
],
"nixpkgs": [
"nci",
"nixpkgs"
],
"poetry2nix": [
"nci",
"nixpkgs"
],
"pre-commit-hooks": [
"nci",
"nixpkgs"
]
},
"locked": {
"lastModified": 1660322711,
"narHash": "sha256-Q7im8L9UEVs448V4dv4XdCZy5yFfO1J+KpBi8O9Fe4o=",
"owner": "nix-community",
"repo": "dream2nix",
"rev": "9331fb1376068e24f0ab5080aec85f43a369ebdb",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "dream2nix",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs": [
@ -117,28 +35,23 @@
"type": "github"
}
},
"nci": {
"naersk": {
"inputs": {
"devshell": "devshell",
"dream2nix": "dream2nix",
"nixpkgs": [
"nixpkgs"
],
"rust-overlay": [
"rust"
]
},
"locked": {
"lastModified": 1660543949,
"narHash": "sha256-dnKsU4qMXzbOaWRFQ9W2f5gOCXMtqo2ZRSugdNs/80s=",
"owner": "yusdacra",
"repo": "nix-cargo-integration",
"rev": "670586d5d3a2a786fa2adfc4273b4419206c1e78",
"lastModified": 1659610603,
"narHash": "sha256-LYgASYSPYo7O71WfeUOaEUzYfzuXm8c8eavJcel+pfI=",
"owner": "nix-community",
"repo": "naersk",
"rev": "c6a45e4277fa58abd524681466d3450f896dc094",
"type": "github"
},
"original": {
"owner": "yusdacra",
"repo": "nix-cargo-integration",
"owner": "nix-community",
"repo": "naersk",
"type": "github"
}
},
@ -161,7 +74,7 @@
"root": {
"inputs": {
"flake-parts": "flake-parts",
"nci": "nci",
"naersk": "naersk",
"nixpkgs": "nixpkgs",
"rust": "rust"
}

View File

@ -9,25 +9,38 @@
rust.url = "github:oxalica/rust-overlay";
rust.inputs.nixpkgs.follows = "nixpkgs";
nci.url = "github:yusdacra/nix-cargo-integration";
nci.inputs.nixpkgs.follows = "nixpkgs";
nci.inputs.rust-overlay.follows = "rust";
naersk.url = "github:nix-community/naersk";
naersk.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, flake-parts, ... }@inputs:
outputs = { self, flake-parts, nixpkgs, naersk, rust }:
let
overlays = [ rust.overlays.default ];
in
flake-parts.lib.mkFlake { inherit self; } {
imports = [{
config.perSystem = { system, ... }: {
config._module.args.pkgs = (import inputs.nixpkgs {
inherit system;
overlays = [ inputs.rust.overlays.default ];
});
config._module.args.pkgs = import nixpkgs { inherit system overlays; };
};
}];
systems = inputs.nixpkgs.lib.systems.flakeExposed;
perSystem = { config, self', inputs', pkgs, system, ... }: { };
flake = inputs.nci.lib.makeOutputs {
root = ./.;
};
systems = nixpkgs.lib.systems.flakeExposed;
perSystem = { config, self', inputs', pkgs, system, ... }:
let
toolchain = pkgs.rust-bin.stable.latest.default;
naersk-lib = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
};
in
{
packages.default = naersk-lib.buildPackage {
src = ./.;
buildInputs = with pkgs; [ SDL2 ];
};
apps.default.program = "${config.packages.default}/bin/medit";
devShells.default = pkgs.mkShell {
nativeBuildInputs = [ toolchain ];
};
};
};
}

View File

@ -1,3 +1,36 @@
use sdl2::{event::Event, init, keyboard::Keycode, pixels::Color};
fn main() {
println!("Hello, world!");
let ctx = init().unwrap();
let vid = ctx.video().unwrap();
let win = vid
.window("medit", 800, 600)
.position_centered()
.build()
.unwrap();
let mut can = win.into_canvas().build().unwrap();
can.set_draw_color(Color::RGB(0, 255, 255));
can.clear();
can.present();
let mut event_pump = ctx.event_pump().unwrap();
let mut i = 0;
'running: loop {
i = (i + 1) % 255;
can.set_draw_color(Color::RGB(i, 64, 255 - i));
can.clear();
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
_ => {}
}
}
// The rest of the game loop goes here...
can.present();
std::thread::sleep(std::time::Duration::new(0, 1_000_000_000u32 / 60));
}
}