-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
201 lines (184 loc) · 7.13 KB
/
main.c
File metadata and controls
201 lines (184 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <assert.h>
#include <raylib.h>
#include <raymath.h>
typedef struct {
Texture2D texture;
Rectangle rectangle;
int frames;
int rows;
int cols;
} SpriteAnimation;
typedef struct {
SpriteAnimation sprite_animation;
int reverse;
int fps;
int current_frame;
float time_per_frame;
float time_counter;
} SpriteAnimationPlayer;
SpriteAnimation LoadSpriteAnimation(const char *path, const Rectangle size, const int frames) {
assert(path);
const Texture2D texture = LoadTexture(path);
if (texture.width == 0 || texture.height == 0) {
return (SpriteAnimation){};
}
SpriteAnimation sprite_animation = {
.texture = texture,
.rectangle = size,
.frames = frames,
.cols = (int) (((float) texture.width - size.x) / size.width),
.rows = (int) (((float) texture.height - size.y) / size.height),
};
if (sprite_animation.frames == 0) {
sprite_animation.frames = sprite_animation.rows * sprite_animation.cols;
}
return sprite_animation;
}
SpriteAnimationPlayer GetSpriteAnimationPlayer(const SpriteAnimation sprite_animation, const int fps) {
assert(sprite_animation.frames >0);
const SpriteAnimationPlayer player = {
.sprite_animation = sprite_animation,
.fps = fps,
.time_per_frame = 1.0 / (float) fps,
.current_frame = 0,
};
return player;
}
void SpriteAnimationPlayerDraw(SpriteAnimationPlayer *player, Rectangle place) {
player->time_counter += GetFrameTime();
assert(player->time_per_frame >0);
while (player->time_counter > player->time_per_frame) {
player->current_frame += player->reverse ? -1 : 1;
player->time_counter -= player->time_per_frame;
}
const SpriteAnimation sprite = player->sprite_animation;
if (false) {
// 好像用倒放的方式有点
if (player->current_frame < 0) {
player->current_frame *= -1;
player->reverse = 0;
}
if (player->current_frame > player->sprite_animation.frames) {
player->current_frame = player->sprite_animation.frames - (
player->current_frame % player->sprite_animation.frames);
player->reverse = 1;
}
} else if (player->current_frame >= player->sprite_animation.frames) {
player->current_frame = player->current_frame % player->sprite_animation.frames;
}
const int row = player->current_frame / player->sprite_animation.cols;
const int col = player->current_frame % player->sprite_animation.cols;
const Rectangle src = {
.x = sprite.rectangle.x + (float) col * sprite.rectangle.width,
.y = sprite.rectangle.y + (float) row * sprite.rectangle.width,
.width = sprite.rectangle.width,
.height = sprite.rectangle.height,
};
DrawTexturePro(sprite.texture, src, place, (Vector2){}, 0.0, WHITE);
}
#define CREATE_ANIMATION_PLAYER(PATH_SUFFIX, FRAME_PARAM) \
GetSpriteAnimationPlayer(LoadSpriteAnimation("spritesheet/mambo/" PATH_SUFFIX, \
(Rectangle){.height = 300, .width = 300}, \
FRAME_PARAM), 30)
const int width = 300;
const int height = 300;
int main(void) {
SetConfigFlags(FLAG_WINDOW_TRANSPARENT | FLAG_WINDOW_TOPMOST); // 如果希望透明窗口,必须在 InitWindows 之前执行
InitAudioDevice();
InitWindow(width, height, "Raylib Gremlin");
SetTargetFPS(60);
SetWindowState(FLAG_WINDOW_UNDECORATED);
bool dragging = false;
Vector2 origin = {};
Vector2 last = {};
float idle_time = 0;
const float sleep_timer = 30; // after 3s goto sleep
struct {
SpriteAnimationPlayer backward;
SpriteAnimationPlayer click;
SpriteAnimationPlayer emote;
SpriteAnimationPlayer forward;
SpriteAnimationPlayer grab;
SpriteAnimationPlayer hover;
SpriteAnimationPlayer idle;
SpriteAnimationPlayer intro;
SpriteAnimationPlayer left;
SpriteAnimationPlayer outro;
SpriteAnimationPlayer right;
SpriteAnimationPlayer sleep;
SpriteAnimationPlayer walk_idle;
} Gremlin = {
.backward = CREATE_ANIMATION_PLAYER("backward.png", 18),
.click = CREATE_ANIMATION_PLAYER("click.png", 120),
.emote = CREATE_ANIMATION_PLAYER("emote.png", 54),
.forward = CREATE_ANIMATION_PLAYER("forward.png", 18),
.grab = CREATE_ANIMATION_PLAYER("grab.png", 60),
.hover = CREATE_ANIMATION_PLAYER("hover.png", 18),
.idle = CREATE_ANIMATION_PLAYER("idle.png", 18),
.intro = CREATE_ANIMATION_PLAYER("intro.png", 56),
.left = CREATE_ANIMATION_PLAYER("left.png", 18),
.outro = CREATE_ANIMATION_PLAYER("outro.png", 18),
.right = CREATE_ANIMATION_PLAYER("right.png", 18),
.sleep = CREATE_ANIMATION_PLAYER("sleep.png", 74),
.walk_idle = CREATE_ANIMATION_PLAYER("walk-idle.png", 55),
};
SetMouseCursor(MOUSE_CURSOR_POINTING_HAND);
SpriteAnimationPlayer *state = &Gremlin.walk_idle;
while (!WindowShouldClose()) {
// 用户闲置状态检查
if (GetKeyPressed() != 0
|| IsMouseButtonDown(MOUSE_LEFT_BUTTON)
|| IsMouseButtonDown(MOUSE_RIGHT_BUTTON)
|| IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) {
idle_time = 0;
} else if (idle_time < sleep_timer) {
idle_time += GetFrameTime();
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
dragging = true;
origin = GetMousePosition();
} else if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
dragging = false;
} else if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
const Vector2 current = GetMousePosition();
if (current.x == last.x && current.y == last.y) {
} else {
last = GetMousePosition();
const Vector2 w = Vector2Add(GetWindowPosition(), Vector2Subtract(GetMousePosition(), origin));
SetWindowPosition((int) w.x, (int) w.y);
}
}
if (IsKeyPressed(KEY_ESCAPE)) {
break;
}
if (dragging) {
state = &Gremlin.grab;
} else if (IsKeyDown(KEY_W)) {
state = &Gremlin.backward;
} else if (IsKeyDown(KEY_S)) {
state = &Gremlin.forward;
} else if (IsKeyDown(KEY_A)) {
state = &Gremlin.left;
} else if (IsKeyDown(KEY_D)) {
state = &Gremlin.right;
} else if (IsKeyDown(KEY_E)) {
state = &Gremlin.emote;
} else if (IsKeyDown(KEY_Q)) {
state = &Gremlin.intro;
} else if (idle_time > sleep_timer) {
state = &Gremlin.sleep;
} else {
state = &Gremlin.walk_idle;
}
BeginDrawing();
ClearBackground(BLANK);
SpriteAnimationPlayerDraw(state, (Rectangle){
.width = width,
.height = height
});
EndDrawing();
}
CloseWindow();
CloseAudioDevice();
return 0;
}