#ifndef __CONSOLE__H_ #define __CONSOLE__H_ #include "mx.h" #include struct mxWin *console = 0; int dim_con = 0; char console_buffer[5000] = { 0 }; int console_offset = 0; void draw_console(); void printconf(char *src, ...); int master_main(int, char **); int inchr(int console_offset, char *src, char c); int scroll_mode = 0; int prev_offset = 0; void set_scroll(int mode) { //scroll_mode = mode; } void proccess_command(char *cmd) { char *buf = strdup(cmd); char *dat = strtok(buf, " "); char *tokenz[255] = { 0 }; int tok_off = 0; int spot = 0; tokenz[tok_off++] = cmd; spot = inchr(0,tokenz[0],' '); spot = spot < 0 ? strlen(tokenz[0]) : spot; if(spot < 0) spot = 0; tokenz[0][spot] = 0; while(dat != 0) { dat = strtok(0, " "); tokenz[tok_off++] = dat; } printconf("\n%s", cmd); if(master_main(tok_off,tokenz) != 0) { printconf("\n - unknown command"); } keyboard.key_offset = 0; keyboard.keybuffer[0] = 0; free(buf); } int console_key(struct mxEvent *e) { if(e->key == '=') { proccess_command(keyboard.keybuffer); e->key = 0; } else { def_key(e->key); if(console_offset < 0) console_offset = 0; } return 0; } int con_event(struct mxEvent *e) { switch(e->type) { case SDL_RENDER: { SDL_FillRect(e->cur_surface, 0, 0); draw_console(); if(console->active && console->show) { if(SDL_JoystickGetButton(stick, 6)) { if(console_offset < strlen(console_buffer)) console_offset++; } if(SDL_JoystickGetButton(stick, 8)) { if(console_offset > 0) console_offset--; } } } break; case SDL_MENU_CLICKED: { keyboard.event = console_key; } break; case SDL_JOYBUTTONDOWN: { switch(e->button ) { case 6: if(console_offset < strlen(console_buffer)) console_offset++; break; case 8: if(console_offset > 0) console_offset--; break; } } break; } return 0; } void init_console() { static int con_init = 0; if(con_init == 0) { SDL_Rect rc = { 0,0,480,170 }; console = init_windows(0,&rc,0x0,0x0,0x0,"",con_event); console->active = 1; console->full = 1; console->show = 1; dim_con = add_new_dim("console", console, console_key); con_init = 1; console_buffer[0] = 0; } } char *proccess_text() { static char kbuf[5000]; sprintf(kbuf, "$%s%s", keyboard.key_offset == 0 ? "" : keyboard.keybuffer, rand()%2==0?"_":""); return kbuf; return 0; } void mid(char *out, const char *in, int start, int stop) { int i, pos = 0; for(i = start; i < stop; i++) out[pos++] = in[i]; out[pos] = 0; } int inchr(int console_offset, char *src, char c) { int i; for(i = console_offset; src[i] != 0; i++) if(src[i] == c) return i; return -1; } int rinchr(int off, char *src, char c) { int i; for(i = off; i != 0; i--) if(src[i] == c) return i; return -1; } int count_chars(int count, const char *src, char c) { int i, z = 0; for(i = count; src[i] != 0; i++) if(src[i] == c) z++; return z; } void shift_up_text() { if(scroll_mode == 0 && count_chars(console_offset, console_buffer, '\n') > 6) { console_offset = rinchr(strlen(console_buffer)-1, console_buffer, '\n'); } } int special_printtext(const char *src, int x, int y, unsigned long color) { SDL_Surface *surf = console->surface; struct SDL_Font *fnt = font; int prev_x = x; int offset_x = prev_x, offset_y = y; int width = 0, height = 0; int i,z,p; void *pbuf = lock(surf, surf->format->BitsPerPixel); for(p = 0; p < (int)strlen(src); p++) { if(src[p] == '\n') { offset_x = prev_x; offset_y += height; continue; } for(i = 0; i < fnt->mx; i++) { for(z = 0; z < fnt->my; z++) { if(fnt->letters[src[p]].fnt_ptr[i][z] != fnt->tcolor) { if(offset_y+z < surf->h && offset_x+i < surf->w) setpixel(pbuf, (Uint32)offset_x+i, (Uint32)offset_y+z, color, surf->format->BitsPerPixel, surf->pitch); width=i; if(height < z) height=z; } } } offset_x += width; if(offset_x+width >= surf->w) { offset_x = prev_x; offset_y += height; } if(offset_y+height*2 > surf->h) { return 1; } } unlock(surf); return 0; } void printconf(char *src, ...) { char buf[5000]; va_list l; va_start(l,src); vsprintf(buf,src,l); va_end(l); sprintf(console_buffer,"%s%s", console_buffer, buf); } void draw_console() { char *buf = proccess_text(); int console_pos = console_offset; if( special_printtext(buf, 10,10, 0xFFFFFFFFL) ) { } if(console_pos < 0 || console_pos > strlen(console_buffer)) console_pos = 0; special_printtext((console_buffer+(console_offset)), 10,40,SDL_MapRGB(front->format, 255, 0,0)); shift_up_text(); } #endif