sdl - SDL2 C++ - Multiple Image Rendering Bug -
before begin, ide use code::blocks.
i going practice things learned online sdl in project separate study project. wanted load image of ball on top of background. worked fine in study project. replicated code on test project, , ball disappeared. background visible.
i removed codes involve background , worked fine.
my code:
#include <iostream> #include <cstdlib> #include <sdl.h> #include <sdl_image.h> #undef main using namespace std; sdl_texture *loadtexture (string filepath, sdl_renderer *rendertarget){ sdl_texture *texture = 0; sdl_surface *surface = img_load(filepath.c_str()); texture = sdl_createtexturefromsurface(rendertarget, surface); sdl_freesurface(surface); return texture; } int main () { sdl_window *window = 0; sdl_texture *ball = 0; sdl_texture *background_sky = 0; sdl_renderer *rendertarget = 0; int framew, frameh; int texturew, textureh; //--crop instructions--// framew = texturew / 1; //row division frameh = textureh / 1; //column division sdl_rect ballcrop; ballcrop.x = ballcrop.y = 0; ballcrop.w = framew; //length of selected area ballcrop.h = frameh; //height of selected area sdl_rect ballpos; ballpos.x = ballpos.y = 10; //ball position ballpos.w = ballpos.h = 64; //size of crop //---------------------// sdl_init(sdl_init_everything); img_init(img_init_png); window = sdl_createwindow("ball", sdl_windowpos_centered, sdl_windowpos_centered, 500, 500, sdl_window_shown); rendertarget = sdl_createrenderer(window, -1, sdl_renderer_accelerated | sdl_renderer_presentvsync); ball = loadtexture("ball.png", rendertarget); background_sky = loadtexture("bg_sky.png", rendertarget); sdl_querytexture(ball, null, null, &texturew, &textureh); //crop action bool active = true; sdl_event ev; while (active) { while (sdl_pollevent(&ev)!=0) { if (ev.type == sdl_quit) active = false; else if (ev.type == sdl_keydown) { switch (ev.key.keysym.sym) { case sdlk_up: ballpos.y -= 5; break; case sdlk_down: ballpos.y += 5; break; } } } sdl_renderclear(rendertarget); sdl_rendercopy(rendertarget, background_sky, null, null); sdl_rendercopy(rendertarget, ball, &ballcrop, &ballpos); sdl_renderpresent(rendertarget); } sdl_destroywindow(window); sdl_destroytexture(background_sky); sdl_destroytexture(ball); sdl_destroyrenderer(rendertarget); window = 0; ball = background_sky = 0; rendertarget = 0; img_quit(); sdl_quit(); return 0; }
i dont think want show source code of study project because extremely messy.
any appreciated :)
you have error in calculation of framew
, frameh
; code calculates
//--crop instructions--// framew = texturew / 1; //row division frameh = textureh / 1; //column division
uses texturew
, textureh
before these initialised. move entire 'crop instructions' block until after have called sdl_querytexture
.
Comments
Post a Comment