stuff... forgot about git <_<

This commit is contained in:
DaniTheSkunk 2023-03-07 19:13:02 +00:00
parent 12d7d0c823
commit daeb385bba
26 changed files with 394 additions and 122 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/.cache/
/compile_commands.json
*.c#
/example_data/*.glsl

23
build.c
View File

@ -53,7 +53,30 @@ int main(int argc, char *argv[]) {
"skunkworks.lib "
"/I../include ");
/* example */
sys("cl /MD /O2 "
"../examples/simple.c "
"skunkworks.lib "
"/I../include "
"/I. ");
/* gsa example */
/*
sys("c:\\dev\\_tools\\dxc\\bin\\x64\\dxc.exe "
"-fvk-use-gl-layout "
"-T vs_6_0 -spirv ..\\src\\gsa_vert.hlsl -Fo gsa_vert.spv");
sys("c:\\dev\\_tools\\dxc\\bin\\x64\\dxc.exe "
"-fvk-use-gl-layout "
"-T ps_6_0 -spirv ..\\src\\gsa_frag.hlsl -Fo gsa_frag.spv");
*/
/*
sys("C:\\dev\\_tools\\gslang\\bin\\glslangValidator.exe "
"-G -S vert -o gsa_vert.spv ..\\src\\gsa_vert.glsl");
sys("C:\\dev\\_tools\\gslang\\bin\\glslangValidator.exe "
"-G -S frag -o gsa_frag.spv ..\\src\\gsa_frag.glsl");
*/
/* sys("copy *.spv ..\\example_data"); */
sys("copy ..\\src\\*.glsl ..\\example_data");
sys("cl /MD /O2 "
"../examples/gsa_simple.c "
"skunkworks.lib "

View File

@ -4,6 +4,7 @@
#include "gsa_map.h"
#include "gsa_text.h"
#include "skip.h"
#include "str.h"
#include <gsa.h>
sw_color32 pal[GSA_PALETTE_SIZE];
@ -32,7 +33,10 @@ void test() {
void init() {
i32 x, y;
char *a, *b;
sw_str_split2("hellonyaworld", "nya", &a, &b);
printf("%s - %s\n", a, b);
// test();
gsa_copy_palette_to(pal);

13
examples/simple.c Normal file
View File

@ -0,0 +1,13 @@
#include <skunkworks.h>
void tick() {
}
int main(int argc, char *argv[]) {
struct sw_window *win;
win = sw_window_create(sw_vec2i(1280, 720), "example");
sw_window_run(win, tick);
return 0;
}

18
include/net/http_client.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef GUARD_6F6DB4EF6B1F202B980E8656237B5E18
#define GUARD_6F6DB4EF6B1F202B980E8656237B5E18
#include "types.h"
struct sw_http_response {
char *body;
u32 body_length;
i16 status;
};
struct sw_http_request_handle {
u32 _handle;
};
struct sw_http_request_handle sw_http_request_async(char *url);
#endif /* GUARD_6F6DB4EF6B1F202B980E8656237B5E18 */

12
include/renderer2d.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef GUARD_2B56918289975E8AA8711D6E080ED4B9
#define GUARD_2B56918289975E8AA8711D6E080ED4B9
#include "shader.h"
struct sw_renderer2d {
struct sw_shaderprogram shader;
};
struct sw_renderer2d *sw_renderer2d_create();
#endif /* GUARD_2B56918289975E8AA8711D6E080ED4B9 */

View File

@ -4,22 +4,28 @@
#include "types.h"
struct sw_shader {
i32 _shader;
u32 _shader;
};
struct sw_shaderprogram {
i32 _program;
u32 _program;
};
#define SW_SHADER_VERTEX 1
#define SW_SHADER_FRAGMENT 2
#define SW_SHADERLOC_POS 0
/*
struct sw_shader sw_shader_create(char const *source, i32 type);
struct sw_shaderprogram
sw_shaderprogram_create(char const *vertex, char const *fragment);
*/
struct sw_shader sw_shader_create(u8 *data, u32 len, i32 type, bool compiled);
struct sw_shaderprogram sw_shaderprogram_create(
u8 *vert_data, u32 vert_len, u8 *frag_data, u32 frag_len, bool compiled
);
void sw_shaderprogram_use(struct sw_shaderprogram program);

View File

@ -2,6 +2,7 @@
#define GUARD_1B77D3247B24AE540CDA7D1880E34980
#include "shader.h"
#include "skip.h"
extern struct sw_shaderprogram sw_shaders_copy;
extern struct sw_shaderprogram sw_shaders_scale;

View File

@ -49,6 +49,8 @@ struct sw_skip {
} *files;
};
extern struct sw_skip *sw_skip_self;
struct sw_skip *sw_skip_create();
struct sw_skip *sw_skip_load(char *filename);
void sw_skip_destroy(struct sw_skip *skip);
@ -59,6 +61,7 @@ void sw_skip_attach(struct sw_skip *skip, char *filename);
void sw_skip_dettach(char *filename);
/* returns 0 if file not avaible */
u8 *sw_skip_get(struct sw_skip *skip, char *name, u32 *out_size);
char *sw_skip_get_string(struct sw_skip *skip, char *name);
void sw_skip_save(struct sw_skip *skip, char *filename);
#endif /* GUARD_62F584E45B48B8F6069643FC44702ED5 */

View File

@ -15,6 +15,6 @@
#include "msvc.h"
void sw_init();
void sw_init(int argc, char *argv[]);
#endif /* GUARD_9C1995B9551EFCCDC93F439C46AEF061 */

View File

@ -16,31 +16,6 @@ struct gsa_sprite sprites[256];
struct sw_image8 *_gfx;
struct sw_window *_win;
static char *vert_source = "#version 300 es\n\
precision highp float;\
in vec2 pos;\
in vec2 tex_coord;\
out vec2 out_tex_coord;\
void main() {\
gl_Position = vec4((pos / vec2(152, -88)) - vec2(1, -1), 0.f, 1.f);\
out_tex_coord = tex_coord;\
}\
";
static char *frag_source = "#version 300 es\n\
precision highp float;\
in vec2 out_tex_coord;\
uniform sampler2D tex;\
uniform vec4 palette[256];\
out vec4 color;\
void main(){\
int r = int(texture(tex, out_tex_coord).r * 255.f);\
if(r > 0)\
color = palette[r];\
else discard;\
}\
";
static struct sw_shaderprogram program;
static u32 vbo, vao;
@ -62,29 +37,33 @@ static u32 tex;
static bool (*loop_stack[GSA_LOOP_STACK_SIZE])();
static i8 loop_stack_i;
static struct sw_skip *skip;
int gsa_main(int argc, char *argv[]) {
(void)argc;
(void)argv;
i32 max_components;
u32 size;
u8 *data;
u8 *data, *frag_data, *vert_data;
sw_log("Initialising GameSkunkAdvance v0.0");
sw_init(argc, argv);
skip = sw_skip_load(argv[0]);
data = sw_skip_get(skip, "gfx.png", &size);
sw_log("Grab data");
data = sw_skip_get(sw_skip_self, "gfx.png", &size);
_gfx = sw_image8_load_png_data(data, size);
memset(loop_stack, 0, sizeof(loop_stack));
loop_stack_i = -1;
_win = sw_window_create(sw_vec2i(304, 176), "Game Skunk Advance v0.0");
program = sw_shaderprogram_create(vert_source, frag_source);
glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &max_components);
sw_log("GL_MAX_VERTEX_UNIFORM_COMPONENTS: %i", max_components);
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &max_components);
sw_log("GL_NUM_PROGRAM_BINARY_FORMATS: %i", max_components);
frag_data = sw_skip_get_string(sw_skip_self, "gsa_frag.glsl");
vert_data = sw_skip_get_string(sw_skip_self, "gsa_vert.glsl");
program = sw_shaderprogram_create(vert_data, 0, frag_data, 0, false);
memcpy(palette, _gfx->palette, sizeof(_gfx->palette));
glGenTextures(1, &tex);

12
src/gsa_frag.glsl Normal file
View File

@ -0,0 +1,12 @@
#version 300 es
precision highp float;
in vec2 out_tex_coord;
uniform sampler2D tex;
uniform vec4 palette[256];
out vec4 color;
void main(){
int r = int(texture(tex, out_tex_coord).r * 255.f);
if(r > 0)
color = palette[r];
else discard;
}

11
src/gsa_frag.hlsl Normal file
View File

@ -0,0 +1,11 @@
struct frag_in {
float2 pos : POSITION0;
float2 tex_coord : TEXCOORD0;
};
Texture2D<float4> tex : register(t0);
SamplerState tex_sampler : register(s0);
float4 main(frag_in input) : SV_Target0 {
return tex.Sample(tex_sampler, input.tex_coord);
}

9
src/gsa_vert.glsl Normal file
View File

@ -0,0 +1,9 @@
#version 300 es
precision highp float;
in vec2 pos;
in vec2 tex_coord;
out vec2 out_tex_coord;
void main() {
gl_Position = vec4((pos / vec2(152, -88)) - vec2(1, -1), 0.f, 1.f);
out_tex_coord = tex_coord;
}

16
src/gsa_vert.hlsl Normal file
View File

@ -0,0 +1,16 @@
struct vert_in {
float2 pos : POSITION0;
float2 tex_coord : TEXCOORD0;
};
struct vert_out {
float2 pos : POSITION0;
float2 tex_coord : TEXCOORD0;
};
vert_out main(vert_in input) {
vert_out output;
output.tex_coord = input.tex_coord;
output.pos = (input.pos / float2(152, -88)) - float2(1, -1);
return output;
}

67
src/net/http_client.c Normal file
View File

@ -0,0 +1,67 @@
#include "net/http_client.h"
#include "error.h"
#include "str.h"
#include <string.h>
#include <windows.h>
#include <wininet.h>
static HINTERNET internet = 0;
static void init_internet();
struct sw_http_request_handle sw_http_request_async(char *url) {
HINTERNET session, http;
u16 port;
char *proto, *rest;
char *server, *rest2;
sw_str_split2(url, "://", &proto, &rest);
if(proto == 0) {
sw_error("url requires ://");
}
if(strcmp(proto, "https") == 0) {
port = INTERNET_DEFAULT_HTTPS_PORT;
} else if(strcmp(proto, "http") == 0) {
port = INTERNET_DEFAULT_HTTP_PORT;
} else {
sw_error("unknown protocol '%s'", proto);
}
sw_str_split2(url, "/", &server, &rest2);
/* TODO: handle urls without / */
init_internet();
session = InternetConnect(
internet, server, port, 0, 0, INTERNET_SERVICE_HTTP, 0, 0
);
if(session == 0) {
sw_win32_error();
}
http = HttpOpenRequest(session, "GET", rest2, "HTTP/1.1", 0, 0, 0, 0);
if(http == 0) {
sw_win32_error();
}
if(!HttpSendRequest(http, 0, 0, 0, 0)) {
sw_win32_error();
}
return (struct sw_http_request_handle){0};
}
static void init_internet() {
if(internet == 0) {
internet = InternetOpen(
"Skunkworks", INTERNET_OPEN_TYPE_DIRECT, 0, 0, INTERNET_FLAG_ASYNC
);
if(internet == 0) {
sw_win32_error();
}
}
}

7
src/renderer2d.c Normal file
View File

@ -0,0 +1,7 @@
#include "renderer2d.h"
static char *shader_vert = "\
\
";
struct sw_renderer2d *sw_renderer2d_create(){};

View File

@ -4,12 +4,78 @@
#include "error.h"
#include "gl.h"
/*
struct sw_shader sw_shader_create(char const *source, i32 type) {
struct sw_shader shader;
i32 gltype;
i32 length;
i32 ret;
sw_log("create shader");
switch(type) {
case SW_SHADER_VERTEX:
gltype = GL_VERTEX_SHADER;
break;
case SW_SHADER_FRAGMENT:
gltype = GL_FRAGMENT_SHADER;
break;
default:
gltype = 0;
sw_error("unknown shader type");
}
shader._shader = glCreateShader(gltype);
length = strlen(source);
glShaderSource(shader._shader, 1, &source, &length);
glCompileShader(shader._shader);
glGetShaderiv(shader._shader, GL_COMPILE_STATUS, &ret);
if(!ret) {
char str[256];
i32 len;
glGetShaderInfoLog(shader._shader, 256, &len, str);
sw_error("Shader Compile Error: %s", str);
}
return shader;
}
struct sw_shaderprogram
sw_shaderprogram_create(char const *vertex, char const *fragment) {
struct sw_shaderprogram program;
struct sw_shader v, f;
i32 ret;
sw_log("create shader program");
v = sw_shader_create(vertex, SW_SHADER_VERTEX);
f = sw_shader_create(fragment, SW_SHADER_FRAGMENT);
program._program = glCreateProgram();
glAttachShader(program._program, v._shader);
glAttachShader(program._program, f._shader);
sw_log("set attribute locations");
glBindAttribLocation(program._program, SW_SHADERLOC_POS, "pos");
sw_log("link shader");
glLinkProgram(program._program);
glGetProgramiv(program._program, GL_LINK_STATUS, &ret);
if(!ret) {
char str[256];
i32 len;
glGetProgramInfoLog(program._program, 256, &len, str);
sw_error("Shader Link Error: %s", str);
}
return program;
}
*/
struct sw_shader sw_shader_create(u8 *data, u32 len, i32 type, bool compiled) {
struct sw_shader shader;
i32 gltype;
i32 length;
i32 ret;
u32 length;
sw_log("create shader");
@ -26,10 +92,27 @@ struct sw_shader sw_shader_create(char const *source, i32 type) {
}
shader._shader = glCreateShader(gltype);
length = strlen(source);
glShaderSource(shader._shader, 1, &source, &length);
glCompileShader(shader._shader);
if(compiled) {
sw_log("loading shader binary of size %i", len);
glShaderBinary(
1, &shader._shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, data, len
);
glGetShaderiv(shader._shader, GL_SPIR_V_BINARY, &ret);
if(ret) {
sw_log("is spirv binary");
} else {
sw_log("isn't spirv binary");
}
glSpecializeShader(shader._shader, "main", 0, 0, 0);
} else {
length = strlen(data);
glShaderSource(shader._shader, 1, &data, &length);
glCompileShader(shader._shader);
}
sw_log("source: %s", data);
glGetShaderiv(shader._shader, GL_COMPILE_STATUS, &ret);
if(!ret) {
char str[256];
@ -38,19 +121,19 @@ struct sw_shader sw_shader_create(char const *source, i32 type) {
glGetShaderInfoLog(shader._shader, 256, &len, str);
sw_error("Shader Compile Error: %s", str);
}
return shader;
}
struct sw_shaderprogram
sw_shaderprogram_create(char const *vertex, char const *fragment) {
struct sw_shaderprogram sw_shaderprogram_create(
u8 *vert_data, u32 vert_len, u8 *frag_data, u32 frag_len, bool compiled
) {
struct sw_shaderprogram program;
struct sw_shader v, f;
i32 ret;
sw_log("create shader program");
v = sw_shader_create(vertex, SW_SHADER_VERTEX);
f = sw_shader_create(fragment, SW_SHADER_FRAGMENT);
v = sw_shader_create(vert_data, vert_len, SW_SHADER_VERTEX, compiled);
f = sw_shader_create(frag_data, frag_len, SW_SHADER_FRAGMENT, compiled);
program._program = glCreateProgram();
glAttachShader(program._program, v._shader);
glAttachShader(program._program, f._shader);

11
src/shader_copy_frag.glsl Normal file
View File

@ -0,0 +1,11 @@
#version 300 es
precision highp float;
in vec2 out_tex_coord;
uniform sampler2D tex;
out vec4 color;
void main() {
vec4 col = texture(tex, out_tex_coord);
if(col.a > 0.0) {
color = col;
}
}

View File

@ -0,0 +1,8 @@
#version 300 es
precision highp float;
in vec2 pos;
out vec2 out_tex_coord;
void main() {
gl_Position = vec4(pos, 0.0f, 1.0f);
out_tex_coord = vec2(pos.x * 0.5 + 0.5, pos.y * 0.5 + 0.5);
}

View File

@ -0,0 +1,20 @@
#version 300 es
precision highp float;
in vec2 out_tex_coord;
uniform sampler2D tex;
uniform float scale;
uniform vec2 offset;
uniform vec2 size_ratio;
out vec4 color;
void main() {
vec2 tc = (out_tex_coord-offset)*size_ratio;
vec4 col = vec4(0,0,0,0);
if(tc.x <= 1.0 && tc.y <= 1.0 && tc.x >= 0.0 && tc.y >= 0.0) {
col = texture(tex, tc);
}
if(col.a > 0.0) {
color = col;
} else {
discard;
}
}

View File

@ -1,53 +1,21 @@
#include "shaders.h"
#include "error.h"
#include "skip.h"
struct sw_shaderprogram sw_shaders_copy;
struct sw_shaderprogram sw_shaders_scale;
static char *vert_copy = "#version 300 es\n\
precision highp float;\
in vec2 pos;\
out vec2 out_tex_coord;\
void main() {\
gl_Position = vec4(pos, 0.0f, 1.0f);\
out_tex_coord = vec2(pos.x * 0.5 + 0.5, pos.y * 0.5 + 0.5);\
}";
static char *frag_copy = "#version 300 es\n\
precision highp float;\
in vec2 out_tex_coord;\
uniform sampler2D tex;\
out vec4 color;\
void main() {\
vec4 col = texture(tex, out_tex_coord);\
if(col.a > 0.0) {\
color = col;\
}\
}";
static char *frag_scale = "#version 300 es\n\
precision highp float;\
in vec2 out_tex_coord;\
uniform sampler2D tex;\
uniform float scale;\
uniform vec2 offset;\
uniform vec2 size_ratio;\
out vec4 color;\
void main() {\
vec2 tc = (out_tex_coord-offset)*size_ratio;\
vec4 col = vec4(0,0,0,0);\
if(tc.x <= 1.0 && tc.y <= 1.0 && tc.x >= 0.0 && tc.y >= 0.0) {\
col = texture(tex, tc);\
}\
if(col.a > 0.0) {\
color = col;\
} else { \
discard;\
}\
}";
void sw_shaders_static_init() {
u8 *vert_copy, *frag_copy, *frag_scale;
u32 size;
sw_log("initialising shaders");
sw_shaders_copy = sw_shaderprogram_create(vert_copy, frag_copy);
sw_shaders_scale = sw_shaderprogram_create(vert_copy, frag_scale);
vert_copy = sw_skip_get_string(sw_skip_self, "shader_copy_vert.glsl");
frag_copy = sw_skip_get_string(sw_skip_self, "shader_copy_frag.glsl");
frag_scale = sw_skip_get_string(sw_skip_self, "shader_scale_frag.glsl");
sw_shaders_copy =
sw_shaderprogram_create(vert_copy, 0, frag_copy, 0, false);
sw_shaders_scale =
sw_shaderprogram_create(vert_copy, 0, frag_scale, 0, false);
}

View File

@ -7,6 +7,8 @@
#include <stdlib.h>
#include <string.h>
struct sw_skip *sw_skip_self;
struct sw_skip *sw_skip_create() {
struct sw_skip *skip;
@ -170,6 +172,23 @@ u8 *sw_skip_get(struct sw_skip *skip, char *name, u32 *out_size) {
return 0;
}
char *sw_skip_get_string(struct sw_skip *skip, char *name) {
u8 *data;
u32 size;
char *str;
data = sw_skip_get(skip, name, &size);
if(data == 0) {
return 0;
}
str = malloc(size + 1);
memcpy(str, data, size);
str[size] = 0;
return str;
}
void sw_skip_save(struct sw_skip *skip, char *filename) {
sw_skip_save_to(skip, fopen(filename, "wb"));
}

View File

@ -2,12 +2,19 @@
#include <assert.h>
#include "skip.h"
#include "types.h"
void assert_types();
void sw_init() {
assert_types();
static bool init = false;
void sw_init(int argc, char *argv[]) {
if(!init) {
assert_types();
sw_skip_self = sw_skip_load(argv[0]);
init = true;
}
}
/* private */

View File

@ -1,30 +0,0 @@
#include <stdio.h>
#include "image32.h"
#include "vec2i.h"
#include "window.h"
#include <png.h>
#include <skunkworks.h>
struct sw_window *win;
void tick();
int main(int argc, char *argv[]) {
struct sw_image32 *img;
(void)argc;
(void)argv;
sw_init();
win = sw_window_create(sw_vec2i(640, 480), "Skunkworks");
img = sw_image32_load_png("d:/art/pfp.png");
sw_debug("%08X", sw_image32_get(img, sw_vec2i(10, 10)));
sw_window_run(win, tick);
return 0;
}
void tick() {
}

View File

@ -32,6 +32,8 @@ static void GLAPIENTRY gldebug(
struct sw_window *sw_window_create(struct sw_vec2i size, char *title) {
struct sw_window *win;
sw_init();
win = malloc(sizeof(struct sw_window));
win->window_size = size;
win->game_size = size;
@ -39,7 +41,8 @@ struct sw_window *sw_window_create(struct sw_vec2i size, char *title) {
sw_log("init glfw");
glfwInit();
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
win->_window = glfwCreateWindow(size.x, size.y, title, 0, 0);
glfwSetWindowUserPointer(win->_window, win);
glfwShowWindow(win->_window);
@ -96,6 +99,7 @@ void _tick() {
sw_scaler_apply(
SW_SCALE_INTEGER, _tick_window->game_size, _tick_window->window_size
);
glfwSwapBuffers(_tick_window->_window);
glfwPollEvents();
}