From 6b4602a9846fa53e123ec8436329e0f7fa04570f Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sat, 20 Dec 2025 00:32:49 +0900 Subject: [PATCH] better --- client/gl.c | 12 ++++++++---- client/main.c | 4 ++-- client/net.c | 29 +++++++++++++++++++++++++++++ client/ui.c | 2 +- include/af_client.h | 8 ++------ 5 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 client/net.c diff --git a/client/gl.c b/client/gl.c index bbb7d03..c4d48b3 100644 --- a/client/gl.c +++ b/client/gl.c @@ -9,8 +9,8 @@ double gl_cam_z; int gl_scene = AF_SCENE_MAIN; gl_scene_t gl_scenes[] = { - {gl_main_changed, gl_main_init, gl_main_draw}, /**/ - {gl_connecting_changed, gl_connecting_init, gl_connecting_draw} /**/ + {gl_main_changed, gl_main_init, gl_main_draw}, /**/ + {NULL, NULL, NULL} /**/ }; void gl_resize(int width, int height) { @@ -31,7 +31,7 @@ void gl_init(void) { void gl_render(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - gl_scenes[gl_scene].init(); + if(gl_scenes[gl_scene].init != NULL) gl_scenes[gl_scene].init(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -41,5 +41,9 @@ void gl_render(void) { glLoadIdentity(); gluLookAt(gl_cam_x, gl_cam_y, gl_cam_z, gl_cam_x + cos(gl_cam_lr / 180.0 * M_PI), gl_cam_y + sin(gl_cam_ud / 180.0 * M_PI), gl_cam_z + sin(gl_cam_lr / 180.0 * M_PI), 0, 1, 0); - gl_scenes[gl_scene].draw(); + if(gl_scenes[gl_scene].draw != NULL) gl_scenes[gl_scene].draw(); +} + +void gl_scene_change(void) { + if(gl_scenes[gl_scene].changed != NULL) gl_scenes[gl_scene].changed(); } diff --git a/client/main.c b/client/main.c index dd85505..f0a343f 100644 --- a/client/main.c +++ b/client/main.c @@ -36,8 +36,8 @@ static void* gl_load(const char* name) { void scene_change(int scene) { gl_scene = scene; - gl_scenes[gl_scene].changed(); - ui_scene(); + gl_scene_change(); + ui_scene_change(); } int main(int argc, char** argv) { diff --git a/client/net.c b/client/net.c new file mode 100644 index 0000000..f2e5a48 --- /dev/null +++ b/client/net.c @@ -0,0 +1,29 @@ +#include + +static fishsoup_client_t* client = NULL; + +static void net_return(MwWidget handle, void* user, void* client) { + scene_change(AF_SCENE_MAIN); +} + +void net_connect(const char* username, const char* hostname, int port) { + scene_change(AF_SCENE_CONNECTING); + client = fishsoup_client(hostname, port == 0 ? AF_DEFAULT_PORT : port); + if(client == NULL) { + MwAddUserHandler(ui_message("Failed to connect"), MwNactivateHandler, net_return, NULL); + return; + } +} + +void net_poll(void) { + int ret; + if(client == NULL) return; + + while((ret = fishsoup_client_poll(client)) > 0); + if(ret < 0) { + MwAddUserHandler(ui_message("Connection destroyed"), MwNactivateHandler, net_return, NULL); + fishsoup_client_destroy(client); + client = NULL; + return; + } +} diff --git a/client/ui.c b/client/ui.c index 78ddc70..3605c75 100644 --- a/client/ui.c +++ b/client/ui.c @@ -114,7 +114,7 @@ MwWidget ui_message(const char* message) { return wnd; } -void ui_scene(void) { +void ui_scene_change(void) { if(gl_scene == AF_SCENE_MAIN) { ui_login(); } diff --git a/include/af_client.h b/include/af_client.h index 53bc7a9..326e33d 100644 --- a/include/af_client.h +++ b/include/af_client.h @@ -37,6 +37,7 @@ extern double gl_cam_z; void gl_init(void); void gl_render(void); void gl_resize(int width, int height); +void gl_scene_change(void); /* gl_font.c */ void gl_font_init(void); @@ -49,11 +50,6 @@ void gl_main_changed(void); void gl_main_init(void); void gl_main_draw(void); -/* gl_connecting.c */ -void gl_connecting_changed(void); -void gl_connecting_init(void); -void gl_connecting_draw(void); - /* net.c */ void net_connect(const char* username, const char* hostname, int port); void net_poll(void); @@ -62,7 +58,7 @@ void net_poll(void); MwWidget ui_notice(int width, int height); void ui_notice_destroy(MwWidget widget); void ui_init(void); -void ui_scene(void); +void ui_scene_change(void); MwWidget ui_message(const char* message); #endif