2024-02-29 11:22:52 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2024-03-04 16:13:43 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-03-25 14:59:00 +08:00
|
|
|
#include <math.h>
|
2024-02-29 11:22:52 +08:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2024-04-24 22:23:01 +08:00
|
|
|
#define SCREEN_WIDTH 1440
|
|
|
|
#define SCREEN_HEIGHT 810
|
2024-03-19 11:59:09 +08:00
|
|
|
|
2024-04-14 15:22:55 +08:00
|
|
|
#define WARN(fmt, ...) fprintf(stderr, "[WARN][%s] " fmt "\n", __func__, ##__VA_ARGS__)
|
|
|
|
#define INFO(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", __func__, ##__VA_ARGS__)
|
2024-03-28 14:04:43 +08:00
|
|
|
|
2024-03-19 11:59:09 +08:00
|
|
|
|
2024-03-04 16:13:43 +08:00
|
|
|
static inline void *zero_malloc(size_t size) {
|
|
|
|
void *d = malloc(size);
|
|
|
|
memset(d, 0, size);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:04:43 +08:00
|
|
|
static inline char *copy_malloc(const char *src) {
|
|
|
|
size_t len = strlen(src);
|
|
|
|
char *p = (char *)malloc(len + 1);
|
|
|
|
memcpy(p, src, len + 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2024-04-02 08:56:35 +08:00
|
|
|
static inline void *copy_malloc_size(void *src, size_t size) {
|
|
|
|
void *p = malloc(size);
|
|
|
|
memcpy(p, src, size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2024-04-30 14:48:57 +08:00
|
|
|
static inline char *copy_realloc(char *original, const char *src) {
|
|
|
|
size_t len = strlen(src);
|
|
|
|
char *p = (char *)realloc(original, len + 1);
|
|
|
|
memcpy(p, src, len + 1);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:13:43 +08:00
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
// A 2d vector of double.
|
|
|
|
typedef struct {
|
|
|
|
double x, y;
|
|
|
|
} Vec2;
|
|
|
|
|
|
|
|
|
2024-03-05 14:17:53 +08:00
|
|
|
static inline Vec2 vec2(double x, double y) {
|
2024-03-01 14:37:59 +08:00
|
|
|
Vec2 v = {.x = x, .y = y};
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
Vec2 vec2_Add(Vec2 x, Vec2 y);
|
2024-03-08 14:51:29 +08:00
|
|
|
Vec2 vec2_Minus(Vec2 pos, Vec2 neg);
|
2024-02-29 11:22:52 +08:00
|
|
|
Vec2 vec2_Scale(Vec2 v, double scale);
|
|
|
|
|
2024-03-26 12:17:21 +08:00
|
|
|
Vec2 vec2_Random(double minX, double maxX, double minY, double maxY);
|
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
|
|
|
|
// A 2d box of double.
|
|
|
|
typedef struct {
|
|
|
|
Vec2 lefttop;
|
|
|
|
Vec2 size;
|
|
|
|
} Box2;
|
|
|
|
|
2024-03-30 22:05:49 +08:00
|
|
|
static inline Box2 box2(double x, double y, double width, double height) {
|
|
|
|
Box2 b;
|
|
|
|
b.lefttop.x = x;
|
|
|
|
b.lefttop.y = y;
|
|
|
|
b.size.x = width;
|
|
|
|
b.size.y = height;
|
|
|
|
return b;
|
|
|
|
}
|
2024-04-30 06:57:24 +08:00
|
|
|
static inline Box2 box2v(Vec2 lefttop, Vec2 size) {
|
|
|
|
Box2 b;
|
|
|
|
b.lefttop = lefttop;
|
|
|
|
b.size = size;
|
|
|
|
return b;
|
|
|
|
}
|
2024-03-30 22:05:49 +08:00
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
|
|
|
|
// Intersection test.
|
|
|
|
bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection);
|
2024-03-30 21:25:39 +08:00
|
|
|
// Contain test.
|
|
|
|
bool box2_Contains(const Box2 box, const Vec2 point);
|
2024-02-29 11:22:52 +08:00
|
|
|
|
2024-03-07 09:55:36 +08:00
|
|
|
Vec2 box2_Center(Box2 box);
|
2024-03-08 14:51:29 +08:00
|
|
|
Box2 box2_FromCenter(Vec2 center, Vec2 size);
|
2024-04-30 06:57:24 +08:00
|
|
|
Box2 box2_FromPivot(Vec2 position, Vec2 pivot, Vec2 size);
|
2024-03-07 09:55:36 +08:00
|
|
|
|
2024-02-29 16:51:46 +08:00
|
|
|
Box2 box2_Offset(Box2 box, Vec2 offset);
|
|
|
|
Box2 box2_OffsetX(Box2 box, double offsetX);
|
|
|
|
Box2 box2_OffsetY(Box2 box, double offsetY);
|
|
|
|
|
2024-03-30 21:25:39 +08:00
|
|
|
// Returns true if the area of the box is not 0.
|
|
|
|
bool box2_NotZero(Box2 box);
|
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
|
2024-03-01 14:37:59 +08:00
|
|
|
// Time duration.
|
2024-02-29 16:17:55 +08:00
|
|
|
typedef struct {
|
2024-03-01 15:06:58 +08:00
|
|
|
int64_t microseconds;
|
2024-02-29 16:17:55 +08:00
|
|
|
} Duration;
|
|
|
|
|
2024-03-25 14:59:00 +08:00
|
|
|
static inline double duration_Seconds(const Duration t) { return ((double)t.microseconds) / 1000000.0; }
|
|
|
|
static inline double duration_Milliseconds(const Duration t) { return ((double)t.microseconds) / 1000.0; }
|
|
|
|
static inline Duration duration_FromSeconds(double seconds) {
|
|
|
|
Duration d = {.microseconds = (int64_t)round(seconds * 1000.0 * 1000.0)};
|
|
|
|
return d;
|
|
|
|
}
|
2024-03-04 15:05:21 +08:00
|
|
|
// This function has a precision of at most 1ms under Windows. Sad
|
|
|
|
void duration_Sleep(const Duration t);
|
2024-02-29 16:17:55 +08:00
|
|
|
|
2024-03-04 15:05:21 +08:00
|
|
|
// A Time point, counted since a fixed point in the past.
|
2024-03-01 15:06:58 +08:00
|
|
|
typedef struct {
|
|
|
|
int64_t microseconds;
|
|
|
|
} TimePoint;
|
|
|
|
|
2024-04-22 05:11:11 +08:00
|
|
|
// No game logic should use the time_XXX functions for time.
|
|
|
|
// Use gametime_XXX instead.
|
|
|
|
TimePoint time_Now(); // Don't use this!
|
2024-04-02 09:29:46 +08:00
|
|
|
TimePoint time_After(TimePoint now, Duration after);
|
2024-04-22 05:11:11 +08:00
|
|
|
// Don't use this!
|
|
|
|
Duration time_Since(TimePoint prev);
|
|
|
|
Duration time_Difference(TimePoint now, TimePoint prev);
|
|
|
|
// Don't use this!
|
|
|
|
Duration time_Reset(TimePoint *prev);
|
2024-03-01 14:37:59 +08:00
|
|
|
|
2024-03-01 17:09:24 +08:00
|
|
|
// 1e-6
|
|
|
|
extern const double EPS;
|
|
|
|
|
2024-02-29 16:17:55 +08:00
|
|
|
|
2024-02-29 11:22:52 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|