Add some examples
This commit is contained in:
parent
968f145583
commit
53be5d5871
13 changed files with 484 additions and 66 deletions
|
|
@ -9,6 +9,14 @@ Examples using raylib core platform functionality like window creation, inputs,
|
|||
| ## | example | developer |
|
||||
|----|----------|:----------:|
|
||||
| 01 | [core_basic_window](core/basic_window.zig) | ray
|
||||
| |
|
||||
| 02 | [core_input_keys](core/input_keys.zig) | ray
|
||||
| |
|
||||
| 04 | [core_input_mouse_wheel](core/input_mouse_wheel.zig) | ray
|
||||
| |
|
||||
| 06 | [core_input_multitouch](core/input_multitouch.zig) | [Berni](https://github.com/Berni8k)
|
||||
| |
|
||||
| 08 | [core_2d_camera](core/2d_camera.zig) | ray
|
||||
|
||||
### category: models
|
||||
|
||||
|
|
@ -16,4 +24,4 @@ Examples using raylib models functionality, including models loading/generation
|
|||
|
||||
| ## | example | developer |
|
||||
|----|----------|:----------:|
|
||||
| 74 | [models_loading](models/models_loading.c) ([Won't build](https://github.com/G3bE/raylib-zig#Technical-restrictions)) | ray
|
||||
| 74 | [models_loading](models/models_loading.zig) ([Won't work](https://github.com/G3bE/raylib-zig#Technical-restrictions)) | ray
|
||||
|
|
|
|||
134
examples/core/2d_camera.zig
Normal file
134
examples/core/2d_camera.zig
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
//
|
||||
// 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, c"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) |building, 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 += @intToFloat(f32, GetMouseWheelMove() * @floatToInt(c_int, 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);
|
||||
|
||||
BeginMode2D(camera);
|
||||
|
||||
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);
|
||||
|
||||
EndMode2D();
|
||||
|
||||
DrawText(c"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(c"Free 2d camera controls:", 20, 20, 10, BLACK);
|
||||
DrawText(c"- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
|
||||
DrawText(c"- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
|
||||
DrawText(c"- A / S to Rotate", 40, 80, 10, DARKGRAY);
|
||||
DrawText(c"- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// main
|
||||
// basic_window
|
||||
// Zig version: 0.5.0
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-15
|
||||
|
|
|
|||
55
examples/core/input_keys.zig
Normal file
55
examples/core/input_keys.zig
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
//
|
||||
// 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, c"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(c"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
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
57
examples/core/input_mouse.zig
Normal file
57
examples/core/input_mouse.zig
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
//
|
||||
// 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, c"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();
|
||||
|
||||
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(c"move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
53
examples/core/input_mouse_wheel.zig
Normal file
53
examples/core/input_mouse_wheel.zig
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
//
|
||||
// input_mouse_wheel
|
||||
// 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, c"raylib-zig [core] example - basic window");
|
||||
|
||||
var boxPositionY: i32 = screenHeight / 2 - 40;
|
||||
var scrollSpeed: i32 = 4; // Scrolling speed in pixels
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
boxPositionY -= (GetMouseWheelMove() * scrollSpeed);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(WHITE);
|
||||
|
||||
DrawRectangle(screenWidth/2 - 40, boxPositionY, 80, 80, MAROON);
|
||||
|
||||
DrawText(c"Use mouse wheel to move the cube up and down!", 10, 10, 20, GRAY);
|
||||
DrawText(FormatText(c"Box position Y: %03i", boxPositionY), 10, 40, 20, LIGHTGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
83
examples/core/input_multitouch.zig
Normal file
83
examples/core/input_multitouch.zig
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
//
|
||||
// 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, c"raylib-zig [core] example - basic window");
|
||||
|
||||
var ballPosition = Vector2 { .x = -100.0, .y = -100.0 };
|
||||
var ballColor = BEIGE;
|
||||
|
||||
var touchCounter: i32 = 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);
|
||||
|
||||
// Multitouch
|
||||
for (int i = 0; i < MAX_TOUCH_POINTS; ++i)
|
||||
{
|
||||
touchPosition = GetTouchPosition(i); // Get the touch point
|
||||
|
||||
if ((touchPosition.x >= 0) && (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
|
||||
DrawCircleV(touchPosition, 34, ORANGE);
|
||||
DrawText(FormatText("%d", i), touchPosition.x - 10, 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(c"move ball with mouse and click mouse button to change color", 10, 10, 20, DARKGRAY);
|
||||
DrawText(c"touch the screen at multiple locations to get multiple balls", 10, 30, 20, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// ModelsLoading
|
||||
// models_loading
|
||||
// Zig version:
|
||||
// Author: Nikolas Wipper
|
||||
// Date: 2020-02-15
|
||||
|
|
@ -86,14 +86,14 @@ pub fn main() anyerror!void
|
|||
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
// Check collision between ray and box
|
||||
if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds))
|
||||
{
|
||||
selected = !selected;
|
||||
}
|
||||
else
|
||||
{
|
||||
selected = false;
|
||||
}
|
||||
//if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds))
|
||||
//{
|
||||
// selected = !selected;
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// selected = false;
|
||||
//}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
|
|
@ -105,7 +105,7 @@ pub fn main() anyerror!void
|
|||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(model, position, 1.0, WHITE); // Draw 3d model with texture
|
||||
//DrawModel(model, position, 1.0, WHITE); // Draw 3d model with texture
|
||||
|
||||
DrawGrid(20, 10.0); // Draw a grid
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue