2009-11-19 19:23:12 +01:00
|
|
|
#ifndef __UTILS_H__
|
|
|
|
#define __UTILS_H__
|
|
|
|
|
2009-12-16 18:47:03 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-12-19 09:12:39 +01:00
|
|
|
#include <SDL.h>
|
2009-12-16 18:47:03 +01:00
|
|
|
#include <SDL_ttf.h>
|
|
|
|
|
2009-12-20 11:21:06 +01:00
|
|
|
class Font;
|
|
|
|
|
2010-01-06 08:47:51 +01:00
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
|
|
|
|
|
2009-11-19 19:23:12 +01:00
|
|
|
#define BUG_ON(cond)
|
|
|
|
|
2009-11-19 21:18:45 +01:00
|
|
|
#define panic(x...) do \
|
|
|
|
{ \
|
2009-11-23 19:52:30 +01:00
|
|
|
fprintf(stderr, "=============PANIC PANIC PANIC===========\n"); \
|
2010-01-06 17:42:55 +01:00
|
|
|
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, x); \
|
2009-11-23 19:52:30 +01:00
|
|
|
fprintf(stderr, "=========================================\n"); \
|
2009-11-19 21:18:45 +01:00
|
|
|
exit(1); \
|
|
|
|
} while(0)
|
|
|
|
|
2009-12-19 13:59:09 +01:00
|
|
|
#define warning(x...) do \
|
|
|
|
{ \
|
|
|
|
fprintf(stderr, "==============WARNING WARNING============\n"); \
|
2010-01-06 17:42:55 +01:00
|
|
|
fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); fprintf(stderr, x); \
|
2009-12-19 13:59:09 +01:00
|
|
|
fprintf(stderr, "=========================================\n"); \
|
|
|
|
} while(0)
|
|
|
|
|
2009-11-19 21:18:45 +01:00
|
|
|
#define panic_if(cond, x...) \
|
|
|
|
do { if ((cond)) panic(x); } while(0)
|
|
|
|
|
|
|
|
static inline char *xstrdup(const char *s)
|
|
|
|
{
|
|
|
|
char *out = strdup(s);
|
|
|
|
|
|
|
|
panic_if(!out, "strdup failed");
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *xmalloc(size_t sz)
|
|
|
|
{
|
|
|
|
void *out = malloc(sz);
|
|
|
|
|
|
|
|
panic_if(!out, "malloc failed");
|
2009-11-20 21:28:48 +01:00
|
|
|
memset(out, 0, sz);
|
2009-11-19 21:18:45 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-11-29 19:29:15 +01:00
|
|
|
static inline void *xrealloc(void *ptr, size_t sz)
|
|
|
|
{
|
|
|
|
void *out = realloc(ptr, sz);
|
|
|
|
|
|
|
|
panic_if(!out, "malloc failed");
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2009-11-19 21:18:45 +01:00
|
|
|
|
|
|
|
#define xsnprintf(buf, size, fmt, x...) do { \
|
|
|
|
int r = snprintf(buf, size, fmt, x); \
|
|
|
|
panic_if(r < 0 || r >= (int)(size), "snprintf failed for %s with %d\n", fmt, r); \
|
|
|
|
} while(0)
|
|
|
|
|
2009-11-26 19:29:10 +01:00
|
|
|
TTF_Font *read_and_alloc_font(const char *path, int pt_size);
|
2009-11-23 19:52:30 +01:00
|
|
|
|
2009-12-13 11:02:27 +01:00
|
|
|
const char **get_file_list(const char *base_dir, const char *exts[]);
|
|
|
|
|
2009-12-19 09:12:39 +01:00
|
|
|
void *sdl_surface_to_png(SDL_Surface *src, size_t *out_sz);
|
|
|
|
|
2009-12-20 11:21:06 +01:00
|
|
|
void highlight_background(SDL_Surface *where, Font *font,
|
|
|
|
SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right,
|
|
|
|
int x, int y, int w, int h);
|
|
|
|
|
2009-11-19 19:23:12 +01:00
|
|
|
#endif /* __UTILS_H__ */
|