Fix most warnings.
[runemen.git] / src / utils.c
blob656f7758037bd2d23b841fcb90dab13c2ae0fb50
1 #include <SDL.h>
3 #include "rune.h"
4 #include "ui.h"
6 /* Try to find a window size closest to VGA (640x480) */
7 SDL_DisplayMode* getWindowSize(SDL_DisplayMode *desired) {
8 int display = 0;
10 SDL_DisplayMode desktop;
11 static SDL_DisplayMode ret;
13 if (SDL_GetDesktopDisplayMode(display, &desktop)) {
14 fprintf(stderr, "Unable to get desktop display mode: %s\n", SDL_GetError());
15 return NULL;
18 /* Desktop is weirdly small, use the whole of it... */
19 if (desktop.w < desired->w || desktop.h < desired->h) {
21 ret.w = desktop.w;
22 ret.h = desktop.h;
25 else {
27 /* Enlarge until we can't fit */
28 Uint32 w = desired->w;
29 Uint32 h = desired->h;
31 do {
32 ret.w = w;
33 ret.h = h;
34 w *= 2;
35 h *= 2;
36 } while (w < desktop.w && h < desktop.h);
39 return &ret;
42 /* Try to find fullscreen mode closest to VGA (640x480) */
43 SDL_DisplayMode* getFullscreenSize(SDL_DisplayMode *desired) {
44 int display = 0;
46 static SDL_DisplayMode best;
48 int i;
49 int best_mode = -1;
50 int best_fits = 0;
51 int num_modes = SDL_GetNumDisplayModes(display);
53 /* Find smallest mode closest to desired */
54 for (i = 0; i < num_modes; i++) {
56 SDL_DisplayMode mode;
57 if (SDL_GetDisplayMode(display, i, &mode)) {
58 fprintf(stderr, "Unable to get display mode %d: %s\n", i, SDL_GetError());
59 continue;
62 //printf("Mode %d -- %d x %d\n", i, mode.w, mode.h);
64 /* Correct aspect ration (VGA) */
65 if (mode.w % desired->w == 0 && mode.h % desired->h == 0) {
67 /* AND it's smaller, pick it */
68 if (best_mode == -1 || (mode.w < best.w && mode.h < best.h)) {
70 best_mode = i;
71 best_fits = 1;
72 best.w = mode.w;
73 best.h = mode.h;
77 /* WRONG aspect ratio */
78 else {
79 /* Still going to pick it, if we don't have anything better */
80 if (best_mode == -1 || (best_fits == 0 && (mode.w < best.w && mode.h < best.h)) ) {
82 best_mode = i;
83 best_fits = 0;
84 best.w = mode.w;
85 best.h = mode.h;
91 return &best;
94 void generate_name(unit_t *u) {
96 static const char first_names[][80] = {
97 "Gaborn", "Jaz", "Talon", "Fallion",
98 "Myrrima", "Rhianna", "Averan", "Erin",
99 "Jas Laren", "Mendellas", "Roland",
100 "Saffira", "Skalbairn", "Chemoise", "Wuqaz", "Sarka",
101 "Raj", "Feykaald", "Jureem", "Ivarian", "Aelgir",
102 "Waggit", "Conall", "Celinor", "Connall", "Spring",
103 "Anders", "Zandaros", "Binnesman", "Paldane",
104 "Solette", "Poll", "Faharaqin", "Kaul",
105 "Ahten", "Sylvarresta", "Borenson",
108 int i = rand() % (sizeof(first_names) / (sizeof(char) * 80));
109 sprintf(u->name, "%s", first_names[i]);
112 int count_fps(void) {
114 static Uint32 wait = 0;
115 static Uint32 fps = 0;
116 static Uint32 fps_counter = 0;
118 Uint32 tick = SDL_GetTicks();
119 Uint32 pass = tick - wait;
121 fps_counter++;
123 if (pass >= 1000) {
124 fps = fps_counter;
125 fps_counter = 0;
126 wait = tick;
129 return fps;
132 int profile(Uint8 id, const char *desc) {
134 static Uint32 wait[256] = { 0 };
136 Uint32 tick = SDL_GetTicks();
137 Uint32 pass = tick - wait[id];
139 if (desc != NULL) {
140 printf("Profiler: %s: %d\n", desc, pass);
142 wait[id] = tick;
144 return 0;