//This is the code for the MainGame.h part of the class
#pragma once
#include <SDL/SDL.h>
#include <GL/glew.h>
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;
GameState _gameState;
};
//
//This is the code for the MainGame.cpp part of the class
#include "MainGame.h"
#include <iostream>
MainGame::MainGame() {
_window = nullptr;
_screenWidth = 824;
_screenHeight = 686;
_gameState = GameState::PLAY;
}
MainGame::~MainGame() {
}
void MainGame::run() {
initSystems();
gameLoop();
}
void MainGame::initSystems() {
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
}
void MainGame::gameLoop() {
while (_gameState != GameState::EXIT) {
processInput();
drawGame();
}
}
void MainGame::processInput() {
SDL_Event evnt;
while (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << evnt.motion.x << " " << evnt.motion.y << std::endl;
break;
}
}
}
void MainGame::drawGame() {
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(_window);
}
//
//This is the main.cpp code
#include "MainGame.h"
#include <GL/glew.h>
#include <iostream>
static unsigned int CompileShader(unsigned int type, const std::string& source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
return id;
}
static int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) {
GLuint program = glCreateProgram();
GLuint vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
int main(int argc, char** argv) {
MainGame mainGame;
mainGame.run();
//glewInit();
//glewExperimental = GL_TRUE;
float pos[6] = {
-0.5f,-0.5f,
0.0f, 0.5f,
0.5f,-0.5f};
GLuint voID;
glGenBuffers(1, &voID); //This is where the exception is thrown
glBindBuffer(GL_ARRAY_BUFFER, voID);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), pos, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
std::string vertexShader =
"#version 330 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main()\n"
"{\n"
"gl_Position = position; \n"
"}\n";
std::string fragmentShader =
"#version 330 core\n"
"\n"
"layout(location = 0) out vec4 color;"
"\n"
"void main()\n"
"{\n"
"color = vec4(1.0, 0.0, 0.0, 1.0); \n"
"}\n";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
return 0;
}