sdl 2 - SDL/C++ camera bether ways? -
at moment handle camera that:
if (window.rightarrow) { //player.addxposition(player_speed); if (player.getposition().x > 400 - 50 / 2 && !cam.active) { cam.active = true; } if (cam.active && !blockedleft) { rect.updatecameraleft(player_speed); enemygrp.updatecameraleft(player_speed); } else { player.addxposition(player_speed); } //rect.updatecameraleft(player_speed); } else if (window.leftarrow) { if(cam.active && !blockedright) { rect.updatecameraright(player_speed); enemygrp.updatecameraright(player_speed); } else { player.subxposition(player_speed); } //player.subxposition(player_speed); //rect.updatecameraright(player_speed); }
so if player greater 400 - "the player size" activate camera. if camera active player dosnt move. every gameobject rects or enemygrp moved new position.
by displacement of other elements generated move illusion.
maybe bether way or way go 2d games?
thanks answer!
render function:
void window::update(sdl_rect* rects, int rectssize, sdl_rectsconfig* config, sdl_rect* ppositon, sdl_rect* destpositon, sdl_rect* weapons, int weaponsize, sdl_rect* destweapons, int destsize, sdl_rect* enemys, sdl_rect* destenemys, int enemyssize) { sdl_setrendertarget(renderer, texture); sdl_setrenderdrawcolor(renderer, 255, 255, 255, 0x00); sdl_renderclear(renderer); sdl_rendercopy(renderer, background, null, null); (int = 0; < rectssize; i++) { if (config[i].r != null && config[i].g != null && config[i].b != null) { sdl_renderdrawrect(renderer, &rects[i]); sdl_setrenderdrawcolor(renderer, config[i].r, config[i].g, config[i].b, config[i].a); sdl_renderfillrect(renderer, &rects[i]); } if (config[i].image != null && config[i].font == null) { sdl_rendercopy(renderer, config[i].image, null, &rects[i]); } else if (config[i].image == null && config[i].font != null) { s << config[i].text; surfacemessage = ttf_rendertext_solid(config[i].font, s.str().c_str(), {0, 0, 0}); // ttf_rendertext_solid used on sdl_surface have create surface first if (surfacemessage == null) { printf("unable render text surface! sdl_ttf error: %s\n", ttf_geterror()); } message = sdl_createtexturefromsurface(renderer, surfacemessage); sdl_rendercopy(renderer, message, null, &rects[i]); sdl_freesurface(surfacemessage); sdl_destroytexture(message); s.str(""); s.clear(); } } /*sdl_renderdrawrects(renderer, rects, rectssize); sdl_setrenderdrawcolor(renderer, 0x00, 0x00, 0x00, 0x00); sdl_renderfillrects(renderer, rects, rectssize);*/ //example render in picture in rects (this clickeble) if (weaponsize == destsize) { (int = 0; < weaponsize; i++) { sdl_rendercopy(renderer, weaponsheet, &destweapons[i], &weapons[i]); } }else { std::cout << "error: sizes weaponsize , dessize need same." << std::endl; } //sdl_rendercopy(renderer, imgs, null, &rects[0]); if (leftarrow || lastpressed == "sdlk_left") { sdl_rendercopyex(renderer, playersheet, destpositon, ppositon, null, null, sdl_flip_horizontal); sdl_renderdrawrect(renderer, ppositon); } else { sdl_rendercopy(renderer, playersheet, destpositon, ppositon); sdl_renderdrawrect(renderer, ppositon); } (int = 0; < enemyssize; i++) { sdl_rendercopy(renderer, enemysheet, &destenemys[i], &enemys[i]); } //render value //render background //sdl_rendercopy(renderer, background, {}, &enemys[i]); sdl_setrendertarget(renderer, null); sdl_rendercopy(renderer, texture, null, null); sdl_renderpresent(renderer); }
i have found this:
sdl_rendersetviewport(renderer, &cam);
i think can camera :) without moving objects.
Comments
Post a Comment