I'm trying to make some kind of 'engine' with C++/SDL. So I followed the LazyFoo's Tutorial and searched others places/videos about it [SDL/C++].
Everything was fine, until I organized my code in 2 Classes. It was just Game.h/Game.cpp and main.cpp, but I thought that would be good to separate the part of the code that loads Images from the Game class that loads and destroys the Surfaces/Textures (Game class).
So I literally copy/pasted the code to another class. What I copy/pasted was the two boolean functions textureBMP and textureIMG everything remains the same. And before I copy/pasted the code to another class, everything was working fine, so I don't know what is making this.
Another little question, is the way I'm organizing the code the right way? I want to have good habits straight in the beginning, even with small projects, such as this little project just for learning.
Thanks in advance!
Image.h
#pragma once
#include "Game.h"
class Image
{
public:
Image();
~Image();
Game game;
SDL_Surface *gBMP = NULL;
SDL_Texture *tBMP = NULL;
SDL_Surface *gIMG = NULL;
SDL_Texture *tIMG = NULL;
bool textureBMP(char *mediaLocation, bool SetColorKey, int red, int green, int blue);
bool textureIMG(char *mediaLocation, int imgFlags);
};
Image.cpp
#include "Image.h"
Image::Image()
{
}
Image::~Image()
{
}
bool Image::textureBMP(char *mediaLocation, bool SetColorKey, int red, int green, int blue) {
gBMP = SDL_LoadBMP(mediaLocation);
if (gBMP == NULL) {
printf("Nao foi possivel carregar a imagem %s,por causa do seguinte erro: \n %s \n", mediaLocation, SDL_GetError());
return false;
}
else {
if (SetColorKey) {
SDL_SetColorKey(gBMP, 1, SDL_MapRGB(gBMP->format, red, green, blue));
}
tBMP = SDL_CreateTextureFromSurface(game.renderer, gBMP);
SDL_FreeSurface(gBMP);
}
return true;
}
bool Image::textureIMG(char *mediaLocation, int imgFlags) {
if (!(IMG_Init(imgFlags) & imgFlags)) {
printf("SDL_image não pode ser inicializada! SDL_image Error: %s\n", IMG_GetError());
return false;
}
else {
gIMG = IMG_Load(mediaLocation);
tIMG = SDL_CreateTextureFromSurface(game.renderer, gIMG);
SDL_FreeSurface(gIMG);
}
return true;
}
Game.h
#pragma once
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <string>
class Game
{
public:
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
bool running;
SDL_Event event;
SDL_Window *gWindow = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *gScreenSurface = NULL;
Game();
~Game();
bool initEngine(char *windowName);
void freeSurface(SDL_Surface *surfaceName);
void freeTexture(SDL_Texture *textureName);
void destroyRenderer();
void destroyWindow();
};
Game.cpp
#include "Game.h"
Game::Game()
{
}
Game::~Game()
{
}
bool Game::initEngine(char *windowName) {
bool initSucess = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("A Engine não foi iniciada pelo seguinte erro: \n %s \n", SDL_GetError());
initSucess = false;
return initSucess;
}
else {
gWindow = SDL_CreateWindow(windowName, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gWindow == NULL) {
printf("A janela não pode ser criada pelo seguinte erro: \n %s \n", SDL_GetError());
initSucess = false;
return initSucess;
}
else {
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
}
return initSucess;
}
void Game::freeSurface(SDL_Surface *surfaceName) {
SDL_FreeSurface(surfaceName);
surfaceName = NULL;
}
void Game::freeTexture(SDL_Texture *textureName) {
SDL_DestroyTexture(textureName);
textureName = NULL;
}
void Game::destroyRenderer() {
SDL_DestroyRenderer(renderer);
renderer = NULL;
}
void Game::destroyWindow() {
SDL_DestroyWindow(gWindow);
gWindow = NULL;
}
main.cpp
#include <iostream>
#include <SDL.h>
#include "Game.h"
#include "Image.h"
int main(int argc, char* args[]) {
Game game;
Image img;
if (!game.initEngine("TESTE")) {
printf("Falha ao iniciar! \n");
return 0;
}
game.running = true;
if (!img.textureBMP("res/bouncingball.bmp", true, 255, 0, 255))
printf("Falha ao iniciar imagem!\n");
if (!img.textureIMG("res/Tulips.jpg", IMG_INIT_JPG))
printf("Falha ao iniciar imagem! \n");
SDL_Rect stretchRect{ (game.SCREEN_WIDTH / 2) - 50, (game.SCREEN_HEIGHT / 2) - 50, 100, 100 };
SDL_Rect stretchRect2{ 0, 0, game.SCREEN_WIDTH, game.SCREEN_HEIGHT };
while (game.running) {
while (SDL_PollEvent(&game.event) != 0) {
switch (game.event.type) {
case SDL_QUIT:
game.running = false;
game.freeSurface(game.gScreenSurface);
game.freeTexture(img.tBMP);
game.freeTexture(img.tIMG);
game.destroyRenderer();
game.destroyWindow();
IMG_Quit();
SDL_Quit();
break;
}//Switch Event END
} // PollEvent END
SDL_RenderCopy(game.renderer, img.tIMG, nullptr, &stretchRect2);
SDL_RenderCopy(game.renderer, img.tBMP, nullptr, &stretchRect);
SDL_RenderPresent(game.renderer);
SDL_GL_SetSwapInterval(1);
}// game.running END
return 0;
}
PrintScreen of the Output
I made some changes, and now it works properly. I changed two things, the first was the part that was giving the black screen, and now the functions look like this:
Image.h
bool textureBMP(char *mediaLocation, <...>, SDL_Renderer *render);
bool textureIMG(char *mediaLocation, <...>, SDL_Renderer *render);
main.cpp
if (!img.textureIMG("res/Tulips.jpg", IMG_INIT_JPG, game.renderer))
And, the other change was that, before Image.h was including Game.h, and, in main.cpp I was including Game.h, and Image.h and this would create an error right?
Because of this https://en.wikipedia.org/wiki/Include_guard
Obs: I created an answer because the comment doesn't allow codes
Related
I keep getting this issue when I run my program. My images fail to load in with the error: "Invalid texture". The program used to work fine. I'm working on it in linux ubuntu with all of my drivers updated. Here is the code where the renderer is made and the images are loaded.
//init.h
//#pragma once
#ifndef INIT_H
#define INIT_H
static const int SCREEN_WIDTH = 480;
static const int SCREEN_HEIGHT = 480;
static SDL_Surface* currentSurface = NULL;
static SDL_Window* window = NULL;
static SDL_Surface* screenSurface = NULL;
static SDL_Renderer* renderer = NULL;
//static SDL_Surface *SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_ANYFORMAT);
bool init();
void close();
//void SetColor(int value);
#endif // INIT_H
//init.cpp
//#include "stdafx.h"
#include <stdio.h>
#include "tchar.h"
#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL_image.h>
#include "main.h"
#include "init.h"
#include "load.h"
//#include <conio.h>
//#include <Windows.h>
#include <string>
#include <SDL2/SDL_ttf.h>
#include <cmath>
bool init()
{
bool boot = 1;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
//SetColor(4);
printf("SDL failed to initialize \n");
//SetColor(7);
boot = 0;
}
else {
printf("SDL initialized!\n");
window = SDL_CreateWindow("Light Development Project", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (window == NULL)
{
//SetColor(4);
printf("SDL failed to create the window \n");
//SetColor(7);
boot = 0;
}
else {
printf("Window created!\n");
//screenSurface = SDL_GetWindowSurface(window);
printf("Screen surface created!\n");
}
printf("Initializing SDL_image...\n");
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
//SetColor(4);
printf("Failed to initialize SDL_image\n");
//SetColor(7);
boot = 0;
}
else {
printf("SDL_image initialized!\n");
}
printf("Initializing TTF...\n");
if (TTF_Init() == -1)
{
//SetColor(4);
printf("Failed to initialize TTF\n");
//SetColor(7);
boot = 0;
}
else {
printf("TTF initialized!\n");
}
printf("Creating renderer...\n");
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
//SetColor(4);
printf("Failed to create renderer. Error: %s\n", SDL_GetError());
//SetColor(7);
boot = 0;
} else
printf("Renderer created!\n");
printf("Setting render draw color...\n");
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
printf("Render draw color set!\n");
}
//printf("Done!\n");
return boot;
}
void close()
{
printf("\nShutting down...\nFreeing SDL surfaces...\nDetroying textures and renderers...\n");
SDL_DestroyTexture(texture);
texture = NULL;
SDL_DestroyRenderer(renderer);
renderer = NULL;
printf("SDL surfaces, textures, and renderers freed from memory!\nDestroying SDL window...\n");
SDL_DestroyWindow(window);
//TTF_CloseFont(font);
//font = NULL;
window = NULL;
printf("SDL window detroyed!\nQuitting SDL subsystems...\n");
IMG_Quit();
SDL_Quit();
//TTF_Quit();
printf("All SDL subsystems shutdown!\n");
}
/*void SetColor(int value) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), value);
/*
1: Blue
2: Green
3: Cyan
4: Red
5: Purple
6: Yellow (Dark)
7: Default white
8: Gray/Grey
9: Bright blue
10: Brigth green
11: Bright cyan
12: Bright red
13: Pink/Magenta
14: Yellow
15: Bright white
}
*/
//load.h
//#pragma once
#ifndef LOAD_H
#define LOAD_H
enum KeyPressSurfacese
{
KEY_PRESS_SURFACE_DEFUALT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
class cTexture {
public:
cTexture();
~cTexture();
bool loadFromFile(std::string path);
void free();
void render(int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);
void loadFromRenderedText(std::string text, SDL_Color color);
void setColor(Uint8 red, Uint8 green, Uint8 blue);
void setBlendMode(SDL_BlendMode blending);
void setAlpha(Uint8 alpha);
int getWidth();
int getHeight();
private:
SDL_Texture * hTexture;
int hWidth;
int hHeight;
};
static cTexture keyPresses[KEY_PRESS_SURFACE_TOTAL];
static cTexture sprite;
static cTexture text;
static SDL_Texture* loadTexture(std::string path);
static SDL_Rect spriteClips[4];
//TTF_Font* font = NULL;
static SDL_Color textColor = { 0, 0, 0 };
void loadAssets();
#endif //LOAD_H
//load.cpp
//#include "stdafx.h"
#include <stdio.h>
#include "tchar.h"
#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL_image.h>
#include "main.h"
#include "init.h"
#include "load.h"
//#include <conio.h>
//#include <Windows.h>
#include <string>
#include <SDL2/SDL_ttf.h>
#include <cmath>
cTexture::cTexture()
{
//printf("Constructing texture wrapper class...\n");
hTexture = NULL;
hWidth = 0;
hHeight = 0;
}
cTexture::~cTexture()
{
//printf("Destroying texture wrapper class...\n");
free();
}
void cTexture::free()
{
//if (hTexture != NULL)
SDL_DestroyTexture(hTexture);
hTexture = NULL;
hWidth = 0;
hHeight = 0;
}
bool cTexture::loadFromFile(std::string path)
{
free();
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if(newTexture == NULL)
//printf("fail. error: %s\n", SDL_GetError());
hWidth = loadedSurface->w;
hHeight = loadedSurface->h;
SDL_FreeSurface(loadedSurface);
hTexture = newTexture;
return hTexture != NULL;
}
void cTexture::render(int x, int y, SDL_Rect * clip, double angle, SDL_Point * center, SDL_RendererFlip flip)
{
//SDL_Rect renderQuad = { x, y, hWidth, hHeight };
SDL_Rect renderQuad = { x, y, 480, 480 };
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopyEx(renderer, hTexture, clip, &renderQuad, angle, center, flip);
}
/*
void cTexture::loadFromRenderedText(std::string text, SDL_Color color)
{
free();
SDL_Surface* textSurface = TTF_RenderText_Solid(font, text.c_str(), textColor);
hTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
hWidth = textSurface->w;
hHeight = textSurface->h;
SDL_FreeSurface(textSurface);
}
*/
void cTexture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{
SDL_SetTextureColorMod(hTexture, red, green, blue);
}
void cTexture::setBlendMode(SDL_BlendMode blending)
{
SDL_SetTextureBlendMode(hTexture, blending);
}
void cTexture::setAlpha(Uint8 alpha)
{
SDL_SetTextureAlphaMod(hTexture, alpha);
}
int cTexture::getWidth() { return hWidth; }
int cTexture::getHeight() { return hHeight; }
void loadAssets()
{
printf("Loading image assets in loadAssets()...\n");
if (!keyPresses[KEY_PRESS_SURFACE_DEFUALT].loadFromFile("carrot.png"))
{
//SetColor(4);
printf("Failed to load image. Error: %s\n", SDL_GetError());
//SetColor(7);
} else
printf("Image loaded\n");
if(!keyPresses[KEY_PRESS_SURFACE_UP].loadFromFile("smIamLU.jpg"))
{
//SetColor(4);
printf("Failed to load image\n");
//SetColor(7);
}
else {
//keyPresses[KEY_PRESS_SURFACE_UP].setBlendMode(SDL_BLENDMODE_BLEND);
printf("Image loaded\n");
}
if(!keyPresses[KEY_PRESS_SURFACE_DOWN].loadFromFile("down.bmp"))
{
//SetColor(4);
printf("Failed to load image\n");
//SetColor(7);
} else
printf("Image loaded\n");
if(!keyPresses[KEY_PRESS_SURFACE_LEFT].loadFromFile("left.bmp"))
{
//SetColor(4);
printf("Failed to load image\n");
//SetColor(7);
}else
printf("Image loaded\n");
if(!keyPresses[KEY_PRESS_SURFACE_RIGHT].loadFromFile("right.bmp"))
{
//SetColor(4);
printf("Failed to load image\n");
//SetColor(7);
}else
printf("Image loaded\n");
if (!sprite.loadFromFile("foo.png"))
{
//SetColor(4);
printf("Failed to load image\n");
//SetColor(7);
}else
printf("Image loaded\n");
spriteClips[0].x = 0;
spriteClips[0].y = 0;
spriteClips[0].w = 64;
spriteClips[0].h = 205;
spriteClips[1].x = 64;
spriteClips[1].y = 0;
spriteClips[1].w = 64;
spriteClips[1].h = 205;
spriteClips[2].x = 128;
spriteClips[2].y = 0;
spriteClips[2].w = 64;
spriteClips[2].h = 205;
spriteClips[3].x = 192;
spriteClips[3].y = 0;
spriteClips[3].w = 64;
spriteClips[3].h = 205;
//font = TTF_OpenFont("Ubuntu-L.ttf", 28);
//text.loadFromRenderedText("text", textColor);
printf("Done!\n");
}
With the renderer I've tried making it with differnt flags such as SDL_RENDERER_ACCELERATED or SDL_RENDERER_SOFTWARE but neither work. The error is specifically coming from the load.cpp file
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
This is the line that fails. The error generated by SDL reads "Invalid texture". The renderer is declared correctly but I can't get it to work.
I'm so confused why it isn't working because it used to work fine. I abandoned the project for a few months and it was working when I stopped, came back, and now it doesn't work. Not to mention that I had a bunch of more problems that I already worked out, but this one I can't.
I figured it out and it's the strangest fix I've ever done. I added an error check for the renderer in the load function and it started working again. If I remove the error check if statement it goes back to not working. Seems like a strange bug but I'm glad it's working again.
I've been using SDL for a little awhile and following Lazy Foo's tutorials. I have no problem loading images, animating sprites etc... but when I attempt to separate functions into different header files I'm unable to load an image.
I've looked about for on the internet for a few days now but haven't found an answer.
Sprite.cpp
#include "Sprite.h"
#include "Globals.h"
using namespace Globals;
Graphics sprite;
Sprite::Sprite(){}
bool Sprite::load()
{
bool success = true;
if(!sprite.loadImage("C:/SDL_Project/bin/Debug/PNG_transparency_demonstration_1.png"))
{
printf("Failed to load image!\n");
success = false;
}
return success;
}
void Sprite::renderSprite()
{
SDL_Rect* curSprite = &spriteClips[frames];
sprite.renderImage((SCREEN_WIDTH - curSprite->w) / 2, (SCREEN_HEIGHT - curSprite->h) / 2, curSprite);
}
Graphics.cpp
#include "Graphics.h"
#include "Globals.h"
using namespace Globals;
Graphics::Graphics()
{
hei = 0;
wid = 0;
texture = NULL;
win = SDL_CreateWindow("Foo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(win && ren == NULL)
{
printf("Failed to create Window & Renderer!\n");
}
else
{
int imgFlags = IMG_INIT_PNG;
if( !(IMG_Init(imgFlags) &imgFlags))
{
printf("Image flags not initiated!\n");
}
}
}
Graphics::~Graphics(){}
SDL_Texture* Graphics::loadImage(std::string file)
{
SDL_Texture* newTxtr = NULL;
image = IMG_Load(file.c_str());
if(image == NULL)
{
printf("Unable to load image\n");
}
else
{
newTxtr = SDL_CreateTextureFromSurface(ren, image);
if(newTxtr == NULL)
{
printf("Unable to create texture!\n");
}
else
{
wid = image->w;
hei = image->h;
}
SDL_FreeSurface(image);
}
texture = newTxtr;
return texture;
}
void Graphics::free()
{
if(texture != NULL)
{
texture = NULL;
wid = 0;
hei = 0;
}
}
void Graphics::renderImage(int x, int y, SDL_Rect* clip)
{
SDL_Rect dstClip = {x, y, wid, hei};
if(clip != NULL)
{
dstClip.w = clip->w;
dstClip.h = clip->h;
}
SDL_RenderCopy(ren, texture, clip, &dstClip);
}
void Graphics::render()
{
SDL_RenderPresent(ren);
SDL_UpdateWindowSurface(win);
}
void Graphics::clearScreen()
{
SDL_RenderClear(ren);
}
SDL_Renderer* Graphics::getRenderer()
{
return ren;
}
void Graphics::closeGraphics()
{
IMG_Quit();
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
}
GameLoop.cpp
#include "GameLoop.h"
#include "Graphics.h"
#include "Sprite.h"
GameLoop::GameLoop()
{
if(SDL_Init(SDL_INIT_EVERYTHING)< 0)
{
printf("Failed to init\n");
}
else
{
this->loop();
}
}
GameLoop::~GameLoop(){}
void GameLoop::loop()
{
Graphics graphics;
SDL_Event e;
bool quit = false;
while(!quit)
{
while(SDL_PollEvent(&e)!= 0)
{
if(e.type == SDL_QUIT)
{
quit = true;
}
}
this->renderGraphics(graphics);
}
this->close(graphics);
}
void GameLoop::loadSprite()
{
Sprite sp;
sp.renderSprite();
}
void GameLoop::renderGraphics(Graphics& graphics)
{
SDL_SetRenderDrawColor(graphics.ren, 0x00, 0x00, 0x00, 0x00);
graphics.clearScreen();
graphics.render();
}
void GameLoop::close(Graphics& graphics)
{
SDL_Quit();
graphics.closeGraphics();
}
I've included all but one of the .cpp files.
Thanks.
I'm trying to organize my code a bit more, so I tried putting the SDL2 code used for displaying windows/images into a class. The window opens, the code runs successfully, and the image seems to be loading fine, -but the image will not appear when the code is arranged in a class like this. Why is this happening?
main.cpp:
#include <SDL.h>
#include "display.h"
SDL_Event event;
SDL_Surface* image = nullptr;
int main(int argc, char* args[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
Display display;
display.loadImage("image.bmp");
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
quit = true;
}
display.applySurface(0, 0, image, display.windowSurface);
display.update();
}
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
display.h:
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
class Display
{
public:
SDL_Window* window;
SDL_Surface* windowSurface;
Display();
SDL_Surface* loadImage(std::string fileName);
void applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = nullptr);
void update();
~Display();
private:
const int WINDOW_WIDTH = 612;
const int WINDOW_HEIGHT = 632;
const int SCREEN_BPP = 2;
};
display.cpp:
#pragma once
#include "display.h"
Display::Display()
{
window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
std::cout << "Error: SDL_CreateWindow failed." << std::endl;
windowSurface = SDL_GetWindowSurface(window);
if (windowSurface == NULL)
std::cout << "Error: Window surface is null." << std::endl;
}
SDL_Surface* Display::loadImage(std::string fileName)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(fileName.c_str());
if (loadedImage == NULL)
std::cout << "Loaded image = NULL" << std::endl;
if (loadedImage != NULL)
{
optimizedImage = SDL_ConvertSurface(loadedImage, windowSurface->format, 0);
SDL_FreeSurface(loadedImage);
if (optimizedImage == NULL)
std::cout << "Optimized image = NULL" << std::endl;
if (optimizedImage != NULL)
SDL_SetColorKey(optimizedImage, SDL_TRUE, SDL_MapRGB(optimizedImage->format, 255, 255, 255));
}
return optimizedImage;
}
void Display::applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset);
}
void Display::update()
{
if (SDL_UpdateWindowSurface(window) == -1)
std::cout << "Error: SDL_UpdateWindowSurface() failed." << std::endl;
}
Display::~Display()
{
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
}
In your main.cpp your image is empty because you didn't use the return image :
image = display.loadImage("image.bmp");
Today I started a C++/SDL2 Snake clone, and I've been looking for ways to make my code neater, -particularly with classes. I tried to put all the SDL code used for the window/display in a class. When I run the code, the window closes instantly. From the error tests I set up in display.cpp, it also tells me through the console that SDL_UpdateWindowSurface() (in display.update()) is always returning -1. Why does this happen when I rearrange my code like this?
With this code I load an image in main() and display it through my class' function applySurface(). The idea is to have classes/objects for the game's grid/board, the snake, etc., -each calling applySurface() for their own images. Feel free to tell me if this is a bad idea altogether.
main.cpp:
#include <SDL.h>
#include "display.h"
SDL_Event event;
SDL_Surface* image = nullptr;
int main(int argc, char* args[])
{
Display display;
display.loadImage("image.bmp");
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
quit = true;
}
display.applySurface(0, 0, image, display.windowSurface);
display.update();
}
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}
display.h:
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
class Display
{
public:
SDL_Window* window;
SDL_Surface* windowSurface;
Display();
SDL_Surface *loadImage(std::string fileName);
void applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = nullptr);
void update();
~Display();
private:
const int WINDOW_WIDTH = 612;
const int WINDOW_HEIGHT = 632;
const int SCREEN_BPP = 2;
};
display.cpp:
#pragma once
#include "display.h"
Display::Display()
{
window = SDL_CreateWindow("Snake", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
std::cout << "Error: SDL_CreateWindow failed." << std::endl;
windowSurface = SDL_GetWindowSurface(window);
}
SDL_Surface* Display::loadImage(std::string fileName)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP(fileName.c_str());
if (loadedImage != NULL)
{
optimizedImage = SDL_ConvertSurface(loadedImage, windowSurface->format, 0);
SDL_FreeSurface(loadedImage);
if (optimizedImage != NULL)
SDL_SetColorKey(optimizedImage, SDL_TRUE, SDL_MapRGB(optimizedImage->format, 255, 255, 255));
}
return optimizedImage;
}
void Display::applySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset);
}
void Display::update()
{
if (SDL_UpdateWindowSurface(window) == -1)
std::cout << "Error: SDL_UpdateWindowSurface() failed." << std::endl;
}
Display::~Display()
{
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
}
This is a valid use of classes to structure your code. SDL_Init needs to come before any other SDL functions, which means you're best off moving SDL_Init to the top of main or adding it to the display constructor. If you add it to the beginning of the display constructor this means that you can only have one display class object running at a time, which would likely be fine in this case.
I have a problem creating this second class in my game, this is a class for player but when I call the texturemanager to draw the play, it's not working, but when i directly call the texturemanager to draw the player it was working. this is my class.
Game.h
#pragma once
#include <SDL/SDL.h>
#include "TextureManager.h"
#include "Player.h"
class Game
{
public:
Game(void);
~Game(void);
void startGame();
void init();
void gameLoop();
void eventHandler();
void render();
void exitGame();
private:
bool _isRunning;
SDL_Window* _window;
SDL_Renderer* _renderer;
SDL_Rect _spriteClips[2];
TextureManager _textureManager;
Player _player;
};
Game.cpp
#include "Game.h"
#include "Error.h"
Game::Game(void)
{
_window = nullptr;
_isRunning = true;
}
Game::~Game(void)
{
}
void Game::startGame()
{
init();
gameLoop();
}
void Game::init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
_window = SDL_CreateWindow("Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
640, 480, SDL_WINDOW_SHOWN);
if (_window != nullptr)
{
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(_renderer!= nullptr)
{
SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255);
}
else
fatalError("Failed to create renderer");
}
else
fatalError("Failed to create window!");
}
else
fatalError("Failed to initialize SDL!");
// temp image load
_textureManager.loadFromFile("assets/tiles.png", _renderer);
_spriteClips[0].x = 0;
_spriteClips[0].y = 160;
_spriteClips[0].w = 80;
_spriteClips[0].h = 80;
_spriteClips[1].x = 0;
_spriteClips[1].y = 80;
_spriteClips[1].w = 80;
_spriteClips[1].h = 80;
}
void Game::gameLoop()
{
while (_isRunning != false)
{
eventHandler();
render();
}
exitGame();
}
void Game::eventHandler()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt))
{
switch (evnt.type)
{
case SDL_QUIT:
_isRunning = false;
break;
}
}
}
void Game::render()
{
SDL_RenderClear(_renderer);
_textureManager.draw(0, 0, &_spriteClips[0], _renderer);
_player.draw(200, 200, &_spriteClips[1], _renderer);
// when i used this, it is working
//_textureManager.draw(200, 200, &_spriteClips[1], _renderer);
SDL_RenderPresent(_renderer);
}
void Game::exitGame()
{
SDL_DestroyRenderer(_renderer);
SDL_DestroyWindow(_window);
_window = nullptr;
_renderer = nullptr;
SDL_Quit();
}
TextureManager.h
#pragma once
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
class TextureManager
{
public:
TextureManager(void);
~TextureManager(void);
bool loadFromFile(std::string filePath, SDL_Renderer* renderer);
void free();
void draw(int x, int y, SDL_Rect* clip, SDL_Renderer* renderer);
int getWidth() { return _width; }
int getHeight() { return _height; }
private:
SDL_Texture* _texture;
int _width;
int _height;
};
TextureManager.cpp
#include "TextureManager.h"
#include "Error.h"
TextureManager::TextureManager(void)
{
_texture = nullptr;
_width = 0;
_height = 0;
}
TextureManager::~TextureManager(void)
{
free();
}
bool TextureManager::loadFromFile(std::string filePath , SDL_Renderer* renderer)
{
free();
SDL_Texture* newTexture = nullptr;
SDL_Surface* loadedSurface = IMG_Load(filePath.c_str());
if(loadedSurface != nullptr)
{
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if(newTexture != nullptr)
{
_width = loadedSurface->w;
_height = loadedSurface->h;
}
else
fatalError("unable to create texture from surface!");
SDL_FreeSurface(loadedSurface);
}
else
fatalError("unable to load image path " + filePath);
_texture = newTexture;
return _texture != nullptr;
}
void TextureManager::draw(int x, int y, SDL_Rect* clip, SDL_Renderer* renderer)
{
SDL_Rect renderQuad = { x, y, _width, _height };
if (clip != nullptr)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopy(renderer, _texture, clip, &renderQuad);
}
void TextureManager::free()
{
if (_texture != nullptr)
{
SDL_DestroyTexture(_texture);
_texture = nullptr;
_width = 0;
_height = 0;
}
}
Player.h
#pragma once
#include "TextureManager.h"
class Player
{
public:
Player(void);
~Player(void);
void draw(int x, int y, SDL_Rect* clip, SDL_Renderer* renderer);
private:
TextureManager _textureManager;
};
Player.cpp
#include "Player.h"
Player::Player(void)
{
}
Player::~Player(void)
{
}
void Player::draw(int x, int y, SDL_Rect* clip, SDL_Renderer* renderer)
{
_textureManager.draw(x, y, clip, renderer);
}
That's because Game and Player have two different texture managers. The game initializes its texture manager, that's why when you use it directly it works. The Player never initializes its manager, that's why nothing is drawn when you use it.
To fix it, you might want to only have one texture manager, the one that is in the Game class, and have Player just store a pointer or a reference to it. Here's one way to fix it:
Change the type of the _textureManager in the Player class (but not in the Game class) to be a reference:
TextureManager& _textureManager;
Make Player class accept a TextureManager reference in its constructor, and use it to initialize the private member:
Player::Player(TextureManager& textureManager)
: _textureManager(textureManager)
{
}
Make the Game object properly initialize the player in its constructor:
Game::Game(void)
: _player(_textureManager)
{
...
With these changes your code should now work.