Yay! Just been approved, now I can post
So, hereās my problem - Iām trying to get a fog-of-war effect by doing:
- render the background
- render the fog to a texture, calculating on a tile-basis every frame
- render the fog on top of the background
- render ā¦
The fog is supposed to be black by default (RGBA=0,0,0,255) and Iām tiling the fog texture with āgrey fogā (i.e.: RGBA=0,0,0,0->255) then copying the fog texture over the background in the main rendererā¦ The code below is slightly simplified to get rid of non-relevant logic, but still exhibits the same problem
Code:
-
(void) renderAt:(SDL_Rect)screen withPCs:(NSArray *)pcs
{
_box.x = 32 * (screen.x / 32);
_box.y = 32 * (screen.y / 32);SDL_SetRenderTarget(_renderer, _fogMap);
SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_NONE);SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(_renderer);SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND);
for (int y=0; y<_box.h; y += 32)
for (int x=0; x<_box.w; x += 32)
{
SDL_Rect src = (SDL_Rect){0,0,32,32};
SDL_Rect dst = (SDL_Rect){x, y, 32, 32};
SDL_RenderCopy(_renderer, _tiles, &src, &dst);
}
SDL_SetRenderTarget(_renderer, NULL);SDL_Rect src = (SDL_Rect){screen.x%32, screen.y%32, _screenSize.w, _screenSize.h};
SDL_Rect dst = (SDL_Rect){0, 0, _screenSize.w, _screenSize.h};
SDL_RenderCopy(_renderer, _fogMap, &src, &dst);
}
So with the above, all I see on the screen is black, over which subsequent renders to the main target (the screen) render fine. Basically I donāt see any background at all.
If I change the initial call to SDL_SetRenderDrawColor() to be
Code:
SDL_SetRenderTarget(_renderer, _fogMap);
SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(_renderer,[color=red]0xff, 0xff, 0xff[/color], 0x00);
SDL_RenderClear(_renderer);
ā¦ then I see the tile that Iām rendering in the loop appear, as expected, but Iām still not seeing anything behind the stamped 32-by-32 tile when I render this texture on top of the background at the last line in the code snippet.
To clarify - I can see the render (āSDL_RenderCopy(_renderer, _fogMap, &src, &dst);ā) happening but instead of whatever is behind the texture (my carefully prepared background texture) showing through where the alpha is non-unity, all I see is the ā_fogMapā texture, no background. In other words the fog seems to be replacing the background, not blending with it.
Iām reasonably sure the textures that make up the tiles Iām blitting in the inner loop have alpha because if I put a white background behind them, the areas with lower alpha in the tile show brighter (I basically rendered a gaussian spot in the center of the tile, with the center alpha ādarkestā in the tile, meaning it should show through the background more than the surrounds, and I see a whole bunch of circular spots on the screen when the background is white).
So Iām presumably doing something wrong. Alpha blitting is working when I blit the tiles into the fog texture (which is also texture-to-texture) so I donāt see why it ought not work when I blit from the fog-texture to the main renderer. The only difference I can see between the fog texture and any other is that it has been created with SDL_TEXTUREACCESS_TARGET on itā¦
Code:
-
(BOOL) createScreenWithRenderer:(SDL_Renderer *)renderer
inWindow:(SDL_Window *)window
{
uint32_t format;
BOOL ok = NO;
_renderer = renderer;
_window = window;SDL_GetWindowSize(window, &_screenSize.w, &_screenSize.h);
_box.w = _screenSize.w + 64;
_box.h = _screenSize.h + 64;format = SDL_GetWindowPixelFormat(window);
_fogMap = SDL_CreateTexture(_renderer, format, SDL_TEXTUREACCESS_TARGET, _box.w, _box.h);
if (_fogMap)
ok = YES;
else
NSLog(@āCannot create fog-of-war screen texture\nā);_surface = SDL_ConvertSurfaceFormat([_alpha surface], format, 0); // ā_alphaā contains the fog texture tiles set at 32x32 each
_tiles = SDL_CreateTextureFromSurface(renderer, _surface);return ok;
}
So, any ideas on what Iām doing wrong ?
Cheers
Simon