diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-10-18 23:25:46 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-10-18 23:25:46 +0300 |
commit | 145775818375bafcf191a7b4986e57a00b41ac68 (patch) | |
tree | b539525328515401be6bc2a6c9fb17841b8a16d1 /src/display.c | |
parent | 7e64d0c78e8a21f2ede7eab1303758391001c0dc (diff) |
use glfw and some minor refactoring
Diffstat (limited to 'src/display.c')
-rw-r--r-- | src/display.c | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/src/display.c b/src/display.c index 6d4c55a..a09cc46 100644 --- a/src/display.c +++ b/src/display.c @@ -1,15 +1,24 @@ #include <stdio.h> -#include <unistd.h> #include <string.h> -#include <GL/glut.h> -#include "display.h" +#include <GLFW/glfw3.h> #include "typedef.h" #define MAX(x, y) (((x) > (y)) ? (x) : (y)) -extern unsigned char *cam_data; +static void error_callback(int error, const char* description) +{ + err("display: %s\n", description); +} + +static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(window, GLFW_TRUE); +} + +extern unsigned char cam_data[]; -unsigned char WIDTH = 1; +unsigned char WIDTH = 1; unsigned char HEIGHT = 1; GLubyte pixels[BUF_CAP] = {0}; @@ -22,30 +31,52 @@ void pixel(int x, int y, char *buf) pixels[p+2] = buf[b+2]; } -void render() +int display(void) { - WIDTH = MAX(cam_data[0], 1); - HEIGHT = MAX(cam_data[1], 1); + GLFWwindow* window; + glfwSetErrorCallback(error_callback); - for(int i = 0; i < HEIGHT; i++) - for(int j = 0; j < WIDTH; j++) - pixel(j, i, &(cam_data[2])); + if (!glfwInit()) { + return 1; + } - glClear(GL_COLOR_BUFFER_BIT); - glDrawPixels(WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); - glutSwapBuffers(); - glutReshapeWindow(WIDTH, HEIGHT); - glutPostRedisplay(); -} + window = glfwCreateWindow(WIDTH, HEIGHT, "Client", NULL, NULL); + if (!window) { + glfwTerminate(); + return 1; + } -void display(int *argc, char **argv) -{ - glutInit(argc, argv); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); - glutInitWindowSize(69, 69); - glutCreateWindow(argv[0]); + glfwSetKeyCallback(window, key_callback); + glfwMakeContextCurrent(window); + glfwSwapInterval(1); + + + unsigned char *copy_cam_data; + while (!glfwWindowShouldClose(window)) + { + // memcpy(copy_cam_data, cam_data, BUF_CAP); + copy_cam_data = cam_data; + + WIDTH = MAX(copy_cam_data[0], 1); + HEIGHT = MAX(copy_cam_data[1], 1); + glfwSetWindowSize(window, WIDTH, HEIGHT); + + for(int i = 0; i < HEIGHT; i++) + for(int j = 0; j < WIDTH; j++) + pixel(j, i, &(copy_cam_data[2])); + + glClear(GL_COLOR_BUFFER_BIT); + glDrawPixels(WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); - glutDisplayFunc(render); - glutMainLoop(); + return 0; } |