Cleaning and started working on sockets

This commit is contained in:
Hoguchi-live 2023-01-19 12:09:42 +01:00
parent 5fd9f25fb8
commit aae1a9224c
15 changed files with 255 additions and 167 deletions

7
.gitignore vendored
View File

@ -3,6 +3,13 @@ build/**
assets/**
bin/**
.cache/**
src/old/**
# Bear config
./compile_commands.json
# Shortcut launchers
./*.sh
# Prerequisites
*.d

2
run.sh
View File

@ -1 +1 @@
./bin/wotos
make run ARGS=assets/aa.mkv

38
src/connection/sock.c Normal file
View File

@ -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);
}
}

28
src/connection/sock.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef _SOCK_H_
#define _SOCK_H_
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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 <unistd.h> // for usleep

View File

@ -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});
}

View File

@ -1,89 +1,45 @@
#ifndef __MAIN_H
#define __MAIN_H
#ifndef _MAIN_H_
#define _MAIN_H_
//#include <iostream>
#include "gfx/gfx.h"
#include "window_setup.h"
#include "socket_setup.h"
#include "connection/sock.h"
#include <fcntl.h>
#include <mpv/client.h>
#include <mpv/render_gl.h>
#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 <sys/socket.h>
//#include <sys/types.h>
//#include <netdb.h>
//#include <netinet/in.h>
//#include <errno.h>
#define QUEUE_LENGTH 10
#define RECV_BUFFER_SIZE 2048
#define SERVER_PORT 10000
//#include <ft2build.h>
//#include FT_FREETYPE_H
//
//#include <fcntl.h>
//
//#define STB_IMAGE_IMPLEMENTATION
//#include <stb/stb_image.h>
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

35
src/socket_setup.c Normal file
View File

@ -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
}

7
src/socket_setup.h Normal file
View File

@ -0,0 +1,7 @@
#include "connection/sock.h"
#include <fcntl.h>
#include <stdbool.h> //TODO Unnecessary
extern struct Socket *sock;
void socket_accept();

69
src/window_setup.c Normal file
View File

@ -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);
}
}

26
src/window_setup.h Normal file
View File

@ -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