From 6b6dd344bf4b96e45262c2c9942eabca439bc617 Mon Sep 17 00:00:00 2001 From: mat ess Date: Mon, 15 Aug 2022 23:28:39 -0400 Subject: [PATCH] Draw SDL2 window --- Cargo.lock | 56 ++++++++++++++++++++++++++++ Cargo.toml | 1 + flake.lock | 105 +++++----------------------------------------------- flake.nix | 39 ++++++++++++------- src/main.rs | 35 +++++++++++++++++- 5 files changed, 126 insertions(+), 110 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12175bb..7e51b54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index d28ef6b..1474919 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/flake.lock b/flake.lock index f3cd91e..98f2e98 100644 --- a/flake.lock +++ b/flake.lock @@ -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" } diff --git a/flake.nix b/flake.nix index 8d0cee5..e56eb54 100644 --- a/flake.nix +++ b/flake.nix @@ -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 ]; + }; + }; }; } diff --git a/src/main.rs b/src/main.rs index e7a11a9..2f8c740 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)); + } }