Skip to content

Adding some examples. #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
**/target
**/Cargo.lock
**/*.rs.bk
Cargo.lock
7 changes: 7 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[workspace]

members = [
"core/basic_window/",
"core/input_mouse/",
"core/input_keys/",
]
27 changes: 27 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Examples

## Getting started

Follow the instructions on the main README to install raylib.

### Creating a new raylib project

To create a new raylib project, you can use cargo to create a new project folder:

```bash
cargo new --bin <name_of_project_here>
```

Inside, there will be a `src` folder where all the code source files go, and `Cargo.toml`, which holds information about the project.

Edit `Cargo.toml` using a text editor and under `[dependencies]`, add the line:
```
raylib = "0.9"
```

Then to edit your program, edit the file `src/main.rs`. To run the program, run:
```bash
cargo run
```

while in the root folder of your project.
8 changes: 8 additions & 0 deletions examples/core/basic_window/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "basic_window"
version = "0.1.0"
authors = ["eggmund"]
edition = "2018"

[dependencies]
raylib = "0.9"
20 changes: 20 additions & 0 deletions examples/core/basic_window/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use raylib::Color;

fn main() {
let rl = raylib::init() // Initialize the raylib handle
.size(800, 450)
.title("raylib [core] example - basic window")
.build();

rl.set_target_fps(60); // Limits fps

while !rl.window_should_close() {
// Update stuff here

rl.begin_drawing();
rl.clear_background(Color::RAYWHITE);
// Draw stuff here
rl.draw_text("Congrats! You created your first window!", 190, 200, 20, Color::LIGHTGRAY);
rl.end_drawing();
}
}
8 changes: 8 additions & 0 deletions examples/core/input_keys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "input_keys"
version = "0.1.0"
authors = ["eggmund"]
edition = "2018"

[dependencies]
raylib = "0.9"
39 changes: 39 additions & 0 deletions examples/core/input_keys/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use raylib::{Color, Vector2};
use raylib::consts::*;

fn main() {
let screen_width: i32 = 800;
let screen_height: i32 = 450;

let rl = raylib::init()
.size(screen_width, screen_height)
.title("raylib [core] example - keyboard input")
.build();

let mut ball_position = Vector2 { x: screen_width as f32/2.0, y: screen_height as f32/2.0 };

rl.set_target_fps(60);

while !rl.window_should_close() {
if rl.is_key_down(KEY_RIGHT as i32) {
ball_position.x += 0.8;
}
if rl.is_key_down(KEY_LEFT as i32) {
ball_position.x -= 0.8;
}
if rl.is_key_down(KEY_UP as i32) {
ball_position.y -= 0.8;
}
if rl.is_key_down(KEY_DOWN as i32) {
ball_position.y += 0.8;
}

rl.begin_drawing();
rl.clear_background(Color::RAYWHITE);

rl.draw_text("move the ball with arrow keys", 10, 10, 20, Color::DARKGRAY);
rl.draw_circle_v(ball_position, 50.0, Color::MAROON);

rl.end_drawing();
}
}
8 changes: 8 additions & 0 deletions examples/core/input_mouse/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "input_mouse"
version = "0.1.0"
authors = ["eggmund"]
edition = "2018"

[dependencies]
raylib = "0.9"
34 changes: 34 additions & 0 deletions examples/core/input_mouse/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use raylib::Color;
use raylib::consts::*;

fn main() {
let rl = raylib::init()
.size(800, 450)
.title("raylib [core] example - mouse input")
.build();

rl.set_target_fps(60);

let mut ball_color = Color::DARKBLUE;

while !rl.window_should_close() {
let ball_position = rl.get_mouse_position();

if rl.is_mouse_button_pressed(MOUSE_LEFT_BUTTON as i32) {
ball_color = Color::MAROON;
} else if rl.is_mouse_button_pressed(MOUSE_MIDDLE_BUTTON as i32) {
ball_color = Color::LIME;
} else if rl.is_mouse_button_pressed(MOUSE_RIGHT_BUTTON as i32) {
ball_color = Color::DARKBLUE;
}

rl.begin_drawing();

rl.clear_background(Color::RAYWHITE);
rl.draw_circle_v(ball_position, 40.0, ball_color);

rl.draw_text("move ball with mouse and click mouse button to change color", 10, 10, 20, Color::DARKGRAY);

rl.end_drawing();
}
}