refactor: constant logo height instead of using scale
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user