Nevermind....
[pineappletracker.git] / sdl_gui.c
blob1cb238d6b86757c3fb52c8072b8ebcec5942aa8d
1 /*
2 * (C) David Olofson, 2003, 2006
3 * modified by the pineapple team, 2009
4 */
5 #include <SDL/SDL.h>
6 #include <signal.h>
7 #include <stdio.h>
9 #include "sdl_gui.h"
10 #include "gui.h"
11 #include "pineapple.h"
12 #include "filetypes.h"
14 static int die = 0;
15 static SDL_Surface *screen;
16 static SDL_Surface *font = NULL;
17 static Uint32 boxcolor;
19 #define FONT_CW 10
20 #define FONT_CH 16
21 #define WINW 800
22 #define WINH 600
23 #define SP 2 //spacing
24 #define BOXW 1 //box width
26 #define FPS 30
28 // these aren't #define's because doing this in the preprocessor:
29 // #define COLW = (WINW/3)-3
30 // made gcc complain
31 static int voffs;
32 static int colh;
34 pineapple_tune tune;
36 int main(int argc, char *args[]){
37 int tick;
39 //------------------------------------------------------------------\\
40 // args
41 //------------------------------------------------------------------//
42 if(argc==1){
43 tune = pt_empty_tune();
44 }else{
47 if(SDL_Init(SDL_INIT_VIDEO)!=0)
48 return 1;
50 voffs = FONT_CH+4;
51 colh = WINH-voffs-SP-BOXW;
53 atexit(SDL_Quit);
54 signal(SIGTERM,breakhandler);
55 signal(SIGINT,breakhandler);
57 screen = SDL_SetVideoMode(WINW,WINH,32,SDL_SWSURFACE);
58 if(screen==NULL){
59 fprintf(stderr,"video init failed");
60 SDL_Quit();
61 return 1;
62 }else{
63 fprintf(stdout,"initialized the surface\n");
66 boxcolor = SDL_MapRGB(screen->format, 255, 0, 255);
67 SDL_WM_SetCaption("pineapple-tracker","pineapple.ico");
68 if(gui_open(screen) != 0){
69 fprintf(stderr, "Couldn't start GUI!\n");
70 SDL_Quit();
71 return -1;
74 draw_main();
75 SDL_Flip(screen);
77 //last_tick=SDL_GetTicks();
78 while(!die){
79 SDL_Event ev;
80 tick = SDL_GetTicks();
81 //int dt = tick - last_tick;
82 //last_tick = tick;
84 /* Handle GUI events */
85 while(SDL_PollEvent(&ev)){
86 switch(ev.type){
87 case SDL_KEYDOWN:
88 switch(currmode){
89 case PM_NORMAL:
90 handle_key_normal(&ev);
91 break;
92 case PM_VISUAL:
93 break;
94 case PM_VISUALLINE:
95 break;
96 case PM_CMDLINE:
97 break;
98 case PM_INSERT:
99 break;
100 case PM_JAMMER:
101 break;
103 break;
104 case SDL_QUIT:
105 die = 1;
106 break;
107 default:
108 break;
112 //update_main(screen);
113 if((SDL_GetTicks() - tick) < 1000/FPS){
114 SDL_Delay( (1000/FPS) - (SDL_GetTicks() - tick));
116 gui_refresh();
119 SDL_Quit();
120 return 0;
123 static void breakhandler(int a){
124 die = 1;
127 static void draw_songed(const int x, const int y, const int w, const int h){
128 int i, pos;
129 char buf[1024];
131 for(i=0; (i+y < h)
132 &&(i < h/FONT_CH)
133 &&(i < tune->songlen); i+=(FONT_CH+2))
135 pos = i/FONT_CH;
136 snprintf(buf, sizeof(buf),
137 "%02x:__ __:__ __:__ __:__", pos);
138 gui_text(x+SP,i+y+SP,buf,screen);
140 // draw a box if selected
141 if(songy==pos){
142 gui_box(x,i,w,FONT_CH+(SP*2),boxcolor,screen);
145 gui_box(x,y,w,h,boxcolor,screen);
148 static void draw_tracked(const int x, const int y, const int w, const int h){
149 gui_box(x,y,w,h,boxcolor,screen);
152 static void draw_instred(const int x, const int y, const int w, const int h){
153 gui_box(x,y,w,h,boxcolor,screen);
156 static void draw_main(void){
157 gui_text(2,2,"PINEAPPLEtRACKER",screen);
158 draw_songed(SP,voffs,(23*FONT_CW)+(SP*2),colh);
159 draw_tracked((23*FONT_CW)+(SP*4)+(BOXW*2),voffs,(18*FONT_CW)+(SP*2),
160 colh);
161 draw_instred((41*FONT_CW)+(SP*7)+(BOXW*4),voffs,(9*FONT_CW)+(SP*2),
162 colh);
165 static void handle_key_normal(SDL_Event *ev){
166 switch(ev->key.keysym.sym){
167 case SDLK_KP_PLUS:
168 case SDLK_PLUS:
169 case SDLK_KP_MINUS:
170 case SDLK_MINUS:
171 break;
172 case SDLK_h:
173 case SDLK_LEFT:
174 fprintf(stderr,"H\n");
175 fprintf(stderr,"pos: %d, %d\n", songx, songy);
176 act_mvleft();
177 break;
178 case SDLK_l:
179 case SDLK_RIGHT:
180 fprintf(stderr,"J\n");
181 fprintf(stderr,"pos: %d, %d\n", songx, songy);
182 act_mvright();
183 break;
184 case SDLK_k:
185 case SDLK_UP:
186 fprintf(stderr,"K\n");
187 fprintf(stderr,"pos: %d, %d\n", songx, songy);
188 act_mvup();
189 break;
190 case SDLK_j:
191 case SDLK_DOWN:
192 fprintf(stderr,"L\n");
193 fprintf(stderr,"pos: %d, %d\n", songx, songy);
194 act_mvdown();
195 break;
196 case SDLK_PAGEUP:
197 act_bigmvup();
198 break;
199 case SDLK_PAGEDOWN:
200 act_bigmvdown();
201 break;
202 case SDLK_F1:
203 case SDLK_F2:
204 case SDLK_F3:
205 case SDLK_F4:
206 case SDLK_F5:
207 case SDLK_F6:
208 case SDLK_F7:
209 case SDLK_F8:
210 case SDLK_F9:
211 case SDLK_F10:
212 case SDLK_F11:
213 case SDLK_F12:
214 case SDLK_PERIOD:
215 case SDLK_DELETE:
216 case SDLK_BACKSPACE:
217 case SDLK_0:
218 case SDLK_1:
219 case SDLK_2:
220 case SDLK_3:
221 case SDLK_4:
222 case SDLK_5:
223 case SDLK_6:
224 case SDLK_7:
225 case SDLK_8:
226 case SDLK_9:
227 case SDLK_KP0:
228 case SDLK_KP1:
229 case SDLK_KP2:
230 case SDLK_KP3:
231 case SDLK_KP4:
232 case SDLK_KP5:
233 case SDLK_KP6:
234 case SDLK_KP7:
235 case SDLK_KP8:
236 case SDLK_KP9:
237 case SDLK_SPACE:
238 case SDLK_TAB:
239 case SDLK_ESCAPE:
240 default:
241 break;
245 static void update_main(SDL_Surface *screen){
246 //unsigned int pos;
248 //draw_songed(SP,voffs,230+(SP*2),colh);
249 //draw_tracked(230+(SP*4),voffs,100,colh);
250 //draw_instred(230+100*(SP*8),voffs,100,colh);
254 // \\ // \\ // \\ //
255 // gui_* functions //
256 // \\ // \\ // \\ //
258 void gui_bar(int x, int y, int w, int h, Uint32 c, SDL_Surface *dst){
259 SDL_Rect r;
260 r.x = x;
261 r.y = y;
262 r.w = w;
263 r.h = h;
264 SDL_FillRect(dst, &r, SDL_MapRGB(dst->format, 0, 0, 0));
265 gui_box(x, y, w, h, c, dst);
268 void gui_box(int x, int y, int w, int h, Uint32 c, SDL_Surface *dst){
269 SDL_Rect r;
270 r.x = x;
271 r.y = y;
272 r.w = w;
273 r.h = BOXW;
274 SDL_FillRect(dst, &r, c);
276 r.x = x;
277 r.y = y + h - BOXW;
278 r.w = w;
279 r.h = BOXW;
280 SDL_FillRect(dst, &r, c);
282 r.x = x;
283 r.y = y + BOXW;
284 r.w = BOXW;
285 r.h = h - (BOXW*2);
286 SDL_FillRect(dst, &r, c);
288 r.x = x + w - BOXW;
289 r.y = y + BOXW;
290 r.w = BOXW;
291 r.h = h - (BOXW*2);
292 SDL_FillRect(dst, &r, c);
294 r.x = x;
295 r.y = y;
296 r.w = w;
297 r.h = h;
298 //gui_dirty(&r);
301 void gui_close(void){
302 SDL_FreeSurface(font);
303 font = NULL;
306 SDL_Surface *gui_load_image(const char *fn){
307 SDL_Surface *cvt;
308 SDL_Surface *img = SDL_LoadBMP(fn);
309 if(!img){
310 return NULL;
312 cvt = SDL_DisplayFormat(img);
313 SDL_FreeSurface(img);
314 return cvt;
317 int gui_open(SDL_Surface *scrn){
318 screen = scrn;
319 font = gui_load_image("font.bmp");
320 if(!font){
321 fprintf(stderr, "Couldn't load font!\n");
322 return 1;
324 SDL_EnableKeyRepeat(250, 25);
325 //memset(activity, 0, sizeof(activity));
326 return 0;
329 void gui_refresh(void){
330 SDL_Flip(screen);
333 void gui_text(int x, int y, const char *txt, SDL_Surface *dst){
334 int sx = x;
335 int sy = y;
336 const char *stxt = txt;
337 int highlights = 0;
338 SDL_Rect sr;
339 sr.w = FONT_CW;
340 sr.h = FONT_CH;
341 while(*txt){
342 int c = *txt++;
343 switch(c){
344 case 0: /* terminator */
345 break;
346 case '\n': /* newline */
347 x = sx;
348 y += FONT_CH;
349 break;
350 case '\t': /* tab */
351 x -= sx;
352 x += 8 * FONT_CW;
353 x %= 8 * FONT_CW;
354 x += sx;
355 break;
356 case '\001': /* red highlight */
357 case '\002': /* green highlight */
358 case '\003': /* yellow highlight */
359 case '\004': /* blue highlight */
360 case '\005': /* purple highlight */
361 case '\006': /* cyan highlight */
362 case '\007': /* white highlight */
363 highlights = 1;
364 if(*txt == '\001'){
365 txt += 2;
367 break;
368 case '\021': /* red bullet */
369 case '\022': /* green bullet */
370 case '\023': /* yellow bullet */
371 case '\024': /* blue bullet */
372 case '\025': /* purple bullet */
373 case '\026': /* cyan bullet */
374 case '\027': /* white bullet */
376 SDL_Rect r;
377 int hlr = c & 1 ? 255 : 0;
378 int hlg = c & 2 ? 255 : 0;
379 int hlb = c & 4 ? 255 : 0;
380 Uint32 hlc = SDL_MapRGB(dst->format, hlr, hlg, hlb);
381 r.x = x;
382 r.y = y;
383 r.w = FONT_CW;
384 r.h = FONT_CH;
385 SDL_FillRect(dst, &r,
386 SDL_MapRGB(dst->format, 0, 0, 0));
387 //gui_dirty(&r);
388 r.x = x + 2;
389 r.y = y + 2;
390 r.w = FONT_CW - 6;
391 r.h = FONT_CH - 6;
392 SDL_FillRect(dst, &r, hlc);
393 x += FONT_CW;
394 break;
396 default: /* printables */
398 SDL_Rect dr;
399 if(c < ' ' || c > 127)
400 c = 127;
401 c -= 32;
402 sr.x = (c % (font->w / FONT_CW)) * FONT_CW;
403 sr.y = (c / (font->w / FONT_CW)) * FONT_CH;
404 dr.x = x;
405 dr.y = y;
406 SDL_BlitSurface(font, &sr, dst, &dr);
407 //gui_dirty(&dr);
408 x += FONT_CW;
409 break;
413 if(!highlights){
414 return;
416 x = sx;
417 y = sy;
418 txt = stxt;
419 while(*txt){
420 int c = *txt++;
421 switch(c){
422 case 0: /* terminator */
423 break;
424 case '\n': /* newline */
425 x = sx;
426 y += FONT_CH;
427 break;
428 case '\t': /* tab */
429 x -= sx;
430 x += 8 * FONT_CW;
431 x %= 8 * FONT_CW;
432 x += sx;
433 break;
434 case '\001': /* red highlight */
435 case '\002': /* green highlight */
436 case '\003': /* yellow highlight */
437 case '\004': /* blue highlight */
438 case '\005': /* purple highlight */
439 case '\006': /* cyan highlight */
440 case '\007': /* white highlight */
442 int hlr = c & 1 ? 255 : 0;
443 int hlg = c & 2 ? 255 : 0;
444 int hlb = c & 4 ? 255 : 0;
445 Uint32 hlc = SDL_MapRGB(screen->format, hlr, hlg, hlb);
446 int hlw = 1;
447 if(*txt == '\001'){
448 hlw = txt[1];
449 txt += 2;
451 gui_box(x - 2, y - 2,
452 FONT_CW * hlw + 2, FONT_CH + 2,
453 hlc, dst);
454 break;
456 default: /* printables */
458 x += FONT_CW;
459 break;