我试图制作某种引擎'用C ++ / SDL。所以我遵循了LazyFoo的教程并搜索了其他地方/视频[SDL / C ++]。 一切都很好,直到我在2个类中组织我的代码。它只是Game.h / Game.cpp和main.cpp,但我认为将加载和破坏Surfaces / Textures(Game类)的Game类加载Image的代码部分分开是很好的。
所以我将代码复制/粘贴到另一个类。我复制/粘贴的是两个布尔函数textureBMP和textureIMG一切都保持不变。在我将代码复制/粘贴到另一个类之前,一切都运行正常,所以我不知道是什么造成的。
另一个小问题是,我以正确的方式组织代码的方式是什么?我想在一开始就养成良好的习惯,即使是小项目,比如这个小项目只是为了学习。 提前谢谢!
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;
}
答案 0 :(得分:1)
我做了一些更改,现在它正常工作了。我改变了两件事,第一件是给出黑屏的部分,现在功能看起来像这样:
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))
另一个变化是,在Image.h包含Game.h之前,在main.cpp中我包含Game.h和Image.h,这会产生错误吗? 因为https://en.wikipedia.org/wiki/Include_guard
Obs:我创建了一个答案,因为评论不允许代码