//Appends a subimage void sprite_add_subimage(int sprid, unsigned int w, unsigned int h, unsigned char* chunk, unsigned char* collision_data, collision_type ct) { unsigned int fullwidth = nlpo2dc(w)+1, fullheight = nlpo2dc(h)+1; char *imgpxdata = new char[4*fullwidth*fullheight+1], *imgpxptr = imgpxdata; unsigned int rowindex,colindex; for (rowindex = 0; rowindex < h; rowindex++) { for(colindex = 0; colindex < w; colindex++) { *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; } memset(imgpxptr, 0, (fullwidth-colindex) << 2); imgpxptr += (fullwidth-colindex) << 2; } memset(imgpxptr,0,(fullheight-h) * fullwidth); unsigned texture = graphics_create_texture(fullwidth,fullheight,imgpxdata,false); sprite* sprstr = spritestructarray[sprid]; sprstr->texturearray.push_back(texture); sprstr->texbordxarray.push_back((double) w/fullwidth); sprstr->texbordyarray.push_back((double) h/fullheight); sprstr->colldata.push_back(get_collision_mask(sprstr,collision_data,ct)); sprstr->subcount += 1; delete[] imgpxdata; }
//Adds a subimage to an existing sprite from the exe void sprite_set_subimage(int sprid, int imgindex, int x,int y, unsigned int w,unsigned int h,unsigned char*chunk) { unsigned int fullwidth = nlpo2dc(w)+1, fullheight = nlpo2dc(h)+1; char *imgpxdata = new char[4*fullwidth*fullheight+1], *imgpxptr = imgpxdata; unsigned int rowindex,colindex; for (rowindex = 0; rowindex < h; rowindex++) { for(colindex = 0; colindex < w; colindex++) { *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; } memset(imgpxptr, 0, (fullwidth-colindex) << 2); imgpxptr += (fullwidth-colindex) << 2; } memset(imgpxptr,0,(fullheight-h) * fullwidth); unsigned texture = graphics_create_texture(fullwidth,fullheight,imgpxdata); delete[] imgpxdata; enigma::sprite* sprstr = enigma::spritestructarray[sprid]; sprstr->texturearray[imgindex] = texture; sprstr->colldata[imgindex] = collisionsystem_sprite_data_create(imgpxdata,x,y,w,h); }
//Adds an empty sprite to the list int sprite_new_empty(unsigned sprid, unsigned subc, int w, int h, int x, int y, int bbt, int bbb, int bbl, int bbr, bool pl, bool sm) { int fullwidth=nlpo2dc(w)+1,fullheight=nlpo2dc(h)+1; sprite *as = new sprite(subc); spritestructarray[sprid] = as; as->id = sprid; as->subcount = subc; as->width = w; as->height = h; as->bbox.bottom = bbb; as->bbox.left = bbl; as->bbox.top = bbt; as->bbox.right = bbr; as->bbox_relative.bottom = bbb - y; as->bbox_relative.left = bbl - x; as->bbox_relative.top = bbt - y; as->bbox_relative.right = bbr - x; as->xoffset = x; as->yoffset = y; as->texbordx = (double)w/fullwidth; as->texbordy = (double)h/fullheight; as->texturearray = new unsigned int[subc]; as->colldata = new void*[subc]; if (enigma::sprite_idmax < sprid+1) enigma::sprite_idmax = sprid+1; return sprid; }
void sprite_add_from_surface(int ind, int id, int x, int y, int w, int h, bool removeback, bool smooth) { get_surface(surf,id); int full_width=nlpo2dc(w)+1, full_height=nlpo2dc(h)+1; unsigned sz=full_width*full_height; unsigned char *surfbuf=new unsigned char[sz*4]; glBindFramebuffer(GL_READ_FRAMEBUFFER, surf->fbo); glReadPixels(x,y,w,h,GL_RGBA,GL_UNSIGNED_BYTE,surfbuf); glBindFramebuffer(GL_READ_FRAMEBUFFER, enigma::bound_framebuffer); enigma::sprite_add_subimage(ind, w, h, surfbuf, surfbuf, enigma::ct_precise); //TODO: Support toggling of precise. delete[] surfbuf; }
int sprite_create_from_surface(int id, int x, int y, int w, int h, bool removeback, bool smooth, bool preload, int xorig, int yorig) { get_surfacev(surf,id,-1); int full_width=nlpo2dc(w)+1, full_height=nlpo2dc(h)+1; enigma::spritestructarray_reallocate(); int sprid=enigma::sprite_idmax; enigma::sprite_new_empty(sprid, 1, w, h, xorig, yorig, 0, h, 0, w, preload, smooth); unsigned sz=full_width*full_height; unsigned char *surfbuf=new unsigned char[sz*4]; glBindFramebuffer(GL_READ_FRAMEBUFFER, surf->fbo); glReadPixels(x,y,w,h,GL_RGBA,GL_UNSIGNED_BYTE,surfbuf); glBindFramebuffer(GL_READ_FRAMEBUFFER, enigma::bound_framebuffer); enigma::sprite_set_subimage(sprid, 0, w, h, surfbuf, surfbuf, enigma::ct_precise); //TODO: Support toggling of precise. delete[] surfbuf; return sprid; }
int background_create_from_surface(int id, int x, int y, int w, int h, bool removeback, bool smooth, bool preload) { get_surfacev(surf,id,-1); int full_width=nlpo2dc(w)+1, full_height=nlpo2dc(h)+1; unsigned sz=full_width*full_height; unsigned char *surfbuf=new unsigned char[sz*4]; glBindFramebuffer(GL_READ_FRAMEBUFFER, surf->fbo); glReadPixels(x,y,w,h,GL_RGBA,GL_UNSIGNED_BYTE,surfbuf); glBindFramebuffer(GL_READ_FRAMEBUFFER, enigma::bound_framebuffer); enigma::backgroundstructarray_reallocate(); int bckid=enigma::background_idmax; enigma::background_new(bckid, w, h, surfbuf, removeback, smooth, preload, false, 0, 0, 0, 0, 0, 0); delete[] surfbuf; enigma::background_idmax++; return bckid; }
//Adds a subimage to an existing sprite from the exe void sprite_add_subimage(int sprid, int x,int y, unsigned int w,unsigned int h,unsigned char*chunk) { GLuint texture; unsigned int fullwidth=nlpo2dc(w)+1,fullheight=nlpo2dc(h)+1; GLbyte *imgpxdata = new GLbyte[4*fullwidth*fullheight+1], *imgpxptr = imgpxdata; unsigned int rowindex,colindex; for (rowindex = 0; rowindex < h; rowindex++) { for(colindex = 0; colindex < w; colindex++) { *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; *imgpxptr++ = *chunk++; } memset(imgpxptr, 0, (fullwidth-colindex) << 2); imgpxptr += (fullwidth-colindex) << 2; } memset(imgpxptr,0,(fullheight-h) * fullwidth); glGenTextures(1,&texture); glBindTexture(GL_TEXTURE_2D,texture); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // OPENGLES added glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // OPENGLES added glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,fullwidth,fullheight,0,GL_RGBA,GL_UNSIGNED_BYTE,imgpxdata); //OPENGLES changed 4 to GL_RGBA //removed fullwidth, fullheight //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); //OPENGLES //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); //OPENGLES //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); //OPENGLES //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); //OPENGLES //gluBuild2DMipmaps( GL_TEXTURE_2D, 3, bmpwidth, bmpheight, GL_RGB, GL_UNSIGNED_BYTE, readbuffer); glBindTexture(GL_TEXTURE_2D,0); delete[] imgpxdata; enigma::sprite* sprstr = enigma::spritestructarray[sprid]; sprstr->texturearray[sprstr->subcount] = texture; sprstr->subcount++; //std::c out << "Added subimage " << sprstr->subcount << " to sprite " << sprid << std::endl; }
unsigned char* image_load_png(string filename, unsigned int* width, unsigned int* height, unsigned int* fullwidth, unsigned int* fullheight, bool flipped) { unsigned error; unsigned char* image = nullptr; unsigned pngwidth, pngheight; error = libpng_decode32_file(&image, &pngwidth, &pngheight, filename.c_str()); if (error) { printf("libpng-util error %u\n", error); return NULL; } unsigned widfull = nlpo2dc(pngwidth) + 1, hgtfull = nlpo2dc(pngheight) + 1, ih,iw; const int bitmap_size = widfull*hgtfull*4; unsigned char* bitmap = new unsigned char[bitmap_size](); // Initialize to zero. for (ih = 0; ih < pngheight; ih++) { unsigned tmp = 0; if (!flipped) { tmp = ih*widfull*4; } else { tmp = (pngheight - 1 - ih)*widfull*4; } for (iw = 0; iw < pngwidth; iw++) { bitmap[tmp+0] = image[4*pngwidth*ih+iw*4+2]; bitmap[tmp+1] = image[4*pngwidth*ih+iw*4+1]; bitmap[tmp+2] = image[4*pngwidth*ih+iw*4+0]; bitmap[tmp+3] = image[4*pngwidth*ih+iw*4+3]; tmp+=4; } } delete[] image; *width = pngwidth; *height = pngheight; *fullwidth = widfull; *fullheight = hgtfull; return bitmap; }
int background_create_from_screen(int x, int y, int w, int h, bool removeback, bool smooth, bool preload) { int full_width=nlpo2dc(w)+1, full_height=nlpo2dc(h)+1; int prevFbo; glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &prevFbo); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); int patchSize = full_width*full_height; std::vector<unsigned char> rgbdata(4*patchSize); glReadPixels(x, enigma_user::window_get_region_height_scaled()-h-y,w,h,GL_RGBA, GL_UNSIGNED_BYTE, &rgbdata[0]); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, prevFbo); unsigned char* data = enigma::image_flip(&rgbdata[0], w, h, 4); enigma::backgroundstructarray_reallocate(); int bckid=enigma::background_idmax; enigma::background_new(bckid, w, h, &data[0], removeback, smooth, preload, false, 0, 0, 0, 0, 0, 0); delete[] data; rgbdata.clear(); enigma::background_idmax++; return bckid; }