Font * font_load( char *filename, int pointSize ) { Font *font = NULL; font = font_get_by_name_size(filename,pointSize); if (font != NULL) { font->_refCount++; return font; } font = font_new(); if (font == NULL)return NULL; font->_font = TTF_OpenFont(filename,pointSize); if(font->_font == NULL) { slog("Couldn't initialize Font: %s\n",SDL_GetError()); font_delete(font); return NULL; } font->point = pointSize; strncpy(font->filename,filename,LINELEN); return font; }
int main(int argc, char** argv) { Display *dpy; Window *windows[NUMWINS]; int inside[NUMWINS]; Font *pcf = font_new (); u32 fg,bg; int i; int vc=-1; i=1; while(i<argc) { if (!strncmp("-c",argv[i],2)) { if (argv[i][2]) vc = atoi (2 + argv[i]); } i++; } if (!pcf_read (pcf, "timR12.pcf")) { font_free (pcf); pcf = NULL; FATAL ("cannot load font"); } short line_height = pcf->ascent + pcf->descent; fg = RGB_YELLOW; bg = RGB_BROWN; short win_w, win_h; dpy = fbui_display_open (); if (!dpy) FATAL ("cannot open display"); srand (time(NULL)); for (i=0; i< NUMWINS; i++) { Window *win; char subtitle[10]; inside[i] = 0; sprintf (subtitle, "%d", i); do { short win_w = 150; short win_h = 150; int x = rand () % (dpy->width - win_w); int y = rand () & (dpy->height - win_h); win = fbui_window_open (dpy, win_w, win_h, &win_w, &win_h, 999,999, x, y, &fg, &bg, "fbmtest", subtitle, FBUI_PROGTYPE_APP, false,false, vc, false, false, i == 3 ? true : false, // hide the 4th window 0,NULL); } while (!win); windows[i] = win; } printf ("MultiTest: All windows created.\n"); while (1) { int total; int need=0; Event ev; Window *win; int x=0, y=0; int type; int err; /* Get any event for ANY of our windows */ if (err = fbui_wait_event (dpy, &ev, FBUI_EVENTMASK_ALL)) { fbui_print_error (err); continue; } win = ev.win; if (!win) FATAL("null window"); type = ev.type; printf ("%s (subwindow %d) got event %s\n", argv[0], win->id, fbui_get_event_name (type)); if (type == FBUI_EVENT_ENTER) inside [win->id] = 1; else if (type == FBUI_EVENT_LEAVE) inside [win->id] = 0; fbui_clear (dpy, win); char expr[100]; sprintf (expr, "This is window %d", win->id); i=0; int in = 0; while(i < NUMWINS) { if (win->id == windows[i]->id) { in = inside[i]; break; } i++; } if (in) fbui_draw_rect (dpy, win, 0, 0, 149,149, RGB_YELLOW); fbui_draw_string (dpy, win, pcf, 6,6, expr, fg); if (type == FBUI_EVENT_MOTION) { fbui_draw_line (dpy, win, 0,0, ev.x, ev.y, RGB_WHITE); fbui_flush (dpy, win); } } fbui_display_close (dpy); }
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int shown) { LARGE_INTEGER freq; long long start; long long dt; /* GUI */ struct gui_input in; struct gui_font font; struct demo_gui gui; /* Window */ QueryPerformanceFrequency(&freq); xw.wc.style = CS_HREDRAW|CS_VREDRAW; xw.wc.lpfnWndProc = wnd_proc; xw.wc.hInstance = hInstance; xw.wc.lpszClassName = "GUI"; RegisterClass(&xw.wc); xw.hWnd = CreateWindowEx( 0, xw.wc.lpszClassName, "Demo", WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, hInstance, 0); xw.hdc = GetDC(xw.hWnd); GetClientRect(xw.hWnd, &xw.rect); xw.backbuffer = surface_new(xw.hdc, xw.rect.right, xw.rect.bottom); xw.font = font_new(xw.hdc, "Times New Roman", 14); xw.width = xw.rect.right; xw.height = xw.rect.bottom; /* GUI */ memset(&in, 0, sizeof in); memset(&gui, 0, sizeof gui); font.userdata.ptr = &xw; font.height = (gui_float)xw.font->height; font.width = font_get_text_width; gui.running = gui_true; gui.memory = malloc(MAX_MEMORY); init_demo(&gui, &font); while (gui.running && !quit) { /* Input */ MSG msg; start = timestamp(freq); gui_input_begin(&in); while (PeekMessage(&msg, xw.hWnd, 0, 0, PM_REMOVE)) { if (msg.message == WM_KEYDOWN) key(&in, &msg, gui_true); else if (msg.message == WM_KEYUP) key(&in, &msg, gui_false); else if (msg.message == WM_LBUTTONDOWN) btn(&in, &msg, gui_true); else if (msg.message == WM_LBUTTONUP) btn(&in, &msg, gui_false); else if (msg.message == WM_MOUSEMOVE) motion(&in, &msg); else if (msg.message == WM_CHAR) text(&in, &msg); TranslateMessage(&msg); DispatchMessage(&msg); } gui_input_end(&in); /* GUI */ run_demo(&gui, &in); /* Draw */ surface_begin(xw.backbuffer); surface_clear(xw.backbuffer, 100, 100, 100); draw(xw.backbuffer, &gui.stack); surface_end(xw.backbuffer, xw.hdc); /* Timing */ dt = timestamp(freq) - start; if (dt < DTIME) Sleep(DTIME - dt); } free(gui.memory); font_del(xw.font); surface_del(xw.backbuffer); ReleaseDC(xw.hWnd, xw.hdc); return 0; }