Skip to content
Snippets Groups Projects
Commit cc111afa authored by BATON Theau's avatar BATON Theau
Browse files

Add Window class

parent 99fcf965
No related branches found
No related tags found
No related merge requests found
assets/textures/Cube_Saumon.png

314 B

assets/textures/Cube_Yellow.png

314 B

assets/textures/Cube_Yellowy.png

1.36 KiB

......@@ -23,8 +23,8 @@ namespace megu {
return *this;
}
GLuint TextureBuffer::get(GLuint x, GLuint y) const {
return this->_data.at((this->_width * y) + x);
void TextureBuffer::flipVerticaly() {
stbi__vertical_flip(this->_data.data(), this->_width, this->_height, TextureBuffer::BytePerPixel(this->_format));
}
void TextureBuffer::load(const GLubyte * data, size_t size) {
......@@ -75,7 +75,28 @@ namespace megu {
this->_data.clear();
}
void TextureBuffer::set(GLuint x, GLuint y, GLubyte v) {
this->_data[(y * this->_width) + x] = v;
int TextureBuffer::BytePerPixel(GLenum format) {
switch (format) {
case GL_ZERO:
return 0;
case GL_RED:
return 1;
case GL_ONE:
return 1;
case GL_RG:
return 2;
case GL_RGB:
return 3;
case GL_RGBA:
return 4;
default:
throw std::runtime_error("Unkwon byter per pixel for " + format);
}
}
}
\ No newline at end of file
......@@ -21,13 +21,13 @@ namespace megu {
inline const GLubyte * data_ptr() const {return this->_data.data();}
inline bool empty() const {return this->_data.empty();}
GLuint get(GLuint, GLuint) const;
void flipVerticaly();
void load(const GLubyte *, size_t);
void load(const std::filesystem::path &, bool = true);
void free();
void set(GLuint, GLuint, GLubyte);
static int BytePerPixel(GLenum);
private:
GLuint _width, _height;
......
......@@ -13,7 +13,7 @@ namespace megu {
}
void Image::load(const TextureBuffer & buffer) {
this->setSize(buffer.width(), buffer.height());
this->setSize(static_cast<float>(buffer.width()), static_cast<float>(buffer.height()));
this->_texture.store(buffer);
}
}
\ No newline at end of file
#include "Window.hpp"
#include <stdexcept>
namespace megu {
Window::Window()
: _pointer(nullptr) {
if(glfwInit() == GLFW_FALSE) {
throw std::runtime_error("Cannot initialize GLFW.");
}
}
uint16_t Window::width() const {
if(this->_pointer != nullptr) {
int width;
glfwGetWindowSize(this->_pointer, &width, NULL);
return width;
}
return 0;
}
uint16_t Window::height() const {
if(this->_pointer != nullptr) {
int height;
glfwGetWindowSize(this->_pointer, NULL, &height);
return height;
}
return 0;
}
bool Window::isOpen() const {
return this->_pointer != nullptr ? !glfwWindowShouldClose(this->_pointer) : false;
}
void Window::resize(uint16_t width, uint16_t height) {
if(this->_pointer != nullptr) {
glfwSetWindowSize(this->_pointer, width, height);
}
}
void Window::open(const std::string & title, uint16_t width, uint16_t height) {
if(this->_pointer == nullptr) {
this->_pointer = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if(this->_pointer== NULL) {
throw std::runtime_error("Cannot open Window.");
}
glfwMakeContextCurrent(this->_pointer);
glfwSwapInterval(0);
if(glewInit()) {
throw std::runtime_error("Cannot initialize GLEW.");
}
glViewport(0, 0, width, height);
}
}
void Window::close() {
if(this->_pointer != nullptr) {
glfwSetWindowShouldClose(this->_pointer, true);
}
}
void Window::pollEvents() {
glfwPollEvents();
}
void Window::swapBuffers() {
if(this->_pointer != nullptr) {
glfwSwapBuffers(this->_pointer);
}
}
}
\ No newline at end of file
#pragma once
#include <string>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
namespace megu {
class Window {
public:
Window();
Window(const Window &) = delete;
~Window() = default;
uint16_t width() const;
uint16_t height() const;
bool isOpen() const;
void resize(uint16_t, uint16_t);
void open(const std::string &, uint16_t, uint16_t);
void close();
void pollEvents();
void swapBuffers();
inline static double Time() {return glfwGetTime();}
private:
GLFWwindow * _pointer;
};
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
#define WINDOW_WIDTH 1200
#define WINDOW_HEIGHT 720
#include <engine/io/Window.hpp>
#include <engine/graphics/back/cameras/View.hpp>
#include <engine/graphics/front/object/Image.hpp>
#include <engine/graphics/front/group/ImageGroup.hpp>
......@@ -13,7 +14,7 @@
int main(int argc, const char * argv[]) {
try {
//? GLFW
/*//? GLFW
if(glfwInit() == GLFW_FALSE) {
std::cerr << "GLFW : GLFW Init Error" << std::endl;
return EXIT_FAILURE;
......@@ -46,44 +47,42 @@ int main(int argc, const char * argv[]) {
if(glewInit()) {
std::cerr << "Failed to initialize GLAD" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Glew Inited" << std::endl;
//? Viewport
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
}*/
//? Window
megu::Window window;
window.open("Window", WINDOW_WIDTH, WINDOW_HEIGHT);
megu::error::opengl_error::check();
std::cout << "Window Inited" << std::endl;
//? Camera
megu::View view(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
std::cout << "View Initied" << std::endl;
//? Image
megu::Image image;
image.load("assets/textures/Test.png");
image.load("assets/textures/Cube_Red.png");
image.setSize(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
std::cout << "Texture" << std::endl;
std::cout << "Image Inited" << std::endl;
//? Group
megu::ImageGroup group;
group.temp_set(image);
std::cout << "Group Inited" << std::endl;
//? Render Loop
glClearColor(0.0f, 0.0f, 0.0f, 0.f);
std::cout << "Render Loop Begin !" << std::endl;
while(!glfwWindowShouldClose(window)) {
while(window.isOpen()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
window.pollEvents();
group.draw(view);
image.setPosition(sin(glfwGetTime()) * WINDOW_WIDTH/2, cos(glfwGetTime()) * WINDOW_HEIGHT/2);
image.setPosition(static_cast<float>(sin(glfwGetTime()) * WINDOW_WIDTH/2), static_cast<float>(cos(glfwGetTime()) * WINDOW_HEIGHT/2));
glfwSwapBuffers(window);
window.swapBuffers();
}
std::cout << "Render Loop End !" << std::endl;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment