在我的代码中,我有这些东西可以检查我的代码是否在创建SDL窗口时出错,初始化GLEW等等。他们会离开,我的程序仍然会工作,在the guide I'm following,没有任何错误发生。有人能告诉我我做错了吗?
此外,glClearColor()
似乎无效。我认为它与上述错误有关。
我的代码:
// g++ `pkg-config --cflags glew sdl2` main.cpp `pkg-config --libs glew sdl2`
#include <GL/glew.h>
#include <SDL.h>
#include <iostream>
#include <string>
enum class GameState { PLAY, EXIT };
class MainGame
{
public:
MainGame();
~MainGame();
void run();
private:
void initSystems();
void gameLoop();
void processInput();
void drawGame();
SDL_Window* _window;
int _screenWidth;
int _screenHeight;
int _errorCount;
GameState _gameState;
};
//Function To Display A Error Message When Something Doesnt Work As Inteded/Properly.
void fatalError(std::string errorMsg)
{
std::cout << errorMsg << std::endl;
}
//When Called, Inits Most Of The Important Vars, Sets Game State And Does An Error Check
MainGame::MainGame()
{
_errorCount = 0;
_window = nullptr;
_screenWidth = 1024;
_screenHeight = 768;
_gameState = GameState::PLAY;
if (_window == nullptr) {
fatalError("SDL Window Could Not Be Created.");
_errorCount += 1;
}
SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr) {
fatalError("SDL_GL Context Could Not Be Created.");
_errorCount += 1;
}
GLenum error = glewInit();
if (error != GLEW_OK) {
fatalError("Could Not Initialize Glew.");
_errorCount += 1;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
}
MainGame::~MainGame()
{
}
void MainGame::run()
{
initSystems();
gameLoop();
}
// Initializes The SDL Window.
void MainGame::initSystems()
{
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
}
void MainGame::gameLoop()
{
while (_gameState != GameState::EXIT) {
processInput();
drawGame();
}
}
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt) == true) {
switch (evnt.type) {
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << evnt.motion.x << ", " << evnt.motion.y << "\n";
break;
}
}
}
void MainGame::drawGame()
{
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(_window);
}
int main(int argc, char** argv)
{
MainGame mainGame;
mainGame.run();
return 0;
}
答案 0 :(得分:2)
SDL_GL_CreateContext()
。SDL_GL_SetAttribute()
,不会做任何有用的事情。exit()
,但throw
也可以使用。所有在一起:
// g++ `pkg-config --cflags sdl2` main.cpp `pkg-config --libs glew sdl2`
#include <GL/glew.h>
#include <SDL.h>
#include <iostream>
#include <string>
#include <cstdlib>
enum class GameState { PLAY, EXIT };
class MainGame
{
public:
MainGame();
~MainGame();
void run();
private:
void processInput();
void drawGame();
SDL_Window* _window;
SDL_GLContext _context;
int _screenWidth;
int _screenHeight;
int _errorCount;
GameState _gameState;
};
//Function To Display A Error Message When Something Doesnt Work As Inteded/Properly.
void fatalError(std::string errorMsg)
{
std::cout << errorMsg << std::endl;
exit( EXIT_FAILURE );
}
//When Called, Inits Most Of The Important Vars, Sets Game State And Does An Error Check
MainGame::MainGame()
{
_errorCount = 0;
_window = nullptr;
_screenWidth = 1024;
_screenHeight = 768;
_gameState = GameState::PLAY;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
if (_window == nullptr)
{
fatalError("SDL Window Could Not Be Created.");
_errorCount += 1;
}
_context = SDL_GL_CreateContext(_window);
if (_context == nullptr)
{
fatalError("SDL_GL Context Could Not Be Created.");
_errorCount += 1;
}
GLenum error = glewInit();
if (error != GLEW_OK)
{
fatalError("Could Not Initialize Glew.");
_errorCount += 1;
}
}
MainGame::~MainGame()
{
SDL_GL_DeleteContext( _context );
SDL_DestroyWindow( _window );
}
void MainGame::run()
{
while (_gameState != GameState::EXIT)
{
processInput();
drawGame();
}
}
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt) == true)
{
switch (evnt.type)
{
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << evnt.motion.x << ", " << evnt.motion.y << "\n";
break;
}
}
}
void MainGame::drawGame()
{
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(_window);
}
int main(int argc, char** argv)
{
MainGame mainGame;
mainGame.run();
return EXIT_SUCCESS;
}