SDL Interface

Recommended Posts

lsauceda    108

Well, I'm trying to create an interface for my game with SDL. At first it worked correctly, but I was limited to using .bmp as my image format, I needed support for .png and I downloaded and installed SDL2_Image. The problem is that the images are not being loaded to the window it just creates a white window and nothing is displayed. I read somewhere that using IMG_Load() worked the same way SDL_LoadBMP() does, but it doesn't. Here's my code:

#define SDL_MAIN_HANDLED
#include "SDL.h"
#include "SDL_image.h"

int main(int argc, char *argv[])
{
int a = 1;
SDL_Event event;

SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Surface* background_surface = NULL;
SDL_Texture* background_texture = NULL;
SDL_Surface* button_surface = NULL;
SDL_Texture* button_texture = NULL;

SDL_Rect button_pos;
button_pos.w = 115;
button_pos.h = 65;
button_pos.x = 103;
button_pos.y = 10;

SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
window = SDL_CreateWindow(
"My Game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
320,
568,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
background_texture = SDL_CreateTextureFromSurface(renderer, background_surface);

button_texture = SDL_CreateTextureFromSurface(renderer, button_surface);

while(a)
{
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
{
a = 0;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
SDL_RenderCopy(renderer, button_texture, NULL, &button_pos);
SDL_RenderPresent(renderer);
}

SDL_DestroyTexture(button_texture);
SDL_FreeSurface(button_surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(background_texture);
SDL_FreeSurface(background_surface);
SDL_DestroyWindow(window);

IMG_Quit();
SDL_Quit();
return 0;
}


I've checked evrything is spelled correctly and that the files are in the same directory and can't find what's wrong.

Share on other sites
deftware    1778

Perform some error checking and validation on the values being returned from all the SDL functions. Try logging the result of an SDL_GetError() call after each function, just to make sure that everything is actually doing what your code assumes it is doing. Then we will go from there.

Share on other sites
lsauceda    108

@radioteeth ok I'll do that, then I will share the results.

Share on other sites
fastcall22    10840
As with any third party library, you should always consult its documentation and cover every possible point of failure. It's better to have your program tell you where and why something went wrong rather than having a constant "it's not working" and trying to guess what will fix it.