SDL_RenderCopy not doing anything
up vote
1
down vote
favorite
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);
}
c++ sdl sdl-2 sdl-image
add a comment |
up vote
1
down vote
favorite
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);
}
c++ sdl sdl-2 sdl-image
2
This isn't nearly enough code to tell where the bug is coming from. Are you checking return values from SDL functions and checkingSDL_GetError()
when something fails? Please edit your question to include information to reproduce the problem, and consider creating a Minimal, Complete, Verifiable Example.
– alter igel
Nov 20 at 2: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 at 3:02
Thanks! This looks much better. I suspect thatkingRect.x
andkingRect.y
are uninitialized and might have garbage values, which could place the target rectangle somewhere really far off screen. Try addingkingRect.x = 0; kingRect.y = 0;
in your constructor
– alter igel
Nov 20 at 4:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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);
}
c++ sdl sdl-2 sdl-image
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);
}
c++ sdl sdl-2 sdl-image
c++ sdl sdl-2 sdl-image
edited Nov 20 at 3:00
asked Nov 19 at 23:37
SorSorSor
84
84
2
This isn't nearly enough code to tell where the bug is coming from. Are you checking return values from SDL functions and checkingSDL_GetError()
when something fails? Please edit your question to include information to reproduce the problem, and consider creating a Minimal, Complete, Verifiable Example.
– alter igel
Nov 20 at 2: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 at 3:02
Thanks! This looks much better. I suspect thatkingRect.x
andkingRect.y
are uninitialized and might have garbage values, which could place the target rectangle somewhere really far off screen. Try addingkingRect.x = 0; kingRect.y = 0;
in your constructor
– alter igel
Nov 20 at 4:18
add a comment |
2
This isn't nearly enough code to tell where the bug is coming from. Are you checking return values from SDL functions and checkingSDL_GetError()
when something fails? Please edit your question to include information to reproduce the problem, and consider creating a Minimal, Complete, Verifiable Example.
– alter igel
Nov 20 at 2: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 at 3:02
Thanks! This looks much better. I suspect thatkingRect.x
andkingRect.y
are uninitialized and might have garbage values, which could place the target rectangle somewhere really far off screen. Try addingkingRect.x = 0; kingRect.y = 0;
in your constructor
– alter igel
Nov 20 at 4:18
2
2
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()
when something fails? Please edit your question to include information to reproduce the problem, and consider creating a Minimal, Complete, Verifiable Example.– alter igel
Nov 20 at 2:12
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()
when something fails? Please edit your question to include information to reproduce the problem, and consider creating a Minimal, Complete, Verifiable Example.– alter igel
Nov 20 at 2:12
1
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 at 3:02
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 at 3: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 at 4:18
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 at 4:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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).
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 at 16:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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).
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 at 16:32
add a comment |
up vote
1
down vote
accepted
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).
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 at 16:32
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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).
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).
answered Nov 20 at 4:26
keltar
12.2k12534
12.2k12534
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 at 16:32
add a comment |
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 at 16:32
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 at 16:32
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 at 16:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384197%2fsdl-rendercopy-not-doing-anything%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
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()
when something fails? Please edit your question to include information to reproduce the problem, and consider creating a Minimal, Complete, Verifiable Example.– alter igel
Nov 20 at 2: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 at 3:02
Thanks! This looks much better. I suspect that
kingRect.x
andkingRect.y
are uninitialized and might have garbage values, which could place the target rectangle somewhere really far off screen. Try addingkingRect.x = 0; kingRect.y = 0;
in your constructor– alter igel
Nov 20 at 4:18