-/* $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
#include <stdlib.h>
#include <string.h>
/* XXX UNIX DEBUG */
-#include <signal.h>
+/* #include <signal.h> */
/* THIRD PARTY LIBRARY HEADERS */
#include <SDL.h>
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 */
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 *);
/* Initialization */
screen_init();
- /* XXX UNIX DEBUGGING! */
- signal(SIGSEGV, SIG_DFL);
+ /* XXX UNIX DEBUGGING */
+ /* signal(SIGSEGV, SIG_DFL); */
trig_init();
(void) SDL_ShowCursor(0);
(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,
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
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;
&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);
}
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);
+ }
}
}
}
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 */
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
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 *
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 */
-/* $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>
int screen_width, screen_height;
SDL_Joystick *gamepad;
+static font_t *error_font = NULL;
+
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);
+}
+