Compare commits

..

6 Commits

Author SHA1 Message Date
6b1ce83aba feat(ada): implement 2026-03-11 21:51:36 +01:00
f906676b21 feat(zig): implement 2026-03-11 20:46:40 +01:00
d55b1486d6 feat(oding): implement 2026-03-11 20:36:11 +01:00
4a4a630eaf feat(go): implement 2026-03-11 20:29:13 +01:00
54f2fc9bd4 refactor: constant logo height instead of using scale 2026-03-11 19:06:11 +01:00
96a26cea5c chore: update readme 2026-03-11 19:05:30 +01:00
22 changed files with 538 additions and 82 deletions

View File

@@ -2,3 +2,5 @@
Bouncing logos in different programming languages.
Get raylib at https://github.com/raysan5/raylib. Idea stolen from https://tsoding.github.io.
All projects are build using `make`, so all commands you need are inside `Makefile`s.

6
ada/Makefile Normal file
View File

@@ -0,0 +1,6 @@
raylib_ada: main.adb
gnatmake main.adb -o raylib_ada \
-I../_raylib-5.5_linux_amd64/include \
-largs ../_raylib-5.5_linux_amd64/lib/libraylib.a \
-lm
rm -f main.ali main.o

BIN
ada/ada_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

147
ada/main.adb Normal file
View File

@@ -0,0 +1,147 @@
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Ada.Calendar;
with System;
procedure Main is
WINDOW_WIDTH : constant := 800;
WINDOW_HEIGHT : constant := 600;
LOGO_HEIGHT : constant := 64;
LOGO_SPEED : constant float := 300.0;
type Unsigned_Char is mod 2 ** 8;
for Unsigned_Char'Size use 8;
type Color is record
R, G, B, A : Unsigned_Char;
end record;
pragma Convention (C_Pass_By_Copy, Color);
type Vector2 is record
X, Y : float;
end record;
pragma Convention (C_Pass_By_Copy, Vector2);
type Image is record
Data : System.Address;
Width : int;
Height : int;
Mipmaps : int;
Format : int;
end record;
pragma Convention (C_Pass_By_Copy, Image);
type Texture2D is record
Id : unsigned;
Width : int;
Height : int;
Mipmaps : int;
Format : int;
end record;
pragma Convention (C_Pass_By_Copy, Texture2D);
BLACK : constant Color := (R => 0, G => 0, B => 0, A => 255);
WHITE : constant Color := (R => 255, G => 255, B => 255, A => 255);
procedure Init_Window (Width : int; Height : int; Title : chars_ptr);
pragma Import (C, Init_Window, "InitWindow");
procedure Set_Target_FPS (Fps : int);
pragma Import (C, Set_Target_FPS, "SetTargetFPS");
procedure Set_Random_Seed (Seed : unsigned);
pragma Import (C, Set_Random_Seed, "SetRandomSeed");
function Load_Image (File_Name : chars_ptr) return Image;
pragma Import (C, Load_Image, "LoadImage");
procedure Image_Resize (Img : access Image; New_Width : int; New_Height : int);
pragma Import (C, Image_Resize, "ImageResize");
function Load_Texture_From_Image (Img : Image) return Texture2D;
pragma Import (C, Load_Texture_From_Image, "LoadTextureFromImage");
procedure Unload_Image (Img : Image);
pragma Import (C, Unload_Image, "UnloadImage");
function Get_Random_Value (Min : int; Max : int) return int;
pragma Import (C, Get_Random_Value, "GetRandomValue");
function Window_Should_Close return int;
pragma Import (C, Window_Should_Close, "WindowShouldClose");
function Get_Frame_Time return float;
pragma Import (C, Get_Frame_Time, "GetFrameTime");
procedure Begin_Drawing;
pragma Import (C, Begin_Drawing, "BeginDrawing");
procedure End_Drawing;
pragma Import (C, End_Drawing, "EndDrawing");
procedure Clear_Background (C : Color);
pragma Import (C, Clear_Background, "ClearBackground");
procedure Draw_Texture_V (Tex : Texture2D; Pos : Vector2; Tint : Color);
pragma Import (C, Draw_Texture_V, "DrawTextureV");
procedure Unload_Texture (Tex : Texture2D);
pragma Import (C, Unload_Texture, "UnloadTexture");
procedure Close_Window;
pragma Import (C, Close_Window, "CloseWindow");
use type System.Address;
Logo_Image : aliased Image;
Logo_Texture : Texture2D;
X, Y : float;
Dx, Dy : float;
Delta_Time : float;
Seed : unsigned;
begin
Init_Window (WINDOW_WIDTH, WINDOW_HEIGHT, New_String ("Raylib in Ada"));
Set_Target_FPS (60);
Seed := unsigned (Ada.Calendar.Seconds (Ada.Calendar.Clock));
Set_Random_Seed (Seed);
Logo_Image := Load_Image (New_String ("./ada_logo.png"));
Image_Resize (Logo_Image'Access,
Logo_Image.Width * LOGO_HEIGHT / Logo_Image.Height,
LOGO_HEIGHT);
Logo_Texture := Load_Texture_From_Image (Logo_Image);
Unload_Image (Logo_Image);
X := float (Get_Random_Value (0, WINDOW_WIDTH - Logo_Texture.Width));
Y := float (Get_Random_Value (0, WINDOW_HEIGHT - Logo_Texture.Height));
Dx := (if Get_Random_Value (0, 1) = 1 then -LOGO_SPEED else LOGO_SPEED);
Dy := (if Get_Random_Value (0, 1) = 1 then -LOGO_SPEED else LOGO_SPEED);
while Window_Should_Close = 0 loop
Delta_Time := Get_Frame_Time;
X := X + Dx * Delta_Time;
Y := Y + Dy * Delta_Time;
if X < 0.0 or else X + float (Logo_Texture.Width) >= float (WINDOW_WIDTH) - 1.0 then
Dx := -Dx;
X := X + Dx * Delta_Time;
end if;
if Y < 0.0 or else Y + float (Logo_Texture.Height) >= float (WINDOW_HEIGHT) - 1.0 then
Dy := -Dy;
Y := Y + Dy * Delta_Time;
end if;
Begin_Drawing;
Clear_Background (BLACK);
Draw_Texture_V (Logo_Texture, (X => X, Y => Y), WHITE);
End_Drawing;
end loop;
Unload_Texture (Logo_Texture);
Close_Window;
end Main;

View File

@@ -4,7 +4,7 @@
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define LOGO_SCALE 0.04f
#define LOGO_HEIGHT 64
#define LOGO_SPEED 300
int main()
@@ -13,13 +13,13 @@ int main()
SetTargetFPS(60);
SetRandomSeed(time(NULL));
Texture2D logo_texture = LoadTexture("./c_logo.png");
Image logo_image = LoadImage("./c_logo.png");
ImageResize(&logo_image, logo_image.width * LOGO_HEIGHT / logo_image.height, LOGO_HEIGHT);
Texture2D logo_texture = LoadTextureFromImage(logo_image);
UnloadImage(logo_image);
float logo_width = logo_texture.width * LOGO_SCALE;
float logo_height = logo_texture.height * LOGO_SCALE;
float x = GetRandomValue(0, WINDOW_WIDTH - logo_width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_height);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_texture.width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height);
float dx = LOGO_SPEED * (GetRandomValue(0, 1) ? -1 : 1);
float dy = LOGO_SPEED * (GetRandomValue(0, 1) ? -1 : 1);
@@ -31,12 +31,12 @@ int main()
x += dx * delta_time;
y += dy * delta_time;
if (x < 0 || (x + logo_width) >= WINDOW_WIDTH - 1)
if (x < 0 || (x + logo_texture.width) >= WINDOW_WIDTH - 1)
{
dx *= -1;
x += dx * delta_time;
}
if (y < 0 || (y + logo_height) >= WINDOW_HEIGHT - 1)
if (y < 0 || (y + logo_texture.height) >= WINDOW_HEIGHT - 1)
{
dy *= -1;
y += dy * delta_time;
@@ -45,7 +45,7 @@ int main()
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(logo_texture, (Vector2){x, y}, 0, LOGO_SCALE, WHITE);
DrawTextureV(logo_texture, (Vector2){x, y}, WHITE);
EndDrawing();
}

View File

@@ -1,7 +1,18 @@
using System;
using System.Runtime.InteropServices;
static class Program
{
[StructLayout(LayoutKind.Sequential)]
struct Image
{
public IntPtr data;
public int width;
public int height;
public int mipmaps;
public int format;
}
[StructLayout(LayoutKind.Sequential)]
struct Texture2D
{
@@ -31,14 +42,16 @@ static class Program
private const string RAYLIB = "../_raylib-5.5_linux_amd64/lib/libraylib.so";
[DllImport(RAYLIB)] private static extern void InitWindow(int width, int height, string title);
[DllImport(RAYLIB)] private static extern void CloseWindow();
[DllImport(RAYLIB)] private static extern void SetTargetFPS(int fps);
[DllImport(RAYLIB)] private static extern void SetRandomSeed(uint seed);
[DllImport(RAYLIB)] private static extern int GetRandomValue(int min, int max);
[DllImport(RAYLIB)] private static extern Texture2D LoadTexture(string file_name);
[DllImport(RAYLIB)] private static extern Image LoadImage(string file_name);
[DllImport(RAYLIB)] private static extern void ImageResize(ref Image image, int new_width, int new_height);
[DllImport(RAYLIB)] private static extern Texture2D LoadTextureFromImage(Image image);
[DllImport(RAYLIB)] private static extern void UnloadImage(Image image);
[DllImport(RAYLIB)] private static extern void UnloadTexture(Texture2D texture);
[DllImport(RAYLIB)] private static extern bool WindowShouldClose();
@@ -46,12 +59,12 @@ static class Program
[DllImport(RAYLIB)] private static extern void BeginDrawing();
[DllImport(RAYLIB)] private static extern void ClearBackground(Color color);
[DllImport(RAYLIB)] private static extern void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);
[DllImport(RAYLIB)] private static extern void DrawTextureV(Texture2D texture, Vector2 position, Color tint);
[DllImport(RAYLIB)] private static extern void EndDrawing();
private const int WINDOW_WIDTH = 800;
private const int WINDOW_HEIGHT = 600;
private const float LOGO_SCALE = 0.05f;
private const int LOGO_HEIGHT = 64;
private const float LOGO_SPEED = 300;
private static Color BLACK = new Color() { r = 0, g = 0, b = 0, a = 255 };
@@ -62,13 +75,13 @@ static class Program
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in C#");
SetTargetFPS(60);
Texture2D logo_texture = LoadTexture("./csharp_logo.png");
Image logo_image = LoadImage("./csharp_logo.png");
ImageResize(ref logo_image, logo_image.width * LOGO_HEIGHT / logo_image.height, LOGO_HEIGHT);
Texture2D logo_texture = LoadTextureFromImage(logo_image);
UnloadImage(logo_image);
int logo_width = (int)(logo_texture.width * LOGO_SCALE);
int logo_height = (int)(logo_texture.height * LOGO_SCALE);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_height);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_texture.width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height);
float dx = LOGO_SPEED * (GetRandomValue(0, 1) == 1 ? -1 : 1);
float dy = LOGO_SPEED * (GetRandomValue(0, 1) == 1 ? -1 : 1);
@@ -80,12 +93,12 @@ static class Program
x += dx * delta_time;
y += dy * delta_time;
if (x < 0 || (x + logo_width) >= WINDOW_WIDTH - 1)
if (x < 0 || (x + logo_texture.width) >= WINDOW_WIDTH - 1)
{
dx *= -1;
x += dx * delta_time;
}
if (y < 0 || (y + logo_height) >= WINDOW_HEIGHT - 1)
if (y < 0 || (y + logo_texture.height) >= WINDOW_HEIGHT - 1)
{
dy *= -1;
y += dy * delta_time;
@@ -94,7 +107,7 @@ static class Program
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(logo_texture, new Vector2() { x = x, y = y }, 0, LOGO_SCALE, WHITE);
DrawTextureV(logo_texture, new Vector2() { x = x, y = y }, WHITE);
EndDrawing();
}

View File

@@ -1,6 +1,14 @@
import std.stdio;
import std.datetime;
struct Image {
void* data;
int width;
int height;
int mipmaps;
int format;
}
struct Texture2D {
uint id;
int width;
@@ -29,7 +37,10 @@ extern(C):
void SetRandomSeed(uint seed);
int GetRandomValue(int min, int max);
Texture2D LoadTexture(const char* file_name);
Image LoadImage(const char* file_name);
void ImageResize(Image* image, int new_width, int new_height);
Texture2D LoadTextureFromImage(Image image);
void UnloadImage(Image image);
void UnloadTexture(Texture2D texture);
bool WindowShouldClose();
@@ -37,29 +48,29 @@ extern(C):
void BeginDrawing();
void ClearBackground(Color color);
void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);
void DrawTextureV(Texture2D texture, Vector2 position, Color tint);
void EndDrawing();
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const float LOGO_SCALE = 0.1f;
const int LOGO_HEIGHT = 64;
const float LOGO_SPEED = 300;
const Color BLACK = Color(0, 0, 0, 255);
const Color WHITE = Color(255, 255, 255, 255);
void main() {
InitWindow(800, 600, "Raylib in D");
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in D");
SetTargetFPS(60);
SetRandomSeed(cast(uint)Clock.currStdTime());
Texture2D logo_texture = LoadTexture("./d_logo.png");
Image logo_image = LoadImage("./d_logo.png");
ImageResize(&logo_image, logo_image.width * LOGO_HEIGHT / logo_image.height, LOGO_HEIGHT);
Texture2D logo_texture = LoadTextureFromImage(logo_image);
UnloadImage(logo_image);
int logo_width = cast(int)(logo_texture.width * LOGO_SCALE);
int logo_height = cast(int)(logo_texture.height * LOGO_SCALE);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_height);
float x = GetRandomValue(0, WINDOW_WIDTH - logo_texture.width);
float y = GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height);
float dx = LOGO_SPEED * (GetRandomValue(0, 1) ? -1 : 1);
float dy = LOGO_SPEED * (GetRandomValue(0, 1) ? -1 : 1);
@@ -70,12 +81,12 @@ void main() {
x += dx * delta_time;
y += dy * delta_time;
if (x < 0 || (x + logo_width) >= WINDOW_WIDTH - 1)
if (x < 0 || (x + logo_texture.width) >= WINDOW_WIDTH - 1)
{
dx *= -1;
x += dx * delta_time;
}
if (y < 0 || (y + logo_height) >= WINDOW_HEIGHT - 1)
if (y < 0 || (y + logo_texture.height) >= WINDOW_HEIGHT - 1)
{
dy *= -1;
y += dy * delta_time;
@@ -84,7 +95,7 @@ void main() {
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(logo_texture, Vector2(x, y), 0, LOGO_SCALE, WHITE);
DrawTextureV(logo_texture, Vector2(x, y), WHITE);
EndDrawing();
}

2
go/Makefile Normal file
View File

@@ -0,0 +1,2 @@
raylib_go: main.go
go build -o raylib_go main.go

8
go/README.md Normal file
View File

@@ -0,0 +1,8 @@
# Raylib in Go
## Quick start
```sh
$ make
$ ./raylib_go
```

BIN
go/go_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

66
go/main.go Normal file
View File

@@ -0,0 +1,66 @@
package main
/*
#cgo CFLAGS: -I../_raylib-5.5_linux_amd64/include
#cgo LDFLAGS: -L../_raylib-5.5_linux_amd64/lib -l:libraylib.a -lm
#include <raylib.h>
*/
import "C"
import "time"
const (
windowWidth = 800
windowHeight = 600
logoHeight = 64
logoSpeed = 300.0
)
func main() {
C.InitWindow(windowWidth, windowHeight, C.CString("Raylib in Go"))
C.SetTargetFPS(60)
C.SetRandomSeed(C.uint(time.Now().Unix()))
logoImage := C.LoadImage(C.CString("./go_logo.png"))
C.ImageResize(&logoImage, logoImage.width*logoHeight/logoImage.height, logoHeight)
logoTexture := C.LoadTextureFromImage(logoImage)
C.UnloadImage(logoImage)
x := float32(C.GetRandomValue(0, windowWidth-C.int(logoTexture.width)))
y := float32(C.GetRandomValue(0, windowHeight-C.int(logoTexture.height)))
dx := float32(logoSpeed)
if C.GetRandomValue(0, 1) == 1 {
dx = -dx
}
dy := float32(logoSpeed)
if C.GetRandomValue(0, 1) == 1 {
dy = -dy
}
black := C.Color{0, 0, 0, 255}
white := C.Color{255, 255, 255, 255}
for !bool(C.WindowShouldClose()) {
deltaTime := float32(C.GetFrameTime())
x += dx * deltaTime
y += dy * deltaTime
if x < 0 || x+float32(logoTexture.width) >= windowWidth-1 {
dx *= -1
x += dx * deltaTime
}
if y < 0 || y+float32(logoTexture.height) >= windowHeight-1 {
dy *= -1
y += dy * deltaTime
}
C.BeginDrawing()
C.ClearBackground(black)
C.DrawTextureV(logoTexture, C.Vector2{x: C.float(x), y: C.float(y)}, white)
C.EndDrawing()
}
C.UnloadTexture(logoTexture)
C.CloseWindow()
}

2
odin/Makefile Normal file
View File

@@ -0,0 +1,2 @@
raylib_odin: main.odin
odin build main.odin -file -out:raylib_odin

8
odin/README.md Normal file
View File

@@ -0,0 +1,8 @@
# Raylib in Odin
## Quick start
```sh
$ make
$ ./raylib_odin
```

107
odin/main.odin Normal file
View File

@@ -0,0 +1,107 @@
package main
import "core:time"
foreign import raylib {
"../_raylib-5.5_linux_amd64/lib/libraylib.a",
"system:m",
}
Image :: struct {
data: rawptr,
width: i32,
height: i32,
mipmaps: i32,
format: i32,
}
Texture2D :: struct {
id: u32,
width: i32,
height: i32,
mipmaps: i32,
format: i32,
}
Vector2 :: struct {
x: f32,
y: f32,
}
Color :: struct {
r: u8,
g: u8,
b: u8,
a: u8,
}
@(default_calling_convention = "c")
foreign raylib {
InitWindow :: proc(width: i32, height: i32, title: cstring) ---
CloseWindow :: proc() ---
SetTargetFPS :: proc(fps: i32) ---
SetRandomSeed :: proc(seed: u32) ---
GetRandomValue :: proc(min: i32, max: i32) -> i32 ---
LoadImage :: proc(file_name: cstring) -> Image ---
ImageResize :: proc(image: ^Image, new_width: i32, new_height: i32) ---
LoadTextureFromImage :: proc(image: Image) -> Texture2D ---
UnloadImage :: proc(image: Image) ---
UnloadTexture :: proc(texture: Texture2D) ---
WindowShouldClose :: proc() -> bool ---
GetFrameTime :: proc() -> f32 ---
BeginDrawing :: proc() ---
ClearBackground :: proc(color: Color) ---
DrawTextureV :: proc(texture: Texture2D, position: Vector2, tint: Color) ---
EndDrawing :: proc() ---
}
WINDOW_WIDTH :: 800
WINDOW_HEIGHT :: 600
LOGO_HEIGHT :: 64
LOGO_SPEED :: 300
BLACK :: Color{0, 0, 0, 255}
WHITE :: Color{255, 255, 255, 255}
main :: proc() {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Odin")
SetTargetFPS(60)
SetRandomSeed(cast(u32)time.now()._nsec)
logo_image := LoadImage("./odin_logo.png")
ImageResize(&logo_image, logo_image.width * LOGO_HEIGHT / logo_image.height, LOGO_HEIGHT)
logo_texture := LoadTextureFromImage(logo_image)
UnloadImage(logo_image)
x := f32(GetRandomValue(0, WINDOW_WIDTH - logo_texture.width))
y := f32(GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height))
dx: f32 = LOGO_SPEED
if GetRandomValue(0, 1) == 1 do dx = -dx
dy: f32 = LOGO_SPEED
if GetRandomValue(0, 1) == 1 do dy = -dy
for !WindowShouldClose() {
delta_time := GetFrameTime()
x += dx * delta_time
y += dy * delta_time
if x < 0 || x + f32(logo_texture.width) >= WINDOW_WIDTH - 1 {
dx *= -1
x += dx * delta_time
}
if y < 0 || y + f32(logo_texture.height) >= WINDOW_HEIGHT - 1 {
dy *= -1
y += dy * delta_time
}
BeginDrawing()
ClearBackground(BLACK)
DrawTextureV(logo_texture, Vector2{x, y}, WHITE)
EndDrawing()
}
UnloadTexture(logo_texture)
CloseWindow()
}

BIN
odin/odin_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

View File

@@ -3,6 +3,15 @@ from time import time
rl = CDLL("../_raylib-5.5_linux_amd64/lib/libraylib.so")
class Image(Structure):
_fields_ = [
("data", c_void_p),
("width", c_int),
("height", c_int),
("mipmaps", c_int),
("format", c_int),
]
class Texture2D(Structure):
_fields_ = [
("id", c_uint),
@@ -26,28 +35,29 @@ class Color(Structure):
("a", c_ubyte),
]
rl.LoadTexture.restype = Texture2D
rl.LoadImage.restype = Image
rl.LoadTextureFromImage.restype = Texture2D
rl.GetFrameTime.restype = c_float
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
LOGO_SCALE = 0.04
LOGO_HEIGHT = 64
LOGO_SPEED = 300
BLACK = Color(r=0,g=0,b=0,a=255)
WHITE = Color(r=255,g=255,b=255,a=255)
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Python")
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, b"Raylib in Python")
rl.SetTargetFPS(60)
rl.SetRandomSeed(int(time()))
logo_texture: Texture2D = rl.LoadTexture(b"./python_logo.png")
logo_image = rl.LoadImage(b"./python_logo.png")
rl.ImageResize(byref(logo_image), logo_image.width * LOGO_HEIGHT // logo_image.height, LOGO_HEIGHT)
logo_texture = rl.LoadTextureFromImage(logo_image)
rl.UnloadImage(logo_image)
logo_width = int(logo_texture.width * LOGO_SCALE)
logo_height = int(logo_texture.height * LOGO_SCALE)
x = rl.GetRandomValue(0, WINDOW_WIDTH - logo_width)
y = rl.GetRandomValue(0, WINDOW_HEIGHT - logo_height)
x = rl.GetRandomValue(0, WINDOW_WIDTH - logo_texture.width)
y = rl.GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height)
dx = LOGO_SPEED * (-1 if rl.GetRandomValue(0, 1) else 1)
dy = LOGO_SPEED * (-1 if rl.GetRandomValue(0, 1) else 1)
@@ -58,17 +68,17 @@ while not rl.WindowShouldClose():
x += dx * delta_time
y += dy * delta_time
if x < 0 or (x + logo_width) >= WINDOW_WIDTH - 1:
if x < 0 or (x + logo_texture.width) >= WINDOW_WIDTH - 1:
dx *= -1
x += dx * delta_time
if y < 0 or (y + logo_height) >= WINDOW_HEIGHT - 1:
if y < 0 or (y + logo_texture.height) >= WINDOW_HEIGHT - 1:
dy *= -1
y += dy * delta_time
rl.BeginDrawing()
rl.ClearBackground(BLACK)
rl.DrawTextureEx(logo_texture, Vector2(x=x, y=y), c_float(0), c_float(LOGO_SCALE), WHITE)
rl.DrawTextureV(logo_texture, Vector2(x=x, y=y), WHITE)
rl.EndDrawing()

View File

@@ -1,2 +1,2 @@
raylib_rust: main.rs
rustc main.rs -C link-args="-L ../_raylib-5.5_linux_amd64/lib -l:libraylib.a -lm" -o raylib_rust
rustc --edition 2021 main.rs -C link-args="-L ../_raylib-5.5_linux_amd64/lib -l:libraylib.a -lm" -o raylib_rust

View File

@@ -1,17 +1,20 @@
use std::{
ffi::{c_float, c_int, c_uchar, c_uint},
ffi::{c_char, c_float, c_int, c_uchar, c_uint, c_void},
time::{SystemTime, UNIX_EPOCH},
};
extern "C" {
fn InitWindow(width: c_int, height: c_int, title: *const u8);
fn InitWindow(width: c_int, height: c_int, title: *const c_char);
fn CloseWindow();
fn SetTargetFPS(fps: c_int);
fn SetRandomSeed(seed: c_uint);
fn GetRandomValue(min: c_int, max: c_int) -> c_int;
fn LoadTexture(file_name: *const u8) -> Texture2D;
fn LoadImage(file_name: *const c_char) -> Image;
fn ImageResize(image: *mut Image, new_width: c_int, new_height: c_int);
fn LoadTextureFromImage(image: Image) -> Texture2D;
fn UnloadImage(image: Image);
fn UnloadTexture(texture: Texture2D);
fn WindowShouldClose() -> bool;
@@ -19,16 +22,20 @@ extern "C" {
fn BeginDrawing();
fn ClearBackground(color: Color);
fn DrawTextureEx(
texture: Texture2D,
position: Vector2,
rotation: c_float,
scale: c_float,
tint: Color,
);
fn DrawTextureV(texture: Texture2D, position: Vector2, tint: Color);
fn EndDrawing();
}
#[derive(Clone, Copy)]
#[repr(C)]
struct Image {
data: *mut c_void,
width: c_int,
height: c_int,
mipmaps: c_int,
format: c_int,
}
#[derive(Clone, Copy)]
#[repr(C)]
struct Texture2D {
@@ -55,7 +62,7 @@ struct Color {
const WINDOW_WIDTH: c_int = 800;
const WINDOW_HEIGHT: c_int = 600;
const LOGO_SCALE: f32 = 0.04;
const LOGO_HEIGHT: c_int = 64;
const LOGO_SPEED: f32 = 300.0;
const BLACK: Color = Color {
@@ -73,7 +80,7 @@ const WHITE: Color = Color {
fn main() {
unsafe {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Rust".as_ptr());
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, c"Raylib in Rust".as_ptr());
SetTargetFPS(60);
SetRandomSeed(
SystemTime::now()
@@ -82,13 +89,17 @@ fn main() {
.as_secs() as u32,
);
let logo_texture = LoadTexture("./rust_logo.png".as_ptr());
let mut logo_image = LoadImage(c"./rust_logo.png".as_ptr());
ImageResize(
&mut logo_image,
logo_image.width * LOGO_HEIGHT / logo_image.height,
LOGO_HEIGHT,
);
let logo_texture = LoadTextureFromImage(logo_image);
UnloadImage(logo_image);
let logo_width = (logo_texture.width as f32 * LOGO_SCALE) as i32;
let logo_height = (logo_texture.height as f32 * LOGO_SCALE) as i32;
let mut x = GetRandomValue(0, WINDOW_WIDTH - logo_width) as f32;
let mut y = GetRandomValue(0, WINDOW_HEIGHT - logo_height) as f32;
let mut x = GetRandomValue(0, WINDOW_WIDTH - logo_texture.width) as f32;
let mut y = GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height) as f32;
let mut dx = LOGO_SPEED * if GetRandomValue(0, 1) == 1 { -1.0 } else { 1.0 };
let mut dy = LOGO_SPEED * if GetRandomValue(0, 1) == 1 { -1.0 } else { 1.0 };
@@ -99,11 +110,11 @@ fn main() {
x += dx * delta_time;
y += dy * delta_time;
if x < 0.0 || x + logo_width as f32 >= WINDOW_WIDTH as f32 - 1.0 {
if x < 0.0 || x + logo_texture.width as f32 >= WINDOW_WIDTH as f32 - 1.0 {
dx *= -1.0;
x += dx * delta_time;
}
if y < 0.0 || y + logo_height as f32 >= WINDOW_HEIGHT as f32 - 1.0 {
if y < 0.0 || y + logo_texture.height as f32 >= WINDOW_HEIGHT as f32 - 1.0 {
dy *= -1.0;
y += dy * delta_time;
}
@@ -111,7 +122,7 @@ fn main() {
BeginDrawing();
ClearBackground(BLACK);
DrawTextureEx(logo_texture, Vector2 { x, y }, 0.0, LOGO_SCALE, WHITE);
DrawTextureV(logo_texture, Vector2 { x, y }, WHITE);
EndDrawing();
}

2
zig/Makefile Normal file
View File

@@ -0,0 +1,2 @@
raylib_zig: main.zig
zig build-exe main.zig -I../_raylib-5.5_linux_amd64/include ../_raylib-5.5_linux_amd64/lib/libraylib.a -lm -femit-bin=raylib_zig

8
zig/README.md Normal file
View File

@@ -0,0 +1,8 @@
# Raylib in Zig
## Quick start
```sh
$ make
$ ./raylib_zig
```

53
zig/main.zig Normal file
View File

@@ -0,0 +1,53 @@
const std = @import("std");
const rl = @cImport({
@cInclude("raylib.h");
});
const WINDOW_WIDTH = 800;
const WINDOW_HEIGHT = 600;
const LOGO_HEIGHT = 64;
const LOGO_SPEED: f32 = 300;
const BLACK = rl.Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
const WHITE = rl.Color{ .r = 255, .g = 255, .b = 255, .a = 255 };
pub fn main() void {
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Zig");
rl.SetTargetFPS(60);
rl.SetRandomSeed(@intCast(std.time.timestamp()));
var logo_image = rl.LoadImage("./zig_logo.png");
rl.ImageResize(&logo_image, @divTrunc(logo_image.width * LOGO_HEIGHT, logo_image.height), LOGO_HEIGHT);
const logo_texture = rl.LoadTextureFromImage(logo_image);
rl.UnloadImage(logo_image);
var x: f32 = @floatFromInt(rl.GetRandomValue(0, WINDOW_WIDTH - logo_texture.width));
var y: f32 = @floatFromInt(rl.GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height));
var dx: f32 = if (rl.GetRandomValue(0, 1) == 1) -LOGO_SPEED else LOGO_SPEED;
var dy: f32 = if (rl.GetRandomValue(0, 1) == 1) -LOGO_SPEED else LOGO_SPEED;
while (!rl.WindowShouldClose()) {
const delta_time = rl.GetFrameTime();
x += dx * delta_time;
y += dy * delta_time;
if (x < 0 or x + @as(f32, @floatFromInt(logo_texture.width)) >= WINDOW_WIDTH - 1) {
dx *= -1;
x += dx * delta_time;
}
if (y < 0 or y + @as(f32, @floatFromInt(logo_texture.height)) >= WINDOW_HEIGHT - 1) {
dy *= -1;
y += dy * delta_time;
}
rl.BeginDrawing();
rl.ClearBackground(BLACK);
rl.DrawTextureV(logo_texture, rl.Vector2{ .x = x, .y = y }, WHITE);
rl.EndDrawing();
}
rl.UnloadTexture(logo_texture);
rl.CloseWindow();
}

BIN
zig/zig_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB