This commit is contained in:
John Alanbrook 2022-06-30 15:31:23 +00:00
parent b49c2b36c9
commit 2bc9bb65af
15 changed files with 143 additions and 106 deletions

View file

@ -158,7 +158,7 @@ bin/libglfw3.a:
$(objprefix)/%.o:%.c
@mkdir -p $(@D)
@echo Making C object $@
-@$(CC) $(COMPILER_FLAGS)
@$(CC) $(COMPILER_FLAGS)
tags: $(ALLFILES)
@echo Making tags

View file

@ -6078,6 +6078,7 @@ NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_prop
#define STB_RECT_PACK_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_STATIC
/* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */
#ifndef STBTT_malloc

View file

@ -3,8 +3,8 @@
#define STB_DS_IMPLEMENTATION
#include <stb_ds.h>
//#define STB_TRUETYPE_IMPLEMENTATION
//#include <stb_truetype.h>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#define PL_MPEG_IMPLEMENTATION
#include <pl_mpeg.h>

View file

@ -9,8 +9,6 @@
#include <stdlib.h>
#include <window.h>
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_STATIC
#include <stb_truetype.h>
static uint32_t VBO = 0;
@ -22,13 +20,12 @@ unsigned char temp_bitmap[512 * 512];
struct sFont *font;
static struct mShader *shader;
void font_init() {
shader = MakeShader("textvert.glsl", "textfrag.glsl");
void font_init(struct mShader *textshader) {
shader = textshader;
}
void font_frame(struct mSDLWindow *w) {
shader_use(shader);
shader_setmat4(shader, "projection", w->projection);
}
struct sFont *MakeFont(const char *fontfile, int height)
@ -90,8 +87,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 4, NULL,
GL_DYNAMIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 4 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
@ -102,8 +98,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
return newfont;
}
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale,
struct mShader *shader, float color[3])
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct mShader *shader, float color[3])
{
float xpos = cursor[0] + c.Bearing[0] * scale;
float ypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale;
@ -118,6 +113,8 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale,
xpos + w, ypos, 1.f, 1.f
};
////// Outline calculation
// float outlineWidth = 1.1;
// float ow = c.Size[0] * scale * outlineWidth;
@ -126,8 +123,6 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale,
// float oxpos = cursor[0] + c.Bearing[0] * scale * outlineWidth - ((ow-w)/2);
// float oypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale * outlineWidth - ((oh-h)/2);
// float overts[4*4] = {
// oxpos, oypos + oh, 0.f, 0.f,
// oxpos, oypos, 0.f, 1.f,
@ -135,12 +130,13 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale,
// oxpos + ow, oypos, 1.f, 1.f
// };
/////////// Shadow calculation
/*
float shadowOffset = 6.f;
float sxpos =
cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset);
float sypos =
cursor[1] - (c.Size[1] - c.Bearing[1]) * scale -
(scale * shadowOffset);
float sxpos = cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset);
float sypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale - (scale * shadowOffset);
float sverts[4 * 4] = {
sxpos, sypos + h, 0.f, 0.f,
@ -148,22 +144,20 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale,
sxpos + w, sypos + h, 1.f, 0.f,
sxpos + w, sypos, 1.f, 1.f
};
*/
glBindTexture(GL_TEXTURE_2D, c.TextureID);
//// Shadow pass
/*
float black[3] = { 0, 0, 0 };
shader_setvec3(shader, "textColor", black);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(sverts), sverts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
*/
//// Outline pass
//// Character pass
shader_setvec3(shader, "textColor", color);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

View file

@ -4,6 +4,7 @@
#include "mathc.h"
struct mShader;
struct mSDLWindow;
/// Holds all state information relevant to a character as loaded using FreeType
struct Character {
@ -19,17 +20,14 @@ struct sFont {
struct Character Characters[127];
};
struct mSDLWindow;
void font_init();
void font_init(struct mShader *s);
void font_frame(struct mSDLWindow *w);
struct sFont *MakeFont(const char *fontfile, int height);
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale,
struct mShader *shader, float color[3]);
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct mShader *shader, float color[3]);
void text_settype(struct sFont *font);
void renderText(
const char *text, mfloat_t pos[2], float scale,
mfloat_t color[3], float lw);
void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw);
#endif

View file

@ -96,28 +96,25 @@ void openglInit()
{
if (!mainwin) {
YughError("No window to init OpenGL on.", 1);
abort();
exit(1);
}
sprite_initialize();
////// MAKE SHADERS
outlineShader = MakeShader("outlinevert.glsl", "outline.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");
font_init(textShader);
sprite_initialize();
debugdraw_init();
stdFont = MakeFont("notosans.ttf", 300);
text_settype(stdFont);
//glEnable(GL_STENCIL_TEST);
glClearColor(editorClearColor[0], editorClearColor[1],
editorClearColor[2], editorClearColor[3]);
glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -131,8 +128,7 @@ void openglInit()
glGenBuffers(1, &projUBO);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
glBufferData(GL_UNIFORM_BUFFER, 64, NULL, GL_DYNAMIC_DRAW);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, projUBO, 0,
sizeof(float) * 16);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, projUBO, 0, sizeof(float) * 16);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
shader_setUBO(spriteShader, "Projection", 0);
@ -154,33 +150,37 @@ void openglRender(struct mSDLWindow *window, struct mCamera *mcamera)
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
// Clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
////// TEXT && GUI
glDepthFunc(GL_ALWAYS);
//shader_use(textShader);
shader_use(textShader);
//shader_setmat4(textShader, "projection", window->projection);
//mfloat_t fontpos[2] = { 25.f, 25.f };
//mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f };
//renderText("Sample text", fontpos, 0.4f, fontcolor, -1.f);
mfloat_t fontpos[2] = { 25.f, 25.f };
mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f };
renderText("Sample text", fontpos, 4.f, fontcolor, -1.f);
/*
///// Sprites
shader_use(spriteShader);
shader_setmat4(spriteShader, "projection", window->projection);
for (int i = 0; i < numSprites; i++) {
sprite_draw(sprites[i]);
}
*/
//glDepthFunc(GL_LESS);

View file

@ -75,7 +75,7 @@ GLuint load_shader_from_file(const char *path, int type)
const char *code = buf;
glShaderSource(id, 1, &code, NULL);
glCompileShader(id);
if ( shader_compile_error(id))
if (shader_compile_error(id))
return 0;
return id;
@ -116,51 +116,39 @@ void shader_setfloat(struct mShader *shader, const char *name, float val)
glUniform1f(glGetUniformLocation(shader->id, name), val);
}
void shader_setvec2(struct mShader *shader, const char *name,
mfloat_t val[2])
void shader_setvec2(struct mShader *shader, const char *name, mfloat_t val[2])
{
glUniform2fv(glGetUniformLocation(shader->id, name), 1, val);
}
void shader_setvec3(struct mShader *shader, const char *name,
mfloat_t val[3])
void shader_setvec3(struct mShader *shader, const char *name, mfloat_t val[3])
{
glUniform3fv(glGetUniformLocation(shader->id, name), 1, val);
}
void shader_setvec4(struct mShader *shader, const char *name,
mfloat_t val[4])
void shader_setvec4(struct mShader *shader, const char *name, mfloat_t val[4])
{
glUniform4fv(glGetUniformLocation(shader->id, name), 1, val);
}
void shader_setmat2(struct mShader *shader, const char *name,
mfloat_t val[4])
void shader_setmat2(struct mShader *shader, const char *name, mfloat_t val[4])
{
glUniformMatrix2fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE,
val);
glUniformMatrix2fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
}
void shader_setmat3(struct mShader *shader, const char *name,
mfloat_t val[9])
void shader_setmat3(struct mShader *shader, const char *name, mfloat_t val[9])
{
glUniformMatrix3fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE,
val);
glUniformMatrix3fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
}
void shader_setmat4(struct mShader *shader, const char *name,
mfloat_t val[16])
void shader_setmat4(struct mShader *shader, const char *name, mfloat_t val[16])
{
shader_use(shader);
glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE,
val);
glUniformMatrix4fv(glGetUniformLocation(shader->id, name), 1, GL_FALSE, val);
}
void shader_setUBO(struct mShader *shader, const char *name,
unsigned int index)
void shader_setUBO(struct mShader *shader, const char *name, unsigned int index)
{
glUniformBlockBinding(shader->id,
glGetUniformBlockIndex(shader->id, name), index);
glUniformBlockBinding(shader->id, glGetUniformBlockIndex(shader->id, name), index);
}

View file

@ -1,6 +1,7 @@
#include "sound.h"
#include "resources.h"
#include <stdlib.h>
#include "log.h"
ma_engine engine;
@ -34,7 +35,8 @@ void sound_init()
ma_result result = ma_engine_init(NULL, &engine);
if (result != MA_SUCCESS) {
printf("UHOH!!!");
YughError("Miniaudio did not start properly.",1);
exit(1);
}
ma_sound_group_init(&engine, 0, NULL, &mus_grp);

View file

@ -95,8 +95,7 @@ void sprite_initialize()
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices,
GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
@ -111,7 +110,7 @@ void sprite_draw(struct mSprite *sprite)
{
if (sprite->tex != NULL) {
shader_use(spriteShader);
//shader_use(spriteShader);
mfloat_t model[16] = { 0.f };
@ -126,10 +125,8 @@ void sprite_draw(struct mSprite *sprite)
mfloat_t t_move[2] = { 0.f };
mfloat_t t_scale[2] = { 0.f };
t_scale[0] =
sprite->size[0] * sprite->tex->width * sprite->go->scale;
t_scale[1] =
sprite->size[1] * sprite->tex->height * sprite->go->scale;
t_scale[0] = sprite->size[0] * sprite->tex->width * sprite->go->scale;
t_scale[1] = sprite->size[1] * sprite->tex->height * sprite->go->scale;
t_move[0] = sprite->pos[0] * t_scale[0];
t_move[1] = sprite->pos[1] * t_scale[1];
@ -184,8 +181,7 @@ 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);

View file

@ -35,14 +35,12 @@ extern int numSprites;
struct mSprite *MakeSprite(struct mGameObject *go);
void sprite_init(struct mSprite *sprite, struct mGameObject *go);
void sprite_loadtex(struct mSprite *sprite, const char *path);
void sprite_loadanim(struct mSprite *sprite, const char *path,
struct Anim2D anim);
void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim);
void sprite_settex(struct mSprite *sprite, struct Texture *tex);
void sprite_initialize();
void sprite_draw(struct mSprite *sprite);
void spriteanim_draw(struct mSprite *sprite);
void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2],
float rotate, mfloat_t color[3]);
void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]);
unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite);
struct mSprite *gui_makesprite();

View file

@ -15,6 +15,14 @@ struct timer *timer_make(int interval, void *(*callback)(void *param), void *par
return new;
}
void timer_pause(struct timer *t) {
}
void timer_start(struct timer *t) {
}
void timer_remove(struct timer *t) {
free(t);
}

View file

@ -27,9 +27,7 @@ void *vec_get(struct vec *vec, int n)
if (n < vec->len)
return (char *) vec->data + (n * vec->width);
YughLog(0, 4,
"Attempted to access element %d of a vec with %d elements.", n,
vec->len);
YughLog(0, 4,"Attempted to access element %d of a vec with %d elements.", n, vec->len);
return NULL;
}
@ -71,8 +69,7 @@ void *vec_add(struct vec *vec, const void *data)
return vec_get(vec, vec->len - 1);
}
void *vec_add_sort(struct vec *vec, void *data,
int (*sort)(void *a, void *b))
void *vec_add_sort(struct vec *vec, void *data, int (*sort)(void *a, void *b))
{
if(vec->size == vec->len)
vec_expand(vec);
@ -84,6 +81,14 @@ void *vec_add_sort(struct vec *vec, void *data,
return vec_insert(vec, data, n);
}
void *vec_find(struct vec *v, int (*valid)(void *a, void *data), void *data)
{
for (int i = 0; i < v->len; i++) {
if (valid(vec_p(v, i), data))
return vec_p(v, i);
}
}
void *vec_insert(struct vec *vec, void *data, int n)
{
if (vec->size == vec->len)

View file

@ -18,10 +18,10 @@ void vec_clear(struct vec *vec);
void *vec_add(struct vec *vec, const void *data);
/* sort returns 0 for a<=b, 1 for a>b */
void *vec_add_sort(struct vec *vec, void *data,
int (*sort)(void *a, void *b));
void *vec_add_sort(struct vec *vec, void *data, int (*sort)(void *a, void *b));
void *vec_insert(struct vec *vec, void *data, int n);
void *vec_set(struct vec *vec, int n, void *data);
void *vec_find(struct vec *v, int (*valid)(void *a, void *data), void *data);
void vec_delete(struct vec *vec, int n);
void vec_del_order(struct vec *vec, int n);
void vec_store(struct vec *vec, FILE *f);

View file

@ -5,14 +5,56 @@
#include <stdlib.h>
#include <stdio.h>
#include <vec.h>
#include "input.h"
static struct mSDLWindow *mainwin;
static struct vec windows;
struct Texture *icon = NULL;
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height,
uint32_t flags)
int is_win(struct mSDLWindow *s, GLFWwindow *w)
{
return s->window == w;
}
void window_size_callback(GLFWwindow *w)
{
}
void window_iconify_callback(GLFWwindow *w, int iconified)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
win->iconified = iconified;
}
void window_focus_callback(GLFWwindow *w, int focused)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
win->keyboardFocus = focused;
}
void window_maximize_callback(GLFWwindow *w, int maximized)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
win->minimized = !maximized;
}
void window_framebuffer_size_cb(GLFWwindow *w, int width, int height)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
win->width = width;
win->height = height;
window_makecurrent(win);
win->render = 1;
}
void window_close_callback(GLFWwindow *w)
{
quit = 1;
}
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags)
{
struct mSDLWindow *w;
@ -44,7 +86,11 @@ struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height,
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
// Set callbacks
glfwSetWindowCloseCallback(w->window, window_close_callback);
glfwSetWindowSizeCallback(w->window, window_size_callback);
glfwSetFramebufferSizeCallback(w->window, window_framebuffer_size_cb);
glfwSetWindowFocusCallback(w->window, window_focus_callback);
return w;
}
@ -60,6 +106,8 @@ void window_destroy(struct mSDLWindow *w)
vec_delete(&windows, w->id);
}
void window_handle_event(struct mSDLWindow *w)
{
/*

View file

@ -16,6 +16,7 @@ struct mSDLWindow {
bool keyboardFocus;
bool fullscreen;
bool minimized;
bool iconified;
bool shown;
float projection[16];
};
@ -25,9 +26,7 @@ struct Texture;
extern struct mSDLWindow *mainwin;
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height,
uint32_t flags);
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags);
void window_set_icon(const char *png);
void window_destroy(struct mSDLWindow *w);
void window_handle_event(struct mSDLWindow *w);