Reputation: 29
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;
}
Upvotes: 1
Views: 373
Reputation: 29
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
Upvotes: 1