- Fix some input sync issue where we sometimes discarded auth packets
authorMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 25 Jun 2014 11:52:24 +0000 (11:52 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Wed, 25 Jun 2014 11:52:24 +0000 (11:52 +0000)
- Write a graphical SDL error reporting function and use it instead of
  stderr

mmsoftware/mystic_ships/client/src/main.c
mmsoftware/mystic_ships/client/src/screen.c
mmsoftware/mystic_ships/client/src/screen.h

index 83fbdb2..6c2df42 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.5 2014/06/25 07:47:28 mmondor Exp $ */
+/* $Id: main.c,v 1.6 2014/06/25 11:52:24 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -14,7 +14,7 @@
 #include <stdlib.h>
 #include <string.h>
 /* XXX UNIX DEBUG */
-#include <signal.h>
+/* #include <signal.h> */
 
 /* THIRD PARTY LIBRARY HEADERS */
 #include <SDL.h>
@@ -53,15 +53,6 @@ enum userevents {
        UE_THRUST_DEC
 };
 
-struct font {
-       int     w, h;
-       void    *data;
-       size_t  size;
-};
-
-#define        VECTOR_X(x, a, r)       ((int)(x) + (cos_table[(a)] * (r)))
-#define        VECTOR_Y(y, a, r)       ((int)(y) + (sin_table[(a)] * (r)))
-
 
 
 /* PRIVATE PROTOTYPES */
@@ -77,9 +68,7 @@ static int            surface_blit_angle(SDL_Surface *, int, int, int);
 
 static Mix_Chunk       *sample_load(const uint8_t *, size_t);
 
-static struct font     *font_load(const uint8_t *, size_t, int, int);
-static void            font_blit_string(struct font *, int, int,
-                           const char *, uint8_t, uint8_t, uint8_t, uint8_t);
+static font_t          *font_load(const uint8_t *, size_t, int, int);
 
 static Uint32          interval_callback(Uint32, void *);
 
@@ -134,8 +123,8 @@ main(int argc, char **argv)
        /* Initialization */
        screen_init();
 
-       /* XXX UNIX DEBUGGING! */
-       signal(SIGSEGV, SIG_DFL);
+       /* XXX UNIX DEBUGGING */
+       /* signal(SIGSEGV, SIG_DFL); */
 
        trig_init();
        (void) SDL_ShowCursor(0);
@@ -152,22 +141,25 @@ main(int argc, char **argv)
        (void) SDL_EventState(SDL_KEYUP, SDL_IGNORE);
 
        /*
+        * Bitmap fonts
+        */
+       font = font_load(static_7x13_fnt_enc_data,
+           static_7x13_fnt_enc_size, 7, 13);
+       screen_error_setfont(font);
+
+       /*
         * Network
         */
-       if (SDLNet_Init() != 0) {
-               (void) fprintf(stderr, "main() - SDLNet_Init() - %s\n",
+       if (SDLNet_Init() != 0)
+               screen_error("main() - SDLNet_Init() - %s",
                    SDLNet_GetError());
-               exit(EXIT_FAILURE);
-       }
 
        /*
         * Audio
         */
-       if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) != 0) {
-               (void) fprintf(stderr, "main() - Mix_OpenAudio() - %s\n",
+       if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) != 0)
+               screen_error("main() - Mix_OpenAudio() - %s",
                    Mix_GetError());
-               exit(EXIT_FAILURE);
-       }
        snd_cloak = sample_load(static_nt_cloaked_wav_enc_data,
            static_nt_cloaked_wav_enc_size);
        snd_uncloak = sample_load(static_nt_uncloak_wav_enc_data,
@@ -184,16 +176,10 @@ main(int argc, char **argv)
            static_nt_explosion_other_wav_enc_size);
        (void) Mix_AllocateChannels(16);
        (void) Mix_ReserveChannels(2);
-       if ((mus = Mix_LoadMUS("ogg/1.ogg")) == NULL) {
-               (void) fprintf(stderr, "main() - Mix_LoadMUS() - %s\n",
-                   Mix_GetError());
-               exit(EXIT_FAILURE);
-       }
-       if (Mix_PlayMusic(mus, -1) != 0) {
-               (void) fprintf(stderr, "main() - Mix_PlayMusic() - %s\n",
-                   Mix_GetError());
-               exit(EXIT_FAILURE);
-       }
+       if ((mus = Mix_LoadMUS("ogg/1.ogg")) == NULL)
+               screen_error("main() - Mix_LoadMUS() - %s", Mix_GetError());
+       if (Mix_PlayMusic(mus, -1) != 0)
+               screen_error("main() - Mix_PlayMusic() - %s", Mix_GetError());
 
        /*
         * Bitmap graphics
@@ -204,59 +190,38 @@ main(int argc, char **argv)
            static_FedCA_bmp_enc_size);
 
        /*
-        * Bitmap fonts
-        */
-       font = font_load(static_7x13_fnt_enc_data,
-           static_7x13_fnt_enc_size, 7, 13);
-
-       /*
         * We're already the main thread.
         * Initialize our message port and notification ring.
         */
-       if (thread_ring_init(&main_ring) == -1) {
-               (void) fprintf(stderr,
-                   "main() - thread_ring_init(main_port) - %s\n",
+       if (thread_ring_init(&main_ring) == -1)
+               screen_error("main() - thread_ring_init(main_port) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
-       if (thread_port_init(&main_port) == -1) {
-               (void) fprintf(stderr,
-                   "main() - thread_port_init(main_port) - %s\n",
+       if (thread_port_init(&main_port) == -1)
+               screen_error("main() - thread_port_init(main_port) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
        thread_port_set_ring(&main_port, &main_ring);
 
        /* XXX We should obtain user login information */
 
-       if (thread_amsg_pool_init() != 0) {
-               (void) fprintf(stderr, "main() - thread_amsg_init()\n");
-               exit(EXIT_FAILURE);
-       }
+       if (thread_amsg_pool_init() != 0)
+               screen_error("main() - thread_amsg_init()");
 
        /* Launch network utility threads */
        if ((recv_threadid = SDL_CreateThread(thread_net_recv, NULL))
-           == NULL) {
-               (void) fprintf(stderr,
-                   "main() - SDL_CreateThread(thread_net_recv) - %s\n",
+           == NULL)
+               screen_error(
+                   "main() - SDL_CreateThread(thread_net_recv) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
        if ((send_threadid = SDL_CreateThread(thread_net_send, NULL))
-           == NULL) {
-               (void) fprintf(stderr,
-                   "main() - SDL_CreateThread(thread_net_send) - %s\n",
+           == NULL)
+               screen_error(
+                   "main() - SDL_CreateThread(thread_net_send) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
 
        /*
-        * XXX Wait until we receive the server's connection status.
-        * We should display a "connecting to server" or such message to the
-        * user during this time.
+        * Wait until we receive the server's connection status.
         */
-       font_blit_string(font, 16, 16,
-           "Connecting to server, press [SPACE] to abort.",
+       font_blit_string(font, 16, 16, "Connecting to server...",
            0x00, 0xff, 0x00, 0xff);
        {
                struct msg_connect      *cmsg;
@@ -265,10 +230,8 @@ main(int argc, char **argv)
                    &main_port)) == NULL)
                        (void) thread_ring_wait(&main_ring, -1);
 
-               if (cmsg->status == -1) {
-                       (void) fprintf(stderr, "%s\n", cmsg->error);
-                       exit(EXIT_FAILURE);
-               }
+               if (cmsg->status == -1)
+                       screen_error("%s", cmsg->error);
 
                (void) thread_msg_reply(&cmsg->msg);
        }
@@ -372,13 +335,17 @@ main(int argc, char **argv)
                                pending_many = 0;
                                /* Drop pending frames */
                                while ((amsg = (thread_amsg_t *)thread_msg_get(
-                                   &main_port)) != NULL)
+                                   &main_port)) != NULL) {
+                                       spackets_handle(amsg);
                                        thread_amsg_destroy(amsg);
+                               }
                        } else {
                                /* Drop single frame */
                                if ((amsg = (thread_amsg_t *)thread_msg_get(
-                                   &main_port)) != NULL)
+                                   &main_port)) != NULL) {
+                                       spackets_handle(amsg);
                                        thread_amsg_destroy(amsg);
+                               }
                        }
                }
        }
@@ -608,10 +575,8 @@ send_event(int type, int arg1)
                break;
        }
 
-       if (err == -1) {
-               (void) fprintf(stderr, "Error writing to server socket\n");
-               exit(EXIT_FAILURE);
-       }
+       if (err == -1)
+               screen_error("Error writing to server socket");
 }
 
 /* Extern, called by spacket_eof_handler */
@@ -729,8 +694,9 @@ bmp_load_key(const uint8_t *data, size_t size)
        return s;
 
 err:
-       (void) fprintf(stderr, "bmp_load_key() - %s\n", SDL_GetError());
-       exit(EXIT_FAILURE);
+       screen_error("bmp_load_key() - %s", SDL_GetError());
+       /* NOTREACHED */
+       return NULL;
 }
 
 static int
@@ -780,8 +746,9 @@ sample_load(const uint8_t *data, size_t size)
        return c;
 
 err:
-       (void) fprintf(stderr, "sample_load() - %s\n", SDL_GetError());
-       exit(EXIT_FAILURE);
+       screen_error("sample_load() - %s", SDL_GetError());
+       /* NOTREACHED */
+       return NULL;
 }
 
 static struct font *
@@ -803,17 +770,9 @@ font_load(const uint8_t *data, size_t size, int width, int height)
        return font;
 
 err:
-       (void) fprintf(stderr, "font_load()\n");
-       exit(EXIT_FAILURE);
-}
-
-static void
-font_blit_string(struct font *font, int x, int y, const char *str,
-    uint8_t r, uint8_t g, uint8_t b, uint8_t a)
-{
-
-       (void) gfxPrimitivesSetFont(font->data, font->w, font->h);
-       (void) stringRGBA(screen_surface, x, y, str, r, g, b, a);
+       screen_error("font_load()");
+       /* NOTREACHED */
+       return NULL;
 }
 
 /* ARGSUSED */
index d2fbc64..cf2e2d4 100644 (file)
@@ -1,20 +1,23 @@
-/* $Id: screen.c,v 1.2 2014/06/19 19:53:34 mmondor Exp $ */
+/* $Id: screen.c,v 1.3 2014/06/25 11:52:24 mmondor Exp $ */
 
 /*
- * Copyright (c) 2006, Matthew Mondor
+ * Copyright (c) 2006, 2014, Matthew Mondor
  * ALL RIGHTS RESERVED.
  */
 
 /*
- * Display related initialization.
+ * Display related
  */
 
 
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include <SDL.h>
+#include <SDL_gfxPrimitives.h>
 
 #include <conf.h>
 #include <screen.h>
@@ -30,6 +33,8 @@ SDL_Surface   *screen_surface;
 int            screen_width, screen_height;
 SDL_Joystick   *gamepad;
 
+static font_t  *error_font = NULL;
+
 
 
 void
@@ -63,3 +68,39 @@ screen_destroy(void)
 
        SDL_Quit();
 }
+
+void
+font_blit_string(font_t *font, int x, int y, const char *str,
+    uint8_t r, uint8_t g, uint8_t b, uint8_t a)
+{
+
+       (void) gfxPrimitivesSetFont(font->data, font->w, font->h);
+       (void) stringRGBA(screen_surface, x, y, str, r, g, b, a);
+}
+
+void
+screen_error_setfont(font_t *font)
+{
+
+       error_font = font;
+}
+
+void
+screen_error(const char *fmt, ...)
+{
+       char    buf[81];
+       va_list ap;
+
+       va_start(ap, fmt);
+       (void) vsnprintf(buf, 80, fmt, ap);
+       va_end(ap);
+
+       if (error_font != NULL) {
+               font_blit_string(error_font, 32, 32, buf,
+                   0xff, 0x0a, 0x0a, 0xff);
+               (void) sleep(3);
+       } else
+               (void) fprintf(stderr, "%s\n", buf);
+       exit(EXIT_FAILURE);
+}
+
index 1c901a8..57cc6c2 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: screen.h,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: screen.h,v 1.2 2014/06/25 11:52:24 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
 
 
 
+typedef struct font {
+       int     w, h;
+       void    *data;
+       size_t  size;
+} font_t;
+
+
+
 extern SDL_Surface     *screen_surface;
 extern int             screen_width, screen_height;
 extern SDL_Joystick    *gamepad;
@@ -28,6 +36,10 @@ extern SDL_Joystick  *gamepad;
 
 void                   screen_init(void);
 void                   screen_destroy(void);
+void                   font_blit_string(font_t *, int, int, const char *,
+                           uint8_t, uint8_t, uint8_t, uint8_t);
+void                   screen_error_setfont(font_t *);
+void                   screen_error(const char *, ...);