Video works

This commit is contained in:
John Alanbrook 2022-07-02 08:40:50 +00:00
parent 3190c7edcd
commit d506349462
12 changed files with 121 additions and 90 deletions

View file

@ -7,16 +7,15 @@
#include "sound.h" #include "sound.h"
#include <stdbool.h> #include <stdbool.h>
#include "log.h" #include "log.h"
#include "texture.h"
struct mShader *vid_shader; struct mShader *vid_shader;
static void ds_update_texture(uint32_t unit, uint32_t texture, static void ds_update_texture(uint32_t unit, uint32_t texture, plm_plane_t * plane)
plm_plane_t * plane)
{ {
glActiveTexture(unit); glActiveTexture(unit);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, plane->width, plane->height, 0, glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, plane->width, plane->height, 0, GL_RED, GL_UNSIGNED_BYTE, plane->data);
GL_RED, GL_UNSIGNED_BYTE, plane->data);
} }
static void render_frame(plm_t * mpeg, plm_frame_t * frame, void *user) static void render_frame(plm_t * mpeg, plm_frame_t * frame, void *user)
@ -30,16 +29,25 @@ static void render_frame(plm_t * mpeg, plm_frame_t * frame, void *user)
static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user) static void render_audio(plm_t * mpeg, plm_samples_t * samples, void *user)
{ {
struct datastream *ds = (struct datastream *) user; struct datastream *ds = user;
int size = sizeof(float) * samples->count * 2; int size = sizeof(float) * samples->count * 2;
play_raw(ds->audio_device, samples->interleaved, size); play_raw(ds->audio_device, samples->interleaved, size);
} }
struct Texture *ds_maketexture(struct datastream *ds)
{
struct Texture *new = malloc(sizeof(*new));
new->id = ds->texture_cb;
new->width = 500;
new->height = 500;
return new;
}
void ds_openvideo(struct datastream *ds, const char *video, const char *adriver) void ds_openvideo(struct datastream *ds, const char *video, const char *adriver)
{ {
ds_stop(ds); // ds_stop(ds);
char buf[MAXPATH] = {'\0'}; char buf[MAXPATH] = {'\0'};
sprintf(buf, "%s%s", DATA_PATH, video); sprintf(buf, "%s%s", "video/", video);
ds->plm = plm_create_with_filename(buf); ds->plm = plm_create_with_filename(buf);
if (!ds->plm) { if (!ds->plm) {
@ -99,15 +107,14 @@ struct datastream *MakeDatastream()
return newds; return newds;
} }
void ds_advance(struct datastream *ds, uint32_t ms) void ds_advance(struct datastream *ds, double s)
{ {
if (ds->playing) { if (ds->playing) {
double advanceTime = ms / 1000.f; plm_decode(ds->plm, s);
plm_decode(ds->plm, advanceTime);
} }
} }
void ds_seek(struct datastream *ds, uint32_t time) void ds_seek(struct datastream *ds, double time)
{ {
clear_raw(ds->audio_device); clear_raw(ds->audio_device);
plm_seek(ds->plm, time, false); plm_seek(ds->plm, time, false);

View file

@ -15,11 +15,15 @@ struct datastream {
uint32_t texture_cr; uint32_t texture_cr;
}; };
struct Texture;
extern struct mShader *vid_shader;
struct datastream *MakeDatastream(); struct datastream *MakeDatastream();
void ds_openvideo(struct datastream *ds, const char *path, void ds_openvideo(struct datastream *ds, const char *path, const char *adriver);
const char *adriver); struct Texture *ds_maketexture(struct datastream*);
void ds_advance(struct datastream *ds, uint32_t ms); void ds_advance(struct datastream *ds, double);
void ds_seek(struct datastream *ds, uint32_t time); void ds_seek(struct datastream *ds, double);
void ds_advanceframes(struct datastream *ds, int frames); void ds_advanceframes(struct datastream *ds, int frames);
void ds_pause(struct datastream *ds); void ds_pause(struct datastream *ds);
void ds_stop(struct datastream *ds); void ds_stop(struct datastream *ds);

View file

@ -98,7 +98,6 @@ struct sFont *MakeFont(const char *fontfile, int height)
newfont->Characters[c].Size[1] = h; newfont->Characters[c].Size[1] = h;
newfont->Characters[c].Bearing[0] = x0; newfont->Characters[c].Bearing[0] = x0;
newfont->Characters[c].Bearing[1] = y0*-1; newfont->Characters[c].Bearing[1] = y0*-1;
printf("Y0 is %d for %c.\n", y0, c);
} }
return newfont; return newfont;

View file

@ -7,7 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#define logLevel 4 #define logLevel 0
void mYughLog(int category, int priority, int line, const char *file, const char *message, ...) void mYughLog(int category, int priority, int line, const char *file, const char *message, ...)
{ {

View file

@ -10,6 +10,7 @@
#include "window.h" #include "window.h"
#include "debugdraw.h" #include "debugdraw.h"
#include "log.h" #include "log.h"
#include "datastream.h"
int renderMode = 0; int renderMode = 0;
@ -92,6 +93,31 @@ struct mSprite *tsprite = NULL;
static unsigned int projUBO; static unsigned int projUBO;
const char *textvert =
"#version 330 core\n"
"layout (location = 0) in vec4 vertex; \n"
"out vec2 TexCoords;\n"
"uniform mat4 projection;\n"
"void main() {\n"
" gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);\n"
" TexCoords = vec2(vertex.z, 1.0 - vertex.w);\n"
"}\n";
const char *textfrag =
"#version 330 core\n"
"in vec2 TexCoords;\n"
"out vec4 color;\n"
"uniform sampler2D text;\n"
"uniform vec3 textColor;\n"
"void main() { \n"
" vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);\n"
" color = vec4(textColor, 1.0) * sampled;\n"
"}\n";
void openglInit() void openglInit()
{ {
if (!mainwin) { if (!mainwin) {
@ -104,6 +130,7 @@ void openglInit()
spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl"); spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl");
animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl"); animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl");
textShader = MakeShader("textvert.glsl", "textfrag.glsl"); textShader = MakeShader("textvert.glsl", "textfrag.glsl");
//textShader = CreateShader(textverg, textfrag);
shader_use(textShader); shader_use(textShader);
shader_setint(textShader, "text", 0); shader_setint(textShader, "text", 0);
@ -156,10 +183,7 @@ void openglRender(struct mSDLWindow *window, struct mCamera *mcamera)
glBindBuffer(GL_UNIFORM_BUFFER, projUBO); glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
shader_setmat4(vid_shader, "projection", projection);
glColorMask(true, true, true, true);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
// Clear color and depth // Clear color and depth
@ -167,19 +191,15 @@ void openglRender(struct mSDLWindow *window, struct mCamera *mcamera)
////// TEXT && GUI ////// TEXT && GUI
///// Sprites ///// Sprites
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
shader_use(spriteShader); shader_use(spriteShader);
sprite_draw_all(); sprite_draw_all();
glDepthFunc(GL_ALWAYS); glDepthFunc(GL_ALWAYS);
shader_use(textShader); shader_use(textShader);
shader_setmat4(textShader, "projection", projection); shader_setmat4(textShader, "projection", projection);
} }

View file

@ -7,21 +7,26 @@
#include <stdlib.h> #include <stdlib.h>
#include "log.h" #include "log.h"
#include "resources.h" #include "resources.h"
#include "vec.h"
#define SHADER_BUF 10000 #define SHADER_BUF 10000
struct mShader *mshaders[255]; struct vec shaders;
struct mShader **lastShader = mshaders;
struct mShader *MakeShader(const char *vertpath, const char *fragpath) struct mShader *MakeShader(const char *vertpath, const char *fragpath)
{ {
if (shaders.data == NULL) shaders = vec_init(sizeof(struct mShader), 10);
struct mShader init = { 0, vertpath, fragpath }; struct mShader init = { 0, vertpath, fragpath };
struct mShader *newshader = struct mShader *new = vec_add(&shaders, NULL);
(struct mShader *) malloc(sizeof(struct mShader)); memcpy(new, &init, sizeof(*new));
memcpy(newshader, &init, sizeof(*newshader)); shader_compile(new);
*lastShader++ = newshader; return new;
shader_compile(newshader); }
return newshader;
struct mShader *CreateShader(const char *vert, const char *frag)
{
return NULL;
} }
int shader_compile_error(GLuint shader) int shader_compile_error(GLuint shader)
@ -75,8 +80,10 @@ GLuint load_shader_from_file(const char *path, int type)
const char *code = buf; const char *code = buf;
glShaderSource(id, 1, &code, NULL); glShaderSource(id, 1, &code, NULL);
glCompileShader(id); glCompileShader(id);
if (shader_compile_error(id)) if (shader_compile_error(id)) {
printf("Error with shader %s.\n", path);
return 0; return 0;
}
return id; return id;
} }
@ -155,10 +162,5 @@ void shader_setUBO(struct mShader *shader, const char *name, unsigned int index)
void shader_compile_all() void shader_compile_all()
{ {
struct mShader **curshader = mshaders; vec_walk(&shaders, shader_compile);
do {
YughLog(0, LOG_INFO, "Compiled Shader %d", 1);
shader_compile(*curshader);
} while (++curshader != lastShader);
} }

View file

@ -11,6 +11,7 @@ struct mShader {
void shader_compile_all(); void shader_compile_all();
struct mShader *MakeShader(const char *vertpath, const char *fragpath); struct mShader *MakeShader(const char *vertpath, const char *fragpath);
struct mShader *CreateShader(const char *vert, const char *frag);
void shader_compile(struct mShader *shader); void shader_compile(struct mShader *shader);
void shader_use(struct mShader *shader); void shader_use(struct mShader *shader);
@ -18,20 +19,13 @@ void shader_setbool(struct mShader *shader, const char *name, int val);
void shader_setint(struct mShader *shader, const char *name, int val); void shader_setint(struct mShader *shader, const char *name, int val);
void shader_setfloat(struct mShader *shader, const char *name, float val); void shader_setfloat(struct mShader *shader, const char *name, float val);
void shader_setvec2(struct mShader *shader, const char *name, void shader_setvec2(struct mShader *shader, const char *name, mfloat_t val[2]);
mfloat_t val[2]); void shader_setvec3(struct mShader *shader, const char *name, mfloat_t val[3]);
void shader_setvec3(struct mShader *shader, const char *name, void shader_setvec4(struct mShader *shader, const char *name, mfloat_t val[4]);
mfloat_t val[3]); void shader_setmat2(struct mShader *shader, const char *name, mfloat_t val[4]);
void shader_setvec4(struct mShader *shader, const char *name, void shader_setmat3(struct mShader *shader, const char *name, mfloat_t val[9]);
mfloat_t val[4]); void shader_setmat4(struct mShader *shader, const char *name, mfloat_t val[16]);
void shader_setmat2(struct mShader *shader, const char *name,
mfloat_t val[4]);
void shader_setmat3(struct mShader *shader, const char *name,
mfloat_t val[9]);
void shader_setmat4(struct mShader *shader, const char *name,
mfloat_t val[16]);
void shader_setUBO(struct mShader *shader, const char *name, void shader_setUBO(struct mShader *shader, const char *name, unsigned int index);
unsigned int index);
#endif #endif

View file

@ -12,34 +12,47 @@ ma_sound_group mus_grp;
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{ {
printf("audio cb\n");
// In playback mode copy data to pOutput. In capture mode read data from pInput. In full-duplex mode, both // In playback mode copy data to pOutput. In capture mode read data from pInput. In full-duplex mode, both
// pOutput and pInput will be valid and you can move data from pInput into pOutput. Never process more than // pOutput and pInput will be valid and you can move data from pInput into pOutput. Never process more than
// frameCount frames. // frameCount frames.
ma_engine_read_pcm_frames(&engine, pOutput, frameCount, NULL);
} }
void sound_init() void sound_init()
{ {
/*
ma_device_config cnf = ma_device_config_init(ma_device_type_playback); ma_device_config cnf = ma_device_config_init(ma_device_type_playback);
cnf.playback.format = ma_format_f32; cnf.playback.format = ma_format_f32;
cnf.playback.channels = 0; cnf.playback.channels = 2;
cnf.sampleRate = 0; cnf.sampleRate = 48000;
cnf.dataCallback = data_callback; cnf.dataCallback = data_callback;
cnf.pUserData = mus_cur;
ma_device device; ma_device device;
ma_device_init(NULL, &cnf, &device); if (ma_device_init(NULL, &cnf, &device) != MA_SUCCESS) {
YughError("Did not initialize audio playback!!",0);
}
ma_device_start(&device);
*/ if (ma_device_start(&device) != MA_SUCCESS) {
printf("Failed to start playback device.\n");
}
ma_result result = ma_engine_init(NULL, &engine);
ma_engine_config enginecnf = ma_engine_config_init();
enginecnf.pDevice = &device;
ma_result result = ma_engine_init(&enginecnf, &engine);
if (result != MA_SUCCESS) { if (result != MA_SUCCESS) {
YughError("Miniaudio did not start properly.",1); YughError("Miniaudio did not start properly.",1);
exit(1); exit(1);
} }
ma_sound_group_init(&engine, 0, NULL, &mus_grp); ma_sound_group_init(&engine, 0, NULL, &mus_grp);
} }
void audio_open(const char *device) void audio_open(const char *device)

View file

@ -9,6 +9,7 @@
#include "datastream.h" #include "datastream.h"
#include "gameobject.h" #include "gameobject.h"
#include <string.h> #include <string.h>
#include "vec.h"
static struct mGameObject *gui_go = NULL; static struct mGameObject *gui_go = NULL;
@ -21,16 +22,16 @@ static struct mShader *animSpriteShader = NULL;
struct TextureOptions TEX_SPRITE = { 1, 0, 0 }; struct TextureOptions TEX_SPRITE = { 1, 0, 0 };
struct mSprite *sprites[100] = { NULL }; struct vec sprites;
int numSprites = 0;
static uint32_t quadVAO; static uint32_t quadVAO;
struct mSprite *MakeSprite(struct mGameObject *go) struct mSprite *MakeSprite(struct mGameObject *go)
{ {
struct mSprite *sprite = malloc(sizeof(struct mSprite)); // TODO: Init this once and never check again
sprites[numSprites++] = sprite; if (sprites.data == NULL) sprites = vec_init(sizeof(struct mSprite), 10);
struct mSprite *sprite = vec_add(&sprites, NULL);
sprite->color[0] = 1.f; sprite->color[0] = 1.f;
sprite->color[1] = 1.f; sprite->color[1] = 1.f;
sprite->color[2] = 1.f; sprite->color[2] = 1.f;
@ -51,8 +52,7 @@ void sprite_init(struct mSprite *sprite, struct mGameObject *go)
void sprite_draw_all() void sprite_draw_all()
{ {
shader_use(spriteShader); shader_use(spriteShader);
for (int i = 0; i < numSprites; i++) vec_walk(&sprites, sprite_draw);
sprite_draw(sprites[i]);
} }
void sprite_loadtex(struct mSprite *sprite, const char *path) void sprite_loadtex(struct mSprite *sprite, const char *path)
@ -60,8 +60,7 @@ void sprite_loadtex(struct mSprite *sprite, const char *path)
sprite->tex = texture_loadfromfile(path); sprite->tex = texture_loadfromfile(path);
} }
void sprite_loadanim(struct mSprite *sprite, const char *path, void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim)
struct Anim2D anim)
{ {
sprite->tex = texture_loadfromfile(path); sprite->tex = texture_loadfromfile(path);
sprite->anim = anim; sprite->anim = anim;
@ -171,7 +170,7 @@ void spriteanim_draw(struct mSprite *sprite)
mat4_translate_vec2(model, sprite->pos); mat4_translate_vec2(model, sprite->pos);
mfloat_t msize[2] = mfloat_t msize[2] =
{ sprite->size[0] * sprite->anim.dimensions[0], { sprite->size[0] * sprite->anim.dimensions[0],
sprite->size[1] * sprite->anim.dimensions[1] }; sprite->size[1] * sprite->anim.dimensions[1] };
mat4_scale_vec2(model, msize); mat4_scale_vec2(model, msize);
shader_setmat4(animSpriteShader, "model", model); shader_setmat4(animSpriteShader, "model", model);
@ -189,15 +188,15 @@ sprite->size[1] * sprite->anim.dimensions[1] };
void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3]) void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3])
{ {
shader_use(spriteShader); shader_use(vid_shader);
static mfloat_t model[16]; static mfloat_t model[16];
memcpy(model, UNITMAT4, sizeof(UNITMAT4)); memcpy(model, UNITMAT4, sizeof(UNITMAT4));
mat4_translate_vec2(model, position); mat4_translate_vec2(model, position);
mat4_scale_vec2(model, size); mat4_scale_vec2(model, size);
shader_setmat4(spriteShader, "model", model); shader_setmat4(vid_shader, "model", model);
shader_setvec3(spriteShader, "spriteColor", color); shader_setvec3(vid_shader, "spriteColor", color);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, stream->texture_y); glBindTexture(GL_TEXTURE_2D, stream->texture_y);
@ -207,9 +206,8 @@ void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2
glBindTexture(GL_TEXTURE_2D, stream->texture_cr); glBindTexture(GL_TEXTURE_2D, stream->texture_cr);
// TODO: video bind VAO // TODO: video bind VAO
//glBindVertexArray(stream->quadVAO); glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
} }
void gui_init() void gui_init()

View file

@ -29,9 +29,6 @@ struct mSprite {
struct mGameObject *go; struct mGameObject *go;
}; };
extern struct mSprite *sprites[100];
extern int numSprites;
struct mSprite *MakeSprite(struct mGameObject *go); struct mSprite *MakeSprite(struct mGameObject *go);
void sprite_init(struct mSprite *sprite, struct mGameObject *go); void sprite_init(struct mSprite *sprite, struct mGameObject *go);
void sprite_loadtex(struct mSprite *sprite, const char *path); void sprite_loadtex(struct mSprite *sprite, const char *path);

View file

@ -249,17 +249,15 @@ double frame_time()
return glfwGetTime(); return glfwGetTime();
} }
double elapsed_time() int elapsed_time()
{ {
static double last_time; static double last_time;
static double elapsed; double elapsed;
elapsed = frame_time() - last_time; elapsed = frame_time() - last_time;
return elapsed; last_time = frame_time();
} //printf("Elapsed: %d.\n", elapsed);
return elapsed * 1000;
int elapsed_time_ms()
{
return elapsed_time() * 1000;
} }

View file

@ -39,7 +39,6 @@ void window_seticon(struct mSDLWindow *w, struct Texture *icon);
int window_hasfocus(struct mSDLWindow *w); int window_hasfocus(struct mSDLWindow *w);
double frame_time(); double frame_time();
double elapsed_time(); int elapsed_time();
int elapsed_time_ms();
#endif #endif