Hello!
I have a weird behavior when trying to lock a texture with a purpose of setting individual pixels and then presenting the whole stuff, and this behavior manifests itself with three different errors occurring in three different places of the program and each time I try to run the program only one on them happens seemingly at random.
Context:
I am doing a simple Woolfenstien-like raycaster. I first used SDL_DrawPoint
and everything worked well. Then I decided to check if writing directly to “screen” would be faster.
Code snippets:
First, I have a class with a set of methods that wrap calls to SDL.
Below is a part of initialization of resources:
SDL_Window *window = SDL_CreateWindow(
"Window name",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED);
SDL_Texture *screen = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
Drawing facilities are as follows:
void update() {
SDL_RenderCopy(RENDERER, SCREEN, NULL, NULL);
SDL_RenderPresent(RENDERER);
};
void clear() {
SDL_SetRenderDrawColor(RENDERER, 0x80, 0x80, 0x80, 0xFF);
SDL_RenderClear(RENDERER);
};
void lock() {
int pitch;
SDL_LockTexture(SCREEN, NULL, (void**)&m_screen_pixels, &pitch);
assert(pitch/4 == SCREEN_WIDTH); //this is early in the development =)
}
void unlock() {
SDL_UnlockTexture(SCREEN);
m_screen_pixels = NULL;
}
void setPixel(int x, int y, uint32_t color) {
m_screen_pixels[SCREEN_WIDTH*y+x] = color;
}
m_screen_pointer is defined like this:
uint32_t *m_screen_pixels {nullptr};
lock()
is called in the begging of the drawing function (each frame), unlock()
is called when this function is exited. setPixel()
is called inside this function every time a pixel color should be set.
The errors:
I am getting one of the three following errors each time the program is run:
-
[1] 573049 segmentation fault (core dumped)
- before call toSDL_RenderPresent()
; -
munmap_chunk(): invalid pointer
- before call toSDL_UnlockTexture()
; -
malloc(): invalid size (unsorted)
- before call toSDL_RenderCopy()
.
I would appreciate any advise on what is going on.
Thank you in advance and wish you a great weekend!