Reputation: 1
I use Visual Studio Community as an IDE.
My main problem is that my code simply doesn't present the information produced by the renderer onto the window. It opens a blank white window with nothing on it. However, it is strange as there are no build errors and every line of code runs, just nothing is presented.
After searching around for the issue i found that many, if not all, code examples aren't split into more than one function so i couldn't compare the issue. After thinking this, i changed my code to another version which basically had all the rendering and production (Same base code as below) in one function with a 'SDL_RenderPresent' at the end of that function. This worked for one part of the code, the part that produced a tiled grass background, but not for the randomly placed Rabbit sprite. I searched for this issue as well but equally found nothing as all just had once section they wanted to render.
I know my issue contains a few issues that should probably be in separate questions but it seemed to go from problem to problem so i've reached out for higher knowledge on all grounds.
This is my Display.cpp file where the problem, i suspect, lies.
#include "Display.h"
Display::Display(){
}
Display::~Display(){
}
void Display::World() {
//Gets display size to scale from :)
SDL_DisplayMode screen;
SDL_GetDesktopDisplayMode(0, &screen);
int screenW = screen.w; //These produce 0 for some reason??
int screenH = screen.h; // ^^
//Fin
SDL_Window * window = SDL_CreateWindow("SDL2 Displaying Image", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenW, screenH - 20, SDL_WINDOW_SHOWN);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_TARGETTEXTURE || SDL_RENDERER_ACCELERATED);
SDL_Surface * surfaceGrass = IMG_Load("Grass.jpg"); //Grass Sprite is 200 by 200 px
SDL_Texture * grass = SDL_CreateTextureFromSurface(renderer, surfaceGrass);
SDL_FreeSurface(surfaceGrass);
//Start of grass make things
int grassW = screenW * 0.25;
int grassH = grassW;
int vertical;
for (int i = 0; i <= 3; i++) {
switch (i) {
case(0):
vertical = 0;
break;
case(1):
vertical = grassW;
break;
case(2):
vertical = 2 * grassW;
break;
}
for (int j = 0; j < screenW; j = j + grassW) {
SDL_Rect screenRectForGrass = { j, vertical, grassW, grassH };
SDL_RenderCopy(renderer, grass, NULL, &screenRectForGrass);
}
}
//End of grass make things
MakeRabbit();
}
void Display::MakeRabbit() {
int colour = rand() % 10;
if (colour <= 3) {
std::cout << "Colour statement" << std::endl;
SDL_Surface * surfaceRabbit = IMG_Load("Brown Rabbit.jpg"); //Produces a square 50 by 50 pixels
SDL_Texture * rabbit = SDL_CreateTextureFromSurface(renderer, surfaceRabbit);
}
else {
std::cout << "Colour statement" << std::endl;
SDL_Surface * surfaceRabbit = IMG_Load("Black Rabbit.jpg");
SDL_Texture * rabbit = SDL_CreateTextureFromSurface(renderer, surfaceRabbit);
}
int randomX = rand() % 50;
int randomY = rand() % 50;
SDL_Rect screenRectForRabbit = { randomX, randomY, 50, 50 };
SDL_RenderCopy(renderer, rabbit, NULL, &screenRectForRabbit);
DisplayRenderer();
}
void Display::DisplayRenderer() {
SDL_RenderPresent(renderer);
Quit();
}
void Display::Quit() {
bool quit = false;
SDL_Event event;
while (!quit) {
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
SDL_DestroyTexture(grass);
SDL_DestroyTexture(rabbit);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_FreeSurface(surfaceRabbit);
}
Display.h file
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
class Display{
private:
SDL_Window * window;
SDL_Renderer * renderer;
SDL_Surface * surfaceGrass;
SDL_Surface * surfaceRabbit;
SDL_Texture * rabbit;
SDL_Texture * grass;
private:
int screenW;
int screenH;
public:
Display();
~Display();
void MakeRabbit();
void World();
void DisplayRenderer();
void Quit();
};
and Main.cpp
#include "Display.h"
int main(int argc, char ** argv){
srand(time(NULL));
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_JPG);
Display Main;
Main.World();
SDL_Quit();
IMG_Quit();
return 0;
}
Upvotes: 0
Views: 354
Reputation: 656
You are redefining several of your variables each time you assign to them.
Instead of
SDL_Texture * rabbit = ...
use
rabbit = ...
This will use the SDL_Texture * rabbit
variable that you already have defined in the class. The way you are doing it creates two different pointers, both called rabbit
, but in different scopes.
Upvotes: 2