gpt4 book ai didi

c++ - SDL/C++(加速)黑屏

转载 作者:行者123 更新时间:2023-11-30 05:19:16 54 4
gpt4 key购买 nike

我正在尝试使用 C++/SDL 制作某种“引擎”。所以我遵循了 LazyFoo 的教程并搜索了其他关于它的地方/视频 [SDL/C++]。一切都很好,直到我将我的代码组织成 2 个类。它只是 Game.h/Game.cpp 和 main.cpp,但我认为将加载图像的代码部分与加载和销毁表面/纹理(游戏类)的游戏类分开会很好。

所以我直接将代码复制/粘贴到另一个类中。我复制/粘贴的是两个 bool 函数 textureBMP 和 textureIMG 一切都保持不变。在我将代码复制/粘贴到另一个类之前,一切正常,所以我不知道是什么造成的。

另一个小问题,我组织代码的方式是否正确?我想从一开始就养成良好的习惯,即使是小项目,比如这个只是为了学习的小项目。提前致谢!

图像.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);
};

图片.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;
}

游戏.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();

};

游戏.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;

}

主要.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

最佳答案

我做了一些修改,现在它可以正常工作了。我改变了两个东西,第一个是黑屏的部分,现在函数看起来像这样:

图像.h

bool textureBMP(char *mediaLocation, <...>, SDL_Renderer *render);
bool textureIMG(char *mediaLocation, <...>, SDL_Renderer *render);

主要.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:我创建了一个答案,因为评论不允许代码

关于c++ - SDL/C++(加速)黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214738/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com