git.haldean.org ana / 8be87ea src / ana.cpp
8be87ea

Tree @8be87ea (Download .tar.gz)

ana.cpp @8be87earaw · history · blame

#include <cstdlib>
#include <iostream>

#include <GL/glew.h>
#include <SDL2/SDL.h>

#include "node.hpp"
#include "shaders.hpp"
#include "ui.hpp"

int main(void) {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window* window = SDL_CreateWindow("ana",
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 400,
		SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);

	if (window == NULL) {
        std::cerr << "[E] create window: " << SDL_GetError() << std::endl;
		return EXIT_FAILURE;
	}
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);
	if (SDL_GL_CreateContext(window) == NULL) {
        std::cerr << "[E] SDL_GL_CreateContext: " << SDL_GetError() << std::endl;
		return EXIT_FAILURE;
	}

	GLenum glew_status = glewInit();
	if (glew_status != GLEW_OK) {
        std::cerr << "[E] glewInit: " << glewGetErrorString(glew_status) << std::endl;
		return EXIT_FAILURE;
	}

	if (!ana::shaders::init())
		return EXIT_FAILURE;

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    ana::nodeset nodes;
    nodes.nodes.push_back(ana::node{glm::vec2(0, 0), glm::vec2(0.1, 0.2), "hello"});
    nodes.nodes.push_back(ana::node{glm::vec2(-0.5, -0.5), glm::vec2(0.2, 0.2), "hello"});

    ana::ui ui;

    bool run = true;
	while (run) {
		SDL_Event ev;
		while (SDL_PollEvent(&ev)) {
            switch (ev.type) {
            case SDL_QUIT:
                run = false;
                break;

            case SDL_WINDOWEVENT:
                if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
                    glViewport(0, 0, ev.window.data1, ev.window.data2);
                    ui.set_aspect(ev.window.data1, ev.window.data2);
                }
                break;

            case SDL_MOUSEWHEEL:
                if (ev.wheel.y != 0) {
                    ui.view_step(ev.wheel.y);
                }
                break;

            case SDL_MOUSEMOTION:
                if (ev.motion.state & SDL_BUTTON_MMASK) {
                    ui.translate(-ev.motion.xrel, ev.motion.yrel);
                }
                break;
            }
		}
        glClearColor(0.15, 0.12, 0.1, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        ana::shaders::draw(ui, nodes);

        SDL_GL_SwapWindow(window);
	}

    ana::shaders::destroy();
	return EXIT_SUCCESS;
}