diff --git a/.gitignore b/.gitignore index e9e2286..530b507 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,13 @@ build/** assets/** bin/** .cache/** +src/old/** + +# Bear config +./compile_commands.json + +# Shortcut launchers +./*.sh # Prerequisites *.d diff --git a/run.sh b/run.sh index 34a4645..b49b68c 100755 --- a/run.sh +++ b/run.sh @@ -1 +1 @@ -./bin/wotos +make run ARGS=assets/aa.mkv diff --git a/src/connection/sock.c b/src/connection/sock.c new file mode 100644 index 0000000..3ffc423 --- /dev/null +++ b/src/connection/sock.c @@ -0,0 +1,38 @@ +#include "sock.h" + +struct Socket *socket_create(){ + struct Socket *self = malloc(sizeof(struct Socket)); + return self; +} + +void socket_init(struct Socket* self){ + if ((self->server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){ + perror("Socket failed"); + exit(EXIT_FAILURE); + } + + // Create socket address + // Forcefully attach socket to the port + self->opt = 1; + if (setsockopt(self->server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &(self->opt), sizeof(int))){ + perror("Setsockopt failed"); + exit(EXIT_FAILURE); + } + self->address.sin_family = AF_INET; + self->address.sin_addr.s_addr = INADDR_ANY; + self->address.sin_port = htons(SERVER_PORT); + + // Bind socket to address + if (bind(self->server_fd, (struct sockaddr *)&(self->address), sizeof(self->address)) < 0){ + perror("Bind failed"); + exit(EXIT_FAILURE); + } +} + +void socket_listen(struct Socket *self){ + // listen to incoming connections + if (listen(self->server_fd, QUEUE_LENGTH) < 0){ + perror("listen failed"); + exit(EXIT_FAILURE); + } +} diff --git a/src/connection/sock.h b/src/connection/sock.h new file mode 100644 index 0000000..f32094c --- /dev/null +++ b/src/connection/sock.h @@ -0,0 +1,28 @@ +#ifndef _SOCK_H_ +#define _SOCK_H_ + +#include +#include + +#include +#include +#include +#include +#include + +#define QUEUE_LENGTH 10 +#define RECV_BUFFER_SIZE 2048 +#define SERVER_PORT 10000 + +struct Socket{ + int server_fd; // File descriptor + struct sockaddr_in address; // Socket address + int opt; + int endpoint; +}; + +struct Socket *socket_create(); +void socket_init(struct Socket* socket); +void socket_listen(struct Socket* socket); + +#endif diff --git a/src/gfx/shader.h b/src/gfx/shader.h index 7d45beb..3ea82a9 100644 --- a/src/gfx/shader.h +++ b/src/gfx/shader.h @@ -16,16 +16,7 @@ struct Shader { struct Shader shader_create(char *vs_path, char *fs_path, size_t n, struct VertexAttr attributes[]); void shader_destroy(struct Shader self); + void shader_bind(struct Shader self); -//void shader_uniform_mat4(struct Shader self, char *name, mat4s m); -//void shader_uniform_view_proj(struct Shader self, struct ViewProj view_proj); -//void shader_uniform_texture2D(struct Shader self, char *name, struct Texture texture, GLuint n); -//void shader_uniform_float(struct Shader self, char *name, f32 f); -//void shader_uniform_vec2(struct Shader self, char *name, vec2s v); -//void shader_uniform_vec3(struct Shader self, char *name, vec3s v); -//void shader_uniform_vec4(struct Shader self, char *name, vec4s v); -//void shader_uniform_int(struct Shader self, char *name, int v); -//void shader_uniform_uint(struct Shader self, char *name, unsigned int v); - #endif diff --git a/src/gfx/texture.h b/src/gfx/texture.h index 1389e53..44e0a40 100644 --- a/src/gfx/texture.h +++ b/src/gfx/texture.h @@ -14,5 +14,6 @@ void texture_load_pixels(char *path, u8 **pixels_out, size_t *width_out, size_t struct Texture texture_create_from_pixels(u8 *pixels, size_t width, size_t height); struct Texture texture_create_from_path(char *path); void texture_destroy(struct Texture self); + void texture_bind(struct Texture self); #endif diff --git a/src/gfx/vao.h b/src/gfx/vao.h index 45e7b6f..76f302f 100644 --- a/src/gfx/vao.h +++ b/src/gfx/vao.h @@ -11,6 +11,7 @@ struct VAO { struct VAO vao_create(); void vao_destroy(struct VAO self); + void vao_bind(struct VAO self); void vao_attr( struct VAO self, struct VBO vbo, GLuint index, GLint size, GLenum type, diff --git a/src/gfx/vbo.h b/src/gfx/vbo.h index a443ce9..6b62e23 100644 --- a/src/gfx/vbo.h +++ b/src/gfx/vbo.h @@ -11,11 +11,9 @@ struct VBO { }; struct VBO vbo_create(GLint type, bool dynamic); - void vbo_destroy(struct VBO self); void vbo_bind(struct VBO self); - void vbo_buffer(struct VBO self, void *data, size_t offset, size_t count); #endif diff --git a/src/gfx/window.h b/src/gfx/window.h index eb88ddc..c56f52b 100644 --- a/src/gfx/window.h +++ b/src/gfx/window.h @@ -4,9 +4,9 @@ #include "gfx.h" #include "../util/util.h" #include "./renderer.h" -#include "player.h" +#include "./player.h" -#define WINDOW_NAME "wotos + mpv" +#define WINDOW_NAME "Wotos-mpv" #include // for usleep diff --git a/src/main.c b/src/main.c index 9c5af14..207de05 100644 --- a/src/main.c +++ b/src/main.c @@ -1,99 +1,30 @@ #include "main.h" -#include "gfx/vao.h" -#include "gfx/vbo.h" -#include "glad/glad.h" -static inline void check_error(int); -static inline void _setup(); - -int main(int argc, char const *argv[]){ - if (argc < 2){ return -1;} - _setup(); - - player_loadfile(player, argv[1]); - window_loop(); - return 0; -} +bool SetSocketBlockingEnabled(int fd, bool blocking); void _setup(){ - window = window_create(init, NULL, NULL, update, render); + sock = socket_create(); + socket_init(sock); + socket_listen(sock); + socket_accept(); + //window = window_create(init, destroy, tick, update, render); + //player = player_create(); + //renderer = renderer_create(); - player = player_create(); - player_init(player); - - renderer = renderer_create(); - renderer_init(renderer); + //player_init(player); + //renderer_init(renderer); /* Glyph shenanigans */ //mat4s transformation = glms_ortho(0.0f, (float)(window_width), 0.0f, (float)window_height, -100, 100); } -// TODO Remove these function from here -void processGLFWInput(GLFWwindow *window, mpv_handle *ctx) -{ - glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_FALSE); - if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { - glfwSetWindowShouldClose(window, true); - } - - if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { - const char *c[] = {"show-text", "lol", NULL}; - check_error(mpv_command(ctx, c)); - } +int main(int argc, char const *argv[]){ + if (argc < 2){ return -1;} + _setup(); + //player_loadfile(player, argv[1]); + //window_loop(); + return 0; } -void framebuffer_size_callback(GLFWwindow *window, int width, int height) -{ - // we have to rescale the Texture and renderbuffer storage. - window_height = height; - window_width = width; - glBindTexture(GL_TEXTURE_2D, renderer->screen_textureColorbuffer); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glBindRenderbuffer(GL_RENDERBUFFER, screen_rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, window_width, window_height); -} -static inline void check_error(int status) -{ - if (status < 0) { - printf("mpv API error: %s\n", mpv_error_string(status)); - - //exit(1); - } -} - -void init() { - -} - -void update() { - processGLFWInput(window->handle, player->handle); - - if (wakeup) - { - if ((mpv_render_context_update(player->ctx) & MPV_RENDER_UPDATE_FRAME)) - { - mpv_render_context_render(player->ctx, renderer->params_fbo); - glViewport(0, 0, window->size.x, window->size.y); - } - } -} - -void render(){ - shader_bind(renderer->shaders[SHADER_MPV]); - - vao_bind(renderer->screenVAO); - glBindTexture(GL_TEXTURE_2D, renderer->video_textureColorbuffer); // <-- SCREEN Colorbuffer IS THE TEXTURE - glDrawArrays(GL_TRIANGLES, 0, 6); - - // ----- - if (wakeup) - { - mpv_render_context_report_swap(player->ctx); - wakeup = 0; - } - - const char *text = "I Joe you Joe ;)"; - render_text(renderer, text, strlen(text), 0, 0, 0.001, (float [3]){0.0, 1.0, 0.0}); -} diff --git a/src/main.h b/src/main.h index 111a22c..996afb5 100644 --- a/src/main.h +++ b/src/main.h @@ -1,89 +1,45 @@ -#ifndef __MAIN_H -#define __MAIN_H +#ifndef _MAIN_H_ +#define _MAIN_H_ -//#include -#include "gfx/gfx.h" +#include "window_setup.h" +#include "socket_setup.h" + +#include "connection/sock.h" +#include #include #include -#include "gfx/vao.h" -#include "gfx/vbo.h" -#include "gfx/player.h" -#include "gfx/renderer.h" -#include "gfx/shader.h" -#include "gfx/bitmap.h" -#include "gfx/window.h" - - -//#include -//#include -//#include -//#include -//#include - -#define QUEUE_LENGTH 10 -#define RECV_BUFFER_SIZE 2048 -#define SERVER_PORT 10000 - - -//#include -//#include FT_FREETYPE_H -// -//#include -// //#define STB_IMAGE_IMPLEMENTATION //#include int main(int argc, char const *argv[]); -int window_width = 1920; -int window_height = 1080; - +/* + Global variables +*/ +struct Socket *sock; struct Window *window; - -int flip_y = 1; - -unsigned int screen_rbo; -unsigned int video_rbo; -unsigned int quadVAO, quadVBO; -unsigned int cubeVAO, cubeVBO; - struct Player *player; struct Renderer *renderer; -float deltaTime, lastFrame; -bool animation=true; - -//static void *get_proc_address(void *ctx, const char *name); -void processGLFWInput(GLFWwindow *window, mpv_handle *); -void framebuffer_size_callback(GLFWwindow *window, int width, int height); - -float imgVertices[] = { - // positions // texCoords - -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 0.0f , 0.0f, 0.0f, - 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, - - -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, - 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 0.0f, 1.0f, 1.0f}; - - -//static void on_mpv_render_update(void *ctx); -//static void on_mpv_events(void *ctx); +int flip_y = 1; +unsigned int screen_rbo; +unsigned int video_rbo; int wakeup = 0; -//extern int wakeup; -void init(); -void update(); -void render(); +//float imgVertices[] = { +// // positions // texCoords +// -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, +// -1.0f, -1.0f, 0.0f , 0.0f, 0.0f, +// 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, +// +// -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, +// 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, +// 1.0f, 1.0f, 0.0f, 1.0f, 1.0f}; -bool SetSocketBlockingEnabled(int fd, bool blocking); - - #endif diff --git a/src/socket_setup.c b/src/socket_setup.c new file mode 100644 index 0000000..a52929f --- /dev/null +++ b/src/socket_setup.c @@ -0,0 +1,35 @@ +#include "socket_setup.h" + +inline bool _SetSocketBlockingEnabled(int fd, bool blocking); + +void socket_accept(){ + int addrlen = sizeof(sock->address); + if ((sock->endpoint = accept(sock->server_fd, (struct sockaddr *)&(sock->address), (socklen_t *)&addrlen)) < 0) + { + perror("Accept failed"); + exit(EXIT_FAILURE); + } + if(_SetSocketBlockingEnabled(sock->endpoint, 0)){ + perror("Setting socket as blocking failed"); + exit(EXIT_FAILURE); + } +} + +/** + Sets socket as blocking + Returns true on success, or false if there was an error +*/ +bool _SetSocketBlockingEnabled(int fd, bool blocking) +{ + if (fd < 0) return false; + +#ifdef _WIN32 + unsigned long mode = blocking ? 0 : 1; + return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false; +#else + int flags = fcntl(fd, F_GETFL, 0); + if (flags == -1) return false; + flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); + return (fcntl(fd, F_SETFL, flags) == 0) ? true : false; +#endif +} diff --git a/src/socket_setup.h b/src/socket_setup.h new file mode 100644 index 0000000..0de2e56 --- /dev/null +++ b/src/socket_setup.h @@ -0,0 +1,7 @@ +#include "connection/sock.h" +#include +#include //TODO Unnecessary + +extern struct Socket *sock; + +void socket_accept(); diff --git a/src/window_setup.c b/src/window_setup.c new file mode 100644 index 0000000..2e8be00 --- /dev/null +++ b/src/window_setup.c @@ -0,0 +1,69 @@ +#include "window_setup.h" + +static inline void _check_error(int); +static inline void _processGLFWInput(GLFWwindow *window, mpv_handle *ctx); + + +void init() { +} + +void destroy() { +} + +void tick() { +} + +void update() { + _processGLFWInput(window->handle, player->handle); + + if (wakeup) + { + if ((mpv_render_context_update(player->ctx) & MPV_RENDER_UPDATE_FRAME)) + { + mpv_render_context_render(player->ctx, renderer->params_fbo); + glViewport(0, 0, window->size.x, window->size.y); + } + } +} + +void render(){ + shader_bind(renderer->shaders[SHADER_MPV]); + + vao_bind(renderer->screenVAO); + glBindTexture(GL_TEXTURE_2D, renderer->video_textureColorbuffer); // <-- SCREEN Colorbuffer IS THE TEXTURE + glDrawArrays(GL_TRIANGLES, 0, 6); + + // ----- + if (wakeup) + { + mpv_render_context_report_swap(player->ctx); + wakeup = 0; + } + + const char *text = "I Joe you Joe ;)"; + render_text(renderer, text, strlen(text), 0, 0, 0.001, (float [3]){0.0, 1.0, 0.0}); +} + +void _processGLFWInput(GLFWwindow *window, mpv_handle *ctx) +{ + glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_FALSE); + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { + glfwSetWindowShouldClose(window, true); + } + + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { + const char *c[] = {"show-text", "lol", NULL}; + _check_error(mpv_command(ctx, c)); + } + +} + +static inline void _check_error(int status) +{ + if (status < 0) { + printf("mpv API error: %s\n", mpv_error_string(status)); + + //exit(1); + } +} + diff --git a/src/window_setup.h b/src/window_setup.h new file mode 100644 index 0000000..75bbb29 --- /dev/null +++ b/src/window_setup.h @@ -0,0 +1,26 @@ +#ifndef _WINDOW_SETUP_H_ +#define _WINDOW_SETUP_H_ + +#include "gfx/player.h" +#include "gfx/renderer.h" +#include "gfx/window.h" // Also imports player.h and renderer.h + +/* + Global variables +*/ +extern struct Window *window; +extern struct Player *player; +extern struct Renderer *renderer; + +extern int wakeup; + +/* + Window functions +*/ +void init(); +void destroy(); +void tick(); +void update(); +void render(); + +#endif