Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL Interface


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1   Members   -  Reputation: 108

Like
0Likes
Like

Posted 10 May 2014 - 11:30 AM

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_surface = IMG_Load("Background.png");
background_texture = SDL_CreateTextureFromSurface(renderer, background_surface);

button_surface = IMG_Load("newRaceButton.png");
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.



#2   Prime Members   -  Reputation: 1643

Like
0Likes
Like

Posted 10 May 2014 - 01:18 PM

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.



#3   Members   -  Reputation: 108

Like
0Likes
Like

Posted 10 May 2014 - 01:26 PM

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



#4   Moderators   -  Reputation: 9444

Like
0Likes
Like

Posted 10 May 2014 - 02:23 PM

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.
gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS