refactor: constant logo height instead of using scale
This commit is contained in:
20
c/main.c
20
c/main.c
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -102,4 +115,4 @@ static class Program
|
||||
UnloadTexture(logo_texture);
|
||||
CloseWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
39
d/main.d
39
d/main.d
@@ -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,11 +95,11 @@ void main() {
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(BLACK);
|
||||
DrawTextureEx(logo_texture, Vector2(x, y), 0, LOGO_SCALE, WHITE);
|
||||
DrawTextureV(logo_texture, Vector2(x, y), WHITE);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
UnloadTexture(logo_texture);
|
||||
CloseWindow();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,19 +3,28 @@ 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),
|
||||
("width", c_int),
|
||||
("height", c_int),
|
||||
("mipmaps", c_int),
|
||||
("format", c_int),
|
||||
("id", c_uint),
|
||||
("width", c_int),
|
||||
("height", c_int),
|
||||
("mipmaps", c_int),
|
||||
("format", c_int),
|
||||
]
|
||||
|
||||
class Vector2(Structure):
|
||||
_fields_ = [
|
||||
("x", c_float),
|
||||
("y", c_float),
|
||||
("x", c_float),
|
||||
("y", c_float),
|
||||
]
|
||||
|
||||
class Color(Structure):
|
||||
@@ -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,19 +68,19 @@ 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()
|
||||
|
||||
rl.UnloadTexture(logo_texture)
|
||||
rl.CloseWindow()
|
||||
rl.CloseWindow()
|
||||
|
||||
@@ -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
|
||||
53
rust/main.rs
53
rust/main.rs
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user