font.c was created, and the Makefiles updated to reflect this. It contains reload_fon...
[xuni.git] / xuni.c
blob4a8e45027eee0c653008395cd08c38d55be82ba9
1 /*! \mainpage xuni is a 2D game written with the SDL and released under the
2 GNU General Public License (GPL). See the file COPYING for licensing
3 details.
5 xuni stands for "Explore the Universe". It's entirely coincidental that
6 shifting one of the letters in "xuni" spells UNIX, and that reversing the
7 letters and adding another letter spells Linux. "xuni" is pronounced
8 "zoo-nee" in the author's opinion. But "ecks-you-nee" or
9 "ecks-you-en-eye" or other variations work as well.
11 xuni was written from the ground up without incorporating any code from my
12 numerous previous SDL projects. It uses SDL_image, SDL_gfx, and SDL_ttf
13 (my first project to do so) in addition to the SDL.
16 /*! \file xuni.c
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <time.h>
25 #include "SDL.h"
27 #include "font.h"
28 #include "graphics.h"
29 #include "gui.h"
30 #include "loop.h"
31 #include "menu.h"
32 #include "settings.h"
33 #include "xuni.h"
35 static const char *get_ctime(void);
36 static void log_printf(const char *format, ...);
37 static void print_message(const char *type, const char *message,
38 const char *file, int line);
40 #ifdef WIN32
41 #include <windows.h>
42 int STDCALL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow) {
43 #else
44 int main(int argc, char *argv[]) {
45 #endif
46 struct smode_t smode;
47 struct font_t font;
48 struct theme_t theme;
49 struct gui_t gui;
51 load_settings(&smode, &font, &theme, &gui);
53 init_sdl(&smode);
55 reload_fonts(&font, &smode);
57 load_theme(&theme);
58 resize_theme(&theme, &smode);
60 main_loop(&smode, &font, &theme, &gui);
62 write_settings(&smode, &font, &theme, &gui);
64 free_fonts(&font, 1);
65 free_theme(&theme);
66 free_gui(&gui);
67 quit_sdl();
68 return 0;
71 char *duplicate_string(const char *str, size_t len) {
72 char *p = malloc(len + 1);
73 strcpy(p, str);
74 return p;
77 static const char *get_ctime(void) {
78 time_t tt = time(0);
80 return ctime(&tt);
83 /*! Calls vfprintf() for stderr and the file LOG_FILE with the same data.
84 @param format Format string, including optional format specifiers
85 @param ... Variables matching format specifiers
87 static void log_printf(const char *format, ...) {
88 FILE *log = fopen(LOG_FILE, "at");
89 va_list arg;
91 va_start(arg, format);
92 vfprintf(stderr, format, arg);
93 va_end(arg);
95 if(log) {
96 va_start(arg, format);
97 vfprintf(log, format, arg);
98 va_end(arg);
100 fclose(log);
104 static void print_message(const char *type, const char *message,
105 const char *file, int line) {
107 const char *sdlerror = SDL_GetError(), *t = get_ctime();
109 log_printf("xuni: %s from %s:%i at %s %s\n SDL: %s\n",
110 type, file, line, t, message, sdlerror);
113 void print_warning(const char *message, const char *file, int line) {
114 print_message("Warning", message, file, line);
117 void settings_error(const char *message, const char *sfile, int sline) {
118 const char *t = get_ctime();
120 log_printf("xuni: Error in settings file %s:%i at %s %s\n",
121 sfile, sline, t, message);
124 void fatal_error(const char *message, const char *file, int line) {
125 print_message("Fatal error", message, file, line);
127 quit_sdl();
128 exit(1);