Created
January 15, 2021 11:48
-
-
Save peterhellberg/5738c738e1250820b833db9f88b81555 to your computer and use it in GitHub Desktop.
Raylib examples translated from C to Zig https://github.com/raysan5/raylib/tree/master/examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const ray = @cImport({ | |
@cInclude("raylib.h"); | |
}); | |
pub fn main() void { | |
// Initialization | |
//-------------------------------------------------------------------------------------- | |
const screenWidth = 800; | |
const screenHeight = 450; | |
ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input"); | |
defer ray.CloseWindow(); | |
var ballPosition = ray.Vector2{ | |
.x = -100.0, | |
.y = -100.0, | |
}; | |
var ballColor = ray.DARKBLUE; | |
// Set our game to run at 60 frames-per-second | |
ray.SetTargetFPS(60); | |
//-------------------------------------------------------------------------------------- | |
// Main game loop | |
while (!ray.WindowShouldClose()) { | |
// Update | |
//---------------------------------------------------------------------------------- | |
// For some reason ballPosition = ray.GetMousePosition(); | |
// resulted in diagonal movement of the ball, | |
// instead I’m using GetMouseX and GetMouseY: | |
ballPosition.x = @intToFloat(f32, ray.GetMouseX()); | |
ballPosition.y = @intToFloat(f32, ray.GetMouseY()); | |
if (ray.IsMouseButtonPressed(ray.MOUSE_LEFT_BUTTON)) { | |
ballColor = ray.MAROON; | |
} else if (ray.IsMouseButtonPressed(ray.MOUSE_MIDDLE_BUTTON)) { | |
ballColor = ray.LIME; | |
} else if (ray.IsMouseButtonPressed(ray.MOUSE_RIGHT_BUTTON)) { | |
ballColor = ray.DARKBLUE; | |
} | |
//---------------------------------------------------------------------------------- | |
// Draw | |
//---------------------------------------------------------------------------------- | |
ray.BeginDrawing(); | |
defer ray.EndDrawing(); | |
ray.ClearBackground(ray.RAYWHITE); | |
ray.DrawCircleV(ballPosition, 40, ballColor); | |
ray.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, ray.DARKGRAY); | |
//---------------------------------------------------------------------------------- | |
} | |
} |
Related Zig issues
const std = @import("std");
const ray = @cImport({
@cInclude("raylib.h");
});
pub fn main() void {
const screenWidth = 650;
const screenHeight = 420;
ray.InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input");
defer ray.CloseWindow();
var ballPosition = ray.Vector2{
.x = -100.0,
.y = -100.0,
};
var ballColor = ray.DARKBLUE;
ray.SetTargetFPS(60);
while (!ray.WindowShouldClose()) {
ballPosition = ray.GetMousePosition();
if (ray.IsMouseButtonPressed(ray.MOUSE_LEFT_BUTTON)) {
ballColor = ray.MAROON;
} else if (ray.IsMouseButtonPressed(ray.MOUSE_MIDDLE_BUTTON)) {
ballColor = ray.LIME;
} else if (ray.IsMouseButtonPressed(ray.MOUSE_RIGHT_BUTTON)) {
ballColor = ray.DARKBLUE;
}
ray.BeginDrawing();
defer ray.EndDrawing();
ray.ClearBackground(ray.RAYWHITE);
ray.DrawCircleV(ballPosition, 40, ballColor);
ray.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, ray.DARKGRAY);
}
}
Now (v0.10.0
) does what you expect.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build.zig that links to raylib