06-27-2021, 04:32 PM
Hi!
It would be better if you post here source code of your current attempt, so I can check why it's failing.
The "tricky" part of setting up the external rendering is not in tilengine -as it takes just one function-, but on the host side.
In SDL2, at the beginning you must:
Note:
Tilengine supports window scaling, so internal framebuffer resolution is not the same than target window size
This is basically what the built-in tilengine window does, you can find it in window.c source code. As you can see it is mostly SDL2 stuff, not tilengine stuff. This is different for each framework where you want to embed external rendering.
Hope this helps!
It would be better if you post here source code of your current attempt, so I can check why it's failing.
The "tricky" part of setting up the external rendering is not in tilengine -as it takes just one function-, but on the host side.
In SDL2, at the beginning you must:
- create the window
- create the renderer
- create 32-bit ARGB backbuffer texture with "streaming" attribute (so it gets optimized for frequent writes):
Code:
SDL_Window* window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, wnd_width, wnd_height, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED + SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, wnd_params.width, wnd_params.height);
Note:
Tilengine supports window scaling, so internal framebuffer resolution is not the same than target window size
- wnd_width and wnd_height are target window dimensions after scaling
- wnd_params.width and wnd.params.height are original framebuffer size (the one you set with TLN_Init())
- Gain write access to framebuffer by locking the texture
- Set render target to the locked texture
- Render the frame
- Unlock the backbuffer texture so SDL has access again
- Copy the backbuffer texture to the renderer, scaling it to fit window size
- Present the renderer to the screen:
Code:
// setup render target
static uint8_t* rt_pixels; // pointer to pixel data
static int rt_pitch; // bytes per scanline
SDL_LockTexture (backbuffer, NULL, (void**)&rt_pixels, &rt_pitch);
TLN_SetRenderTarget (rt_pixels, rt_pitch);
// render frame
TLN_UpdateFrame(frame);
// present to screen
SDL_UnlockTexture(backbuffer);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backbuffer, NULL, &dstrect);
SDL_RenderPresent(renderer);
This is basically what the built-in tilengine window does, you can find it in window.c source code. As you can see it is mostly SDL2 stuff, not tilengine stuff. This is different for each framework where you want to embed external rendering.
Hope this helps!