ios - Retina & non-retina device when we move say in the center of the device, some point on left bottom also gets moved -
we have created grid on texture straight dividing texture using gridsize. have divided texture 10x10. goal modify texture image using finger. facing issues on non-retina retina device when move in center of device, point on left bottom gets moved. not sure why happens.
- (id)init { // apple recommends re-assign "self" "super" homecoming value if( (self=[super init])) { [[uidevice currentdevice] begingeneratingdeviceorientationnotifications]; [[cctouchdispatcher shareddispatcher] addtargeteddelegate:self priority:0 swallowstouches:yes]; [cctexture2d setdefaultalphapixelformat:kcctexture2dpixelformat_rgba8888]; if ([cocosdistort isretinadisplay]) originalimage = [uiimage imagenamed:@"img_1968-hd.png"]; else originalimage = [uiimage imagenamed:@"img_1968.png"]; texture2d = [[cctexture2d alloc] initwithimage:originalimage]; [self body_init]; self.istouchenabled = yes; } homecoming self; } - (void)draw { gldisableclientstate(gl_color_array); glblendfunc(gl_src_alpha, gl_one_minus_src_alpha); glcolor4ub(224,224,244,200); [self body_redraw]; glblendfunc(cc_blend_src, cc_blend_dst); glenableclientstate(gl_color_array); } - (bool)cctouchbegan:(uitouch *)touch withevent:(uievent *)event { isfirsttouch=yes; cgpoint location = [self converttouchtonodespace: touch]; if ([cocosdistort isretinadisplay]) { mousex = (location.x * 2); mousey = (location.y * 2); } else { mousex = location.x ; mousey = location.y ; } firstpoint=location; grab = [self body_grab:mousex:mousey]; homecoming yes; } - (void)cctouchmoved:(uitouch *)touch withevent:(uievent *)event { isfirsttouch=no; cgpoint location = [self converttouchtonodespace: touch]; mousex1 = 0.0; mousey1 = 0.0; if ([cocosdistort isretinadisplay]) { mousex1 = (location.x * 2) - mousex; mousey1 = (location.y * 2) - mousey ; } else { mousex1 = location.x - mousex; mousey1 = location.y - mousey ; } if ([cocosdistort isretinadisplay]) { mousex = (location.x * 2); mousey = (location.y * 2); } else { mousex = location.x ; mousey = location.y ; } [self body_dynamics:mousex:mousey]; } - (void)cctouchended:(uitouch *)touch withevent:(uievent *)event { grab = -1; } - (void)body_dynamics:(int)x:(int)y { if (mass[grab].x[0] > x && mass[grab].x[1] > y) { grab2 = grab - grid_size_x; grab3 = grab2 - 1; grab4 = grab - 1; } if (mass[grab].x[0] > x && mass[grab].x[1] < y) { grab2 = grab - grid_size_x; grab3 = grab2 + 1; grab4 = grab + 1; } if (mass[grab].x[0] < x && mass[grab].x[1] < y) { grab2 = grab + grid_size_x; grab3 = grab2 + 1; grab4 = grab + 1; } if (mass[grab].x[0] < x && mass[grab].x[0] > y) { grab2 = grab + grid_size_x; grab3 = grab2 - 1; grab4 = grab - 1; } if (grab != -1 && !mass[grab].nail &&!isfirsttouch) { mass[grab].x[0] = mass[grab].x[0] + mousex1; mass[grab].x[1] = mass[grab].x[1] + mousey1; mass[grab].x[2] = -(clip_far - clip_near)/4.0; mass[grab2].x[0] = mass[grab2].x[0] + mousex1; mass[grab2].x[1] = mass[grab2].x[1] + mousey1; mass[grab2].x[2] = -(clip_far - clip_near)/4.0; mass[grab3].x[0] = mass[grab3].x[0] + mousex1; mass[grab3].x[1] = mass[grab3].x[1] + mousey1; mass[grab3].x[2] = -(clip_far - clip_near)/4.0; mass[grab4].x[0] = mass[grab4].x[0] + mousex1; mass[grab4].x[1] = mass[grab4].x[1] + mousey1; mass[grab4].x[2] = -(clip_far - clip_near)/4.0; } } - (int)body_grab:(int)x:(int)y { float dx[2]; float d; float min_d; float min_i; int i; (i = 0; < grid_size_x*grid_size_y; i++) { dx[0] = mass[i].x[0] - x; dx[1] = mass[i].x[1] - y; d = sqrt(dx[0]*dx[0] + dx[1]*dx[1]); if (i == 0 || d < min_d) { min_i = i; min_d = d; } } homecoming min_i; } - (void)body_redraw { int k; int i, j; if(mass == null) return; glbindtexture(gl_texture_2d, [texture2d name]); k = 0; (i = 0; < grid_size_x - 1; i++) { (j = 0; j < grid_size_y - 1; j++) { glfloat vertices[]= { mass[k].x[0],mass[k].x[1],mass[k].x[2], mass[k + 1].x[0],mass[k + 1].x[1],mass[k + 1].x[2], mass[k + grid_size_y + 1].x[0],mass[k + grid_size_y + 1].x[1], mass[k + grid_size_y + 1].x[2], mass[k + grid_size_y].x[0],mass[k + grid_size_y].x[1],mass[k + grid_size_y].x[2] }; glfloat tex[]={ mass[k].t[0], mass[k].t[1], mass[k + 1].t[0], mass[k + 1].t[1], mass[k + grid_size_y + 1].t[0], mass[k + grid_size_y + 1].t[1], mass[k + grid_size_y].t[0], mass[k + grid_size_y].t[1] }; glvertexpointer(3, gl_float, 0, vertices); gltexcoordpointer(2, gl_float, 0, tex); gldrawarrays(gl_line_strip, 0,4); k++; } k++; } } - (void)body_init { glint width = texture2d.contentsizeinpixels.width; glint height = texture2d.contentsizeinpixels.height; int i, j, k; if (mass == null) { mass = (mass *) malloc(sizeof(mass)*grid_size_x*grid_size_y); if (mass == null) { fprintf(stderr, "body: can't allocate memory.\n"); exit(-1); } } k = 0; (i = 0; < grid_size_x; i++) (j = 0; j < grid_size_y; j++) { mass[k].nail = (i == 0 || j == 0 || == grid_size_x - 1 || j == grid_size_y - 1);//value 0/1 mass[k].x[0] = i/(grid_size_x - 1.0)*width; nslog(@"mass[%d].x[0]:: %f",k,mass[k].x[0]); mass[k].x[1] = j/(grid_size_y - 1.0)*height; nslog(@"mass[%d].x[1]:: %f",k,mass[k].x[1]); mass[k].x[2] = -(clip_far - clip_near)/4.0; nslog(@"mass[%d].x[2]:: %f",k,mass[k].x[2]); mass[k].v[0] = 0.0; mass[k].v[1] = 0.0; mass[k].v[2] = 0.0; mass[k].t[0] = i/(grid_size_x - 1.0); mass[k].t[1] = j/(grid_size_y - 1.0); k++; } } }
i have solved issue modifying touches moved method:
- (void)cctouchmoved:(uitouch *)touch withevent:(uievent *)event { isfirsttouch=no; cgpoint location = [self converttouchtonodespace: touch];//[touch locationinview:touch.view];// int gridsizex2 = 20; if ((location.x < firstpoint.x + gridsizex2 && location.y < firstpoint.y + gridsizex2) && (location.x > firstpoint.x - gridsizex2 && location.y > firstpoint.y - gridsizex2)) { mousex1 = 0.0; mousey1 = 0.0; if ([cocosdistort isretinadisplay]) { mousex1 = (location.x * 2) - mousex; mousey1 = (location.y * 2) - mousey ; mousex = (location.x * 2); mousey = (location.y * 2); } else { mousex1 = location.x - mousex; mousey1 = location.y - mousey ; mousex = location.x ; mousey = location.y ; } mousex1 = mousex1 > 0 ? 1:-1; mousey1 = mousey1 > 0 ? 1:-1; [self body_dynamics:mousex:mousey]; } } ios xcode opengl-es retina-display
No comments:
Post a Comment