int exec_fct(SDL_Surface *screen, t_map *map) { t_player player; int continuer; t_image img; int jump; t_npc *monsters; SDL_EnableKeyRepeat(10, 10); continuer = 1; monsters = init_all(&img, &player, map); jump = 0; while (continuer > 0) { display_map(screen, map, &img); display_npcs(screen, monsters); display_player(screen, &player); display_life(screen, &player); SDL_Flip(screen); continuer = gravite(&player, map); if (continuer >= 0) player_act(&continuer, map, &player); if (continuer > 0) continuer = env_act(map, &player, monsters); } img_delete(&img); return (continuer); }
static void do_gtkdisplay(void) { GIOChannel *chan; struct imgrect *ir; /* have our main loop poll the pipe file descriptor */ chan = g_io_channel_unix_new(imgpipe_readfd); g_io_add_watch(chan, G_IO_IN | G_IO_ERR | G_IO_HUP, (GIOFunc)pipe_event, NULL); fcntl(imgpipe_readfd, F_SETFL, O_NONBLOCK); /* set up list of image rectangles. */ imgrects = xcalloc(nimgrects = 16, sizeof *imgrects); /* do some init thing */ gtk_init(0, NULL); gtk_widget_push_colormap(gdk_rgb_get_colormap()); /* Make our own window. */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request(window, DEFAULT_WIDTH + 2 * BORDER, DEFAULT_HEIGHT + 2 * BORDER); darea = gtk_drawing_area_new(); gtk_container_add(GTK_CONTAINER(window), darea); gtk_widget_set_events(darea, GDK_EXPOSURE_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK); g_signal_connect(G_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event), NULL); g_signal_connect(G_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL); g_signal_connect(G_OBJECT(darea), "expose-event", GTK_SIGNAL_FUNC(expose_event), NULL); g_signal_connect(G_OBJECT(darea), "configure_event", GTK_SIGNAL_FUNC(expose_event), NULL); /* mouse button press/release for saving images */ g_signal_connect(G_OBJECT(darea), "button_press_event", GTK_SIGNAL_FUNC(button_press_event), NULL); g_signal_connect(G_OBJECT(darea), "button_release_event", GTK_SIGNAL_FUNC(button_release_event), NULL); gtk_widget_show_all(window); gtk_main(); /* Get rid of all remaining images. */ for (ir = imgrects; ir < imgrects + nimgrects; ++ir) if (ir->filename) unlink(ir->filename); img_delete(backing_image); gtk_exit(0); return; /* NOTREACHED */ }
/* make_backing_image: * Create the img structure which represents our back-buffer. */ void make_backing_image() { img I; I = img_new_blank(width, height); img_alloc(I); /* wry += height - backing_image->height; if (wry < BORDER || wry > height - BORDER) wry = height - BORDER;*/ if (backing_image) { int w2, h2; struct imgrect *ir; /* Copy old contents of backing image to ll corner of new one. */ w2 = backing_image->width; if (w2 > width) w2 = width; h2 = backing_image->height; if (h2 > height) h2 = height; img_simple_blt(I, 0, height - h2, backing_image, 0, backing_image->height - h2, w2, h2); /* Move all of the image rectangles. */ for (ir = imgrects; ir < imgrects + nimgrects; ++ir) { if (ir->filename) { ir->y += height - backing_image->height; /* Possible it has scrolled off the window. */ if (ir->x > width || ir->y + ir->h < 0) { unlink(ir->filename); xfree(ir->filename); memset(ir, 0, sizeof *ir); } } } /* Adjust placement of new images. */ if (wrx >= w2) wrx = w2; img_delete(backing_image); } backing_image = I; wrx = BORDER; wry = height - BORDER; rowheight = 2 * BORDER; }
gboolean pipe_event(GIOChannel chan, GIOCondition cond, gpointer data) { static char *path; char name[TMPNAMELEN]; ssize_t rr; int nimgs = 0; if (!path) path = xmalloc(strlen(tmpdir) + 64); /* We are sent messages of size TMPNAMELEN containing a null-terminated * file name. */ while (nimgs < 4 && (rr = xread(dpychld_fd, name, sizeof name)) == sizeof name) { int saveimg = 0; struct stat st; ++nimgs; sprintf(path, "%s/%s", tmpdir, name); if (stat(path, &st) == -1) continue; if (verbose) fprintf(stderr, PROGNAME": received image %s of size %d\n", name, (int)st.st_size); /* Check to see whether this looks like an image we're interested in. */ if (st.st_size > 100) { /* Small images are probably bollocks. */ img i = img_new(); if (!img_load_file(i, path, header, unknown)) fprintf(stderr, PROGNAME": %s: bogus image (err = %d)\n", name, i->err); else { if (i->width > 8 && i->height > 8) { if (img_load(i, full, i->type)) { /* slot in the new image at some plausible place. */ int w, h; if (i->width > width - 2 * BORDER) w = width - 2 * BORDER; else w = i->width; if (i->height > height - 2 * BORDER) h = height - 2 * BORDER; else h = i->height; /* is there space on this row? */ if (width - wrx < w) { /* no */ scroll_backing_image(h + BORDER); wrx = BORDER; rowheight = h + BORDER; } if (rowheight < h + BORDER) { scroll_backing_image(h + BORDER - rowheight); rowheight = h + BORDER; } img_simple_blt(backing_image, wrx, wry - h, i, 0, 0, w, h); add_image_rectangle(path, wrx, wry - h, w, h); saveimg = 1; if (beep) write(1, "\a", 1); update_window(); wrx += w + BORDER; } else fprintf(stderr, PROGNAME": %s: bogus image (err = %d)\n", name, i->err); } else if (verbose) fprintf(stderr, PROGNAME": %s: image dimensions (%d x %d) too small to bother with\n", name, i->width, i->height); } img_delete(i); } else if (verbose) fprintf(stderr, PROGNAME": image data too small (%d bytes) to bother with\n", (int)st.st_size); if (!saveimg) unlink(name); } if (rr == -1 && errno != EINTR && errno != EAGAIN) { perror(PROGNAME": read"); gtk_main_quit(); } else if (rr == 0) { /* pipe closed, exit. */ gtk_main_quit(); } return TRUE; }