Donaldo Mac Eachen
Donaldo Mac Eachen

Reputation: 11

Image not showing up in SDL

When I run this it just shows a black screen, and if i Put SDL_GetError() at the end it prints a blank line.....

Any ideas on how to fix this?

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>

class character {

public: SDL_Rect src, cur;
public: SDL_Texture *image;

void setSrc(int x, int y, int w, int h) {

    src.x = x;
    src.y = y;
    src.w = w;
    src.h = h;

}

void setCur(int x, int y, int w, int h) {

    src.x = x;
    src.y = y;
    src.w = w;
    src.h = h;

}

};  

int main(int argc, char* argv[]) {

bool in = true;
character p1, p2, ball; 
SDL_Window *window = 0;
SDL_Renderer *renderer = 0;
SDL_Surface *screen, *imageLoader;

SDL_Init(SDL_INIT_VIDEO);

window = SDL_CreateWindow("Pong",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);

renderer = SDL_CreateRenderer(window, -1, 0);

p1.setSrc(0, 0, 100, 500);
p1.setCur(0, 0, 100, 500);

imageLoader = IMG_Load("/home/donaldo/Documents/Games/Images/player.bmp");

p1.image = SDL_CreateTextureFromSurface(renderer, imageLoader);

SDL_RenderCopy(renderer, p1.image, &p1.src, &p1.cur);

SDL_RenderPresent(renderer);

SDL_Delay(3000);

return 0;

}

Upvotes: 1

Views: 88

Answers (1)

i-am-wells
i-am-wells

Reputation: 61

You have a typo in your setCur() function. I assume you want to be setting the cur rectangle, not src again.

Also, not related but it's good practice to free the surface and texture you allocated when you're done with them.

Upvotes: 1

Related Questions