Here’s example code to replicate the problem. I removed the code that would mess with the pixels; just using SDL_UpdateTexture() is enough.
Code:
#include
#include
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
using std::stringstream;
using std::string;
string IntToString(int x)
{
stringstream ss;
ss << x;
return ss.str();
}
void ShowError(const char * title, SDL_Window * window)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, SDL_GetError(), window);
}
void ShowDebug(const char * title, int x, SDL_Window * window)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, IntToString(x).c_str(), window);
}
int main(int argc, char ** argv)
{
if (argc == 2)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window = SDL_CreateWindow("Picaxo3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
if (window != NULL)
{
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer != NULL)
{
char * filename = argv[1];
SDL_Surface * image = IMG_Load(filename);
if (image != NULL)
{
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image);
if (texture != NULL)
{
while (!quit)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:
quit = true;
break;
}
break;
}
if (SDL_UpdateTexture(texture, NULL, image->pixels, image->pitch) < 0)
ShowError("Update texture failed", window);
if (SDL_RenderClear(renderer) < 0)
ShowError("Render clear failed", window);
if (SDL_RenderCopy(renderer, texture, NULL, NULL) < 0)
ShowError("Render copy failed", window);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
}
else
ShowError("Create texture failed", window);
SDL_FreeSurface(image);
}
else
ShowError("Load image failed", window);
SDL_DestroyWindow(window);
SDL_Quit();
}
else
ShowError("Create renderer failed", window);
}
else
ShowError("Create window failed", window);
}
else
{
// TODO printf usage
}
return 0;
}