diff --git a/src/gui/GUI.cc b/src/gui/GUI.cc new file mode 100644 index 0000000..5149807 --- /dev/null +++ b/src/gui/GUI.cc @@ -0,0 +1,69 @@ +#include "GUI.h" +#include "gl3w.h" +#include + +#define WIDTH 500 +#define HEIGHT 500 + +namespace jes +{ + GUI::GUI() + { + if (SDL_Init(SDL_INIT_VIDEO)) + { + std::cerr << "Failed to initialize SDL!" << std::endl; + return; + } + + atexit(SDL_Quit); + + m_window = SDL_CreateWindow("jes", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WIDTH, + HEIGHT, + SDL_WINDOW_OPENGL); + if (!m_window) + { + std::cerr << "Failed to create window!" << std::endl; + SDL_Quit(); + return; + } + + (void)SDL_GL_CreateContext(m_window); + + if (gl3wInit() != 0) + { + std::cerr << "Failed to gl3wInit()" << std::endl; + SDL_Quit(); + return; + } + + if (!gl3wIsSupported(3, 0)) + { + std::cerr << "OpenGL 3.0 is not supported!" << std::endl; + SDL_Quit(); + return; + } + + glClearColor (0.0, 0.0, 0.0, 0.0); + } + + void GUI::run() + { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + SDL_GL_SwapWindow(m_window); + + SDL_Event event; + while (SDL_WaitEvent(&event)) + { + if (event.type == SDL_QUIT) + break; + else if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_ESCAPE) + break; + } + } + } +} diff --git a/src/gui/GUI.h b/src/gui/GUI.h new file mode 100644 index 0000000..f7a3638 --- /dev/null +++ b/src/gui/GUI.h @@ -0,0 +1,21 @@ +#ifndef JES_GUI_H +#define JES_GUI_H + +#include "jes/Ref.h" +#include + +namespace jes +{ + class GUI + { + public: + GUI(); + void run(); + protected: + SDL_Window * m_window; + }; + + typedef Ref GUIRef; +} + +#endif diff --git a/src/gui/main.cc b/src/gui/main.cc index b5f2c45..f58d17a 100644 --- a/src/gui/main.cc +++ b/src/gui/main.cc @@ -1,75 +1,10 @@ -#include "gl3w.h" -#include #include +#include "GUI.h" using namespace std; -#define WIDTH 500 -#define HEIGHT 500 - -void init(void) -{ - glClearColor (0.0, 0.0, 0.0, 0.0); -} - -void display(SDL_Window * window) -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - SDL_GL_SwapWindow(window); -} - int main(int argc, char *argv[]) { - if (SDL_Init(SDL_INIT_VIDEO)) - { - cerr << "Failed to initialize SDL!" << endl; - return 1; - } - - atexit(SDL_Quit); - - SDL_Window * window = SDL_CreateWindow(argv[0], - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - WIDTH, - HEIGHT, - SDL_WINDOW_OPENGL); - if (!window) - { - cerr << "Failed to create window!" << endl; - SDL_Quit(); - return 2; - } - - (void)SDL_GL_CreateContext(window); - - if (gl3wInit() != 0) - { - cerr << "Failed to gl3wInit()" << endl; - SDL_Quit(); - return 2; - } - - if (!gl3wIsSupported(3, 0)) - { - cerr << "OpenGL 3.0 is not supported!" << endl; - SDL_Quit(); - return 2; - } - - init(); - display(window); - SDL_Event event; - while (SDL_WaitEvent(&event)) - { - if (event.type == SDL_QUIT) - break; - else if (event.type == SDL_KEYDOWN) - { - if (event.key.keysym.sym == SDLK_ESCAPE) - break; - if (event.key.keysym.sym == SDLK_RETURN) - display(window); - } - } + jes::GUIRef gui = new jes::GUI(); + gui->run(); }