Large refactoring. Working state

This commit is contained in:
Hoguchi-live 2023-01-15 00:45:15 +01:00
parent f2db0dc5e8
commit 8e89e7d905
13 changed files with 15884 additions and 344 deletions

View File

@ -15,7 +15,7 @@ DEPEXT := d
OBJEXT := o
#Flags, Libraries and Includes
CFLAGS := -Wall -pedantic
CFLAGS := -Wall -pedantic -Wno-gnu-statement-expression
CFLAGS += -Ilib/cglm/include -Ilib/glad/include -Ilib/glfw/include -Ilib/stb
CFLAGS += -Iinclude/ -I/usr/include/freetype2 -I/usr/include/stb
LDFLAGS := -lglfw -ldl -lmpv -lfreetype -lm

View File

@ -4,6 +4,7 @@
"/usr/bin/clang",
"-Wall",
"-pedantic",
"-Wno-gnu-statement-expression",
"-Ilib/cglm/include",
"-Ilib/glad/include",
"-Ilib/glfw/include",

2
run.sh
View File

@ -1 +1 @@
./bin/wotos assets/aa.mkv
./bin/wotos

25
src/chat/ircRead.py Normal file
View File

@ -0,0 +1,25 @@
import socket
import time
import sys
server = "irc.rizon.net" #settings
channel = "#soranowoto"
botnick = "assam__"
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
print("connecting to:"+server)
irc.connect((server, 6667))
irc.send(bytes("NICK "+ botnick +"\n", encoding='utf8'))
irc.send(bytes("PASS PekoRrat\n", encoding='utf8'))
irc.send(bytes("USER "+ botnick +" " + "assam assam " + "assam\n", encoding='utf8'))
time.sleep(5)
irc.send(bytes("JOIN "+ channel +"\n", encoding='utf8'))
while 1:
text=irc.recv(2040)
print(text)
if text.find(bytes('PING', encoding='utf8')) != -1: #check if 'PING' is found
irc.send(bytes('PONG ', encoding='utf8') + text.split()[1] + bytes('\r\n', encoding='utf8') )

26
src/chat/test.py Normal file
View File

@ -0,0 +1,26 @@
import socket, string, time, thread
SERVER = 'irc.rizon.net'
PORT = 6667
NICKNAME = 'GataGoto'
CHANNEL = '#soranowoto'
def main():
global IRC
IRC = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IRC.connect((SERVER, PORT))
thread.start_new_thread(Listener(),("Thread No:1",2))
def send_data(command):
IRC.send(command + '\n')
def Listener():
send_data('USER Blah')
send_data('NICK Blah')
while (1):
buffer = IRC.recv(1024)
msg = string.split(buffer)
if msg[0] == "PING":
print('Pinged!')
IRC.send("PONG %s" % msg[1] + '\n')
main()

View File

@ -3,71 +3,34 @@
#include <stdio.h>
#include <stdlib.h>
// global window
struct Window window;
static void _size_callback(GLFWwindow *handle, int width, int height) {
glViewport(0, 0, width, height);
window.size = (ivec2s) {{height, width}};
window->size = (ivec2s) {{height, width}};
window->size.x = width;
window->size.y = height;
glBindTexture(GL_TEXTURE_2D, renderer->screen_textureColorbuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glBindRenderbuffer(GL_RENDERBUFFER, screen_rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
}
//static void _cursor_callback(GLFWwindow *handle, double xp, double yp) {
// vec2s p = {{xp, yp}};
//
// window.mouse.delta = glms_vec2_sub(p, window.mouse.position);
// window.mouse.delta.x = clamp(window.mouse.delta.x, -100.0f, 100.0f);
// window.mouse.delta.y = clamp(window.mouse.delta.y, -100.0f, 100.0f);
//
// window.mouse.position = p;
//}
//static void _key_callback(GLFWwindow *handle, int key, int scancode, int action, int mods) {
// if (key < 0) {
// return;
// }
//
// switch (action) {
// case GLFW_PRESS:
// window.keyboard.keys[key].down = true;
// break;
// case GLFW_RELEASE:
// window.keyboard.keys[key].down = false;
// break;
// default:
// break;
// }
//}
//static void _mouse_callback(GLFWwindow *handle, int button, int action, int mods) {
// if (button < 0) {
// return;
// }
//
// switch (action) {
// case GLFW_PRESS:
// window.mouse.buttons[button].down = true;
// break;
// case GLFW_RELEASE:
// window.mouse.buttons[button].down = false;
// break;
// default:
// break;
// }
//}
static void _error_callback(int code, const char *description) {
fprintf(stderr, "GLFW error %d: %s\n", code, description);
}
void window_create(FWindow init, FWindow destroy, FWindow tick, FWindow update, FWindow render) {
window.init = init;
window.destroy = destroy;
window.tick = tick;
window.update = update;
window.render = render;
struct Window *window_create(FWindow init, FWindow destroy, FWindow tick, FWindow update, FWindow render) {
struct Window *window = malloc(sizeof(struct Window));
window.last_frame = NOW();
window.last_second = NOW();
window->init = init;
window->destroy = destroy;
window->tick = tick;
window->update = update;
window->render = render;
window->last_frame = time(NULL);
window->last_second = time(NULL);
glfwSetErrorCallback(_error_callback);
@ -82,21 +45,18 @@ void window_create(FWindow init, FWindow destroy, FWindow tick, FWindow update,
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window.size = (ivec2s) {{1280, 720}};
window.handle = glfwCreateWindow(window.size.x, window.size.y, "Project", NULL, NULL);
if (window.handle == NULL) {
window->size = (ivec2s) {{1920, 1080}};
window->handle = glfwCreateWindow(window->size.x, window->size.y, WINDOW_NAME, NULL, NULL);
if (window->handle == NULL) {
fprintf(stderr, "%s", "error creating window\n");
glfwTerminate();
exit(1);
}
glfwMakeContextCurrent(window.handle);
glfwMakeContextCurrent(window->handle);
// configure callbacks
glfwSetFramebufferSizeCallback(window.handle, _size_callback);
//glfwSetCursorPosCallback(window.handle, _cursor_callback);
//glfwSetKeyCallback(window.handle, _key_callback);
//glfwSetMouseButtonCallback(window.handle, _mouse_callback);
glfwSetFramebufferSizeCallback(window->handle, _size_callback);
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
fprintf(stderr, "%s", "error initializing GLAD\n");
@ -104,85 +64,78 @@ void window_create(FWindow init, FWindow destroy, FWindow tick, FWindow update,
exit(1);
}
glEnable(GL_DEPTH);
glEnable(GL_MULTISAMPLE);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwSwapInterval(1);
}
static void button_array_tick(size_t n, struct Button *buttons) {
for (size_t i = 0; i < n; i++) {
buttons[i].pressed_tick = buttons[i].down && !buttons[i].last_tick;
buttons[i].last_tick = buttons[i].down;
}
}
static void button_array_update(size_t n, struct Button *buttons) {
for (size_t i = 0; i < n; i++) {
buttons[i].pressed = buttons[i].down && !buttons[i].last;
buttons[i].last = buttons[i].down;
}
return window;
}
static void _init() {
window.init();
window->init();
}
static void _destroy() {
window.destroy();
window->destroy();
glfwTerminate();
}
static void _tick() {
window.ticks++;
button_array_tick(GLFW_MOUSE_BUTTON_LAST, window.mouse.buttons);
button_array_tick(GLFW_KEY_LAST, window.keyboard.keys);
window.tick();
window->ticks++;
window->tick();
}
static void _update() {
button_array_update(GLFW_MOUSE_BUTTON_LAST, window.mouse.buttons);
button_array_update(GLFW_KEY_LAST, window.keyboard.keys);
window.update();
// reset update delta
window.mouse.delta = GLMS_VEC2_ZERO;
window->update();
}
static void _render() {
window.frames++;
window.render();
window->frames++;
window->render();
}
void window_loop() {
_init();
//_init();
while (!glfwWindowShouldClose(window.handle)) {
const u64 now = NOW();
while (!glfwWindowShouldClose(window->handle)) {
const u64 now = time(NULL);
window.frame_delta = now - window.last_frame;
window.last_frame = now;
window->frame_delta = now - window->last_frame;
window->last_frame = now;
if (now - window.last_second > NS_PER_SECOND) {
window.fps = window.frames;
window.tps = window.ticks;
window.frames = 0;
window.ticks = 0;
window.last_second = now;
if (now - window->last_second > NS_PER_SECOND) {
window->fps = window->frames;
window->tps = window->ticks;
window->frames = 0;
window->ticks = 0;
window->last_second = now;
printf("FPS: %lld | TPS: %lld\n", window.fps, window.tps);
printf("FPS: %ld | TPS: %ld\n", window->fps, window->tps);
}
/* TEST FOR REPLACEMENET OF MAIN LOOP
*/
// tick processing
const u64 NS_PER_TICK = (NS_PER_SECOND / 60);
u64 tick_time = window.frame_delta + window.tick_remainder;
while (tick_time > NS_PER_TICK) {
_tick();
tick_time -= NS_PER_TICK;
}
window.tick_remainder = max(tick_time, 0);
/* END TEST FOR REPLACEMENET OF MAIN LOOP
*/
//// tick processing
//const u64 NS_PER_TICK = (NS_PER_SECOND / 60);
//u64 tick_time = window->frame_delta + window->tick_remainder;
//while (tick_time > NS_PER_TICK) {
// _tick();
// tick_time -= NS_PER_TICK;
//}
//window->tick_remainder = MAX(tick_time, 0);
_update();
_render();
glfwSwapBuffers(window.handle);
glfwSwapBuffers(window->handle);
glfwPollEvents();
usleep(16 * 1000); // we LIMIT the main render loop to 100FPS! If VSYSNC is enabled the limit is the VSYNC limit (~60fps)
}
_destroy();
@ -190,9 +143,9 @@ void window_loop() {
}
void mouse_set_grabbed(bool grabbed) {
glfwSetInputMode(window.handle, GLFW_CURSOR, grabbed ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
glfwSetInputMode(window->handle, GLFW_CURSOR, grabbed ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
}
bool mouse_get_grabbed() {
return glfwGetInputMode(window.handle, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
return glfwGetInputMode(window->handle, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
}

View File

@ -3,29 +3,36 @@
#include "gfx.h"
#include "../util/util.h"
#include "./renderer.h"
#include "player.h"
#define WINDOW_NAME "wotos + mpv"
#include <unistd.h> // for usleep
#define MAX(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
extern struct Renderer *renderer;
extern struct Window *window;
extern unsigned int screen_rbo;
extern int wakeup;
extern mpv_render_context *mpv_ctx;
struct Button {
bool down, last, last_tick, pressed, pressed_tick;
};
struct Mouse {
struct Button buttons[GLFW_MOUSE_BUTTON_LAST];
vec2s position, delta;
};
struct Keyboard {
struct Button keys[GLFW_KEY_LAST];
};
typedef void (*FWindow)();
struct Window {
GLFWwindow *handle;
ivec2s size;
FWindow init, destroy, tick, update, render;
struct Mouse mouse;
struct Keyboard keyboard;
// timing variables
u64 last_second;
@ -33,13 +40,6 @@ struct Window {
u64 ticks, tps, tick_remainder;
};
// global window
extern struct Window window;
void window_loop();
void window_create(FWindow init, FWindow destroy, FWindow tick, FWindow update, FWindow render);
//void mouse_set_grabbed(bool grabbed);
//bool mouse_get_grabbed();
struct Window *window_create(FWindow init, FWindow destroy, FWindow tick, FWindow update, FWindow render);
#endif

15685
src/glad/glad.h Normal file

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,41 @@
static inline void check_error(int);
void init() {
}
void update() {
processGLFWInput(wwindow, mpv);
if (wakeup)
{
if ((mpv_render_context_update(mpv_ctx) & MPV_RENDER_UPDATE_FRAME))
{
mpv_render_context_render(mpv_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(mpv_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});
}
int main(int argc, char const *argv[])
{
if (argc < 2){
@ -12,41 +47,18 @@ int main(int argc, char const *argv[])
return -1;
}
////////// GLFW INIT
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
///////// Create window
if ((wwindow = glfwCreateWindow(window_width, window_height, "a-mpv", NULL, NULL)) == NULL){
printf("ERROR::GLFW::Failed to create window\n");
return -1;
}
glfwMakeContextCurrent(wwindow);
glfwSetFramebufferSizeCallback(wwindow, framebuffer_size_callback);
/*
Create the main window
wwindow is temporary
*/
FWindow nof = NULL;
window = window_create(init, nof, nof, update, render);
///////// Load GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
printf("ERROR::GLAD::Failed to initialize GLAD\n");
return -1;
}
wwindow = window->handle;
//////////// Initializing placeholder for FT text glyphs
//std::map<char, Character> Characters;
//makeBitmaps(Characters);
///////// GL parameters
glEnable(GL_DEPTH);
glEnable(GL_MULTISAMPLE);
glfwWindowHint(GLFW_SAMPLES, 4);
/* Glyph shenanigans
*/
//mat4s transformation = glms_ortho(0.0f, (float)(window_width), 0.0f, (float)window_height, -100, 100);
/* Setting up MPV player
/*
Set up the MPV player
The mpv and mpv_ctx variables are temporary
*/
player = player_create();
@ -54,13 +66,20 @@ int main(int argc, char const *argv[])
mpv = player->handle;
mpv_ctx = player->ctx;
/* Setting the renderer
The mpv and mpv_ctx variables are temporary
*/
renderer = renderer_create();
renderer_init(renderer);
/*
Glyph shenanigans
*/
//mat4s transformation = glms_ortho(0.0f, (float)(window_width), 0.0f, (float)window_height, -100, 100);
// this doesn't work when called inside the Renderer struct...
// this doesn't work when called from inside the Renderer struct...
int flip_y = 1;
renderer->params_fbo = (mpv_render_param [3]){
{MPV_RENDER_PARAM_OPENGL_FBO, &(renderer->mpv_fbo)},
@ -70,84 +89,21 @@ int main(int argc, char const *argv[])
/*
Start video playback
*/
const char filename[] = "https://stream.wotos.eu/snw_master.m3u8";
player_loadfile(player, filename);
player_loadfile(player, argv[1]);
const char *text = "I Joe";
window_loop();
while (!glfwWindowShouldClose(wwindow))
{
fcount++;
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
//if (fcount % 100 == 0){
// std::cout << "FPS: " << 1 / deltaTime << "\n" << std::endl;
// }
processGLFWInput(wwindow, mpv);
// -----
if (wakeup)
{
if ((mpv_render_context_update(mpv_ctx) & MPV_RENDER_UPDATE_FRAME))
{
mpv_render_context_render(mpv_ctx, renderer->params_fbo); // this "renders" to the video_framebuffer "linked by ID" in the params_fbo - BLOCKING
glViewport(0, 0, window_width, window_height); // fucky
}
}
//screenShader->use();
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(mpv_ctx);
wakeup = 0;
}
render_text(renderer, text, strlen(text), 0, 0, 0.001, (float [3]){0.0, 1.0, 0.0});
//glUniformMatrix4fv(glGetUniformLocation(renderer->shaders[SHADER_GLYPH].handle, "ransformation"), 1, GL_FALSE, (GLfloat *)&transformation.raw);
//Distance during deltaTime for consistent translation
//// Draw image
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//imgShader->use();
//glBindVertexArray(imgVBO); // <-- The SCREEN QUAD
//glBindTexture(GL_TEXTURE_2D, imgTex); // <-- SCREEN Colorbuffer IS THE TEXTURE
//imgMatrix = glm::translate(imgMatrix, glm::vec3(0.5/deltaPix, 0.0f, 0.0f));
//glUniformMatrix4fv(glGetUniformLocation(imgShader->ID, "transformation"), 1, GL_FALSE, glm::value_ptr(imgMatrix));
//glDrawArrays(GL_TRIANGLES, 0, 6);
// // Glyphs
//transformation = glm::translate(transformation, glm::vec3(deltaPix , 0.0f, 0.0f));
/////////////////////////////////////////// SOCKET
//recv_bytes = recv(sock, buffer, RECV_BUFFER_SIZE, 0);
//if(recv_bytes >= 1){
// danmakus.push_back({buffer, 1, (float)window_width, 400.0f, 1, 0, 0});
//}
//std::cout << danmakus.front().text << std::endl;
/////////////////////////////////////////// Danmaku update
//updateDanmakus(r);
//}
glfwSwapBuffers(wwindow);
glfwPollEvents();
usleep(16 * 1000); // we LIMIT the main render loop to 100FPS! If VSYSNC is enabled the limit is the VSYNC limit (~60fps)
}
renderer_destroy(renderer);
player_destroy(player);

View File

@ -11,10 +11,10 @@
#include "gfx/vbo.h"
#include "gfx/player.h"
#include "gfx/renderer.h"
#include <unistd.h>
#include "gfx/shader.h"
#include "gfx/bitmap.h"
#include "gfx/window.h"
//#include <sys/socket.h>
//#include <sys/types.h>
@ -41,6 +41,7 @@ int main(int argc, char const *argv[]);
int window_width = 1920;
int window_height = 1080;
struct Window *window;
GLFWwindow *wwindow = NULL;
mpv_handle *mpv;
mpv_render_context *mpv_ctx;
@ -50,14 +51,10 @@ unsigned int video_rbo;
unsigned int quadVAO, quadVBO;
unsigned int cubeVAO, cubeVBO;
struct VAO quadVAOTEST;
struct VBO quadVBOTEST;
struct Player *player;
struct Renderer *renderer;
float deltaTime, lastFrame;
unsigned int fcount = 0;
bool animation=true;
//static void *get_proc_address(void *ctx, const char *name);
@ -79,7 +76,7 @@ float imgVertices[] = {
//static void on_mpv_events(void *ctx);
int wakeup = 0;
extern int wakeup;
//extern int wakeup;
bool SetSocketBlockingEnabled(int fd, bool blocking);

View File

@ -1,103 +0,0 @@
import json
import queue
import requests
import websocket
from helper import *
import sys
import socket
import fcntl, os
SEND_BUFFER_SIZE = 2048
connected = 0
try: import thread
except ImportError: import _thread as thread
class Socket_:
def __init__(self, queue = None):
self.URL = None
self.sid = None
self.sidWSS = None
self.parser = Parser().parser
self.queue = queue
self.ws = None
self.sock = None
# Start the connection
self.handshake()
def handshake(self):
# First, establish a polling-transport HTTP connection
resp_1 = requests.get(POLLING_URL_1, headers = HEADER)
print('Resp 1: ', resp_1.text)
# Ignore the bin-packed preliminaries and extract the session ID
# WOTOS: self.sid = json.loads(str(resp_1.content)[3:-1])['sid']
self.sid = json.loads(str(resp_1.content)[3:-1])['sid']
# Second polling with POST, response should be 'ok'
resp_2 = requests.post(POLLING_URL_2.format(self.sid), headers = HEADER, data = b'40')
print('Resp 2: ', resp_2.text)
# Third polling
resp_3 = requests.get(POLLING_URL_3.format(self.sid), headers = HEADER)
print('Resp 3: ', resp_3.text)
# WOTOS: self.sid = json.loads(str(resp_1.content)[3:-1])['sid']
index = str(resp_3.content).find('\\x1e42[')
substr = str(resp_3.content)[4:index]
self.sidWSS = json.loads(substr)['sid']
# Fourth polling with POST
resp_4 = requests.post(POLLING_URL_4.format(self.sid), headers = HEADER, data = b'42["joinChannel",{"name":"testam"}]')
print('Resp 4: ', resp_4.text)
# Fourth polling with GET
resp_5 = requests.get(POLLING_URL_5.format(self.sid), headers = HEADER)
print('Resp 5: ', resp_5.text)
# WSS url
self.URL = WSS_URL.format(self.sid)
print(self.URL)
def on_open(self, ws):
print('### Socket open ###')
ws.send("2probe")
def on_close(self, ws):
print("### Socket closed ###")
self.sock.close()
def on_error(self, ws, error):
print('\nSocket error :\n{}\n'.format(error))
def on_message(self, ws, message):
if message == '2': ws.send('3')
elif message == '3probe': ws.send('5')
elif message[:2] == '42':
j = message[2:].replace("'", '"')
parsed = json.loads(j)
print("received: ", message)
reason = parsed[0]
if reason == 'chatMsg':
data = bytes(parsed[1]['msg'], "utf-8")
data += bytes(SEND_BUFFER_SIZE-len(data))
self.sock.send(data)
else: pass
def listenForever(self):
# SOCKET
server_ip = "localhost"
server_port = 10000
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((server_ip, server_port))
fcntl.fcntl(self.sock, fcntl.F_SETFL, os.O_NONBLOCK)
try:
#websocket.enableTrace(True)
self.ws = websocket.WebSocketApp(self.URL,
on_open = self.on_open,
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close,
header = HEADER_WSS
)
self.ws.run_forever(ping_interval = 25, ping_timeout = 5 )
except Exception as e:
print("Socket::ListenForever: Exception {}", format(e))