Skip to content

Instantly share code, notes, and snippets.

@ZenToad
Created June 26, 2023 20:18
Show Gist options
  • Save ZenToad/934a46f2713f70a5cdd927e923eb9151 to your computer and use it in GitHub Desktop.
Save ZenToad/934a46f2713f70a5cdd927e923eb9151 to your computer and use it in GitHub Desktop.
example of the issue I'm having with displaying textures
emcc -I..\lib\sokol -I..\lib\stb ..\src\exp\simple_wasm_texture.c -DSOKOL_NO_ENTRY -sALLOW_MEMORY_GROWTH -o simple_wasm_texture.html
// going to start from scratch with this sucker.
// if we need to figure anything out we can do that in the
// experiment section and then pull over stuff we want
#define SOKOL_IMPL
#define SOKOL_GLES2
#include <stdint.h>
#include "sokol_gfx.h"
#include "sokol_app.h"
#include "sokol_fetch.h"
#include "sokol_glue.h"
#include "sokol_log.h"
#include "util/sokol_gl.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define SIZE 1024 * 1024
static struct {
sg_pass_action pass_action;
sg_image img;
uint8_t file_buffer[SIZE];
} state;
void frame(void);
static void load_file(const sapp_html5_fetch_response* response) {
int png_width, png_height, num_channels;
const int desired_channels = 4;
printf("load_png...\n");
stbi_uc* pixels = stbi_load_from_memory(
response->data.ptr,
response->data.size,
&png_width, &png_height,
&num_channels, desired_channels);
if (pixels) {
printf("seems like the load worked\n");
state.img = sg_make_image(&(sg_image_desc){
.width = png_width,
.height = png_height,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.min_filter = SG_FILTER_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
.data.subimage[0][0] = {
.ptr = pixels,
.size = (size_t)(png_width * png_height * 4),
}
});
sg_resource_state result = sg_query_image_state(state.img);
if (result == SG_RESOURCESTATE_INVALID) {
// buffer poll is exhasued
printf("state.img buffer pool is exahsted\n");
} else if (result == SG_RESOURCESTATE_FAILED) {
// creating the resource has failed
printf("state.img creating the resource has failed\n");
} else if (result == SG_RESOURCESTATE_VALID) {
printf("state.img SUCCE$$\n");
// SUCCE$$
}
stbi_image_free(pixels);
} else {
printf("OK, PROBLEM, it didn't work... now what?\n");
}
}
static void fetch_callback(const sapp_html5_fetch_response* response) {
printf("Got the fetch callback\n");
if (response->succeeded) {
printf("Success\n");
load_file(response);
}
else if (SAPP_HTML5_FETCH_ERROR_BUFFER_TOO_SMALL == response->error_code) {
printf("Loadstate File too damn big\n");
}
else {
printf("Loadstate Failed\n");
}
}
void init(void) {
printf("In init()...)\n");
sg_setup(&(sg_desc){
.context = sapp_sgcontext(),
.logger.func = slog_func,
});
sgl_setup(&(sgl_desc_t){
.logger.func = slog_func,
});
sfetch_setup(&(sfetch_desc_t){
.logger.func = slog_func
});
// default pass action
state.pass_action = (sg_pass_action) {
.colors[0] = {
.action = SG_ACTION_CLEAR,
.value = { 0.0f, 0.0f, 0.0f, 1.0f }
}
};
}
void sapp_event_files_dropped(const sapp_event *event) {
printf("Got an html dropped file\n");
const int num_dropped_files = sapp_get_num_dropped_files();
printf("Got some dropped files: %d\n", num_dropped_files);
uint32_t size = sapp_html5_get_dropped_file_size(0);
float file_size = (float)(size) / (1024.0f * 1024.0f);
printf("Dropped file size: %f MB\n", file_size);
printf("Is size good? = %s\n", size < SAPP_RANGE(state.file_buffer).size ? "YES" : "NO");
sapp_html5_fetch_dropped_file(&(sapp_html5_fetch_request){
.dropped_file_index = 0,
.callback = fetch_callback,
.buffer = SAPP_RANGE(state.file_buffer)
});
}
void draw_sprite(sg_image img) {
sg_image_desc desc = sg_query_image_desc(img);
sgl_enable_texture();
sgl_texture(img);
float sx = 0.0f;
float sy = 0.0f;
float ex = desc.width;
float ey = desc.height;
sgl_begin_quads();
sgl_c3f(1.0f, 1.0f, 1.0f);
sgl_v3f_t2f( sx, sy, -1.0f, 0.0f, 0.0f);
sgl_v3f_t2f( ex, sy, -1.0f, 1.0f, 0.0f);
sgl_v3f_t2f( ex, ey, -1.0f, 1.0f, 1.0f);
sgl_v3f_t2f( sx, ey, -1.0f, 0.0f, 1.0f);
sgl_end();
}
void event(const sapp_event *event) {
switch (event->type) {
case SAPP_EVENTTYPE_KEY_DOWN:
break;
case SAPP_EVENTTYPE_KEY_UP:
break;
case SAPP_EVENTTYPE_MOUSE_SCROLL:
break;
case SAPP_EVENTTYPE_MOUSE_DOWN:
break;
case SAPP_EVENTTYPE_MOUSE_UP:
break;
case SAPP_EVENTTYPE_MOUSE_MOVE:
break;
case SAPP_EVENTTYPE_FILES_DROPPED:
sapp_event_files_dropped(event);
break;
default: break;
}
}
void frame(void) {
sgl_defaults();
sgl_matrix_mode_projection();
sgl_load_identity();
sgl_ortho(0.0f, sapp_widthf(), sapp_heightf(), 0.0f, 0.1f, 100.0f);
sgl_matrix_mode_modelview();
sgl_load_identity();
draw_sprite(state.img);
sg_begin_default_pass(&state.pass_action, sapp_width(), sapp_height());
sgl_draw();
sg_end_pass();
sg_commit();
}
void cleanup(void) {
sgl_shutdown();
sg_shutdown();
}
int main(int argc, char **argv) {
sapp_desc desc ={
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.high_dpi = true,
.window_title = "IDK if we even need this",
.icon.sokol_default = true,
.enable_dragndrop = true,
.logger.func = slog_func,
};
sapp_run(&desc);
return 0;
}
@ZenToad
Copy link
Author

ZenToad commented Jun 26, 2023

prince_12
test3
These files do not display when dragged onto the webpage. They do work in Windows

@ZenToad
Copy link
Author

ZenToad commented Jun 26, 2023

white1024x1024
alpha1024x1024
assballs2
These files will work when dropped on the webpage.
I have no idea what is different

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment