#include #include /* FINE LSP!! I'll play your games!!!! */ #include /* ~HJAPPY?!?!?! */ #include #include #include #include #include "bettola.h" /* Dacav's resolution ;) */ const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; Bettola::Bettola(void) : _is_running(false), _window(nullptr), _gl_context(nullptr), _vao(0), _vbo(0) {} Bettola::~Bettola(void) { if(_gl_context) { SDL_GL_DestroyContext(_gl_context); } if(_window) { SDL_DestroyWindow(_window); } SDL_Quit(); glDeleteVertexArrays(1, &_vao); glDeleteBuffers(1, &_vbo); } int Bettola::run(void) { if(!init_sdl()) return -1; if(!create_window()) return -1; if(!create_gl_context()) return -1; if(!init_glew()) return -1; if(!_shader.load_from_files("assets/shaders/simple.vert", "assets/shaders/simple.frag")) { return -1; } setup_triangle(); glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT); _is_running = true; Uint64 perf_freq = SDL_GetPerformanceFrequency(); Uint64 last_count = SDL_GetPerformanceCounter(); double delta_time = 0; while(_is_running) { Uint64 current_counter = SDL_GetPerformanceCounter(); delta_time = (double)(current_counter-last_count) / (double)perf_freq; last_count = current_counter; process_events(); update(delta_time); render(); } return 0; } void Bettola::process_events(void) { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_EVENT_QUIT) { _is_running = false; } } } void Bettola::update(double dt) { static char window_title[256]; static double time_since_title_update = 0.0; time_since_title_update += dt; if(time_since_title_update > 0.25) { snprintf(window_title, 256, "Bettola - FPS %.2f", 1.0/dt); SDL_SetWindowTitle(_window, window_title); time_since_title_update = 0.0; } } void Bettola::render(void) { glClearColor(0.1f, 0.1f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); _shader.use(); glBindVertexArray(_vao); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); SDL_GL_SwapWindow(_window); } bool Bettola::init_sdl(void) { if(!SDL_Init(SDL_INIT_VIDEO)) { fprintf(stderr, "Failed to iniit SDL! SDL ERROR: %s\n", SDL_GetError()); return false; } return true; } bool Bettola::init_glew(void) { glewExperimental = GL_TRUE; GLenum glew_error = glewInit(); if(glew_error != GLEW_OK) { fprintf(stderr, "Failed to init GLEW! %s\n", glewGetErrorString(glew_error)); return false; } return true; } bool Bettola::create_window(void) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); /* Don't you dare f.ckin fail to open!!!!! */ _window = SDL_CreateWindow("Bettola Makes No Sense!", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL); if(!_window) { fprintf(stderr, "Failed to create window! SDL_ERROR: %s\n", SDL_GetError()); return false; } return true; } bool Bettola::create_gl_context(void) { _gl_context = SDL_GL_CreateContext(_window); if(!_gl_context) { fprintf(stderr, "Failed to create OpenGL context! SDL_ERROR: %s\n", SDL_GetError()); return false; } return true; } void Bettola::setup_triangle(void) { float vertices[] = { /* Should be a triangle??!?? */ -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.0f, 0.5f, 0.0f, }; glGenVertexArrays(1, &_vao); glGenBuffers(1, &_vbo); glBindVertexArray(_vao); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }