1

I'm calling SDL_RenderCopy and it gets called and returns normally but doesn't draw anything to the window. Edited to make the question and code clearer. I'm thinking I might be trying to use something beyond its scope and hence it can't be called but this doesn't produce any error so I'm not sure. Here's the simple picture I refer to https://commons.wikimedia.org/wiki/Category:PNG_chess_pieces/Standard_transparent#/media/File:Chess_kdt60.png

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

// Recreation of the problem. Doesnt draw anything onto the white screen.

class King{

public:
  King(SDL_Renderer *renderer){
    SDL_Surface *Piece;
    Piece = IMG_Load("Pieces/BK.png"); // I'll attach the picture
    king = SDL_CreateTextureFromSurface(renderer, Piece);
    SDL_FreeSurface(Piece);
    kingRect.h = 100;
    kingRect.w = 100;
  }
  ~King(){}

  void render(SDL_Renderer *renderer){
    SDL_RenderCopy(renderer, king, NULL, &kingRect); // 99% sure the     problem is this
  }

private:
  SDL_Texture *king;
  SDL_Rect kingRect;
};

class Game {

 public:
  Game(const char *title, int sidelength){
    isRunning = true;
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0) isRunning = false;

window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,   SDL_WINDOWPOS_CENTERED, sidelength, sidelength, SDL_WINDOW_OPENGL);
if(window == NULL) isRunning = false;

renderer = SDL_CreateRenderer(window, -1, 0);
if(!renderer) isRunning = false;

SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  }
  ~Game(){}
  void handleEvents(){
    //Handles Events. I know this works.
    }
  }

  void update(){};

  void render(){
    SDL_RenderClear(renderer);
    BK.render(renderer);
    SDL_RenderPresent(renderer);
  }

  void clean(){
    //Cleans up after. I know this works.
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();
  }
  bool running(){return(isRunning);}
  King BK{renderer};
private:
  bool isRunning{true};
  SDL_Window *window;
  SDL_Renderer *renderer;
};

Game *game = nullptr;

int main(int argc, const char *argv[]){
  game = new Game("Testing Window", 800);

  while(game->running()){
    game->handleEvents();
    game->update();
    game->render();
  }
  game->clean();
  return(0);
}
SorSorSor
  • 21
  • 6
  • 3
    This isn't nearly enough code to tell where the bug is coming from. Are you checking return values from SDL functions and checking [`SDL_GetError()`](https://wiki.libsdl.org/SDL_GetError) when something fails? Please [edit](https://stackoverflow.com/posts/53384197/edit) your question to include information to reproduce the problem, and consider creating a [Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve). – alter_igel Nov 20 '18 at 02:12
  • 1
    Thank you for bringing the lack of clarity to my attention. I created a mini program that reproduces the same problem. SDL_GetError doesn't find any error which was part of the problem. – SorSorSor Nov 20 '18 at 03:02
  • Thanks! This looks much better. I suspect that `kingRect.x` and `kingRect.y` are uninitialized and might have garbage values, which could place the target rectangle somewhere really far off screen. Try adding `kingRect.x = 0; kingRect.y = 0;` in your constructor – alter_igel Nov 20 '18 at 04:18

1 Answers1

2

King BK{renderer}; field gets initialised before your Game::Game finishes and gets a chance to assign a renderer, so it gets NULL instead. NULL is not a valid renderer and can't create textures. If you would have checked for error you would have got Invalid renderer message. Also decent compiler with enabled warnings will tell something like warning: 'Game::renderer' is used uninitialized in this function [-Wuninitialized]; consider enabling better warning levels in your compiler.

Second thing is that you never called IMG_Init with required image formats you intend to load.

Third thing is that code is misformatted and wouldn't compile without modifications. I suggest testing code that you post as MCCVE for still being compilable and reproducing your problem (as MCCVE implies).

keltar
  • 17,711
  • 2
  • 37
  • 42
  • Thanks. It seems like there were two problems.The first is what you described in your first paragraph and the second was that even though I called IMG_LOAD with a path to the picture it wouldn't be loaded. So I moved the png right beside the actual code for this project and then it would load. – SorSorSor Nov 20 '18 at 16:32