sdl 2 - SDL_Surface alpha blending -
sdl 2 - SDL_Surface alpha blending -
while building own graphics engine, need have possibility of alpha blending (on sdl_surface
). here testing code:
(...)/test2/test2_1.cpp : #include"../render_sdl/me_render.h" int main(int argc, char* argv[]) { init(); sdl_window* w = sdl_createwindow("test", 50, 50, 800, 600, sdl_window_shown | sdl_window_resizable); me_objects o; me_rect r1(50, 100, 300, 200, me_rgba(0, 0, 255, 255)); me_rect r2(200, 150, 250, 350, me_rgba(255, 0, 0, 127)); o.add(&r1); o.add(&r2); sdl_surface *s1 = sdl_getwindowsurface(w); sdl_setsurfaceblendmode(s1, sdl_blendmode_blend); uint32 rmask, gmask, bmask, amask; #if sdl_byteorder == sdl_big_endian rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif sdl_surface *s = sdl_creatergbsurface(0, 800, 600, 32, rmask, gmask, bmask, amask); sdl_setsurfaceblendmode(s, sdl_blendmode_blend); o.me_rects[0].render(s, 0, 0); o.me_rects[1].render(s, 0, 0); me_img i1(200, 250, "koncepcja.png"); o.add(&i1); me_line l1(10, 10, 160, 200, me_rgba(0, 255, 0, 255)); o.add(&l1); o.me_imgs[0].load(); o.updatebasicdata(2, 0); o.me_imgs[0].render(s, 0, 0); o.me_lines[0].render(s, 0, 0); sdl_rect scr = {0, 0, 800, 600}; sdl_blitsurface(s, null, s1, &scr); sdl_updatewindowsurface(w); uint8 _r, _g, _b, _a; sdl_getrgba(((uint32*)s1->pixels)[(151*s1->w)+201], s1->format, &_r, &_g, &_b, &_a); std::cout<<"rgba: "<<(int)_r<<' '<<(int)_g<<' '<<(int)_b<<' '<<(int)_a<<'\n'; bool running = true; while(running) { sdl_event event; while(sdl_pollevent(&event)) { switch(event.type) { case sdl_quit: running = false; case sdl_windowevent: if(event.window.event==sdl_windowevent_resized) { s1 = sdl_getwindowsurface(w); sdl_blitsurface(s, null, s1, &scr); sdl_updatewindowsurface(w); } } } } sdl_destroywindow(w); char _resp; std::cin>>_resp; quit(); homecoming 0; } (...)/render_sdl/me_render.h : (...) bool me_rect::render(void* surface, uintmax_t scrposx1, uintmax_t scrposy1) { for(uintmax_t = x1-scrposx1; i<x2-scrposx1; ++i) for(uintmax_t j = y1-scrposx1; j<y2-scrposx1; ++j) ((uint32*)(((sdl_surface*)surface)->pixels))[(j*((sdl_surface*)surface)->w)+i] = sdl_maprgba(((sdl_surface*)surface)->format, color.r, color.g, color.b, color.a); homecoming true; } (...)
so setting every pixel value directly. but, despite setting alpha value of rectangle r2
127, rectangle (on screen) isn't transparent - it's color dark reddish without dependence on background colors (black area r1
- bluish rectangle). checked output. shows:
rgba: 127 0 0 255
instead of expected result:
rgba: 127 0 255 255
how create alpha blending work correctly?
i solved problem creating additional surface (of dimensions of rectangle r2
) , filling exclusively color (transparent red). blitted surface s
(in whole screen contents stored) , blitted surface s
s1
(window surface). , alpha blending works!
alpha sdl-2
Comments
Post a Comment