const sc_texture_t * sc_arraytex_add_from_surface(sc_arraytex_t *arr, SDL_Surface *img) { size_t i; size_t w = arr->width, h = arr->height; sc_texture_t *rv; SDL_Surface *helper_img, *previous_img; GLenum format; ASSERT_NOT_FINALIZED(arr); img = sc_prepare_surface_for_upload(img, &format); if (!img) return NULL; /* we currently do not support resizing of the images on adding, so let's just make sure we add only images of the same size */ if (img->w != arr->height || img->h != arr->height) { if (!(arr->flags & SC_ARRAYTEX_NEAREST)) { SDL_FreeSurface(img); sc_set_error(SC_EGRAPHIC, __FILE__, __LINE__, "Texture size (%dx%d) does not match array size " "(%dx%d)", (int)img->w, (int)img->h, (int)arr->width, (int)arr->height); return NULL; } helper_img = img; img = sc_resize_surface_nearest(img, arr->width, arr->height); SDL_FreeSurface(helper_img); } /* same goes for the format. All images added to the array have to be of the same format. Currently not a big deal as we are only having RGBA images anyways, but it's important to check. */ if (arr->slices == 0) arr->format = format; else if (arr->format != format) { SDL_FreeSurface(img); sc_set_error(SC_EGRAPHIC, __FILE__, __LINE__, "Texture format mismatch when adding new texture " "to texture array. Array format is %d, added " "format is %d.", arr->format, format); return NULL; } rv = sc_xalloc(sc_texture_t); rv->id = 0; /* set later, we don't know it yet */ rv->stored_width = arr->width; rv->stored_height = arr->height; rv->off_x = 0; rv->off_y = 0; rv->index = arr->slices++; rv->width = arr->width; rv->height = arr->height; rv->target = TARGET; rv->shared = 1; /* first level is not resized */ append_image(arr->buffers[0], img); /* all other levels are scaled down */ helper_img = NULL; previous_img = NULL; for (i = 1; i < arr->mipmap_levels; i++) { if (w > 1) w /= 2; if (h > 1) h /= 2; helper_img = sc_resize_surface_nearest( previous_img ? previous_img : img, w, h); SDL_FreeSurface(previous_img); append_image(arr->buffers[i], helper_img); previous_img = helper_img; } SDL_FreeSurface(img); SDL_FreeSurface(helper_img); sc_list_append(arr->textures, rv); return rv; }
int main(int argc, char **argv) { int mpiret, i; mpi_context_t mpi_context, *mpi = &mpi_context; p4est_t *p4est; p4est_connectivity_t *connectivity; pchase_world_t *W; /* initialize MPI and p4est internals */ mpiret = MPI_Init(&argc, &argv); SC_CHECK_MPI(mpiret); mpi->mpicomm = MPI_COMM_WORLD; /* your favourite comm here */ mpiret = MPI_Comm_size(mpi->mpicomm, &mpi->mpisize); SC_CHECK_MPI(mpiret); mpiret = MPI_Comm_rank(mpi->mpicomm, &mpi->mpirank); SC_CHECK_MPI(mpiret); /* Sets global program identifiers (e.g. the MPIrank) and some flags */ sc_init(mpi->mpicomm, 1, 1, NULL, SC_LP_SILENT); /* Registers p4est with the SC Library and sets the logging behavior */ p4est_init(NULL, SC_LP_SILENT); /* build up the world */ W = pchase_world_init(); /* store connectivity for a unitsquare */ connectivity = p4est_connectivity_new_unitsquare(); /* build uniform tree and get space for 25 particles each */ p4est = p4est_new_ext(mpi->mpicomm, connectivity, 0, 4, 1, sizeof(pchase_quadrant_data_t), W->init_fn, NULL); /* initialize everything depending on p4est */ pchase_world_init_p4est(W, p4est); /* if (W->p4est->mpirank == 0) { */ /* for (i=0; i<100; i++) */ /* * sc_list_append(W->particle_push_list, * pchase_world_random_particle(W)); */ /* } */ pchase_particle_t * p = P4EST_ALLOC(pchase_particle_t, 1); p->x[0] = 0.5; p->x[1] = 0.1; sc_list_append(W->particle_push_list, p); /* this has to be done for each proc */ pchase_world_insert_particles(W); pchase_world_insert_particles(W); /* let this particle move */ pchase_world_simulate(W); #ifdef DEBUG /* print out all quadrants */ p4est_iterate(W->p4est, NULL, NULL, W->viter_fn, NULL, NULL); #endif /* destroy all particles, p4est and its connectivity structure */ pchase_world_destroy(W); p4est_destroy(p4est); p4est_connectivity_destroy(connectivity); /* clean up and exit */ sc_finalize(); mpiret = MPI_Finalize(); SC_CHECK_MPI(mpiret); return 0; }