Sat Apr 20 00:01:25 PDT 2002
[netwalk.git] / ext_sdl.c
blobe33d10d1921d43b11999a9a36129b9dae1c18691
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <SDL.h>
5 #include <SDL_ttf.h>
6 #include "bl_lib.h"
7 #include "version.h"
8 #include "eiffel.h"
10 static SDL_Surface *screen;
12 void ext_fill_rect(int x, int y, int w, int h, int c)
14 SDL_Rect rect;
16 rect.x = x;
17 rect.y = y;
18 rect.w = w;
19 rect.h = h;
20 SDL_FillRect(screen, &rect, c);
23 void video_init()
26 //screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
27 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);
28 //screen = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE | SDL_FULLSCREEN);
29 if (!screen) {
30 fprintf(stderr, "Can't set video mode: %s\n", SDL_GetError());
31 exit(1);
33 //SDL_ShowCursor(SDL_DISABLE);
36 void ext_init()
38 int status;
40 //printf("SDL_Init()...\n");
41 //status = SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER);
42 status = SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
43 if (status < 0) {
44 fprintf(stderr, "Can't init SDL: %s\n", SDL_GetError());
45 exit(1);
47 atexit(SDL_Quit);
48 //printf("TTF_Init()...\n");
49 status = TTF_Init();
50 if (status) {
51 fprintf(stderr, "init_glue: TTF_Init failed: %s\n", SDL_GetError());
52 exit(-1);
54 atexit(TTF_Quit);
56 video_init();
58 signal(SIGINT, exit);
59 signal(SIGTERM, exit);
61 SDL_WM_SetCaption(NETWALK_VERSION, NETWALK_VERSION);
63 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
66 void ext_update_screen()
68 SDL_Flip(screen);
71 EIF_OBJ ext_poll_event(EIF_OBJ em)
73 SDL_Event event;
75 while (SDL_PollEvent(&event)) {
76 if (event.type == SDL_KEYDOWN) {
77 return EVENTMAKER_make_keydown(em, event.key.keysym.sym, SDL_GetModState());
78 } else if (event.type == SDL_MOUSEBUTTONDOWN) {
79 return EVENTMAKER_make_mbdown(em, event.button.button, SDL_GetModState(), event.button.x, event.button.y);
80 } else if (event.type == SDL_QUIT) {
81 return EVENTMAKER_make_quit(em);
84 return NULL;
87 int is_kmod(int a, int b)
89 return (a & b) != 0;
92 SDL_Surface *ext_render_text(char *s, TTF_Font *font, SDL_Color *c)
94 SDL_Surface *tmp;
95 SDL_Surface *res;
97 tmp = TTF_RenderText_Solid(font, s, *c);
98 res = SDL_DisplayFormat(tmp);
99 SDL_FreeSurface(tmp);
100 return res;
103 void *ext_make_color(int r, int g, int b)
105 SDL_Color *res;
107 res = (SDL_Color *) bl_malloc(sizeof(SDL_Color));
108 res->r = r;
109 res->g = g;
110 res->b = b;
112 return res;
115 int ext_convert_color(int r, int g, int b)
117 return SDL_MapRGB(screen->format, r, g, b);
120 void *free_img(void *image)
122 SDL_FreeSurface((SDL_Surface *) image);
123 return NULL;
126 void blit_img(SDL_Surface *image, int x, int y)
128 SDL_Rect rect;
130 rect.x = x;
131 rect.y = y;
132 SDL_BlitSurface(image, NULL, screen, &rect);
135 SDL_Surface *ext_display_format_alpha(SDL_Surface *img)
137 SDL_Surface *res;
139 //assert(img);
140 res = SDL_DisplayFormatAlpha(img);
141 SDL_FreeSurface(img);
142 return res;
145 SDL_Surface *ext_display_format(SDL_Surface *img)
147 SDL_Surface *res;
149 //assert(img);
150 res = SDL_DisplayFormat(img);
151 SDL_FreeSurface(img);
152 return res;
155 TTF_Font *ext_ttf_openfont(char *font, int size)
157 return TTF_OpenFont(font, size);
160 void *free_ttf_font(TTF_Font *font)
162 TTF_CloseFont(font);
163 return NULL;
166 static int textsizewidth;
167 static int textsizeheight;
168 int ext_get_tsw()
170 return textsizewidth;
172 int ext_get_tsh()
174 return textsizeheight;
177 void ext_get_text_size(TTF_Font *font, const char* text)
179 TTF_SizeText(font, text, &textsizewidth, &textsizeheight);