Add JSON; Reorganize textures

This commit is contained in:
John Alanbrook 2023-01-02 13:55:26 +00:00
parent eadf3524ba
commit 90c830a0e3
12 changed files with 130 additions and 179 deletions

View file

@ -20,9 +20,8 @@ void DrawMesh(struct mesh *mesh, struct shader *shader)
// retrieve texture number (the N in diffuse_textureN)
char number = 0;
// TODO: malloc every single frame ... nope! Change to stack
/*char *name =
(char *) malloc(sizeof(char) *
(strlen(mesh->textures[i].type) + 2));*/
/*char *name = malloc(sizeof(char) *(strlen(mesh->textures[i].type) + 2));*/
/*
if (mesh->textures[i].type == TEX_DIFF)
number = diffuseNr++;
else if (mesh->textures[i].type == TEX_SPEC)
@ -31,7 +30,7 @@ void DrawMesh(struct mesh *mesh, struct shader *shader)
number = normalNr++;
else if (mesh->textures[i].type == TEX_HEIGHT)
number = heightNr++;
*/
/*
glUniform1i(glGetUniformLocation(shader->id, name), i);
@ -97,36 +96,29 @@ void setupmesh(struct mesh *mesh)
// set the vertex attribute pointers
// vertex Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
(void *) 0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), NULL);
// vertex normals
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
(void *) offsetof(struct Vertex, Normal[3]));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, Normal[3]));
// vertex texture coords
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
(void *) offsetof(struct Vertex, TexCoords[2]));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, TexCoords[2]));
// vertex tangent
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
(void *) offsetof(struct Vertex, Tangent[3]));
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, Tangent[3]));
// vertex bitangent
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
(void *) offsetof(struct Vertex, Bitangent[3]));
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, Bitangent[3]));
// Bone ids
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(struct Vertex),
(void *) offsetof(struct Vertex,
glVertexAttribPointer(5, 4, GL_INT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex,
m_BoneIDs
[MAX_BONE_INFLUENCE]));
// Weights
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex),
(void *) offsetof(struct Vertex, m_Weights));
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), offsetof(struct Vertex, m_Weights));
glBindVertexArray(0);
}

View file

@ -780,13 +780,14 @@ void editor_selectasset_str(const char *path) {
asset->data = texture_loadfromfile(path);
load_asset(asset);
}
else
tex_pull(asset->data);
//else
//tex_pull(asset->data);
tex_gui_anim.tex = asset->data;
struct Texture *tex = asset->data;
tex_gui_anim.anim = &tex->anim;
tex_anim_set(&tex_gui_anim);
anim_setframe(&tex_gui_anim, 0);
float tex_scale = (float) ASSET_WIN_SIZE / (float)tex_gui_anim.tex->width;
float tex_scale = (float) ASSET_WIN_SIZE / (float)tex_gui_anim.anim->tex->width;
if (tex_scale >= 10.f) {
tex_scale = 10.f;
}
@ -827,8 +828,8 @@ void editor_asset_tex_gui(struct Texture *tex) {
nuke_checkbox("Sprite", &tex->opts.sprite);
if (old_sprite != tex->opts.sprite)
tex_gpu_load(tex);
//if (old_sprite != tex->opts.sprite)
//tex_gpu_load(tex);
nuke_nel(4);
nuke_radio_btn("Raw", &tex_view, 0);
@ -839,6 +840,7 @@ void editor_asset_tex_gui(struct Texture *tex) {
if (tex->opts.animation) {
/*
int old_frames = tex->anim.frames;
int old_ms = tex->anim.ms;
@ -877,6 +879,7 @@ void editor_asset_tex_gui(struct Texture *tex) {
r.h = st_s_h(tex_gui_anim.st)*tex->height;
nk_image(ctx, nk_subimage_id(tex->id, tex->width, tex->height, r));
*/
} else {
nk_layout_row_static(ctx, tex->height*tex_scale, tex->width*tex_scale, 1);
nk_image(ctx, nk_image_id(tex->id));

View file

@ -34,9 +34,6 @@
#include "sound.h"
// TODO: Init on the heap
#include "engine.h"
void error_callback(int error, const char *description)
@ -55,20 +52,15 @@ void engine_init()
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
resources_init();
YughInfo("Starting scripts ...");
script_init();
registry_init();
//stbi_set_flip_vertically_on_load(1);
YughInfo("Starting physics ...");
phys2d_init();
sound_init();
}
void engine_stop()
{
glfwTerminate();
YughInfo("Starting sound ...");
//sound_init();
}

View file

@ -1,23 +1,10 @@
#ifndef ENGINE_H
#define ENGINE_H
#define FPS30 33
#define FPS60 17
#define FPS120 8
#define FPS144 7
#define FPS300 3
#define sFPS30 0.033
#define sFPS60 0.017
#define sFPS120 0.008
#define sFPS144 0.007
#define sFPS300 0.003
extern double renderMS;
extern double physMS;
extern double updateMS;
void engine_init();
void engine_stop();
#endif

View file

@ -52,7 +52,7 @@ void font_frame(struct window *w) {
struct sFont *MakeFont(const char *fontfile, int height)
{
shader_use(shader);
YughInfo("Making font %s.", fontfile);
int packsize = 128;
@ -81,7 +81,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
}
float scale = stbtt_ScaleForPixelHeight(&fontinfo, height);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &newfont->texID);
glBindTexture(GL_TEXTURE_2D, newfont->texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, packsize, packsize, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
@ -96,10 +96,6 @@ struct sFont *MakeFont(const char *fontfile, int height)
for (unsigned char c = 32; c < 127; c++) {
stbtt_packedchar glyph = glyphs[c-32];
YughInfo("Packed char %c is at %d, %d, %d, %d", c, glyphs[c-32].x0, glyphs[c-32].y0, glyphs[c-32].x1, glyphs[c-32].y1);
YughInfo("Offsets are %f %f %f %f", glyph.xoff, glyph.yoff, glyph.xoff2, glyph.yoff2);
struct glrect r;
r.s0 = glyph.x0 / (float) packsize;
r.s1 = glyph.x1 / (float) packsize;

View file

@ -104,37 +104,41 @@ void wav_norm_gain(struct wav *w, double lv)
}
}
void sound_init()
void print_devices()
{
PaError err = Pa_Initialize();
check_pa_err(err);
int numDevices = Pa_GetDeviceCount();
const PaDeviceInfo *deviceInfo;
for (int i = 0; i < numDevices; i++) {
deviceInfo = Pa_GetDeviceInfo(i);
// printf("Device %i: channels %i, sample rate %f, name %s\n", i, deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate, deviceInfo->name);
YughInfo("Device %i: channels %i, sample rate %f, name %s\n", i, deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate, deviceInfo->name);
}
}
PaStreamParameters outparams;
void sound_init()
{
PaError err = Pa_Initialize();
check_pa_err(err);
/*
PaStreamParameters outparams;
outparams.channelCount = 2;
outparams.device = 19;
outparams.sampleFormat = paInt16;
outparams.suggestedLatency = Pa_GetDeviceInfo(outparams.device)->defaultLowOutputLatency;
outparams.hostApiSpecificStreamInfo = NULL;
*/
err = Pa_OpenStream(&stream_def, NULL, &outparams, 48000, 4096, paNoFlag, patestCallback, &data);
*/
//err = Pa_OpenStream(&stream_def, NULL, &outparams, 48000, 4096, paNoFlag, patestCallback, &data);
err = Pa_OpenDefaultStream(&stream_def, 0, 2, paInt16, SAMPLERATE, BUF_FRAMES, patestCallback, NULL);
check_pa_err(err);
err = Pa_StartStream(stream_def);
check_pa_err(err);
}
void audio_open(const char *device)

View file

@ -26,8 +26,7 @@ struct sprite *make_sprite(struct gameobject *go)
struct sprite sprite = {
.color = {1.f, 1.f, 1.f},
.size = {1.f, 1.f},
.tex = texture_loadfromfile("ph.png"),
.index = arrlen(sprites) };
.tex = texture_loadfromfile("ph.png") };
sprite_init(&sprite, go);
arrput(sprites, sprite);
@ -91,12 +90,6 @@ void sprite_settex(struct sprite *sprite, struct Texture *tex)
sprite->tex = tex;
}
unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite)
{
sprite->anim.frame = (sprite->anim.frame + 1) % sprite->anim.frames;
return interval;
}
static uint32_t VAO = 0;
void sprite_initialize()
@ -170,11 +163,9 @@ void sprite_draw(struct sprite *sprite)
if (sprite->tex->opts.animation) {
float size[2];
struct Anim2D *a = &sprite->anim;
a->frames = sprite->tex->anim.frames;
size[0] = sprite->tex->anim.dimensions[0];
size[1] = sprite->tex->anim.dimensions[1];
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex));
//size[0] = sprite->tex->anim.dimensions[0] * sprite->go->scale;
//size[1] = sprite->tex->anim.dimensions[1] * sprite->go->scale;
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
} else {
float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale };
@ -214,4 +205,3 @@ void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2
// TODO: video bind VAO
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

View file

@ -4,6 +4,7 @@
#include <stdio.h>
#include "timer.h"
#include "mathc.h"
#include "texture.h"
struct datastream;
struct gameobject;
@ -11,22 +12,13 @@ struct Texture;
struct timer;
struct Anim2D {
int frames;
int frame;
int dimensions[2];
struct timer *timer;
int ms;
};
struct sprite {
mfloat_t pos[2];
mfloat_t size[2];
float rotation;
mfloat_t color[3];
int index;
struct Anim2D anim;
struct TexAnimation anim;
struct gameobject *go;
struct Texture *tex;
};

View file

@ -22,14 +22,12 @@ struct Texture *texture_pullfromfile(const char *path)
return texhash[index].value;
struct Texture *tex = calloc(1, sizeof(*tex));
tex->flipy = 0;
tex->opts.sprite = 1;
tex->opts.gamma = 0;
tex->anim.frames = 1;
tex->anim.ms = 1;
int n;
stbi_set_flip_vertically_on_load(0);
unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4);
while (data == NULL) {
@ -37,15 +35,55 @@ struct Texture *texture_pullfromfile(const char *path)
return NULL;
}
tex->data = data;
glGenTextures(1, &tex->id);
glBindTexture(GL_TEXTURE_2D, tex->id);
GLenum fmt;
switch (n) {
case 1:
fmt = GL_RED;
break;
case 2:
fmt = GL_RG;
break;
case 3:
fmt = GL_RGB;
break;
case 4:
fmt = GL_RGBA;
break;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
if (tex->opts.sprite) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
stbi_image_free(data);
if (shlen(texhash) == 0)
sh_new_arena(texhash);
shput(texhash, path, tex);
tex->id = 0;
return tex;
}
@ -72,7 +110,7 @@ struct Texture *texture_loadfromfile(const char *path)
if (new->id == 0) {
glGenTextures(1, &new->id);
tex_gpu_load(new);
//tex_gpu_load(new);
YughInfo("Loaded texture path %s", path);
}
@ -80,94 +118,53 @@ struct Texture *texture_loadfromfile(const char *path)
return new;
}
void tex_pull(struct Texture *tex)
{
if (tex->data != NULL)
tex_flush(tex);
int n;
char *path = tex_get_path(tex);
stbi_set_flip_vertically_on_load(0);
tex->data = stbi_load(path, &tex->width, &tex->height, &n, 4);
if (tex->data == NULL)
YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason());
}
void tex_flush(struct Texture *tex)
{
free(tex->data);
}
void tex_gpu_reload(struct Texture *tex)
{
tex_gpu_free(tex);
tex_gpu_load(tex);
}
void tex_free(struct Texture *tex)
{
free(tex->data);
//free(tex->path);
free(tex);
}
void tex_gpu_load(struct Texture *tex)
{
glBindTexture(GL_TEXTURE_2D, tex->id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->data);
glGenerateMipmap(GL_TEXTURE_2D);
if (tex->opts.sprite) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//tex_gpu_load(tex);
}
void tex_incr_anim(struct TexAnimation *tex_anim)
{
anim_incr(tex_anim);
if (!tex_anim->tex->anim.loop && tex_anim->frame == tex_anim->tex->anim.frames)
if (!tex_anim->loop && tex_anim->frame == arrlen(tex_anim->anim->st_frames))
anim_pause(tex_anim);
}
void anim_incr(struct TexAnimation *anim)
{
anim->frame = (anim->frame + 1) % anim->tex->anim.frames;
tex_anim_calc_uv(anim);
anim->frame = (anim->frame + 1) % arrlen(anim->anim->st_frames);
//tex_anim_calc_uv(anim);
}
void anim_decr(struct TexAnimation *anim)
{
anim->frame = (anim->frame + anim->tex->anim.frames - 1) % anim->tex->anim.frames;
tex_anim_calc_uv(anim);
anim->frame = (anim->frame + arrlen(anim->anim->st_frames) - 1) % arrlen(anim->anim->st_frames);
//tex_anim_calc_uv(anim);
}
struct glrect anim_get_rect(struct TexAnimation *anim)
{
return anim->anim->st_frames[anim->frame];
}
void anim_setframe(struct TexAnimation *anim, int frame)
{
anim->frame = frame;
tex_anim_calc_uv(anim);
//tex_anim_calc_uv(anim);
}
void tex_anim_set(struct TexAnimation *anim)
{
if (anim->playing) {
timer_remove(anim->timer);
anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
anim->timer = timer_make(1.f / anim->anim->ms, tex_incr_anim, anim);
}
tex_anim_calc_uv(anim);
//tex_anim_calc_uv(anim);
}
@ -180,16 +177,9 @@ void tex_gpu_free(struct Texture *tex)
}
}
void tex_anim_calc_uv(struct TexAnimation *anim)
int anim_frames(struct TexAnim *a)
{
struct glrect st;
float w = 1.f / anim->tex->anim.frames;
st.s0 = w * (anim->frame);
st.s1 = st.s0 + w;
st.t0 = 0.f;
st.t1 = 1.f;
anim->st = st;
return arrlen(a->st_frames);
}
struct glrect tex_get_rect(struct Texture *tex)
@ -209,15 +199,15 @@ void anim_play(struct TexAnimation *anim)
if (anim->playing)
return;
if (anim->frame == anim->tex->anim.frames)
if (anim->frame == anim_frames(anim->anim))
anim->frame = 0;
anim->playing = 1;
if (anim->timer == NULL)
anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
anim->timer = timer_make(1.f / anim->anim->ms, tex_incr_anim, anim);
else
timerr_settime(anim->timer, 1.f/anim->tex->anim.ms);
timerr_settime(anim->timer, 1.f/anim->anim->ms);
timer_start(anim->timer);
}
@ -231,7 +221,7 @@ void anim_stop(struct TexAnimation *anim)
anim->frame = 0;
anim->pausetime = 0;
timer_stop(anim->timer);
tex_anim_calc_uv(anim);
//tex_anim_calc_uv(anim);
}
void anim_pause(struct TexAnimation *anim)

View file

@ -35,16 +35,15 @@ struct TexAnimation {
int playing;
int pausetime;
struct timer *timer;
struct glrect st; /* Current ST coordinates for active frame */
struct Texture *tex;
struct TexAnim *anim;
int loop;
};
/* Describes an animation on a particular texture */
struct TexAnim {
int frames;
int dimensions[2];
struct glrect *st_frames; /* Dynamic array of frames of animation */
int ms;
int loop;
struct Texture *tex;
};
struct TextureOptions {
@ -54,12 +53,9 @@ struct TextureOptions {
};
struct Texture {
int type;
unsigned int id;
unsigned int id; /* ID reference for the GPU memory location of the texture */
int width;
int height;
short flipy;
unsigned char *data; // Pixel data of the texture, loaded in at runtime
struct TextureOptions opts;
struct TexAnim anim;
@ -67,12 +63,8 @@ struct Texture {
struct Texture *texture_pullfromfile(const char *path); // Create texture from image
struct Texture *texture_loadfromfile(const char *path); // Create texture & load to gpu
void tex_gpu_load(struct Texture *tex); // Send texture data to gpu
void tex_gpu_reload(struct Texture *tex); // gpu_free then gpu_load
void tex_gpu_free(struct Texture *tex); // Remove texture data from gpu
void tex_free(struct Texture *tex); // Delete struct
void tex_flush(struct Texture *tex); // Remove pixel data from struct
void tex_pull(struct Texture *tex); // Pull pixel data from image
void tex_bind(struct Texture *tex); // Bind to gl context
char * tex_get_path(struct Texture *tex); // Get image path for texture
@ -87,10 +79,11 @@ void anim_incr(struct TexAnimation *anim);
void anim_decr(struct TexAnimation *anim);
void tex_incr_anim(struct TexAnimation *tex_anim);
void tex_anim_calc_uv(struct TexAnimation *anim);
void tex_anim_set(struct TexAnimation *anim);
struct glrect tex_get_rect(struct Texture *tex);
struct glrect anim_get_rect(struct TexAnimation *anim);
int anim_frames(struct TexAnim *a);
#endif

View file

@ -263,7 +263,7 @@ void window_seticon(struct window *w, struct Texture *icon)
static GLFWimage images[1];
images[0].width = icon->width;
images[0].height = icon->height;
images[0].pixels = icon->data;
//images[0].pixels = icon->data;
glfwSetWindowIcon(w->window, 1, images);
}

View file

@ -14,6 +14,7 @@
#include "yugine.h"
#include "2dphysics.h"
#include "parson.h"
#if ED
#include "editor.h"
@ -151,6 +152,17 @@ int main(int argc, char **args) {
engine_init();
JSON_Value *rv = json_value_init_object();
JSON_Object *ro = json_value_get_object(rv);
json_object_set_string(ro, "name", "yugine");
json_object_set_number(ro, "age", 30);
char *serialized = json_serialize_to_string_pretty(rv);
FILE *json = fopen("test.json", "w");
fputs(serialized, json);
json_free_serialized_string(serialized);
json_value_free(rv);
fclose(json);
const GLFWvidmode *vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());