The flag SDL_INIT_TIMER is now passed to SDL_Init() in init_sdl(), which allows the...
[xuni.git] / src / xuni.c
blob949f71d40814499f80585c54d7f0e45d8d41e12d
1 /*! \mainpage
3 xuni, a 2D game written with the SDL with its own unique GUI library \n
4 Copyright (C) 2007 DWK
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 The GNU General Public License version 2 is included in the file COPYING.
22 \author DWK
23 dwks@theprogrammingsite.com \n
24 http://dwks.theprogrammingsite.com \n
26 \version 1.0.0
28 At the time of this writing, xuni can be obtained from:
29 http://dwks.theprogrammingsite.com/myprogs/down/xuni.zip
31 ----
33 xuni stands for "Explore the Universe". It's entirely coincidental that
34 shifting one of the letters in "xuni" spells UNIX, and that reversing the
35 letters and adding another letter spells Linux. "xuni" is pronounced
36 "zoo-nee" in the author's opinion. But "ecks-you-nee" or
37 "ecks-you-en-eye" or other variations work as well.
39 xuni was written from the ground up without incorporating any code from my
40 numerous previous SDL projects. It uses SDL_image, SDL_gfx, and SDL_ttf
41 (my first project to do so) in addition to the SDL.
44 /*! \file xuni.c
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdarg.h>
51 #include <time.h>
53 #include "SDL.h"
55 #include "font.h"
56 #include "graphics.h"
57 #include "gui.h"
58 #include "loop.h"
59 #include "menu.h"
60 #include "settings.h"
61 #include "xuni.h"
63 static const char *get_ctime(void);
64 static void log_printf(const char *format, ...);
65 static void print_message(const char *type, const char *message,
66 const char *file, int line);
68 #ifdef WIN32
69 #include <windows.h>
70 int STDCALL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow) {
71 #else
72 int main(int argc, char *argv[]) {
73 #endif
74 struct smode_t smode;
75 struct font_t font;
76 struct theme_t theme;
77 struct gui_t gui;
79 load_settings(&smode, &font, &theme, &gui);
81 init_sdl(&smode);
83 reload_fonts(&font, &smode);
85 load_theme(&theme);
86 resize_theme(&theme, &smode, &font);
88 main_loop(&smode, &font, &theme, &gui);
90 write_settings(&smode, &font, &theme, &gui);
92 free_fonts(&font, 1);
93 free_theme(&theme);
94 free_gui(&gui);
95 quit_sdl();
96 return 0;
99 char *duplicate_string(const char *str, size_t len) {
100 char *p = malloc(len + 1);
101 strcpy(p, str);
102 return p;
105 static const char *get_ctime(void) {
106 time_t tt = time(0);
108 return ctime(&tt);
111 /*! Calls vfprintf() for stderr and the file LOG_FILE with the same data.
112 @param format Format string, including optional format specifiers
113 @param ... Variables matching format specifiers
115 static void log_printf(const char *format, ...) {
116 FILE *log = fopen(LOG_FILE, "at");
117 va_list arg;
119 va_start(arg, format);
120 vfprintf(stderr, format, arg);
121 va_end(arg);
123 if(log) {
124 va_start(arg, format);
125 vfprintf(log, format, arg);
126 va_end(arg);
128 fclose(log);
132 static void print_message(const char *type, const char *message,
133 const char *file, int line) {
135 const char *sdlerror = SDL_GetError(), *t = get_ctime();
137 log_printf("xuni: %s from %s:%i at %s %s\n SDL: %s\n",
138 type, file, line, t, message, sdlerror);
141 void print_warning(const char *message, const char *file, int line) {
142 print_message("Warning", message, file, line);
145 void settings_error(const char *message, const char *sfile, int sline) {
146 const char *t = get_ctime();
148 log_printf("xuni: Error in settings file %s:%i at %s %s\n",
149 sfile, sline, t, message);
152 void fatal_error(const char *message, const char *file, int line) {
153 print_message("Fatal error", message, file, line);
155 quit_sdl();
156 exit(1);