This commit is contained in:
Hoguchi-live 2023-01-15 01:43:54 +01:00
parent 8e89e7d905
commit 851650cd28
7 changed files with 54 additions and 404 deletions

View File

@ -49,8 +49,8 @@ struct Player* player_create() {
return malloc(sizeof(struct Player));
}
void player_destroy(struct Player* op) {
free(op);
void player_destroy(struct Player* self) {
free(self);
}

View File

@ -79,6 +79,8 @@ static void _init() {
static void _destroy() {
window->destroy();
player_destroy(player);
renderer_destroy(renderer);
glfwTerminate();
}
@ -97,7 +99,7 @@ static void _render() {
}
void window_loop() {
//_init();
_init();
while (!glfwWindowShouldClose(window->handle)) {
const u64 now = time(NULL);
@ -114,25 +116,19 @@ void window_loop() {
printf("FPS: %ld | TPS: %ld\n", window->fps, window->tps);
}
/* TEST FOR REPLACEMENET OF MAIN LOOP
*/
/* 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);
// 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);
glfwPollEvents();
usleep(16 * 1000); // we LIMIT the main render loop to 100FPS! If VSYSNC is enabled the limit is the VSYNC limit (~60fps)
@ -141,11 +137,3 @@ void window_loop() {
_destroy();
exit(0);
}
void mouse_set_grabbed(bool grabbed) {
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;
}

View File

@ -15,12 +15,12 @@
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
extern struct Player *player;
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;

View File

@ -4,80 +4,27 @@
#include "glad/glad.h"
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){
//std::cout << "Usage: " << argv[0] << " videofilename" << std::endl;
return -1;
}
void init();
void update();
void render();
/*
Create the main window
wwindow is temporary
*/
FWindow nof = NULL;
window = window_create(init, nof, nof, update, render);
int main(int argc, char const *argv[]){
if (argc < 2){ return -1;}
wwindow = window->handle;
/* Create the main window*/
window = window_create(init, NULL, NULL, update, render);
/*
Set up the MPV player
The mpv and mpv_ctx variables are temporary
*/
/* Set up the MPV player*/
player = player_create();
player_init(player);
mpv = player->handle;
mpv_ctx = player->ctx;
/* Setting the renderer
*/
/* Setting the renderer*/
renderer = renderer_create();
renderer_init(renderer);
/*
Glyph shenanigans
*/
/* Glyph shenanigans */
//mat4s transformation = glms_ortho(0.0f, (float)(window_width), 0.0f, (float)window_height, -100, 100);
// this doesn't work when called from inside the Renderer struct...
int flip_y = 1;
@ -86,33 +33,9 @@ int main(int argc, char const *argv[])
{MPV_RENDER_PARAM_FLIP_Y, &flip_y},
{MPV_RENDER_PARAM_INVALID, NULL}};
/*
Start video playback
*/
/* Start video playback */
player_loadfile(player, argv[1]);
window_loop();
while (!glfwWindowShouldClose(wwindow))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwSwapBuffers(wwindow);
glfwPollEvents();
}
renderer_destroy(renderer);
player_destroy(player);
mpv_render_context_free(mpv_ctx);
mpv_terminate_destroy(mpv);
glfwTerminate();
return 0;
}
@ -149,169 +72,38 @@ static inline void check_error(int status)
//exit(1);
}
}
//
///** 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
void init() {
}
void update() {
processGLFWInput(window->handle, player->handle);
//////////// SOCKET SERVER
//// create socket file descriptor
//int server_fd;
//if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
//{
// perror("socket failed");
// exit(EXIT_FAILURE);
//}
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);
}
}
}
//// create socket address
//// forcefully attach socket to the port
//struct sockaddr_in address;
//int opt = 1;
//if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
//{
// perror("setsockopt failed");
// exit(EXIT_FAILURE);
//}
//address.sin_family = AF_INET;
//address.sin_addr.s_addr = INADDR_ANY;
//address.sin_port = htons(SERVER_PORT);
void render(){
shader_bind(renderer->shaders[SHADER_MPV]);
//// bind socket to address
//if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
//{
// perror("bind failed");
// exit(EXIT_FAILURE);
//}
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;
}
//// listen to incoming connections
//if (listen(server_fd, QUEUE_LENGTH) < 0)
//{
// perror("listen failed");
// exit(EXIT_FAILURE);
//}
//}
//// BUNCH OF CRAP
////////// IMAGE TEST
//// Import image
//int imgWidth, imgHeight, imgChan;
//stbi_set_flip_vertically_on_load(true);
//unsigned char* bytes = stbi_load("./assets/miporin.png", &imgWidth, &imgHeight, &imgChan, 0);
//GLuint imgTex;
//glGenTextures(1, &imgTex);
//glActiveTexture(GL_TEXTURE1);
//glBindTexture(GL_TEXTURE_2D, imgTex);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
////Load image in texture
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, bytes);
//glGenerateMipmap(GL_TEXTURE_2D);
//unsigned int imgVAO, imgVBO;
//glGenVertexArrays(1, &imgVAO);
//glGenBuffers(1, &imgVBO);
//glBindVertexArray(imgVAO);
//glBindBuffer(GL_ARRAY_BUFFER, imgVBO);
//glBufferData(GL_ARRAY_BUFFER, sizeof(imgVertices), &imgVertices, GL_STATIC_DRAW);
//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0); // coordinates
//glEnableVertexAttribArray(0);
//glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float))); //texture
//glEnableVertexAttribArray(1);
//Shader *imgShader = new Shader("shaders/img_vs.glsl", "shaders/img_fs.glsl");
//imgShader->use();
//GLuint texture1 = glGetUniformLocation(imgShader->ID, "texture1");
//glUniform1i(texture1, 0);
//glm::mat4 imgMatrix(1.0f); // = glm::ortho(0.0f, static_cast<float>(window_width), 0.0f, static_cast<float>(window_height));
//imgMatrix = glm::scale(imgMatrix, glm::vec3(0.25f, 0.25f ,0.25f));
//imgMatrix = glm::translate(imgMatrix, glm::vec3(-15.0f, 0.0f, 0.0f));
////////// Text-rendering configuration
//unsigned int VAO, VBO;
//glGenVertexArrays(1, &VAO);
//glGenBuffers(1, &VBO);
//glBindVertexArray(VAO);
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
//glEnableVertexAttribArray(0);
//glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindVertexArray(0);
//Shader *glyphShader = new Shader("shaders/glyph_vs.glsl", "shaders/glyph_fs.glsl");
//glm::mat4 transformation = glm::ortho(0.0f, static_cast<float>(window_width), 0.0f, static_cast<float>(window_height));
////////// Vertical spacing for danmakus and bool freespace array
//const uint nb_danmakuRows = (uint)(
// (float)(window_height -
// DANMAKU_MARGIN_TOP -
// DANMAKU_MARGIN_TOP) /
// (float)(GLYPH_SIZE + DANMAKU_PADDING_V));
//uint *ph_danmakuRows = new uint[nb_danmakuRows];
//std::fill_n(ph_danmakuRows, nb_danmakuRows, 1);
////////// Other params
//float deltaPix = 0.0f;
//float danmakuNextYPos = (float)window_height-100.0f;
//int danmakuLine = 1;
//int danmakuMaxLines = 10;
//float danmakuDeltaY = 60.0f;
//float glyphXPos = 0;
//int isAvailable = 1;
//uint nextDanmakuRow = 0;
//uint danCount = 0;
///// WAIT FOR SOCKET
//// accept a connection
//int sock;
//int addrlen = sizeof(address);
//if ((sock = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
//{
// perror("accept failed");
// exit(EXIT_FAILURE);
//}
//else {
// std::cout << "accepted" << std::endl;
//}
//int rr = SetSocketBlockingEnabled(sock, 0);
//std::cout << "Blocking: " << rr << std::endl;
//char buffer[RECV_BUFFER_SIZE];
//int recv_bytes;
////Load fake danmakus
//std::vector<Danmaku> danmakus;
////////// Packing infos
//Render r = {window_height, window_width,
// screenShader, glyphShader, imgShader
// };
////////////////////////////////////////////////////////////////////////////////
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,123 +0,0 @@
#ifndef __MAIN_H
#define __MAIN_H
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <mpv/client.h>
#include <mpv/render_gl.h>
#include <cstring>
#include <unistd.h>
#include <shader.h>
#include <map>
#include "glm/glm.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#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>
#include "danmaku.h"
class DanmakuHandler {
public:
private:
}
typedef struct Render {
// Window info
float h;
float w;
// Shaders
Shader *screenShader;
Shader *glyphShader;
Shader *imgShader;
// Danmakus
std::map<char, Character> Characters;
} Render;
int window_width = 1366;
int window_height = 768;
int fbo_width = 1366;
int fbo_height = 768;
GLFWwindow *window = NULL;
mpv_handle *mpv;
mpv_render_context *mpv_ctx;
unsigned int video_framebuffer;
unsigned int video_textureColorbuffer;
unsigned int screen_framebuffer;
unsigned int screen_textureColorbuffer;
unsigned int screen_rbo;
unsigned int video_rbo;
unsigned int quadVAO, quadVBO;
unsigned int cubeVAO, cubeVBO;
float deltaTime, lastFrame;
unsigned int fcount = 0;
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};
float quadVertices[] = {
// 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);
unsigned int wakeup = 0;
static inline void check_error(int);
bool SetSocketBlockingEnabled(int fd, bool blocking);
#endif

View File

@ -1,7 +0,0 @@
class Player() {
public:
int init
private:
}