void event_loop(void) { GP_Event ev; for (;;) { GP_BackendWaitEvent(win, &ev); switch (ev.type) { case GP_EV_KEY: if (ev.code != GP_EV_KEY_DOWN) continue; switch (ev.val.key.key) { case GP_KEY_SPACE: if (font) font_flag = (font_flag + 1) % 5; else font_flag = (font_flag + 1) % 4; redraw_screen(); GP_BackendFlip(win); break; case GP_KEY_UP: tracking++; redraw_screen(); GP_BackendFlip(win); break; case GP_KEY_DOWN: tracking--; redraw_screen(); GP_BackendFlip(win); break; case GP_KEY_B: font_h++; if (font_path) { GP_FontFaceFree(font); font = GP_FontFaceLoad(font_path, 0, font_h); redraw_screen(); GP_BackendFlip(win); } break; case GP_KEY_S: font_h--; if (font_path) { GP_FontFaceFree(font); font = GP_FontFaceLoad(font_path, 0, font_h); redraw_screen(); GP_BackendFlip(win); } break; case GP_KEY_ESC: GP_BackendExit(win); exit(0); break; } } } }
int main(void) { GP_Backend *win_1, *win_2; win_1 = GP_BackendX11Init(NULL, 0, 0, 300, 300, "win 1", 0); win_2 = GP_BackendX11Init(NULL, 0, 0, 300, 300, "win 2", 0); if (win_1 == NULL || win_2 == NULL) { GP_BackendExit(win_1); GP_BackendExit(win_2); return 1; } /* Update the backend screen */ redraw(win_1->context); redraw(win_2->context); GP_BackendFlip(win_1); GP_BackendFlip(win_2); for (;;) { /* * Wait for backend event. * * Either window is fine as they share connection. */ GP_Backend *b = win_1 ? win_1 : win_2; if (b == NULL) return 0; GP_BackendWait(b); if (ev_loop(win_1, "win 1")) win_1 = NULL; if (ev_loop(win_2, "win 2")) win_2 = NULL; } GP_BackendExit(win_1); GP_BackendExit(win_2); return 0; }
static void sighandler(int signo) { if (backend != NULL) GP_BackendExit(backend); fprintf(stderr, "Got signal %i\n", signo); exit(1); }
static int ev_loop(struct GP_Backend *backend, const char *name) { GP_Event ev; if (backend == NULL) return 0; while (GP_BackendGetEvent(backend, &ev)) { printf("-------------------------- %s\n", name); GP_EventDump(&ev); switch (ev.type) { case GP_EV_KEY: switch (ev.val.val) { case GP_KEY_ESC: case GP_KEY_Q: GP_BackendExit(backend); return 1; break; } break; case GP_EV_SYS: switch (ev.code) { case GP_EV_SYS_RESIZE: GP_BackendResizeAck(backend); redraw(backend->context); GP_BackendFlip(backend); break; case GP_EV_SYS_QUIT: GP_BackendExit(backend); return 1; break; } break; } printf("-----------------------------\n"); } return 0; }
void event_loop(void) { GP_Event ev; while (GP_BackendGetEvent(win, &ev)) { switch (ev.type) { case GP_EV_KEY: if (ev.code != GP_EV_KEY_DOWN) continue; switch (ev.val.key.key) { case GP_KEY_SPACE: shape++; if (shape > SHAPE_LAST) shape = SHAPE_FIRST; clear_screen(); pause_flag = 0; break; case GP_KEY_P: pause_flag = !pause_flag; break; case GP_KEY_F: fill_flag = !fill_flag; if (!fill_flag && !outline_flag) outline_flag = 1; break; case GP_KEY_O: outline_flag = !outline_flag; if (!fill_flag && !outline_flag) fill_flag = 1; break; case GP_KEY_C: cliptest_flag = !cliptest_flag; break; case GP_KEY_X: clear_screen(); break; case GP_KEY_ESC: GP_BackendExit(win); exit(0); break; } } } }
int main(int argc, char *argv[]) { const char *backend_opts = "X11"; int opt; int pause_flag = 0; while ((opt = getopt(argc, argv, "b:Ii:Ps:r:")) != -1) { switch (opt) { case 'b': backend_opts = optarg; break; default: fprintf(stderr, "Invalid paramter '%c'\n", opt); } } // GP_SetDebugLevel(10); signal(SIGINT, sighandler); signal(SIGSEGV, sighandler); signal(SIGBUS, sighandler); signal(SIGABRT, sighandler); init_backend(backend_opts); context = backend->context; black_pixel = GP_ColorToContextPixel(GP_COL_BLACK, context); white_pixel = GP_ColorToContextPixel(GP_COL_WHITE, context); GP_Fill(context, black_pixel); GP_BackendFlip(backend); struct space *space; space = space_create(160, 10<<8, 10<<8, (context->w - 10)<<8, (context->h - 10)<<8); for (;;) { if (backend->Poll) GP_BackendPoll(backend); usleep(1000); /* Read and parse events */ GP_Event ev; while (GP_BackendGetEvent(backend, &ev)) { GP_EventDump(&ev); switch (ev.type) { case GP_EV_KEY: if (ev.code != GP_EV_KEY_DOWN) continue; switch (ev.val.key.key) { case GP_KEY_ESC: case GP_KEY_ENTER: case GP_KEY_Q: GP_BackendExit(backend); return 0; break; case GP_KEY_P: pause_flag = !pause_flag; break; case GP_KEY_G: space->gay = 1; break; case GP_KEY_T: space->gay = 0; break; } break; } } if (!pause_flag) { space_time_tick(space, 1); space_draw_particles(context, space); GP_BackendFlip(backend); } } GP_BackendExit(backend); return 0; }
int main(int argc, char *argv[]) { GP_Backend *backend; const char *backend_opts = "X11:350x350"; int opt; GP_PixelType emul_type = GP_PIXEL_UNKNOWN; while ((opt = getopt(argc, argv, "b:h:p:")) != -1) { switch (opt) { case 'b': backend_opts = optarg; break; case 'p': emul_type = GP_PixelTypeByName(optarg); if (emul_type == GP_PIXEL_UNKNOWN) { fprintf(stderr, "Invalid pixel type '%s'\n", optarg); return 1; } break; case 'h': GP_BackendInit("help", NULL); return 0; break; default: fprintf(stderr, "Invalid paramter '%c'\n", opt); return 1; } } /* Turn on debug messages */ GP_SetDebugLevel(10); backend = GP_BackendInit(backend_opts, "Virtual Backend Example"); if (emul_type != GP_PIXEL_UNKNOWN) { GP_Backend *emul; /* * Create an emulated backend on the top of real backend. * * The GP_BACKEND_CALL_EXIT says that when calling exit on * emulated backend, the real backend exit will be called as * well. */ emul = GP_BackendVirtualInit(backend, emul_type, GP_BACKEND_CALL_EXIT); if (emul == NULL) { fprintf(stderr, "Failed to create Virtual Backend\n"); GP_BackendExit(backend); return 1; } /* Once created virtual backend behaves exactly like a real one */ backend = emul; } redraw(backend); for (;;) { if (backend->Poll) GP_BackendPoll(backend); usleep(1000); /* Read and parse events */ GP_Event ev; while (GP_BackendGetEvent(backend, &ev)) { GP_EventDump(&ev); switch (ev.type) { case GP_EV_KEY: switch (ev.val.key.key) { case GP_KEY_ESC: case GP_KEY_Q: GP_BackendExit(backend); return 0; break; } break; case GP_EV_SYS: switch(ev.code) { case GP_EV_SYS_RESIZE: GP_BackendResizeAck(backend); redraw(backend); break; } break; } } } GP_BackendExit(backend); return 0; }
int main(int argc, char *argv[]) { GP_Backend *backend; const char *backend_opts = "X11:100x100"; int opt; while ((opt = getopt(argc, argv, "b:h")) != -1) { switch (opt) { case 'b': backend_opts = optarg; break; case 'h': GP_BackendInit(NULL, NULL); return 0; break; default: fprintf(stderr, "Invalid paramter '%c'\n", opt); return 1; } } backend = GP_BackendInit(backend_opts, "Backend Example"); if (backend == NULL) { fprintf(stderr, "Failed to initialize backend\n"); return 1; } redraw(backend); /* Handle events */ for (;;) { GP_Event ev; GP_BackendWaitEvent(backend, &ev); GP_EventDump(&ev); switch (ev.type) { case GP_EV_KEY: switch (ev.val.val) { case GP_KEY_ESC: case GP_KEY_Q: GP_BackendExit(backend); return 0; break; } break; case GP_EV_SYS: switch (ev.code) { case GP_EV_SYS_RESIZE: GP_BackendResizeAck(backend); redraw(backend); break; case GP_EV_SYS_QUIT: GP_BackendExit(backend); return 0; break; } break; } } GP_BackendExit(backend); return 0; }
static void event_loop(void) { for (;;) { GP_BackendWait(backend); while (GP_BackendEventsQueued(backend)) { GP_Event ev; GP_BackendGetEvent(backend, &ev); GP_EventDump(&ev); switch (ev.type) { case GP_EV_KEY: draw_event(&ev); switch (ev.val.key.key) { case GP_KEY_ESC: GP_BackendExit(backend); exit(0); break; case GP_BTN_LEFT: GP_HLineXXY(win, ev.cursor_x - 3, ev.cursor_x + 3, ev.cursor_y, red); GP_VLineXYY(win, ev.cursor_x, ev.cursor_y - 3, ev.cursor_y + 3, red); GP_BackendFlip(backend); break; default: break; } break; case GP_EV_REL: switch (ev.code) { static int size = 0; case GP_EV_REL_POS: if (GP_EventGetKey(&ev, GP_BTN_LEFT)) { GP_PutPixel(win, ev.cursor_x, ev.cursor_y, green); } int align = GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM; GP_TextClear(win, NULL, 20, 40, align, black, size); size = GP_Print(win, NULL, 20, 40, align, white, black, "X=%3u Y=%3u dX=%3i dY=%3i", ev.cursor_x, ev.cursor_y, ev.val.rel.rx, ev.val.rel.ry); GP_BackendFlip(backend); break; } break; case GP_EV_SYS: switch (ev.code) { case GP_EV_SYS_RESIZE: GP_BackendResizeAck(backend); break; case GP_EV_SYS_QUIT: GP_BackendExit(backend); exit(0); break; } break; } } } }
void event_loop(void) { GP_Event ev; for (;;) { GP_BackendWaitEvent(backend, &ev); switch (ev.type) { case GP_EV_KEY: if (ev.code != GP_EV_KEY_DOWN) continue; switch (ev.val.key.key) { case GP_KEY_SPACE: if (font) font_flag = (font_flag + 1) % 6; else font_flag = (font_flag + 1) % 5; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_RIGHT: tracking++; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_LEFT: tracking--; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_UP: warp_up(1); break; case GP_KEY_DOWN: warp_down(1); break; case GP_KEY_DOT: space++; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_COMMA: space--; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_RIGHT_BRACE: mul++; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_LEFT_BRACE: if (mul > 0) mul--; redraw_screen(); GP_BackendFlip(backend); break; case GP_KEY_PAGE_UP: warp_up(30); break; case GP_KEY_PAGE_DOWN: warp_down(30); break; case GP_KEY_ESC: GP_BackendExit(backend); exit(0); break; } break; case GP_EV_SYS: switch(ev.code) { case GP_EV_SYS_QUIT: GP_BackendExit(backend); exit(0); break; case GP_EV_SYS_RESIZE: GP_BackendResizeAck(backend); redraw_screen(); GP_BackendFlip(backend); break; } break; } } }
static void event_loop(void) { GP_Event ev; for (;;) { GP_BackendWaitEvent(win, &ev); switch (ev.type) { case GP_EV_KEY: if (ev.code != GP_EV_KEY_DOWN) continue; switch (ev.val.key.key) { case GP_KEY_X: win->context->x_swap = !win->context->x_swap; break; case GP_KEY_Y: win->context->y_swap = !win->context->y_swap; break; case GP_KEY_R: win->context->axes_swap = !win->context->axes_swap; GP_SWAP(X, Y); break; case GP_KEY_SPACE: font_flag++; if (font) { if (font_flag > 5) font_flag = 0; } else { if (font_flag > 4) font_flag = 0; } break; case GP_KEY_UP: style.pixel_xspace++; style.pixel_yspace++; break; case GP_KEY_DOWN: style.pixel_xspace--; style.pixel_yspace--; break; case GP_KEY_RIGHT: style.pixel_xmul++; style.pixel_ymul++; break; case GP_KEY_LEFT: style.pixel_xmul--; style.pixel_ymul--; break; case GP_KEY_ESC: GP_BackendExit(win); exit(0); break; } break; case GP_EV_SYS: switch(ev.code) { case GP_EV_SYS_QUIT: GP_BackendExit(win); exit(0); break; case GP_EV_SYS_RESIZE: GP_BackendResizeAck(win); X = win->context->w; Y = win->context->h; break; } break; } redraw_screen(); GP_BackendFlip(win); } }