I have a project that uses SDL 1.2 and want to migrate it to SDL 2.0. To keep things simple I tried writing a simple program just to get familiar with the changes.
However, SDL_CreateWindow is failing, and SDL_GetError is not showing anything. So I don't know why creating the window isn't working.
The window is actually created, but then disappears.
#include <SDL.h>
#include <stdio.h> // printf etc
#include <stdlib.h> // abs, malloc, realloc
#include <stdint.h> // uint8_t, uint16_t, int32_t
#define WIDTH 853
#define HEIGHT 480
int main(int argc, char** argv) {
SDL_Renderer* renderer;
SDL_Window* window;
SDL_Texture* screenTexture;
SDL_Event event;
Uint8* screenBuffer;
FILE* fp;
int quit = 0;
fp = fopen("SDL2test.log", "w");
if (!fp) {
printf("\nCould not create log file");
return -1;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(fp, "\nCould not initialise SDL: %s\n", SDL_GetError() );
return -2;
}
window = NULL;
if ( NULL == ( window = SDL_CreateWindow("SDL2 test",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, 0)));{
fprintf(fp, "Could not create SDL window: %s\n", SDL_GetError() );
SDL_Delay(3000);
SDL_Quit();
return -3;
}
if ( !( renderer = SDL_CreateRenderer(window, -1, 0) ) ); {
SDL_Quit();
return -4;
}
if ( !( screenTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT)));
screenBuffer = (Uint8*)malloc( sizeof(Uint8) * 3 * WIDTH * HEIGHT );
if (NULL == screenBuffer){
SDL_Quit();
return -5;
}
memset(screenBuffer, 0, 3 * WIDTH * HEIGHT); //set to black
SDL_UpdateTexture( screenTexture,
NULL,
screenBuffer,
WIDTH * sizeof(Uint8) );
while( !quit && ( SDL_WaitEvent(&event) ) ) {
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, screenTexture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_Delay(3000);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
This is your create window if statement:
Did you notice the
;
at the end of first line? This means the part of the code in the brackets gets executed every time. Basically the code looks like this:There is at least one such error in the code.