mirror of
https://github.com/Not-Nik/raylib-zig.git
synced 2025-09-09 03:57:29 +00:00
Readd core examples
This commit is contained in:
parent
eba6c5392f
commit
c83263a8a7
50
build.zig
50
build.zig
@ -25,31 +25,31 @@ pub fn build(b: *Builder) void {
|
||||
.path = "examples/core/basic_window.zig",
|
||||
.desc = "Creates a basic window with text",
|
||||
},
|
||||
// .{
|
||||
// .name = "input_keys",
|
||||
// .path = "examples/core/input_keys.zig",
|
||||
// .desc = "Simple keyboard input",
|
||||
// },
|
||||
// .{
|
||||
// .name = "input_mouse",
|
||||
// .path = "examples/core/input_mouse.zig",
|
||||
// .desc = "Simple mouse input",
|
||||
// },
|
||||
// .{
|
||||
// .name = "input_mouse_wheel",
|
||||
// .path = "examples/core/input_mouse_wheel.zig",
|
||||
// .desc = "Mouse wheel input",
|
||||
// },
|
||||
// .{
|
||||
// .name = "input_multitouch",
|
||||
// .path = "examples/core/input_multitouch.zig",
|
||||
// .desc = "Multitouch input",
|
||||
// },
|
||||
// .{
|
||||
// .name = "2d_camera",
|
||||
// .path = "examples/core/2d_camera.zig",
|
||||
// .desc = "Shows the functionality of a 2D camera",
|
||||
// },
|
||||
.{
|
||||
.name = "input_keys",
|
||||
.path = "examples/core/input_keys.zig",
|
||||
.desc = "Simple keyboard input",
|
||||
},
|
||||
.{
|
||||
.name = "input_mouse",
|
||||
.path = "examples/core/input_mouse.zig",
|
||||
.desc = "Simple mouse input",
|
||||
},
|
||||
.{
|
||||
.name = "input_mouse_wheel",
|
||||
.path = "examples/core/input_mouse_wheel.zig",
|
||||
.desc = "Mouse wheel input",
|
||||
},
|
||||
.{
|
||||
.name = "input_multitouch",
|
||||
.path = "examples/core/input_multitouch.zig",
|
||||
.desc = "Multitouch input",
|
||||
},
|
||||
.{
|
||||
.name = "2d_camera",
|
||||
.path = "examples/core/2d_camera.zig",
|
||||
.desc = "Shows the functionality of a 2D camera",
|
||||
},
|
||||
// .{
|
||||
// .name = "models_loading",
|
||||
// .path = "examples/models/models_loading.zig",
|
||||
|
130
examples/core/2d_camera.zig
Executable file
130
examples/core/2d_camera.zig
Executable file
@ -0,0 +1,130 @@
|
||||
//
|
||||
// 2d_camera
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
const rl = @import("raylib");
|
||||
const rlm = @import("raylib-math");
|
||||
|
||||
const MAX_BUILDINGS = 100;
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - 2d camera");
|
||||
|
||||
var player = rl.Rectangle { .x = 400, .y = 280, .width = 40, .height = 40 };
|
||||
var buildings: [MAX_BUILDINGS]rl.Rectangle = undefined;
|
||||
var buildColors: [MAX_BUILDINGS]rl.Color = undefined;
|
||||
|
||||
var spacing: i32 = 0;
|
||||
|
||||
for (buildings) |_, i|
|
||||
{
|
||||
buildings[i].width = @intToFloat(f32, rl.GetRandomValue(50, 200));
|
||||
buildings[i].height = @intToFloat(f32, rl.GetRandomValue(100, 800));
|
||||
buildings[i].y = screenHeight - 130 - buildings[i].height;
|
||||
buildings[i].x = @intToFloat(f32, -6000 + spacing);
|
||||
|
||||
spacing += @floatToInt(i32, buildings[i].width);
|
||||
|
||||
buildColors[i] = rl.Color { .r = @intCast(u8, rl.GetRandomValue(200, 240)), .g = @intCast(u8, rl.GetRandomValue(200, 240)),
|
||||
.b = @intCast(u8, rl.GetRandomValue(200, 250)), .a = 255 };
|
||||
}
|
||||
|
||||
var camera = rl.Camera2D {
|
||||
.target = rl.Vector2 { .x = player.x + 20, .y = player.y + 20 },
|
||||
.offset = rl.Vector2 { .x = screenWidth/2, .y = screenHeight/2 },
|
||||
.rotation = 0,
|
||||
.zoom = 1,
|
||||
};
|
||||
|
||||
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Player movement
|
||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { player.x += 2; }
|
||||
else if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT)) { player.x -= 2; }
|
||||
|
||||
// Camera target follows player
|
||||
camera.target = rl.Vector2 { .x = player.x + 20, .y = player.y + 20 };
|
||||
|
||||
// Camera rotation controls
|
||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_A)) { camera.rotation -= 1; }
|
||||
else if (rl.IsKeyDown(rl.KeyboardKey.KEY_S)) { camera.rotation += 1; }
|
||||
|
||||
// Limit camera rotation to 80 degrees (-40 to 40)
|
||||
camera.rotation = rlm.Clamp(camera.rotation, -40, 40);
|
||||
|
||||
// Camera zoom controls
|
||||
camera.zoom += rl.GetMouseWheelMove() * 0.05;
|
||||
|
||||
camera.zoom = rlm.Clamp(camera.zoom, 0.1, 3.0);
|
||||
|
||||
// Camera reset (zoom and rotation)
|
||||
if (rl.IsKeyPressed(rl.KeyboardKey.KEY_R))
|
||||
{
|
||||
camera.zoom = 1.0;
|
||||
camera.rotation = 0.0;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.BeginDrawing();
|
||||
|
||||
rl.ClearBackground(rl.RAYWHITE);
|
||||
|
||||
camera.Begin();
|
||||
|
||||
rl.DrawRectangle(-6000, 320, 13000, 8000, rl.DARKGRAY);
|
||||
|
||||
for (buildings) |building, i|
|
||||
{
|
||||
rl.DrawRectangleRec(building, buildColors[i]);
|
||||
}
|
||||
|
||||
rl.DrawRectangleRec(player, rl.RED);
|
||||
|
||||
rl.DrawLine(@floatToInt(c_int, camera.target.x), -screenHeight*10, @floatToInt(c_int, camera.target.x), screenHeight*10, rl.GREEN);
|
||||
rl.DrawLine(-screenWidth*10, @floatToInt(c_int, camera.target.y), screenWidth*10, @floatToInt(c_int, camera.target.y), rl.GREEN);
|
||||
|
||||
camera.End();
|
||||
|
||||
rl.DrawText("SCREEN AREA", 640, 10, 20, rl.RED);
|
||||
|
||||
rl.DrawRectangle(0, 0, screenWidth, 5, rl.RED);
|
||||
rl.DrawRectangle(0, 5, 5, screenHeight - 10, rl.RED);
|
||||
rl.DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, rl.RED);
|
||||
rl.DrawRectangle(0, screenHeight - 5, screenWidth, 5, rl.RED);
|
||||
|
||||
rl.DrawRectangle( 10, 10, 250, 113, rl.Fade(rl.SKYBLUE, 0.5));
|
||||
rl.DrawRectangleLines( 10, 10, 250, 113, rl.BLUE);
|
||||
|
||||
rl.DrawText("Free 2d camera controls:", 20, 20, 10, rl.BLACK);
|
||||
rl.DrawText("- Right/Left to move Offset", 40, 40, 10, rl.DARKGRAY);
|
||||
rl.DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, rl.DARKGRAY);
|
||||
rl.DrawText("- A / S to Rotate", 40, 80, 10, rl.DARKGRAY);
|
||||
rl.DrawText("- R to reset Zoom and Rotation", 40, 100, 10, rl.DARKGRAY);
|
||||
|
||||
rl.EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
rl.CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
52
examples/core/input_keys.zig
Executable file
52
examples/core/input_keys.zig
Executable file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// input_keys
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
const rl = @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - keyboard input");
|
||||
|
||||
var ballPosition = rl.Vector2 { .x = screenWidth/2, .y = screenHeight/2 };
|
||||
|
||||
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { ballPosition.x += 2.0; }
|
||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT)) { ballPosition.x -= 2.0; }
|
||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_UP)) { ballPosition.y -= 2.0; }
|
||||
if (rl.IsKeyDown(rl.KeyboardKey.KEY_DOWN)) { ballPosition.y += 2.0; }
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.BeginDrawing();
|
||||
|
||||
rl.ClearBackground(rl.RAYWHITE);
|
||||
|
||||
rl.DrawText("move the ball with arrow keys", 10, 10, 20, rl.DARKGRAY);
|
||||
|
||||
rl.DrawCircleV(ballPosition, 50, rl.MAROON);
|
||||
|
||||
rl.EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
rl.CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
59
examples/core/input_mouse.zig
Executable file
59
examples/core/input_mouse.zig
Executable file
@ -0,0 +1,59 @@
|
||||
//
|
||||
// input_mouse
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
const rl = @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - mouse input");
|
||||
|
||||
var ballPosition = rl.Vector2 { .x = -100.0, .y = -100.0 };
|
||||
var ballColor = rl.DARKBLUE;
|
||||
|
||||
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
ballPosition = rl.GetMousePosition();
|
||||
ballPosition.x = @intToFloat(f32, rl.GetMouseX());
|
||||
ballPosition.y = @intToFloat(f32, rl.GetMouseY());
|
||||
|
||||
if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_LEFT_BUTTON)) { ballColor = rl.MAROON; }
|
||||
else if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_MIDDLE_BUTTON)) { ballColor = rl.LIME; }
|
||||
else if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_RIGHT_BUTTON)) { ballColor = rl.DARKBLUE; }
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.BeginDrawing();
|
||||
|
||||
rl.ClearBackground(rl.RAYWHITE);
|
||||
|
||||
rl.DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, ballColor);
|
||||
//DrawCircleV(ballPosition, 40, ballColor);
|
||||
|
||||
rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DARKGRAY);
|
||||
|
||||
rl.EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
rl.CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
usingnamespace @import("raylib");
|
||||
const rl = @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
@ -14,40 +14,40 @@ pub fn main() anyerror!void
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
||||
|
||||
var boxPositionY: f32 = screenHeight / 2 - 40;
|
||||
var scrollSpeed: f32 = 4; // Scrolling speed in pixels
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
boxPositionY -= (GetMouseWheelMove() * scrollSpeed);
|
||||
boxPositionY -= (rl.GetMouseWheelMove() * scrollSpeed);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
rl.BeginDrawing();
|
||||
|
||||
ClearBackground(WHITE);
|
||||
rl.ClearBackground(rl.WHITE);
|
||||
|
||||
DrawRectangle(screenWidth/2 - 40, @floatToInt(c_int, boxPositionY), 80, 80, MAROON);
|
||||
rl.DrawRectangle(screenWidth/2 - 40, @floatToInt(c_int, boxPositionY), 80, 80, rl.MAROON);
|
||||
|
||||
DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, GRAY);
|
||||
DrawText(FormatText("Box position Y: %03i", @floatToInt(c_int, boxPositionY)), 10, 40, 20, LIGHTGRAY);
|
||||
rl.DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, rl.GRAY);
|
||||
rl.DrawText(rl.FormatText("Box position Y: %03i", @floatToInt(c_int, boxPositionY)), 10, 40, 20, rl.LIGHTGRAY);
|
||||
|
||||
EndDrawing();
|
||||
rl.EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
rl.CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
83
examples/core/input_multitouch.zig
Executable file
83
examples/core/input_multitouch.zig
Executable file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// input_multitouch
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
const rl = @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
||||
|
||||
var ballPosition = rl.Vector2 { .x = -100.0, .y = -100.0 };
|
||||
var ballColor = rl.BEIGE;
|
||||
|
||||
var touchCounter: f32 = 0;
|
||||
var touchPosition = rl.Vector2 { .x = 0.0, .y = 0.0 };
|
||||
|
||||
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
ballPosition = rl.GetMousePosition();
|
||||
|
||||
ballColor = rl.BEIGE;
|
||||
|
||||
if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_LEFT_BUTTON)) { ballColor = rl.MAROON; }
|
||||
if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_MIDDLE_BUTTON)) { ballColor = rl.LIME; }
|
||||
if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_RIGHT_BUTTON)) { ballColor = rl.DARKBLUE; }
|
||||
|
||||
if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_LEFT_BUTTON)) { touchCounter = 10; }
|
||||
if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_MIDDLE_BUTTON)) { touchCounter = 10; }
|
||||
if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_RIGHT_BUTTON)) { touchCounter = 10; }
|
||||
|
||||
if (touchCounter > 0) { touchCounter -= 1; }
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
rl.BeginDrawing();
|
||||
|
||||
rl.ClearBackground(rl.RAYWHITE);
|
||||
|
||||
const nums = [_]i32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
for (nums) |i|
|
||||
{
|
||||
touchPosition = rl.GetTouchPosition(i); // Get the touch point
|
||||
|
||||
if ((touchPosition.x >= 0) and (touchPosition.y >= 0)) // Make sure point is not (-1,-1) as this means there is no touch for it
|
||||
{
|
||||
// Draw circle and touch index number
|
||||
rl.DrawCircle(@floatToInt(c_int, touchPosition.x), @floatToInt(c_int, touchPosition.y), 34, rl.ORANGE);
|
||||
//DrawCircleV(touchPosition, 34, ORANGE);
|
||||
rl.DrawText(rl.FormatText("%d", i), @floatToInt(c_int, touchPosition.x) - 10, @floatToInt(c_int, touchPosition.y) - 70, 40, rl.BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the normal mouse location
|
||||
rl.DrawCircleV(ballPosition, 30 + (touchCounter*3), ballColor);
|
||||
|
||||
rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DARKGRAY);
|
||||
rl.DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, rl.DARKGRAY);
|
||||
|
||||
rl.EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
rl.CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -1,134 +0,0 @@
|
||||
//
|
||||
// 2d_camera
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
usingnamespace @import("raylib");
|
||||
usingnamespace @import("raylib-math");
|
||||
|
||||
const MAX_BUILDINGS = 100;
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - 2d camera");
|
||||
|
||||
var player = Rectangle { .x = 400, .y = 280, .width = 40, .height = 40 };
|
||||
var buildings: [MAX_BUILDINGS]Rectangle = undefined;
|
||||
var buildColors: [MAX_BUILDINGS]Color = undefined;
|
||||
|
||||
var spacing: i32 = 0;
|
||||
|
||||
for (buildings) |_, i|
|
||||
{
|
||||
buildings[i].width = @intToFloat(f32, GetRandomValue(50, 200));
|
||||
buildings[i].height = @intToFloat(f32, GetRandomValue(100, 800));
|
||||
buildings[i].y = screenHeight - 130 - buildings[i].height;
|
||||
buildings[i].x = @intToFloat(f32, -6000 + spacing);
|
||||
|
||||
spacing += @floatToInt(i32, buildings[i].width);
|
||||
|
||||
buildColors[i] = Color { .r = @intCast(u8, GetRandomValue(200, 240)), .g = @intCast(u8, GetRandomValue(200, 240)),
|
||||
.b = @intCast(u8, GetRandomValue(200, 250)), .a = 255 };
|
||||
}
|
||||
|
||||
var camera = Camera2D {
|
||||
.target = Vector2 { .x = player.x + 20, .y = player.y + 20 },
|
||||
.offset = Vector2 { .x = screenWidth/2, .y = screenHeight/2 },
|
||||
.rotation = 0,
|
||||
.zoom = 1,
|
||||
};
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Player movement
|
||||
if (IsKeyDown(KeyboardKey.KEY_RIGHT)) { player.x += 2; }
|
||||
else if (IsKeyDown(KeyboardKey.KEY_LEFT)) { player.x -= 2; }
|
||||
|
||||
// Camera target follows player
|
||||
camera.target = Vector2 { .x = player.x + 20, .y = player.y + 20 };
|
||||
|
||||
// Camera rotation controls
|
||||
if (IsKeyDown(KeyboardKey.KEY_A)) { camera.rotation -= 1; }
|
||||
else if (IsKeyDown(KeyboardKey.KEY_S)) { camera.rotation += 1; }
|
||||
|
||||
// Limit camera rotation to 80 degrees (-40 to 40)
|
||||
camera.rotation = Clamp(camera.rotation, -40, 40);
|
||||
|
||||
// Camera zoom controls
|
||||
camera.zoom += GetMouseWheelMove() * 0.05;
|
||||
|
||||
camera.zoom = Clamp(camera.zoom, 0.1, 3.0);
|
||||
|
||||
// Camera reset (zoom and rotation)
|
||||
if (IsKeyPressed(KeyboardKey.KEY_R))
|
||||
{
|
||||
camera.zoom = 1.0;
|
||||
camera.rotation = 0.0;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
camera.Begin();
|
||||
|
||||
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
|
||||
|
||||
for (buildings) |building, i|
|
||||
{
|
||||
//DrawRectangleRec(building, buildColors[i]);
|
||||
DrawRectangle(@floatToInt(c_int, building.x), @floatToInt(c_int, building.y),
|
||||
@floatToInt(c_int, building.width), @floatToInt(c_int, building.height), buildColors[i]);
|
||||
}
|
||||
|
||||
DrawRectangle(@floatToInt(c_int, player.x), @floatToInt(c_int, player.y),
|
||||
@floatToInt(c_int, player.width), @floatToInt(c_int, player.height), DARKGRAY);
|
||||
//DrawRectangleRec(player, RED);
|
||||
|
||||
DrawLine(@floatToInt(c_int, camera.target.x), -screenHeight*10, @floatToInt(c_int, camera.target.x), screenHeight*10, GREEN);
|
||||
DrawLine(-screenWidth*10, @floatToInt(c_int, camera.target.y), screenWidth*10, @floatToInt(c_int, camera.target.y), GREEN);
|
||||
|
||||
camera.End();
|
||||
|
||||
DrawText("SCREEN AREA", 640, 10, 20, RED);
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, 5, RED);
|
||||
DrawRectangle(0, 5, 5, screenHeight - 10, RED);
|
||||
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
|
||||
DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
|
||||
|
||||
DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5));
|
||||
DrawRectangleLines( 10, 10, 250, 113, BLUE);
|
||||
|
||||
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
|
||||
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
|
||||
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
|
||||
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
|
||||
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
//
|
||||
// input_keys
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
usingnamespace @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - keyboard input");
|
||||
|
||||
var ballPosition = Vector2 { .x = screenWidth/2, .y = screenHeight/2 };
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsKeyDown(KeyboardKey.KEY_RIGHT)) { ballPosition.x += 2.0; }
|
||||
if (IsKeyDown(KeyboardKey.KEY_LEFT)) { ballPosition.x -= 2.0; }
|
||||
if (IsKeyDown(KeyboardKey.KEY_UP)) { ballPosition.y -= 2.0; }
|
||||
if (IsKeyDown(KeyboardKey.KEY_DOWN)) { ballPosition.y += 2.0; }
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawText("move the ball with arrow keys", 10, 10, 20, DARKGRAY);
|
||||
|
||||
DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, MAROON);
|
||||
//DrawCircleV(ballPosition, 50, MAROON);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -1,59 +0,0 @@
|
||||
//
|
||||
// input_mouse
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
usingnamespace @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - mouse input");
|
||||
|
||||
var ballPosition = Vector2 { .x = -100.0, .y = -100.0 };
|
||||
var ballColor = DARKBLUE;
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
ballPosition = GetMousePosition();
|
||||
ballPosition.x = @intToFloat(f32, GetMouseX());
|
||||
ballPosition.y = @intToFloat(f32, GetMouseY());
|
||||
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { ballColor = MAROON; }
|
||||
else if (IsMouseButtonPressed(MouseButton.MOUSE_MIDDLE_BUTTON)) { ballColor = LIME; }
|
||||
else if (IsMouseButtonPressed(MouseButton.MOUSE_RIGHT_BUTTON)) { ballColor = DARKBLUE; }
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, ballColor);
|
||||
//DrawCircleV(ballPosition, 40, ballColor);
|
||||
|
||||
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -1,84 +0,0 @@
|
||||
//
|
||||
// input_multitouch
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-16
|
||||
//
|
||||
|
||||
usingnamespace @import("raylib");
|
||||
|
||||
pub fn main() anyerror!void
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const screenWidth = 800;
|
||||
const screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
|
||||
|
||||
var ballPosition = Vector2 { .x = -100.0, .y = -100.0 };
|
||||
var ballColor = BEIGE;
|
||||
|
||||
var touchCounter: f32 = 0;
|
||||
var touchPosition = Vector2 { .x = 0.0, .y = 0.0 };
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
ballPosition = GetMousePosition();
|
||||
|
||||
ballColor = BEIGE;
|
||||
|
||||
if (IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON)) { ballColor = MAROON; }
|
||||
if (IsMouseButtonDown(MouseButton.MOUSE_MIDDLE_BUTTON)) { ballColor = LIME; }
|
||||
if (IsMouseButtonDown(MouseButton.MOUSE_RIGHT_BUTTON)) { ballColor = DARKBLUE; }
|
||||
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { touchCounter = 10; }
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_MIDDLE_BUTTON)) { touchCounter = 10; }
|
||||
if (IsMouseButtonPressed(MouseButton.MOUSE_RIGHT_BUTTON)) { touchCounter = 10; }
|
||||
|
||||
if (touchCounter > 0) { touchCounter -= 1; }
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
const nums = [_]i32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
for (nums) |i|
|
||||
{
|
||||
touchPosition = GetTouchPosition(i); // Get the touch point
|
||||
|
||||
if ((touchPosition.x >= 0) and (touchPosition.y >= 0)) // Make sure point is not (-1,-1) as this means there is no touch for it
|
||||
{
|
||||
// Draw circle and touch index number
|
||||
DrawCircle(@floatToInt(c_int, touchPosition.x), @floatToInt(c_int, touchPosition.y), 34, ORANGE);
|
||||
//DrawCircleV(touchPosition, 34, ORANGE);
|
||||
DrawText(FormatText("%d", i), @floatToInt(c_int, touchPosition.x) - 10, @floatToInt(c_int, touchPosition.y) - 70, 40, BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the normal mouse location
|
||||
DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 30 + (touchCounter*3), ballColor);
|
||||
//DrawCircleV(ballPosition, 30 + (touchCounter*3), ballColor);
|
||||
|
||||
DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
|
||||
DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
@ -137,4 +137,4 @@ def parse_header(header_name: str, output_file: str, prefix: str):
|
||||
|
||||
|
||||
parse_header("raylib.h", "raylib-wa.zig", "RLAPI ")
|
||||
parse_header("raymath.h", "raylib-zig-math.zig", "RMDEF ")
|
||||
parse_header("raymath.h", "raylib-zig-math.zig", "RMAPI ")
|
||||
|
@ -8,6 +8,101 @@ const Vector4 = rl.Vector4;
|
||||
const float16 = rl.float16;
|
||||
const float3 = rl.float3;
|
||||
|
||||
|
||||
pub extern fn Clamp(value: f32, min: f32, max: f32) f32;
|
||||
pub extern fn Lerp(start: f32, end: f32, amount: f32) f32;
|
||||
pub extern fn Normalize(value: f32, start: f32, end: f32) f32;
|
||||
pub extern fn Remap(value: f32, inputStart: f32, inputEnd: f32, outputStart: f32, outputEnd: f32) f32;
|
||||
pub extern fn Vector2Zero() Vector2;
|
||||
pub extern fn Vector2One() Vector2;
|
||||
pub extern fn Vector2Add(v1: Vector2, v2: Vector2) Vector2;
|
||||
pub extern fn Vector2AddValue(v: Vector2, add: f32) Vector2;
|
||||
pub extern fn Vector2Subtract(v1: Vector2, v2: Vector2) Vector2;
|
||||
pub extern fn Vector2SubtractValue(v: Vector2, sub: f32) Vector2;
|
||||
pub extern fn Vector2Length(v: Vector2) f32;
|
||||
pub extern fn Vector2LengthSqr(v: Vector2) f32;
|
||||
pub extern fn Vector2DotProduct(v1: Vector2, v2: Vector2) f32;
|
||||
pub extern fn Vector2Distance(v1: Vector2, v2: Vector2) f32;
|
||||
pub extern fn Vector2Angle(v1: Vector2, v2: Vector2) f32;
|
||||
pub extern fn Vector2Scale(v: Vector2, scale: f32) Vector2;
|
||||
pub extern fn Vector2Multiply(v1: Vector2, v2: Vector2) Vector2;
|
||||
pub extern fn Vector2Negate(v: Vector2) Vector2;
|
||||
pub extern fn Vector2Divide(v1: Vector2, v2: Vector2) Vector2;
|
||||
pub extern fn Vector2Normalize(v: Vector2) Vector2;
|
||||
pub extern fn Vector2Lerp(v1: Vector2, v2: Vector2, amount: f32) Vector2;
|
||||
pub extern fn Vector2Reflect(v: Vector2, normal: Vector2) Vector2;
|
||||
pub extern fn Vector2Rotate(v: Vector2, angle: f32) Vector2;
|
||||
pub extern fn Vector2MoveTowards(v: Vector2, target: Vector2, maxDistance: f32) Vector2;
|
||||
pub extern fn Vector3Zero() Vector3;
|
||||
pub extern fn Vector3One() Vector3;
|
||||
pub extern fn Vector3Add(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3AddValue(v: Vector3, add: f32) Vector3;
|
||||
pub extern fn Vector3Subtract(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3SubtractValue(v: Vector3, sub: f32) Vector3;
|
||||
pub extern fn Vector3Scale(v: Vector3, scalar: f32) Vector3;
|
||||
pub extern fn Vector3Multiply(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3CrossProduct(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3Perpendicular(v: Vector3) Vector3;
|
||||
pub extern fn Vector3Length(v: Vector3) f32;
|
||||
pub extern fn Vector3LengthSqr(v: Vector3) f32;
|
||||
pub extern fn Vector3DotProduct(v1: Vector3, v2: Vector3) f32;
|
||||
pub extern fn Vector3Distance(v1: Vector3, v2: Vector3) f32;
|
||||
pub extern fn Vector3Angle(v1: Vector3, v2: Vector3) f32;
|
||||
pub extern fn Vector3Negate(v: Vector3) Vector3;
|
||||
pub extern fn Vector3Divide(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3Normalize(v: Vector3) Vector3;
|
||||
pub extern fn Vector3OrthoNormalize(v1: [*c]const Vector3, v2: [*c]const Vector3) void;
|
||||
pub extern fn Vector3Transform(v: Vector3, mat: Matrix) Vector3;
|
||||
pub extern fn Vector3RotateByQuaternion(v: Vector3, q: Quaternion) Vector3;
|
||||
pub extern fn Vector3Lerp(v1: Vector3, v2: Vector3, amount: f32) Vector3;
|
||||
pub extern fn Vector3Reflect(v: Vector3, normal: Vector3) Vector3;
|
||||
pub extern fn Vector3Min(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3Max(v1: Vector3, v2: Vector3) Vector3;
|
||||
pub extern fn Vector3Barycenter(p: Vector3, a: Vector3, b: Vector3, c: Vector3) Vector3;
|
||||
pub extern fn Vector3Unproject(source: Vector3, projection: Matrix, view: Matrix) Vector3;
|
||||
pub extern fn Vector3ToFloatV(v: Vector3) float3;
|
||||
pub extern fn MatrixDeterminant(mat: Matrix) f32;
|
||||
pub extern fn MatrixTrace(mat: Matrix) f32;
|
||||
pub extern fn MatrixTranspose(mat: Matrix) Matrix;
|
||||
pub extern fn MatrixInvert(mat: Matrix) Matrix;
|
||||
pub extern fn MatrixNormalize(mat: Matrix) Matrix;
|
||||
pub extern fn MatrixIdentity() Matrix;
|
||||
pub extern fn MatrixAdd(left: Matrix, right: Matrix) Matrix;
|
||||
pub extern fn MatrixSubtract(left: Matrix, right: Matrix) Matrix;
|
||||
pub extern fn MatrixMultiply(left: Matrix, right: Matrix) Matrix;
|
||||
pub extern fn MatrixTranslate(x: f32, y: f32, z: f32) Matrix;
|
||||
pub extern fn MatrixRotate(axis: Vector3, angle: f32) Matrix;
|
||||
pub extern fn MatrixRotateX(angle: f32) Matrix;
|
||||
pub extern fn MatrixRotateY(angle: f32) Matrix;
|
||||
pub extern fn MatrixRotateZ(angle: f32) Matrix;
|
||||
pub extern fn MatrixRotateXYZ(ang: Vector3) Matrix;
|
||||
pub extern fn MatrixRotateZYX(ang: Vector3) Matrix;
|
||||
pub extern fn MatrixScale(x: f32, y: f32, z: f32) Matrix;
|
||||
pub extern fn MatrixFrustum(left: f64, right: f64, bottom: f64, top: f64, near: f64, far: f64) Matrix;
|
||||
pub extern fn MatrixPerspective(fovy: f64, aspect: f64, near: f64, far: f64) Matrix;
|
||||
pub extern fn MatrixOrtho(left: f64, right: f64, bottom: f64, top: f64, near: f64, far: f64) Matrix;
|
||||
pub extern fn MatrixLookAt(eye: Vector3, target: Vector3, up: Vector3) Matrix;
|
||||
pub extern fn MatrixToFloatV(mat: Matrix) float16;
|
||||
pub extern fn QuaternionAdd(q1: Quaternion, q2: Quaternion) Quaternion;
|
||||
pub extern fn QuaternionAddValue(q: Quaternion, add: f32) Quaternion;
|
||||
pub extern fn QuaternionSubtract(q1: Quaternion, q2: Quaternion) Quaternion;
|
||||
pub extern fn QuaternionSubtractValue(q: Quaternion, sub: f32) Quaternion;
|
||||
pub extern fn QuaternionIdentity() Quaternion;
|
||||
pub extern fn QuaternionLength(q: Quaternion) f32;
|
||||
pub extern fn QuaternionNormalize(q: Quaternion) Quaternion;
|
||||
pub extern fn QuaternionInvert(q: Quaternion) Quaternion;
|
||||
pub extern fn QuaternionMultiply(q1: Quaternion, q2: Quaternion) Quaternion;
|
||||
pub extern fn QuaternionScale(q: Quaternion, mul: f32) Quaternion;
|
||||
pub extern fn QuaternionDivide(q1: Quaternion, q2: Quaternion) Quaternion;
|
||||
pub extern fn QuaternionLerp(q1: Quaternion, q2: Quaternion, amount: f32) Quaternion;
|
||||
pub extern fn QuaternionNlerp(q1: Quaternion, q2: Quaternion, amount: f32) Quaternion;
|
||||
pub extern fn QuaternionSlerp(q1: Quaternion, q2: Quaternion, amount: f32) Quaternion;
|
||||
pub extern fn QuaternionFromVector3ToVector3(from: Vector3, to: Vector3) Quaternion;
|
||||
pub extern fn QuaternionFromMatrix(mat: Matrix) Quaternion;
|
||||
pub extern fn QuaternionToMatrix(q: Quaternion) Matrix;
|
||||
pub extern fn QuaternionFromAxisAngle(axis: Vector3, angle: f32) Quaternion;
|
||||
pub extern fn QuaternionToAxisAngle(q: Quaternion, outAxis: [*c]const Vector3, outAngle: [*c]const f32) void;
|
||||
pub extern fn QuaternionFromEuler(pitch: f32, yaw: f32, roll: f32) Quaternion;
|
||||
pub extern fn QuaternionToEuler(q: Quaternion) Vector3;
|
||||
pub extern fn QuaternionTransform(q: Quaternion, mat: Matrix) Quaternion;
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user