Skip to content
This repository was archived by the owner on Mar 22, 2026. It is now read-only.

Commit 926ed34

Browse files
committed
[BlazeSDL] 添加 BlazeSDL 的 API
1 parent c61f1a5 commit 926ed34

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

blazesdl/api/BlazeSDLAPI.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package top.fifthlight.blazesdl.api;
2+
3+
import org.jspecify.annotations.NonNull;
4+
import top.fifthlight.blazesdl.api.impl.BlazeSDLAPIImpl;
5+
6+
public interface BlazeSDLAPI {
7+
static BlazeSDLAPI getInstance() {
8+
return BlazeSDLAPIImpl.getInstance();
9+
}
10+
11+
void registerEventHandler(@NonNull BlazeSDLEventHandler handler);
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package top.fifthlight.blazesdl.api;
2+
3+
import org.lwjgl.sdl.SDL_Event;
4+
5+
public interface BlazeSDLEventHandler {
6+
int getPriority();
7+
8+
boolean handleEvent(SDL_Event event);
9+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package top.fifthlight.blazesdl.api.impl;
2+
3+
import org.jspecify.annotations.NonNull;
4+
import org.lwjgl.sdl.SDL_Event;
5+
import top.fifthlight.blazesdl.api.BlazeSDLAPI;
6+
import top.fifthlight.blazesdl.api.BlazeSDLEventHandler;
7+
8+
import java.util.ArrayList;
9+
10+
public class BlazeSDLAPIImpl implements BlazeSDLAPI {
11+
private static final BlazeSDLAPIImpl instance = new BlazeSDLAPIImpl();
12+
private static final ArrayList<BlazeSDLEventHandler> eventHandlers = new ArrayList<>();
13+
14+
public static BlazeSDLAPIImpl getInstance() {
15+
return instance;
16+
}
17+
18+
@Override
19+
public synchronized void registerEventHandler(@NonNull BlazeSDLEventHandler handler) {
20+
var priority = handler.getPriority();
21+
var i = 0;
22+
for (; i < eventHandlers.size(); i++) {
23+
if (eventHandlers.get(i).getPriority() < priority) {
24+
break;
25+
}
26+
}
27+
eventHandlers.add(i, handler);
28+
}
29+
30+
public boolean handleEvent(SDL_Event event) {
31+
for (var handler : eventHandlers) {
32+
if (handler.handleEvent(event)) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
}

blazesdl/mixin/RenderSystemMixin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2121
import top.fifthlight.blazesdl.*;
2222
import top.fifthlight.blazesdl.SDLError;
23+
import top.fifthlight.blazesdl.api.impl.BlazeSDLAPIImpl;
2324

2425
@Mixin(RenderSystem.class)
2526
public class RenderSystemMixin {
@@ -51,6 +52,7 @@ private static long windowIdToHandle(int id) {
5152
@Redirect(method = "pollEvents", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwPollEvents()V"))
5253
private static void overridePollEvents() {
5354
var minecraft = Minecraft.getInstance();
55+
var apiImpl = BlazeSDLAPIImpl.getInstance();
5456
if (!(minecraft.getWindow() instanceof SDLWindow sdlWindow)) {
5557
GLFW.glfwPollEvents();
5658
return;
@@ -61,6 +63,10 @@ private static void overridePollEvents() {
6163
try (var stack = MemoryStack.stackPush()) {
6264
var event = SDL_Event.malloc(stack);
6365
while (SDLEvents.SDL_PollEvent(event)) {
66+
if (apiImpl.handleEvent(event)) {
67+
continue;
68+
}
69+
6470
var eventType = event.type();
6571
switch (eventType) {
6672
case SDLEvents.SDL_EVENT_DISPLAY_ADDED, SDLEvents.SDL_EVENT_DISPLAY_REMOVED ->

0 commit comments

Comments
 (0)